avoid 0.0.0.0 & replace by outbound ip
Signed-off-by: Christian Richter <crichter@owncloud.com>
This commit is contained in:
@@ -3,15 +3,24 @@ package checks
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"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 {
|
||||
if strings.Contains(address, "0.0.0.0") {
|
||||
outboundIp, err := handlers.GetOutBoundIP()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
address = strings.Replace(address, "0.0.0.0", outboundIp, 1)
|
||||
}
|
||||
|
||||
conn, err := grpc.NewClient(address, grpc.WithTransportCredentials(insecure.NewCredentials()))
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not connect to grpc server: %v", err)
|
||||
|
||||
@@ -3,13 +3,27 @@ package checks
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/owncloud/ocis/v2/ocis-pkg/handlers"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// NewHTTPCheck checks the reachability of a http server.
|
||||
func NewHTTPCheck(url string) func(context.Context) error {
|
||||
return func(_ context.Context) error {
|
||||
if strings.Contains(url, "0.0.0.0") {
|
||||
outboundIp, err := handlers.GetOutBoundIP()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
url = strings.Replace(url, "0.0.0.0", outboundIp, 1)
|
||||
}
|
||||
|
||||
if !strings.HasPrefix(url, "http://") && !strings.HasPrefix(url, "https://") {
|
||||
url = "http://" + url
|
||||
}
|
||||
|
||||
c := http.Client{
|
||||
Timeout: 3 * time.Second,
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"maps"
|
||||
"net"
|
||||
"net/http"
|
||||
|
||||
"golang.org/x/sync/errgroup"
|
||||
@@ -113,3 +114,20 @@ func (h *CheckHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
h.conf.logger.Panic().Err(err).Msg("failed to write response")
|
||||
}
|
||||
}
|
||||
|
||||
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")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user