From 8e9edcc36f2becf17c4523038e0cf348942e6ce7 Mon Sep 17 00:00:00 2001 From: MohammadReza Palide Date: Tue, 5 Jul 2022 10:48:49 +0430 Subject: [PATCH 1/6] fix nil pointer error --- pkg/visor/init.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pkg/visor/init.go b/pkg/visor/init.go index bcb77aca35..a8cc0c50a9 100644 --- a/pkg/visor/init.go +++ b/pkg/visor/init.go @@ -1181,7 +1181,9 @@ func getPublicIP(v *Visor, service string) (pIP string, err error) { // Wait until stun client is ready <-v.stunReady - + if v.stunClient.PublicIP == nil { + return "", fmt.Errorf("all STUN servers are offline, use direct URL config instead dmsghttp") + } pIP = v.stunClient.PublicIP.IP() return pIP, nil } From 09d05979aec701aaa4d664da4cf6d4ef13388b0d Mon Sep 17 00:00:00 2001 From: MohammadReza Palide Date: Tue, 5 Jul 2022 13:56:39 +0430 Subject: [PATCH 2/6] separate Public IP from NAT --- pkg/visor/init.go | 46 +++++++++++++++++++++++++++++++++------------- 1 file changed, 33 insertions(+), 13 deletions(-) diff --git a/pkg/visor/init.go b/pkg/visor/init.go index a8cc0c50a9..7990fa1c06 100644 --- a/pkg/visor/init.go +++ b/pkg/visor/init.go @@ -2,8 +2,10 @@ package visor import ( "context" + "encoding/json" "errors" "fmt" + "io/ioutil" "net" "net/http" "os" @@ -210,7 +212,7 @@ func initAddressResolver(ctx context.Context, v *Visor, log *logging.Logger) err } // only needed for dmsghttp - pIP, err := getPublicIP(v, conf.AddressResolver) + pIP, err := getPublicIP(conf.AddressResolver) if err != nil { return err } @@ -254,7 +256,7 @@ func initDiscovery(ctx context.Context, v *Visor, log *logging.Logger) error { factory.ServiceDisc = conf.ServiceDisc factory.Client = httpC // only needed for dmsghttp - pIP, err := getPublicIP(v, conf.ServiceDisc) + pIP, err := getPublicIP(conf.ServiceDisc) if err != nil { return err } @@ -790,7 +792,7 @@ func initUptimeTracker(ctx context.Context, v *Visor, log *logging.Logger) error return err } - pIP, err := getPublicIP(v, conf.Addr) + pIP, err := getPublicIP(conf.Addr) if err != nil { return err } @@ -992,7 +994,7 @@ func initPublicAutoconnect(ctx context.Context, v *Visor, log *logging.Logger) e DiscAddr: serviceDisc, } // only needed for dmsghttp - pIP, err := getPublicIP(v, serviceDisc) + pIP, err := getPublicIP(serviceDisc) if err != nil { return err } @@ -1065,7 +1067,7 @@ func connectToTpDisc(ctx context.Context, v *Visor, log *logging.Logger) (transp } // only needed for dmsghttp - pIP, err := getPublicIP(v, conf.AddressResolver) + pIP, err := getPublicIP(conf.AddressResolver) if err != nil { return nil, err } @@ -1167,9 +1169,10 @@ func getHTTPClient(ctx context.Context, v *Visor, service string) (*http.Client, return &http.Client{}, nil } -func getPublicIP(v *Visor, service string) (pIP string, err error) { +func getPublicIP(service string) (string, error) { var serviceURL dmsgget.URL - err = serviceURL.Fill(service) + var pIP string + err := serviceURL.Fill(service) // only get the IP from the stun client if the url is of dmsg // else just send empty string as ip if serviceURL.Scheme != "dmsg" { @@ -1179,11 +1182,28 @@ func getPublicIP(v *Visor, service string) (pIP string, err error) { return pIP, fmt.Errorf("provided URL is invalid: %w", err) } - // Wait until stun client is ready - <-v.stunReady - if v.stunClient.PublicIP == nil { - return "", fmt.Errorf("all STUN servers are offline, use direct URL config instead dmsghttp") - } - pIP = v.stunClient.PublicIP.IP() + pIP = getip2() return pIP, nil } + +type ipAPI struct { + Query string +} + +func getip2() string { + req, err := http.Get("http://ip-api.com/json/") + if err != nil { + return err.Error() + } + defer req.Body.Close() + + body, err := ioutil.ReadAll(req.Body) + if err != nil { + return err.Error() + } + + var ip ipAPI + json.Unmarshal(body, &ip) + + return ip.Query +} From b967ae39894c97bb3e73e4eca72e3b847b048462 Mon Sep 17 00:00:00 2001 From: MohammadReza Palide Date: Tue, 5 Jul 2022 14:02:07 +0430 Subject: [PATCH 3/6] make check && make format --- go.sum | 3 --- pkg/visor/init.go | 21 ++++++++++++++------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/go.sum b/go.sum index 14c6ed80a5..8ed92953cf 100644 --- a/go.sum +++ b/go.sum @@ -474,15 +474,12 @@ github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMB github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE= github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= -github.com/skycoin/dmsg v0.0.0-20220617100223-c17f98a92a47 h1:d/KdILjeiZOj3QFOm8KkOwIr5wwx9zWCl+oGVXPln1o= -github.com/skycoin/dmsg v0.0.0-20220617100223-c17f98a92a47/go.mod h1:7ixxeJVjbe3lxDkI4Yizj/TWoafYxs8cPJfxjlDeG+w= github.com/skycoin/dmsg v0.0.0-20220704102949-fece1bd9c40c h1:v7d+0yOp066U8FmcUdQ0Nh9Q+qshBO7+w3ZybGJlBnk= github.com/skycoin/dmsg v0.0.0-20220704102949-fece1bd9c40c/go.mod h1:7ixxeJVjbe3lxDkI4Yizj/TWoafYxs8cPJfxjlDeG+w= github.com/skycoin/noise v0.0.0-20180327030543-2492fe189ae6 h1:1Nc5EBY6pjfw1kwW0duwyG+7WliWz5u9kgk1h5MnLuA= github.com/skycoin/noise v0.0.0-20180327030543-2492fe189ae6/go.mod h1:UXghlricA7J3aRD/k7p/zBObQfmBawwCxIVPVjz2Q3o= github.com/skycoin/skycoin v0.27.1 h1:HatxsRwVSPaV4qxH6290xPBmkH/HgiuAoY2qC+e8C9I= github.com/skycoin/skycoin v0.27.1/go.mod h1:78nHjQzd8KG0jJJVL/j0xMmrihXi70ti63fh8vXScJw= -github.com/skycoin/skywire-utilities v0.0.0-20220617085111-5c8c8d3ced14 h1:u8NYb1CX3l3I6wy6RWy2lQM6iqA6XbFex6Q/4MmEyfA= github.com/skycoin/skywire-utilities v0.0.0-20220617085111-5c8c8d3ced14/go.mod h1:B63p56igl38Ha+zjqi26d2om6XEe9jozwB6kzAWMnm0= github.com/skycoin/skywire-utilities v0.0.0-20220630144749-6ea8913bf1e8 h1:xUPi4duqObtDt4BYiNhbwssiUOFTor67Nftqx1F6/uc= github.com/skycoin/skywire-utilities v0.0.0-20220630144749-6ea8913bf1e8/go.mod h1:B63p56igl38Ha+zjqi26d2om6XEe9jozwB6kzAWMnm0= diff --git a/pkg/visor/init.go b/pkg/visor/init.go index 7990fa1c06..0db12e9260 100644 --- a/pkg/visor/init.go +++ b/pkg/visor/init.go @@ -1182,7 +1182,11 @@ func getPublicIP(service string) (string, error) { return pIP, fmt.Errorf("provided URL is invalid: %w", err) } - pIP = getip2() + pIP, err = getip2() + if err != nil { + return pIP, fmt.Errorf("cannot fetch public ip") + } + return pIP, nil } @@ -1190,20 +1194,23 @@ type ipAPI struct { Query string } -func getip2() string { +func getip2() (string, error) { req, err := http.Get("http://ip-api.com/json/") if err != nil { - return err.Error() + return "", err } - defer req.Body.Close() + defer req.Body.Close() // nolint body, err := ioutil.ReadAll(req.Body) if err != nil { - return err.Error() + return "", err } var ip ipAPI - json.Unmarshal(body, &ip) + err = json.Unmarshal(body, &ip) + if err != nil { + return "", err + } - return ip.Query + return ip.Query, nil } From d35281382353f646844dd2c6e4de16fb29c51289 Mon Sep 17 00:00:00 2001 From: MohammadReza Palide Date: Tue, 5 Jul 2022 14:24:01 +0430 Subject: [PATCH 4/6] fix review suggestions --- pkg/visor/init.go | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/pkg/visor/init.go b/pkg/visor/init.go index 0db12e9260..05248793d7 100644 --- a/pkg/visor/init.go +++ b/pkg/visor/init.go @@ -212,7 +212,7 @@ func initAddressResolver(ctx context.Context, v *Visor, log *logging.Logger) err } // only needed for dmsghttp - pIP, err := getPublicIP(conf.AddressResolver) + pIP, err := getPublicIP(v, conf.AddressResolver) if err != nil { return err } @@ -256,7 +256,7 @@ func initDiscovery(ctx context.Context, v *Visor, log *logging.Logger) error { factory.ServiceDisc = conf.ServiceDisc factory.Client = httpC // only needed for dmsghttp - pIP, err := getPublicIP(conf.ServiceDisc) + pIP, err := getPublicIP(v, conf.ServiceDisc) if err != nil { return err } @@ -792,7 +792,7 @@ func initUptimeTracker(ctx context.Context, v *Visor, log *logging.Logger) error return err } - pIP, err := getPublicIP(conf.Addr) + pIP, err := getPublicIP(v, conf.Addr) if err != nil { return err } @@ -994,7 +994,7 @@ func initPublicAutoconnect(ctx context.Context, v *Visor, log *logging.Logger) e DiscAddr: serviceDisc, } // only needed for dmsghttp - pIP, err := getPublicIP(serviceDisc) + pIP, err := getPublicIP(v, serviceDisc) if err != nil { return err } @@ -1067,7 +1067,7 @@ func connectToTpDisc(ctx context.Context, v *Visor, log *logging.Logger) (transp } // only needed for dmsghttp - pIP, err := getPublicIP(conf.AddressResolver) + pIP, err := getPublicIP(v, conf.AddressResolver) if err != nil { return nil, err } @@ -1169,7 +1169,7 @@ func getHTTPClient(ctx context.Context, v *Visor, service string) (*http.Client, return &http.Client{}, nil } -func getPublicIP(service string) (string, error) { +func getPublicIP(v *Visor, service string) (string, error) { var serviceURL dmsgget.URL var pIP string err := serviceURL.Fill(service) @@ -1182,20 +1182,28 @@ func getPublicIP(service string) (string, error) { return pIP, fmt.Errorf("provided URL is invalid: %w", err) } - pIP, err = getip2() + pIP, err = getIP() if err != nil { - return pIP, fmt.Errorf("cannot fetch public ip") + <-v.stunReady + if v.stunClient.PublicIP != nil { + pIP = v.stunClient.PublicIP.IP() + return pIP, nil + } + err = fmt.Errorf("cannot fetch public ip") + } + if err != nil { + return pIP, err } return pIP, nil } type ipAPI struct { - Query string + PublicIP string `json:"public_ip"` } -func getip2() (string, error) { - req, err := http.Get("http://ip-api.com/json/") +func getIP() (string, error) { + req, err := http.Get("http://ip.skycoin.com") if err != nil { return "", err } @@ -1212,5 +1220,5 @@ func getip2() (string, error) { return "", err } - return ip.Query, nil + return ip.PublicIP, nil } From c0acf64a5bad193982d5fc89edb660ea057b7f3a Mon Sep 17 00:00:00 2001 From: MohammadReza Palide Date: Tue, 5 Jul 2022 16:02:33 +0430 Subject: [PATCH 5/6] fix json variable --- pkg/visor/init.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/visor/init.go b/pkg/visor/init.go index 05248793d7..7f09cb3fcd 100644 --- a/pkg/visor/init.go +++ b/pkg/visor/init.go @@ -1199,7 +1199,7 @@ func getPublicIP(v *Visor, service string) (string, error) { } type ipAPI struct { - PublicIP string `json:"public_ip"` + PublicIP string `json:"ip_address"` } func getIP() (string, error) { From 7b3eae64e85501bad4c0f7ccace78ab313b45e4d Mon Sep 17 00:00:00 2001 From: MohammadReza Palide Date: Tue, 5 Jul 2022 16:37:07 +0430 Subject: [PATCH 6/6] fix comment --- pkg/visor/init.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/visor/init.go b/pkg/visor/init.go index 7f09cb3fcd..e226ad76ce 100644 --- a/pkg/visor/init.go +++ b/pkg/visor/init.go @@ -1173,7 +1173,7 @@ func getPublicIP(v *Visor, service string) (string, error) { var serviceURL dmsgget.URL var pIP string err := serviceURL.Fill(service) - // only get the IP from the stun client if the url is of dmsg + // only get the IP if the url is of dmsg // else just send empty string as ip if serviceURL.Scheme != "dmsg" { return pIP, nil