This commit is contained in:
2025-06-17 01:40:55 +07:00
parent b17599eb39
commit be8e7fa482
5 changed files with 166 additions and 40 deletions
+1 -39
View File
@@ -9,8 +9,6 @@ import (
"evening_detective/internal/services/story_service"
"evening_detective/proto"
"fmt"
"net"
"net/url"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
@@ -153,7 +151,7 @@ func (s *Services) GetTeams(ctx context.Context, _ *proto.GetTeamsReq) (*proto.G
if err != nil {
return nil, err
}
newTeam.Url, err = getTeamUrl(team)
newTeam.Url, err = team.GetTeamUrl()
if err != nil {
return nil, err
}
@@ -220,39 +218,3 @@ func addLog(team *models.Team, action string, v any) {
fmt.Printf("Team %s: %s %s\n", team.Name, action, string(vJson))
}
}
func getTeamUrl(team *models.Team) (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(team.Name), team.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
}