generated from VLADIMIR/template
50 lines
799 B
Go
50 lines
799 B
Go
package models
|
|
|
|
import (
|
|
"fmt"
|
|
"net"
|
|
"net/url"
|
|
)
|
|
|
|
type Team struct {
|
|
ID int64
|
|
Name string
|
|
Password string
|
|
}
|
|
|
|
func (t *Team) GetTeamUrl() (string, error) {
|
|
ips, err := getLocalIPs()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
ip := ips[0]
|
|
u := fmt.Sprintf("http://%s:8100?name=%s&password=%s", ip, url.PathEscape(t.Name), t.Password)
|
|
return u, nil
|
|
}
|
|
|
|
func getLocalIPs() ([]string, error) {
|
|
var ips []string
|
|
addrs, err := net.InterfaceAddrs()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
for _, addr := range addrs {
|
|
ipNet, ok := addr.(*net.IPNet)
|
|
if !ok {
|
|
continue
|
|
}
|
|
|
|
ip := ipNet.IP
|
|
if ip.IsLoopback() || ip.IsLinkLocalMulticast() || ip.IsLinkLocalUnicast() {
|
|
continue
|
|
}
|
|
|
|
if ipv4 := ip.To4(); ipv4 != nil {
|
|
ips = append(ips, ipv4.String())
|
|
}
|
|
}
|
|
|
|
return ips, nil
|
|
}
|