This commit is contained in:
2025-05-20 02:46:03 +07:00
parent e38b7ebb88
commit 153b50d3a2
4 changed files with 167 additions and 112 deletions
+42
View File
@@ -9,6 +9,8 @@ import (
"evening_detective/internal/services/story_service"
"evening_detective/proto"
"fmt"
"net"
"net/url"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
@@ -119,6 +121,10 @@ func (s *Services) GetTeams(ctx context.Context, _ *proto.GetTeamsReq) (*proto.G
if err != nil {
return nil, err
}
newTeam.Url, err = getTeamUrl(team)
if err != nil {
return nil, err
}
newTeam.SpendTime = int64(20 * len(actions))
applications, err := s.repository.GetApplications(ctx, team.ID, "NEW")
if err != nil {
@@ -182,3 +188,39 @@ func log(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
}