package server import ( "context" "encoding/json" "io" "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 endpointPort(u *url.URL) string { if port := u.Port(); port != "" { return port } if u.Scheme == "https" || u.Scheme == "wss" { return "443" } return "80" } 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 localEndpoint(u *url.URL, hostname string, addresses []protocol.NetworkAddress) bool { host := u.Hostname() if host == "localhost" || host == hostname || net.ParseIP(host).IsLoopback() { return true } for _, address := range addresses { if host == address.IP { return true } } return false } 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"}, Ntfy: protocol.ServiceStatus{Health: "not configured"}, 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} } } endpoints := notificationEndpoints() if len(endpoints) == 0 { return s } s.Topic = endpoints[0].Topic s.NtfyUser = os.Getenv("PIPELINES_NTFY_READER") s.Ntfy.Health = "not checked" // Only the validated public base is returned. Publishing URL and tokens // never leave the server, even on probe failure. s.Ntfy.URL = endpoints[0].ServerURL if u, err := url.Parse(s.Ntfy.URL); err == nil && u.Host != "" { s.Ntfy.Port = endpointPort(u) if localEndpoint(u, hostname, s.Addresses) { s.Ntfy.Candidates = candidateURLs(s.Addresses, u.Scheme, s.Ntfy.Port, "/"+s.Topic) } } publish, err := url.Parse(os.Getenv("PIPELINES_NTFY_URL")) if err != nil { return s } if s.Ntfy.URL == "" && localEndpoint(publish, hostname, s.Addresses) { s.Ntfy.Port = endpointPort(publish) s.Ntfy.Candidates = candidateURLs(s.Addresses, publish.Scheme, s.Ntfy.Port, "/"+s.Topic) } publish.Path = "/v1/health" publish.RawPath = "" publish.RawQuery = "" publish.Fragment = "" ctx, cancel := context.WithTimeout(r.Context(), 2*time.Second) defer cancel() req, err := http.NewRequestWithContext(ctx, http.MethodGet, publish.String(), nil) if err != nil { return s } client := &http.Client{CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }} response, err := client.Do(req) s.Ntfy.Health = "unreachable" if err != nil { return s } defer response.Body.Close() s.Ntfy.Health = "unhealthy" var health struct { Healthy bool `json:"healthy"` } if response.StatusCode == http.StatusOK && json.NewDecoder(io.LimitReader(response.Body, 4096)).Decode(&health) == nil && health.Healthy { s.Ntfy.Health = "healthy" } return s }