From ebd5a373e362f56e451669133ef177b0488bbd4a Mon Sep 17 00:00:00 2001 From: ersonp Date: Mon, 29 Nov 2021 16:01:06 +0530 Subject: [PATCH 1/4] Fix dmsghttp startup --- pkg/visor/init.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pkg/visor/init.go b/pkg/visor/init.go index 1828db292..64dc7abb9 100644 --- a/pkg/visor/init.go +++ b/pkg/visor/init.go @@ -164,6 +164,10 @@ func initDmsgHTTP(ctx context.Context, v *Visor, log *logging.Logger) error { var keys cipher.PubKeys servers := v.conf.Dmsg.Servers + if len(servers) == 0 { + return nil + } + keys = append(keys, v.conf.PK) dClient := direct.NewDirectClient(direct.GetAllEntries(keys, servers)) From 80c2c470fcf6a9443a62925bf1ccc22e1d704778 Mon Sep 17 00:00:00 2001 From: ersonp Date: Mon, 29 Nov 2021 17:42:47 +0530 Subject: [PATCH 2/4] Add dmsgdirect client servers to vpn env This commit adds the vpn servers of the dmsg-direct client to the vpnEnvMaker. For that the dmsgDC is added to the Visor struct. --- pkg/visor/api.go | 2 +- pkg/visor/init.go | 18 ++++++++++++------ pkg/visor/visor.go | 3 ++- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/pkg/visor/api.go b/pkg/visor/api.go index d54b8e4e7..26c252a89 100644 --- a/pkg/visor/api.go +++ b/pkg/visor/api.go @@ -312,7 +312,7 @@ func (v *Visor) StartApp(appName string) error { if appName == skyenv.VPNClientName { // todo: can we use some kind of app start hook that will be used for both autostart // and start? Reason: this is also called in init for autostart - maker := vpnEnvMaker(v.conf, v.dmsgC, v.tpM.STCPRRemoteAddrs()) + maker := vpnEnvMaker(v.conf, v.dmsgC, v.dmsgDC, v.tpM.STCPRRemoteAddrs()) envs, err = maker() if err != nil { return err diff --git a/pkg/visor/init.go b/pkg/visor/init.go index 64dc7abb9..5ccc2c064 100644 --- a/pkg/visor/init.go +++ b/pkg/visor/init.go @@ -171,20 +171,21 @@ func initDmsgHTTP(ctx context.Context, v *Visor, log *logging.Logger) error { keys = append(keys, v.conf.PK) dClient := direct.NewDirectClient(direct.GetAllEntries(keys, servers)) - dmsgD, closeDmsgD, err := direct.StartDmsg(ctx, log, v.conf.PK, v.conf.SK, dClient, dmsg.DefaultConfig()) + dmsgDC, closeDmsgDC, err := direct.StartDmsg(ctx, log, v.conf.PK, v.conf.SK, dClient, dmsg.DefaultConfig()) if err != nil { return fmt.Errorf("failed to start dmsg: %w", err) } - dmsgHTTP := http.Client{Transport: dmsghttp.MakeHTTPTransport(dmsgD)} + dmsgHTTP := http.Client{Transport: dmsghttp.MakeHTTPTransport(dmsgDC)} v.pushCloseStack("dmsg_http", func() error { - closeDmsgD() + closeDmsgDC() return nil }) v.initLock.Lock() v.dClient = dClient v.dmsgHTTP = &dmsgHTTP + v.dmsgDC = dmsgDC v.initLock.Unlock() return nil } @@ -510,8 +511,8 @@ func initLauncher(ctx context.Context, v *Visor, log *logging.Logger) error { } err = launch.AutoStart(launcher.EnvMap{ - skyenv.VPNClientName: vpnEnvMaker(v.conf, v.dmsgC, v.tpM.STCPRRemoteAddrs()), - skyenv.VPNServerName: vpnEnvMaker(v.conf, v.dmsgC, nil), + skyenv.VPNClientName: vpnEnvMaker(v.conf, v.dmsgC, v.dmsgDC, v.tpM.STCPRRemoteAddrs()), + skyenv.VPNServerName: vpnEnvMaker(v.conf, v.dmsgC, v.dmsgDC, nil), }) if err != nil { @@ -528,7 +529,7 @@ func initLauncher(ctx context.Context, v *Visor, log *logging.Logger) error { } // Make an env maker function for vpn application -func vpnEnvMaker(conf *visorconfig.V1, dmsgC *dmsg.Client, tpRemoteAddrs []string) launcher.EnvMaker { +func vpnEnvMaker(conf *visorconfig.V1, dmsgC, dmsgDC *dmsg.Client, tpRemoteAddrs []string) launcher.EnvMaker { return func() ([]string, error) { var envCfg vpn.DirectRoutesEnvConfig @@ -545,6 +546,11 @@ func vpnEnvMaker(conf *visorconfig.V1, dmsgC *dmsg.Client, tpRemoteAddrs []strin return errors.New("no dmsg servers found") } + if dmsgDC != nil { + for _, ses := range dmsgDC.AllSessions() { + envCfg.DmsgServers = append(envCfg.DmsgServers, ses.RemoteTCPAddr().String()) + } + } return nil }) diff --git a/pkg/visor/visor.go b/pkg/visor/visor.go index a28d2ea84..6a82e4178 100644 --- a/pkg/visor/visor.go +++ b/pkg/visor/visor.go @@ -60,7 +60,8 @@ type Visor struct { ebc *appevent.Broadcaster // event broadcaster dmsgC *dmsg.Client - dClient direct.APIClient // dmsg direct client + dmsgDC *dmsg.Client // dmsg direct client + dClient direct.APIClient // dmsg direct api client dmsgHTTP *http.Client // dmsghttp client trackers *dmsgtracker.Manager From 5548083f740d1e478064e49b186a118e93d3c9e0 Mon Sep 17 00:00:00 2001 From: ersonp Date: Mon, 29 Nov 2021 17:44:02 +0530 Subject: [PATCH 3/4] Fix nil pointer error This commit fixes the nil pointer error by adding the dmsg url check in ParseIP(). --- internal/vpn/client.go | 10 ++++++---- internal/vpn/env.go | 14 +++++++++++++- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/internal/vpn/client.go b/internal/vpn/client.go index 381742be3..4e206ad05 100644 --- a/internal/vpn/client.go +++ b/internal/vpn/client.go @@ -792,11 +792,13 @@ func filterOutEqualIPs(ips []net.IP) []net.IP { ipsSet := make(map[string]struct{}) var filteredIPs []net.IP for _, ip := range ips { - ipStr := ip.String() + if ip != nil { + ipStr := ip.String() - if _, ok := ipsSet[ipStr]; !ok { - filteredIPs = append(filteredIPs, ip) - ipsSet[ip.String()] = struct{}{} + if _, ok := ipsSet[ipStr]; !ok { + filteredIPs = append(filteredIPs, ip) + ipsSet[ip.String()] = struct{}{} + } } } diff --git a/internal/vpn/env.go b/internal/vpn/env.go index 298e35427..a10e63dfe 100644 --- a/internal/vpn/env.go +++ b/internal/vpn/env.go @@ -3,12 +3,12 @@ package vpn import ( "fmt" "net" - "net/url" "os" "strconv" "strings" "github.com/skycoin/dmsg/cipher" + "github.com/skycoin/dmsg/dmsgget" ) const ( @@ -127,12 +127,24 @@ func IPFromEnv(key string) (net.IP, bool, error) { // - domain without port; // - IP with port; // - IP without port. +// - dmshhttp url with port; +// - dmshhttp url without port. // In case domain is provided instead of an IP address, a DNS lookup is also // performed to resolve the actual IP address func ParseIP(addr string) (net.IP, bool, error) { if addr == "" { return nil, false, nil } + var url dmsgget.URL + + // in case dmsghttp url is provided + err := url.Fill(addr) + if url.Scheme == "dmsg" { + if err != nil { + return nil, false, err + } + return nil, true, nil + } // in case whole URL is passed with the scheme if strings.Contains(addr, "://") { From afa702ebf874594314dd9b9176c293645fea7d5c Mon Sep 17 00:00:00 2001 From: ersonp Date: Mon, 29 Nov 2021 17:59:19 +0530 Subject: [PATCH 4/4] Exclude dmsghttp-config.json from gitignore --- .gitignore | 1 + dmsghttp-config.json | 52 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 dmsghttp-config.json diff --git a/.gitignore b/.gitignore index 1d597f705..731ac7fa1 100644 --- a/.gitignore +++ b/.gitignore @@ -30,6 +30,7 @@ pkg/visor/foo/ /*-cli /*-server /*.json +!/dmsghttp-config.json /*.sh /*.log diff --git a/dmsghttp-config.json b/dmsghttp-config.json new file mode 100644 index 000000000..4b8d47775 --- /dev/null +++ b/dmsghttp-config.json @@ -0,0 +1,52 @@ +{ + "test": { + "dmsg_servers": [ + { + "static":"024716428e6315d954356e9ad72bea32bb2b41aab5a54a9b5cb4313964016e64d8", + "server":{ + "address":"172.104.188.39:30080" + } + }, + { + "static":"03f6b0a20be8fe0fd2fd0bd850507cfb10d0eaa37dce5c174654d07db5749a41bf", + "server":{ + "address":"192.53.115.181:8083" + } + }, + { + "static":"02ae49c901480b49f9c40f6f90fa8e775cf9b00b61ded673a9b0776dfb7cccd374", + "server":{ + "address":"139.162.45.141:8083" + } + } + ], + "dmsg_discovery": "dmsg://03cd2336e5de74bdab2bbdb44b06b0c8c713a5ee9029615f5526f8c99a6afe87b8:80", + "transport_discovery": "dmsg://02703cf828ea11d25b2c8eb0796132ecc7e53b22325b20ce3674ce5cd8693e4fb6:80", + "address_resolver": "dmsg://030eb7d8cf6eac40c19bbc433de6d6b9bb7a47f2e1d7095c6a01aa676471670ad2:80", + "route_finder": "dmsg://02ece5b69eaee13ef967b7eb67ca93f1dfddad3a51c9cb1808c4bd0d8d8aa32053:80", + "uptime_tracker": "dmsg://022c788cca11f208cdfd83ed0c2a8c7b661221736c461adc7c6738a2c1b041c7f8:80", + "service_discovery": "dmsg://038f751df4af75fb3d51f6693602bfe8289145e633ffdd1e67d686bea595f84d55:80" + }, + "prod": { + "dmsg_servers": [ + { + "static":"03832194c823a39c7b78ff08bddacbd6d6541def182a3f195973ebe7da16fe1b53", + "server":{ + "address":"139.162.45.141:8081" + } + }, + { + "static":"02e4660279c83bc6ca0122d3a78c0cb3f3564e03e04876ae7fa30b4e0a63217425", + "server":{ + "address":"192.53.115.181:8081" + } + } + ], + "dmsg_discovery": "dmsg://pk:port", + "transport_discovery": "dmsg://pk:port", + "address_resolver": "dmsg://pk:port", + "route_finder": "dmsg://pk:port", + "uptime_tracker": "dmsg://pk:port", + "service_discovery": "dmsg://pk:port" + } +}