package daemon import ( "fmt" "net" "strings" ) // StateStoreNeedsRelay reports whether target is bound to this host's // loopback, which the egress relay can reach only through the host bridge's // gateway address. func StateStoreNeedsRelay(target string) bool { _, host, ok := stateStoreTargetAddr(target) return ok && loopbackHost(host) } // RelayStateStoreTarget is the host:port the egress relay forwards task State // Store traffic to. The relay has its own network namespace, so a loopback // target would name the relay itself: it is reached at gateway, the host's // address on the relay's bridge, on the same port. func RelayStateStoreTarget(target, gateway string) (string, error) { addr, host, ok := stateStoreTargetAddr(target) if !ok { return "", fmt.Errorf("state store target %q: want host:port", target) } if !loopbackHost(host) { return addr, nil } if gateway == "" { return "", fmt.Errorf("state store target %q is loopback and the container network has no host gateway", target) } _, port, _ := net.SplitHostPort(addr) return net.JoinHostPort(gateway, port), nil } // stateStoreTargetAddr is target as host:port, accepting the gRPC scheme form // ("dns:///host:port") the configuration allows. func stateStoreTargetAddr(target string) (addr, host string, ok bool) { target = strings.TrimSpace(target) if _, rest, found := strings.Cut(target, ":///"); found { target = rest } host, _, err := net.SplitHostPort(target) if err != nil { return "", "", false } return target, host, true } func loopbackHost(host string) bool { if strings.EqualFold(host, "localhost") { return true } ip := net.ParseIP(host) return ip != nil && ip.IsLoopback() }