From b331fd9455e101c9e44f9a240c5dfd4ee2c7ad5a Mon Sep 17 00:00:00 2001 From: MohammadReza Palide Date: Wed, 26 Jan 2022 16:57:17 +0330 Subject: [PATCH] best-protocol flag to set dmsghttp or direct url automatically --- CHANGELOG.md | 1 + cmd/skywire-cli/commands/config/gen.go | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4496fbef74..eb2d2b4f27 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,6 +28,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - added `--os` flag to `skywire-cli config gen` - added `--disable-apps` flag to `skywire-cli config gen` - added `--disable-auth` and `--enable-auth` flags to `skywire-cli config gen` +- added `--best-protocol` flag to `skywire-cli config gen` ## 0.5.0 ### Changed diff --git a/cmd/skywire-cli/commands/config/gen.go b/cmd/skywire-cli/commands/config/gen.go index ed1646d874..fc5365a993 100644 --- a/cmd/skywire-cli/commands/config/gen.go +++ b/cmd/skywire-cli/commands/config/gen.go @@ -2,7 +2,9 @@ package config import ( "encoding/json" + "io" "io/ioutil" + "net/http" "os" "path/filepath" "strings" @@ -38,6 +40,7 @@ var ( enableAUTH bool selectedOS string disableApps string + bestProtocol bool ) func init() { @@ -56,6 +59,7 @@ func init() { genConfigCmd.Flags().BoolVar(&enableAUTH, "enable-auth", false, "enable auth on hypervisor UI.") genConfigCmd.Flags().StringVar(&selectedOS, "os", "linux", "generate configuration with paths for 'macos' or 'windows'") genConfigCmd.Flags().StringVar(&disableApps, "disable-apps", "", "set list of apps to disable, separated by ','") + genConfigCmd.Flags().BoolVarP(&bestProtocol, "best-protocol", "b", false, "choose best protocol (dmsg / direct) to connect based on location") } var genConfigCmd = &cobra.Command{ @@ -140,6 +144,12 @@ var genConfigCmd = &cobra.Command{ } } + if bestProtocol { + if dmsgProtocol() { + dmsgHTTP = true + } + } + // Use dmsg urls for services and add dmsg-servers if dmsgHTTP { var dmsgHTTPServersList visorconfig.DmsgHTTPServers @@ -267,3 +277,18 @@ func readOldConfig(log *logging.MasterLogger, confPath string, replace bool) (*v return conf, true } + +func dmsgProtocol() bool { + resp, err := http.Get("https://ipinfo.io/country") + if err != nil { + return false + } + respBody, err := io.ReadAll(resp.Body) + if err != nil { + return false + } + if string(respBody)[:2] == "CN" { + return true + } + return false +}