package server import ( "net" "net/http" "net/url" "os" "sort" "strconv" "time" "git.a-wan.net/alan/pipelines/internal/protocol" ) func networkAddresses() []protocol.NetworkAddress { result := []protocol.NetworkAddress{} interfaces, _ := net.Interfaces() for _, iface := range interfaces { if iface.Flags&net.FlagUp == 0 { continue } addresses, _ := iface.Addrs() for _, address := range addresses { ip, _, err := net.ParseCIDR(address.String()) if err != nil || ip.IsUnspecified() { continue } text := ip.String() if ip.IsLinkLocalUnicast() && ip.To4() == nil { text += "%" + iface.Name } result = append(result, protocol.NetworkAddress{Interface: iface.Name, IP: text}) } } sort.Slice(result, func(i, j int) bool { if result[i].Interface != result[j].Interface { return result[i].Interface < result[j].Interface } return result[i].IP < result[j].IP }) return result } func candidateURLs(addresses []protocol.NetworkAddress, scheme, port, path string) []string { result := []string{} for _, address := range addresses { u := url.URL{Scheme: scheme, Host: net.JoinHostPort(address.IP, port), Path: path} result = append(result, u.String()) } return result } func systemStatus(r *http.Request, started time.Time, runErr error) *protocol.SystemStatus { hostname, _ := os.Hostname() s := &protocol.SystemStatus{Hostname: hostname, StartedAt: started.UTC().Format(time.RFC3339), Uptime: time.Since(started).Round(time.Second).String(), Addresses: networkAddresses(), Server: protocol.ServiceStatus{Health: "healthy"}, Note: "Addresses belong to the server computer (or container). Candidate URLs are not reachability checks; binding, firewalls, port mapping and TLS may differ."} if runErr != nil { s.Server.Health = "execution unavailable" } if address, ok := r.Context().Value(http.LocalAddrContextKey).(net.Addr); ok { _, port, err := net.SplitHostPort(address.String()) if err == nil { scheme := "ws" if r.TLS != nil { scheme = "wss" } s.Server.URL = (&url.URL{Scheme: scheme, Host: address.String(), Path: "/connect"}).String() s.Server.Port = port s.Server.Candidates = candidateURLs(s.Addresses, scheme, port, "/connect") } } // Docker interface addresses are not the host's connection addresses. The // installer publishes the detected host LAN IP and actual mapped host port. if ip := net.ParseIP(os.Getenv("PIPELINES_HOST_IP")); ip != nil && !ip.IsLoopback() && !ip.IsUnspecified() { if port, err := strconv.Atoi(os.Getenv("PIPELINES_PUBLIC_PORT")); err == nil && port > 0 && port <= 65535 { s.Server.Port = strconv.Itoa(port) s.Server.URL = (&url.URL{Scheme: "ws", Host: net.JoinHostPort(ip.String(), s.Server.Port), Path: "/connect"}).String() s.Addresses = []protocol.NetworkAddress{{Interface: "LAN", IP: ip.String()}} s.Server.Candidates = []string{s.Server.URL} } } return s }