add support to select server type
This commit is contained in:
parent
6c839d7196
commit
dbb84f0c0c
18
README.md
18
README.md
@ -11,11 +11,21 @@ Download binary from releases for your platform and unpack.
|
||||
### Default usage
|
||||
Execute `mullvad-best-server`. It outputs the code, e.g. `de05`. You can then connect to it with e.g. wireguard using the normal shell scripts.
|
||||
|
||||
### Extended Output
|
||||
If you want the full server information, execute `mullvad-best-server -o json`. It returns the full json output of the server information.
|
||||
### Command line parameters
|
||||
|
||||
```angular2html
|
||||
Usage of dist/mullvad-best-server_darwin_amd64/mullvad-best-server:
|
||||
-c string
|
||||
Server country code, e.g. ch for Switzerland (default "ch")
|
||||
-o string
|
||||
Output format. 'json' outputs server json
|
||||
-t string
|
||||
Server type, e.g. wireguard (default "wireguard")
|
||||
```
|
||||
|
||||
If you want the full server information, execute `mullvad-best-server -o json`. It returns the full json output of the server information.
|
||||
The `-c` flag allows to give a country code. Else `ch` will be used.
|
||||
|
||||
### Specify Country
|
||||
The -c flag allows to give a country code. Else de/at/ch will be used.
|
||||
|
||||
## Background
|
||||
The program uses `https://api.mullvad.net/www/relays/wireguard/` to get the current server list, pings the ones with the right country
|
||||
|
16
main.go
16
main.go
@ -18,18 +18,20 @@ var pings = make(map[string]time.Duration)
|
||||
|
||||
func main() {
|
||||
zerolog.SetGlobalLevel(zerolog.InfoLevel)
|
||||
var outputFlag = flag.String("o", "short", "Output format. 'json' outputs server json")
|
||||
var outputFlag = flag.String("o", "", "Output format. 'json' outputs server json")
|
||||
var countryFlag = flag.String("c", "ch", "Server country code, e.g. ch for Switzerland")
|
||||
var typeFlag = flag.String("t", "wireguard", "Server type, e.g. wireguard")
|
||||
flag.Parse()
|
||||
|
||||
servers := getServers()
|
||||
servers := getServers(*typeFlag)
|
||||
bestIndex := selectBestServerIndex(servers, countryFlag)
|
||||
log.Debug().Interface("server", servers[bestIndex]).Msg("Best latency server found.")
|
||||
hostname := strings.Split(servers[bestIndex].Hostname, "-")[0]
|
||||
best := servers[bestIndex]
|
||||
log.Debug().Interface("server", best).Msg("Best latency server found.")
|
||||
hostname := strings.TrimSuffix(best.Hostname, "-wireguard")
|
||||
if *outputFlag != "json" {
|
||||
fmt.Println(hostname)
|
||||
} else {
|
||||
serverJson, err := json.Marshal(servers[bestIndex])
|
||||
serverJson, err := json.Marshal(best)
|
||||
if err != nil {
|
||||
log.Fatal().Err(err)
|
||||
}
|
||||
@ -63,8 +65,8 @@ func selectBestServerIndex(servers []server, country *string) int {
|
||||
return bestIndex
|
||||
}
|
||||
|
||||
func getServers() []server {
|
||||
resp, err := http.Get("https://api.mullvad.net/www/relays/wireguard/")
|
||||
func getServers(serverType string) []server {
|
||||
resp, err := http.Get("https://api.mullvad.net/www/relays/" + serverType + "/")
|
||||
if err != nil {
|
||||
log.Fatal().Err(err)
|
||||
}
|
||||
|
Reference in New Issue
Block a user