Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

--best-protocol flag #1069

Merged
merged 1 commit into from
Jan 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions cmd/skywire-cli/commands/config/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package config

import (
"encoding/json"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -38,6 +40,7 @@ var (
enableAUTH bool
selectedOS string
disableApps string
bestProtocol bool
)

func init() {
Expand All @@ -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{
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}