Skip to content

Commit

Permalink
Merge pull request #383 from glazychev-art/ipv6_public_ip
Browse files Browse the repository at this point in the history
Fix for IPv6 public IP
  • Loading branch information
denis-tingaikin authored Oct 31, 2022
2 parents 0e35cbc + bb10f42 commit d8fbbae
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 24 deletions.
1 change: 1 addition & 0 deletions internal/imports/imports_linux.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 13 additions & 24 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ package main
import (
"context"
"crypto/tls"
"fmt"
"net"
"net/url"
"os"
Expand Down Expand Up @@ -50,6 +49,7 @@ import (

"github.com/networkservicemesh/sdk/pkg/tools/debug"
"github.com/networkservicemesh/sdk/pkg/tools/grpcutils"
"github.com/networkservicemesh/sdk/pkg/tools/listenonurl"
"github.com/networkservicemesh/sdk/pkg/tools/log"
"github.com/networkservicemesh/sdk/pkg/tools/log/logruslogger"
)
Expand Down Expand Up @@ -159,7 +159,7 @@ func main() {
grpcfd.WithChainUnaryInterceptor(),
)

listenURL := getPublicURL(defaultURL(config))
listenURL := getPublishableURL(config.ListenOn, log.FromContext(ctx))

log.FromContext(ctx).Infof("Listening url: %v", listenURL)

Expand Down Expand Up @@ -200,32 +200,21 @@ func exitOnErr(ctx context.Context, cancel context.CancelFunc, errCh <-chan erro
}(ctx, errCh)
}

func defaultURL(c *Config) *url.URL {
for i := 0; i < len(c.ListenOn); i++ {
u := &c.ListenOn[i]
if u.Scheme == "tcp" {
return u
}
}
return &c.ListenOn[0]
}

func getPublicURL(u *url.URL) *url.URL {
if u.Port() == "" || len(u.Host) != len(":")+len(u.Port()) {
return u
}
func getPublishableURL(listenOn []url.URL, logger log.Logger) *url.URL {
u := defaultURL(listenOn)
addrs, err := net.InterfaceAddrs()
if err != nil {
logrus.Warn(err.Error())
logger.Warn(err.Error())
return u
}
for _, a := range addrs {
if ipnet, ok := a.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
r, _ := url.Parse(fmt.Sprintf("tcp://%v:%v", ipnet.IP.String(), u.Port()))
return r
}
return listenonurl.GetPublicURL(addrs, u)
}
func defaultURL(listenOn []url.URL) *url.URL {
for i := 0; i < len(listenOn); i++ {
u := &listenOn[i]
if u.Scheme == "tcp" {
return u
}
}
return u
return &listenOn[0]
}

0 comments on commit d8fbbae

Please sign in to comment.