Compare commits
27 Commits
v20220306.
...
v20220728.
Author | SHA1 | Date | |
---|---|---|---|
91ba7a4dfa | |||
2b05df2b75 | |||
415cfa8ee7 | |||
0bf9fc463a | |||
44cfbce5f1 | |||
3c17eded13 | |||
d3a3969aee | |||
7f01d016a4 | |||
5f3f234fff | |||
18f5d74402 | |||
71611fd754 | |||
310f59e03c | |||
a6992b2373 | |||
31859569fc | |||
d31571fc50 | |||
5eefe15b3b | |||
dbb84f0c0c | |||
6c839d7196 | |||
d2de3225b0 | |||
130a048394 | |||
0be7f66582 | |||
0f014cf9f3 | |||
230e89e2b7 | |||
20e1092604 | |||
2998e1d39d | |||
5821d890b8 | |||
2dfdb1ad74 |
10
.github/workflows/go.yml
vendored
10
.github/workflows/go.yml
vendored
@ -9,6 +9,8 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v2
|
- uses: actions/checkout@v2
|
||||||
|
with:
|
||||||
|
fetch-depth: 0
|
||||||
|
|
||||||
- name: Set up Go
|
- name: Set up Go
|
||||||
uses: actions/setup-go@v2
|
uses: actions/setup-go@v2
|
||||||
@ -17,7 +19,13 @@ jobs:
|
|||||||
|
|
||||||
- name: Test
|
- name: Test
|
||||||
run: go test -v ./...
|
run: go test -v ./...
|
||||||
|
|
||||||
|
- name: Lint source code
|
||||||
|
run: |
|
||||||
|
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)"
|
||||||
|
26
.golangci.yaml
Normal file
26
.golangci.yaml
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
linters:
|
||||||
|
disable-all: true
|
||||||
|
enable:
|
||||||
|
- deadcode
|
||||||
|
- errcheck
|
||||||
|
- gofmt
|
||||||
|
- goimports
|
||||||
|
- gosimple
|
||||||
|
- ineffassign
|
||||||
|
- misspell
|
||||||
|
- staticcheck
|
||||||
|
- structcheck
|
||||||
|
- unconvert
|
||||||
|
- unused
|
||||||
|
- varcheck
|
||||||
|
- govet
|
||||||
|
|
||||||
|
linters-settings:
|
||||||
|
goimports:
|
||||||
|
local-prefixes: github.com/bastiandoetsch/mullvad-best-server
|
||||||
|
|
||||||
|
output:
|
||||||
|
format: tab
|
||||||
|
|
||||||
|
run:
|
||||||
|
deadline: 10m
|
@ -1,19 +1,20 @@
|
|||||||
project_name: mullvad-best-server
|
project_name: mullvad-best-server
|
||||||
# This is an example .goreleaser.yml file with some sensible defaults.
|
|
||||||
# Make sure to check the documentation at https://goreleaser.com
|
|
||||||
before:
|
before:
|
||||||
hooks:
|
hooks:
|
||||||
# You may remove this if you don't use go modules.
|
|
||||||
- go mod tidy
|
- go mod tidy
|
||||||
# you may remove this if you don't need go generate
|
|
||||||
- go generate ./...
|
|
||||||
builds:
|
builds:
|
||||||
- env:
|
- flags:
|
||||||
|
- -trimpath
|
||||||
|
env:
|
||||||
- CGO_ENABLED=0
|
- CGO_ENABLED=0
|
||||||
goos:
|
goos:
|
||||||
- linux
|
- linux
|
||||||
- windows
|
- windows
|
||||||
- darwin
|
- darwin
|
||||||
|
mod_timestamp: "{{ .CommitTimestamp }}"
|
||||||
|
|
||||||
archives:
|
archives:
|
||||||
- replacements:
|
- replacements:
|
||||||
darwin: Darwin
|
darwin: Darwin
|
||||||
@ -21,13 +22,18 @@ archives:
|
|||||||
windows: Windows
|
windows: Windows
|
||||||
386: i386
|
386: i386
|
||||||
amd64: x86_64
|
amd64: x86_64
|
||||||
|
format: binary
|
||||||
|
name_template: "{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}"
|
||||||
|
|
||||||
checksum:
|
checksum:
|
||||||
name_template: 'checksums.txt'
|
name_template: 'checksums.txt'
|
||||||
|
|
||||||
snapshot:
|
snapshot:
|
||||||
name_template: "{{ incpatch .Version }}-next"
|
name_template: "{{ incpatch .Version }}-next"
|
||||||
|
|
||||||
changelog:
|
changelog:
|
||||||
sort: asc
|
sort: asc
|
||||||
filters:
|
filters:
|
||||||
exclude:
|
exclude:
|
||||||
- '^docs:'
|
- '^docs:'
|
||||||
- '^test:'
|
- '^test:'
|
21
LICENSE
Normal file
21
LICENSE
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
MIT License
|
||||||
|
|
||||||
|
Copyright (c) 2022 Bastian Doetsch
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
60
Makefile
Normal file
60
Makefile
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
# project variables
|
||||||
|
PROJECT_NAME := mullvad-best-server
|
||||||
|
|
||||||
|
# build variables
|
||||||
|
.DEFAULT_GOAL = lint
|
||||||
|
BUILD_DIR := dist
|
||||||
|
DEV_GOARCH := $(shell go env GOARCH)
|
||||||
|
DEV_GOOS := $(shell go env GOOS)
|
||||||
|
|
||||||
|
|
||||||
|
## tools: Install required tooling.
|
||||||
|
.PHONY: tools
|
||||||
|
tools:
|
||||||
|
ifeq (,$(wildcard ./.bin/golangci-lint*))
|
||||||
|
@curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b .bin/ v1.44.2
|
||||||
|
else
|
||||||
|
@echo "==> Required tooling is already installed"
|
||||||
|
endif
|
||||||
|
|
||||||
|
## clean: Delete the build directory
|
||||||
|
.PHONY: clean
|
||||||
|
clean:
|
||||||
|
@echo "==> Removing '$(BUILD_DIR)' directory..."
|
||||||
|
@rm -rf $(BUILD_DIR)
|
||||||
|
|
||||||
|
## lint: Lint code with golangci-lint.
|
||||||
|
.PHONY: lint
|
||||||
|
lint: tools
|
||||||
|
@echo "==> Linting code with 'golangci-lint'..."
|
||||||
|
@.bin/golangci-lint run ./...
|
||||||
|
|
||||||
|
## test: Run all unit tests.
|
||||||
|
.PHONY: test
|
||||||
|
test:
|
||||||
|
@echo "==> Running unit tests..."
|
||||||
|
@mkdir -p $(BUILD_DIR)
|
||||||
|
@go test -count=1 -v -cover -coverprofile=$(BUILD_DIR)/coverage.out -parallel=4 ./...
|
||||||
|
|
||||||
|
## build: Build binary for default local system's OS and architecture.
|
||||||
|
.PHONY: build
|
||||||
|
build:
|
||||||
|
@echo "==> Building binary..."
|
||||||
|
@echo " running go build for GOOS=$(DEV_GOOS) GOARCH=$(DEV_GOARCH)"
|
||||||
|
# workaround for missing .exe extension on Windows
|
||||||
|
ifeq ($(OS),Windows_NT)
|
||||||
|
@go build -o $(BUILD_DIR)/$(PROJECT_NAME).$(DEV_GOOS).$(DEV_GOARCH).exe
|
||||||
|
else
|
||||||
|
@go build -o $(BUILD_DIR)/$(PROJECT_NAME).$(DEV_GOOS).$(DEV_GOARCH)
|
||||||
|
endif
|
||||||
|
|
||||||
|
.PHONY: run
|
||||||
|
run:
|
||||||
|
@echo "==> Running $(PROJECT_NAME)"
|
||||||
|
@go run main.go
|
||||||
|
|
||||||
|
help: Makefile
|
||||||
|
@echo "Usage: make <command>"
|
||||||
|
@echo ""
|
||||||
|
@echo "Commands:"
|
||||||
|
@sed -n 's/^##//p' $< | column -t -s ':' | sed -e 's/^/ /'
|
46
README.md
Normal file
46
README.md
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
# mullvad-best-server
|
||||||
|

|
||||||
|
|
||||||
|
Determines the mullvad.net server with the lowest latency.
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
|
Download binary from releases for your platform and unpack.
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
### 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.
|
||||||
|
|
||||||
|
### 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")
|
||||||
|
-l string
|
||||||
|
Log level. Allowed values: trace, debug, info, warn, error, fatal, panic (default "info")
|
||||||
|
-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.
|
||||||
|
|
||||||
|
|
||||||
|
## 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
|
||||||
|
and outputs the server with the lowest ping.
|
||||||
|
|
||||||
|
## Integration into a script
|
||||||
|
I use it on my router like this (yes, I know I could have done the whole thing with jq and shell scripting, but wanted to use go for maintainability).
|
||||||
|
```
|
||||||
|
#!/bin/sh
|
||||||
|
set -e
|
||||||
|
LATEST_RELEASE=$(curl -sSL https://api.github.com/repos/bastiandoetsch/mullvad-best-server/releases/latest | jq -r '.assets[]| .browser_download_url' | grep Linux_arm64)
|
||||||
|
curl -sSL $LATEST_RELEASE > /root/mullvad-best-server
|
||||||
|
chmod +x /root/mullvad-best-server
|
||||||
|
/usr/bin/wg-quick down $(wg show|grep interface | cut -d: -f2) || echo "nothing to shut down"
|
||||||
|
/usr/bin/wg-quick up "mullvad-$(/root/mullvad-best-server -c de)"
|
||||||
|
```
|
14
go.mod
14
go.mod
@ -3,13 +3,15 @@ module github.com/bastiandoetsch/mullvad-best-server
|
|||||||
go 1.17
|
go 1.17
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/go-ping/ping v0.0.0-20211130115550-779d1e919534
|
github.com/go-ping/ping v1.1.0
|
||||||
github.com/rs/zerolog v1.26.1
|
github.com/rs/zerolog v1.27.0
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/google/uuid v1.2.0 // indirect
|
github.com/google/uuid v1.3.0 // indirect
|
||||||
golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d // indirect
|
github.com/mattn/go-colorable v0.1.12 // indirect
|
||||||
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
|
github.com/mattn/go-isatty v0.0.14 // indirect
|
||||||
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e // indirect
|
golang.org/x/net v0.0.0-20220728211354-c7608f3a8462 // indirect
|
||||||
|
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 // indirect
|
||||||
|
golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10 // indirect
|
||||||
)
|
)
|
||||||
|
52
go.sum
52
go.sum
@ -1,41 +1,33 @@
|
|||||||
github.com/coreos/go-systemd/v22 v22.3.2/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
|
github.com/coreos/go-systemd/v22 v22.3.3-0.20220203105225-a9a7ef127534/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
|
||||||
github.com/go-ping/ping v0.0.0-20211130115550-779d1e919534 h1:dhy9OQKGBh4zVXbjwbxxHjRxMJtLXj3zfgpBYQaR4Q4=
|
github.com/go-ping/ping v1.1.0 h1:3MCGhVX4fyEUuhsfwPrsEdQw6xspHkv5zHsiSoDFZYw=
|
||||||
github.com/go-ping/ping v0.0.0-20211130115550-779d1e919534/go.mod h1:xIFjORFzTxqIV/tDVGO4eDy/bLuSyawEeojSm3GfRGk=
|
github.com/go-ping/ping v1.1.0/go.mod h1:xIFjORFzTxqIV/tDVGO4eDy/bLuSyawEeojSm3GfRGk=
|
||||||
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
|
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
|
||||||
github.com/google/uuid v1.2.0 h1:qJYtXnJRWmpe7m/3XlyhrsLrEURqHRM2kxzoxXqyUDs=
|
|
||||||
github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||||
|
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
|
||||||
|
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||||
|
github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40=
|
||||||
|
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
|
||||||
|
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
|
||||||
|
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
|
||||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||||
github.com/rs/xid v1.3.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
|
github.com/rs/xid v1.3.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
|
||||||
github.com/rs/zerolog v1.26.1 h1:/ihwxqH+4z8UxyI70wM1z9yCvkWcfz/a3mj48k/Zngc=
|
github.com/rs/zerolog v1.27.0 h1:1T7qCieN22GVc8S4Q2yuexzBb1EqjbgjSH9RohbMjKs=
|
||||||
github.com/rs/zerolog v1.26.1/go.mod h1:/wSSJWX7lVrsOwlbyTRSOJvqRlc+WjWlfes+CiJ+tmc=
|
github.com/rs/zerolog v1.27.0/go.mod h1:7frBqO0oezxmnO7GF86FY++uy8I0Tk/If5ni1G9Qc0U=
|
||||||
github.com/yuin/goldmark v1.4.0/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
|
|
||||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
|
||||||
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
|
||||||
golang.org/x/crypto v0.0.0-20211215165025-cf75a172585e/go.mod h1:P+XmwS30IXTQdn5tA2iutPOUgjI07+tq3H3K9MVA1s8=
|
|
||||||
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
|
||||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
|
||||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
|
||||||
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
|
||||||
golang.org/x/net v0.0.0-20210316092652-d523dce5a7f4/go.mod h1:RBQZq4jEuRlivfhVLdyRGr576XBO4/greRjx4P4O3yc=
|
golang.org/x/net v0.0.0-20210316092652-d523dce5a7f4/go.mod h1:RBQZq4jEuRlivfhVLdyRGr576XBO4/greRjx4P4O3yc=
|
||||||
golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d h1:20cMwl2fHAzkJMEA+8J4JgqBQcQGzbisXo31MIeenXI=
|
golang.org/x/net v0.0.0-20220728211354-c7608f3a8462 h1:UreQrH7DbFXSi9ZFox6FNT3WBooWmdANpU+IfkT1T4I=
|
||||||
golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
golang.org/x/net v0.0.0-20220728211354-c7608f3a8462/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk=
|
||||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
|
||||||
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ=
|
|
||||||
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 h1:uVc8UZUe6tr40fFVnUP5Oj+veunVezqYl9z7DYw9xzw=
|
||||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20210315160823-c6e025ad8005/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20210315160823-c6e025ad8005/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e h1:WUoyKPm6nCo1BnNUvPGnFG3T5DUVem42yDJZZ4CNxMA=
|
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10 h1:WIoqL4EROvwiPdUtaip4VcDdpZ4kha7wBWZrbVKCIZg=
|
||||||
|
golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||||
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
|
||||||
golang.org/x/tools v0.1.7/go.mod h1:LGqMHiF4EqQNHR1JncWGqT5BVaXmza+X+BDGol+dOxo=
|
|
||||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
|
||||||
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
|
||||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
|
||||||
|
78
main.go
78
main.go
@ -2,49 +2,74 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"github.com/go-ping/ping"
|
"flag"
|
||||||
"github.com/rs/zerolog/log"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"runtime"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/go-ping/ping"
|
||||||
|
"github.com/rs/zerolog"
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
var pings = make(map[string]time.Duration)
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
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")
|
||||||
|
var logLevel = flag.String("l", "info", "Log level. Allowed values: trace, debug, info, warn, error, fatal, panic")
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
servers := getServers()
|
level, err := zerolog.ParseLevel(*logLevel)
|
||||||
bestIndex := selectBestServerIndex(servers)
|
if err != nil {
|
||||||
log.Info().Interface("best", servers[bestIndex]).Msg("Best Latency Server found.")
|
log.Fatal().Err(err).Msg("Unable to set log level")
|
||||||
|
}
|
||||||
|
zerolog.SetGlobalLevel(level)
|
||||||
|
servers := getServers(*typeFlag)
|
||||||
|
bestIndex := selectBestServerIndex(servers, *countryFlag)
|
||||||
|
if bestIndex == -1 {
|
||||||
|
log.Fatal().Str("country", *countryFlag).Msg("No servers for country found.")
|
||||||
|
}
|
||||||
|
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(best)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal().Err(err).Msg("Couldn't marshal server information to Json")
|
||||||
|
}
|
||||||
|
fmt.Println(string(serverJson))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func selectBestServerIndex(servers []server) int {
|
func selectBestServerIndex(servers []server, country string) int {
|
||||||
best := servers[0].Hostname
|
bestIndex := -1
|
||||||
bestIndex := 0
|
var bestPing time.Duration
|
||||||
allowedCountries := map[string]string{}
|
|
||||||
allowedCountries["de"] = "1"
|
|
||||||
allowedCountries["ch"] = "1"
|
|
||||||
allowedCountries["at"] = "1"
|
|
||||||
for i, server := range servers {
|
for i, server := range servers {
|
||||||
if server.Active && allowedCountries[server.CountryCode] != "" {
|
if server.Active && server.CountryCode == country {
|
||||||
duration, err := serverLatency(server)
|
duration, err := serverLatency(server)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
pings[server.Hostname] = duration
|
if bestIndex == -1 || bestPing > duration {
|
||||||
if best == "" || pings[best] > pings[server.Hostname] {
|
|
||||||
best = server.Hostname
|
|
||||||
bestIndex = i
|
bestIndex = i
|
||||||
|
bestPing = duration
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
log.Err(err).Msg("Error determining the server latency via ping.")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return bestIndex
|
return bestIndex
|
||||||
}
|
}
|
||||||
|
|
||||||
func getServers() []server {
|
func getServers(serverType string) []server {
|
||||||
resp, err := http.Get("https://api.mullvad.net/www/relays/wireguard/")
|
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) {
|
||||||
@ -53,23 +78,30 @@ func getServers() []server {
|
|||||||
log.Err(err)
|
log.Err(err)
|
||||||
}
|
}
|
||||||
}(resp.Body)
|
}(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal().Err(err)
|
||||||
|
}
|
||||||
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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//goland:noinspection GoBoolExpressions
|
||||||
func serverLatency(s server) (time.Duration, error) {
|
func serverLatency(s server) (time.Duration, error) {
|
||||||
pinger, err := ping.NewPinger(s.Ipv4AddrIn)
|
pinger, err := ping.NewPinger(s.Ipv4AddrIn)
|
||||||
|
if runtime.GOOS == "windows" {
|
||||||
|
pinger.SetPrivileged(true)
|
||||||
|
}
|
||||||
pinger.Count = 1
|
pinger.Count = 1
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
var duration time.Duration
|
var duration time.Duration
|
||||||
pinger.OnRecv = func(pkt *ping.Packet) {
|
pinger.OnRecv = func(pkt *ping.Packet) {
|
||||||
log.Info().Str("Server", s.Hostname).IPAddr("IP", pkt.IPAddr.IP).Dur("RTT", pkt.Rtt).Msg("Added server latency.")
|
log.Debug().Str("Server", s.Hostname).IPAddr("IP", pkt.IPAddr.IP).Dur("RTT", pkt.Rtt).Msg("Added server latency.")
|
||||||
duration = pkt.Rtt
|
duration = pkt.Rtt
|
||||||
}
|
}
|
||||||
err = pinger.Run()
|
err = pinger.Run()
|
||||||
|
Reference in New Issue
Block a user