Skip to content

Commit

Permalink
simplify code
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Richter <crichter@owncloud.com>
  • Loading branch information
dragonchaser authored and micbar committed Oct 24, 2024
1 parent 6f789f2 commit d8a12ee
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 31 deletions.
12 changes: 3 additions & 9 deletions ocis-pkg/checks/checkgrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package checks
import (
"context"
"fmt"
"strings"

"github.com/owncloud/ocis/v2/ocis-pkg/handlers"
"google.golang.org/grpc"
Expand All @@ -13,14 +12,9 @@ import (
// 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") || strings.Contains(address, "::") {
outboundIp, err := handlers.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)
address, err := handlers.FailSaveAddress(address)
if err != nil {
return err
}

conn, err := grpc.NewClient(address, grpc.WithTransportCredentials(insecure.NewCredentials()))
Expand Down
14 changes: 5 additions & 9 deletions ocis-pkg/checks/checkhttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,19 @@ package checks
import (
"context"
"fmt"
"github.com/owncloud/ocis/v2/ocis-pkg/handlers"
"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 {
if strings.Contains(url, "0.0.0.0") || strings.Contains(url, "::") {
outboundIp, err := handlers.GetOutBoundIP()
if err != nil {
return err
}
url = strings.Replace(url, "0.0.0.0", outboundIp, 1)
url = strings.Replace(url, "::", outboundIp, 1)
url = strings.Replace(url, "[::]", "["+outboundIp+"]", 1)
url, err := handlers.FailSaveAddress(url)
if err != nil {
return err
}

if !strings.HasPrefix(url, "http://") && !strings.HasPrefix(url, "https://") {
Expand Down
15 changes: 5 additions & 10 deletions ocis-pkg/checks/checktcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,18 @@ package checks

import (
"context"
"github.com/owncloud/ocis/v2/ocis-pkg/handlers"
"net"
"strings"
"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 {
if strings.Contains(address, "0.0.0.0") || strings.Contains(address, "::") {
outboundIp, err := handlers.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)
address, err := handlers.FailSaveAddress(address)
if err != nil {
return err
}

conn, err := net.DialTimeout("tcp", address, 3*time.Second)
Expand Down
21 changes: 18 additions & 3 deletions ocis-pkg/handlers/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (
"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.
Expand Down Expand Up @@ -115,7 +115,22 @@ func (h *CheckHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
}

func GetOutBoundIP() (string, error) {
// 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
Expand Down

0 comments on commit d8a12ee

Please sign in to comment.