Compare commits

..

7 Commits

4 changed files with 20 additions and 8 deletions

View File

@ -23,7 +23,9 @@ jobs:
- name: Lint source code - name: Lint source code
run: | run: |
make tools lint make tools lint
rm -rf .bin/
rm -rf dist/
- name: Create release tag - name: Create release tag
run: | run: |
git tag "v$(git show -s --format=%cd --date=format:%Y%m%d.%H%M%S)" git tag "v$(git show -s --format=%cd --date=format:%Y%m%d.%H%M%S)"

View File

@ -7,7 +7,7 @@ before:
builds: builds:
- flags: - flags:
- -trimpath - -trimpath
- env: env:
- CGO_ENABLED=0 - CGO_ENABLED=0
goos: goos:
- linux - linux
@ -36,4 +36,4 @@ changelog:
filters: filters:
exclude: exclude:
- '^docs:' - '^docs:'
- '^test:' - '^test:'

View File

@ -17,6 +17,8 @@ Execute `mullvad-best-server`. It outputs the code, e.g. `de05`. You can then co
Usage of dist/mullvad-best-server_darwin_amd64/mullvad-best-server: Usage of dist/mullvad-best-server_darwin_amd64/mullvad-best-server:
-c string -c string
Server country code, e.g. ch for Switzerland (default "ch") Server country code, e.g. ch for Switzerland (default "ch")
-l string
Log level. Allowed values: trace, debug, info, warn, error, fatal, panic (default "info")
-o string -o string
Output format. 'json' outputs server json Output format. 'json' outputs server json
-t string -t string
@ -28,7 +30,7 @@ The `-c` flag allows to give a country code. Else `ch` will be used.
## Background ## Background
The program uses `https://api.mullvad.net/www/relays/<SERVER_TYPE/` to get the current server list, pings the ones with the right country The program uses `https://api.mullvad.net/www/relays/<SERVER_TYPE>/` to get the current server list, pings the ones with the right country
and outputs the server with the lowest ping. and outputs the server with the lowest ping.
## Integration into a script ## Integration into a script

16
main.go
View File

@ -16,14 +16,22 @@ import (
) )
func main() { func main() {
zerolog.SetGlobalLevel(zerolog.InfoLevel)
var outputFlag = flag.String("o", "", "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 countryFlag = flag.String("c", "ch", "Server country code, e.g. ch for Switzerland")
var typeFlag = flag.String("t", "wireguard", "Server type, e.g. wireguard") var typeFlag = flag.String("t", "wireguard", "Server type, e.g. wireguard")
var logLevel = flag.String("l", "info", "Log level. Allowed values: trace, debug, info, warn, error, fatal, panic")
flag.Parse() flag.Parse()
level, err := zerolog.ParseLevel(*logLevel)
if err != nil {
log.Fatal().Err(err).Msg("Unable to set log level")
}
zerolog.SetGlobalLevel(level)
servers := getServers(*typeFlag) servers := getServers(*typeFlag)
bestIndex := selectBestServerIndex(servers, *countryFlag) bestIndex := selectBestServerIndex(servers, *countryFlag)
if bestIndex == -1 {
log.Fatal().Str("country", *countryFlag).Msg("No servers for country found.")
}
best := servers[bestIndex] best := servers[bestIndex]
log.Debug().Interface("server", best).Msg("Best latency server found.") log.Debug().Interface("server", best).Msg("Best latency server found.")
hostname := strings.TrimSuffix(best.Hostname, "-wireguard") hostname := strings.TrimSuffix(best.Hostname, "-wireguard")
@ -32,7 +40,7 @@ func main() {
} else { } else {
serverJson, err := json.Marshal(best) serverJson, err := json.Marshal(best)
if err != nil { if err != nil {
log.Fatal().Err(err) log.Fatal().Err(err).Msg("Couldn't marshal server information to Json")
} }
fmt.Println(string(serverJson)) fmt.Println(string(serverJson))
} }
@ -58,7 +66,7 @@ func selectBestServerIndex(servers []server, country string) int {
func getServers(serverType string) []server { func getServers(serverType string) []server {
resp, err := http.Get("https://api.mullvad.net/www/relays/" + serverType + "/") resp, err := http.Get("https://api.mullvad.net/www/relays/" + serverType + "/")
if err != nil { if err != nil {
log.Fatal().Err(err) log.Fatal().Err(err).Msg("Couldn't retrieve servers")
} }
responseBody, err := ioutil.ReadAll(resp.Body) responseBody, err := ioutil.ReadAll(resp.Body)
defer func(Body io.ReadCloser) { defer func(Body io.ReadCloser) {
@ -73,7 +81,7 @@ func getServers(serverType string) []server {
var servers []server var servers []server
err = json.Unmarshal(responseBody, &servers) err = json.Unmarshal(responseBody, &servers)
if err != nil { if err != nil {
log.Fatal().Err(err) log.Fatal().Err(err).Msg("couldn't unmarshall server json")
} }
return servers return servers
} }