2025-09-23 01:07:24 +07:00

54 lines
867 B
Go

package models
import (
"fmt"
"net"
"net/url"
)
type Team struct {
ID int64
Name string
Password string
}
func (t *Team) GetTeamUrl() (string, error) {
ip := selectIP()
u := fmt.Sprintf("http://%s:8100?name=%s&password=%s", ip, url.PathEscape(t.Name), t.Password)
return u, nil
}
func selectIP() string {
ips, err := getLocalIPs()
if err != nil || len(ips) == 0 {
return "127.0.0.1"
}
return ips[0]
}
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
}