diff --git a/ocis-pkg/checks/checkgrpc.go b/ocis-pkg/checks/checkgrpc.go index 5690c67def..c10b53bbf8 100644 --- a/ocis-pkg/checks/checkgrpc.go +++ b/ocis-pkg/checks/checkgrpc.go @@ -4,14 +4,19 @@ import ( "context" "fmt" - "google.golang.org/grpc/credentials/insecure" - + "github.com/owncloud/ocis/v2/ocis-pkg/handlers" "google.golang.org/grpc" + "google.golang.org/grpc/credentials/insecure" ) // NewGRPCCheck checks the reachability of a grpc server. func NewGRPCCheck(address string) func(context.Context) error { return func(_ context.Context) error { + address, err := handlers.FailSaveAddress(address) + if err != nil { + return err + } + conn, err := grpc.NewClient(address, grpc.WithTransportCredentials(insecure.NewCredentials())) if err != nil { return fmt.Errorf("could not connect to grpc server: %v", err) diff --git a/ocis-pkg/checks/checkhttp.go b/ocis-pkg/checks/checkhttp.go index 462749fb87..48502fe6ac 100644 --- a/ocis-pkg/checks/checkhttp.go +++ b/ocis-pkg/checks/checkhttp.go @@ -4,12 +4,24 @@ import ( "context" "fmt" "net/http" + "strings" "time" + + "github.com/owncloud/ocis/v2/ocis-pkg/handlers" ) // NewHTTPCheck checks the reachability of a http server. func NewHTTPCheck(url string) func(context.Context) error { return func(_ context.Context) error { + url, err := handlers.FailSaveAddress(url) + if err != nil { + return err + } + + if !strings.HasPrefix(url, "http://") && !strings.HasPrefix(url, "https://") { + url = "http://" + url + } + c := http.Client{ Timeout: 3 * time.Second, } diff --git a/ocis-pkg/checks/checktcp.go b/ocis-pkg/checks/checktcp.go index 475ff50e82..693ba255ce 100644 --- a/ocis-pkg/checks/checktcp.go +++ b/ocis-pkg/checks/checktcp.go @@ -4,11 +4,18 @@ import ( "context" "net" "time" + + "github.com/owncloud/ocis/v2/ocis-pkg/handlers" ) // NewTCPCheck returns a check that connects to a given tcp endpoint. func NewTCPCheck(address string) func(context.Context) error { return func(_ context.Context) error { + address, err := handlers.FailSaveAddress(address) + if err != nil { + return err + } + conn, err := net.DialTimeout("tcp", address, 3*time.Second) if err != nil { return err diff --git a/ocis-pkg/handlers/checker.go b/ocis-pkg/handlers/checker.go index 48ee3b12b1..0326c580dc 100644 --- a/ocis-pkg/handlers/checker.go +++ b/ocis-pkg/handlers/checker.go @@ -5,11 +5,12 @@ import ( "fmt" "io" "maps" + "net" "net/http" - - "golang.org/x/sync/errgroup" + "strings" "github.com/owncloud/ocis/v2/ocis-pkg/log" + "golang.org/x/sync/errgroup" ) // check is a function that performs a check. @@ -113,3 +114,35 @@ func (h *CheckHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { h.conf.logger.Panic().Err(err).Msg("failed to write response") } } + +// FailSaveAddress replaces wildcard addresses with the outbound IP. +func FailSaveAddress(address string) (string, error) { + if strings.Contains(address, "0.0.0.0") || strings.Contains(address, "::") { + outboundIp, err := getOutBoundIP() + if err != nil { + return "", err + } + address = strings.Replace(address, "0.0.0.0", outboundIp, 1) + address = strings.Replace(address, "::", "["+outboundIp+"]", 1) + address = strings.Replace(address, "[::]", "["+outboundIp+"]", 1) + } + return address, nil +} + +// getOutBoundIP returns the outbound IP address. +func getOutBoundIP() (string, error) { + interfacesAddresses, err := net.InterfaceAddrs() + if err != nil { + return "", err + } + + for _, address := range interfacesAddresses { + if ipNet, ok := address.(*net.IPNet); ok && !ipNet.IP.IsLoopback() { + if ipNet.IP.To4() != nil { + return ipNet.IP.String(), nil + } + } + } + + return "", fmt.Errorf("no IP found") +}