From a6a55a0ace45e9ddd6344198968472c3e1ac2bfd Mon Sep 17 00:00:00 2001 From: "kayos@tcp.direct" Date: Thu, 30 Jun 2022 07:57:07 -0700 Subject: [PATCH] Drop in jsoniter, lint formatting, prepare for endpoint retrieval --- api.go | 50 +++++++++++++++++++++++++++++++++----------------- check.go | 14 +++++++------- check_test.go | 1 - go.mod | 7 +++++++ go.sum | 15 +++++++++++++++ relays.go | 8 ++++++++ 6 files changed, 70 insertions(+), 25 deletions(-) create mode 100644 relays.go diff --git a/api.go b/api.go index 3b6aaef..06d26f1 100644 --- a/api.go +++ b/api.go @@ -1,13 +1,25 @@ package mullsox +import jsoniter "github.com/json-iterator/go" + +var json = jsoniter.ConfigCompatibleWithStandardLibrary + +const ( + baseDomain = "mullvad.net" + baseEndpoint = "am.i." + baseDomain + ipv4Endpoint = `https://ipv4.` + baseEndpoint + ipv6Endpoint = `https://ipv6.` + baseEndpoint + servEndpoint = `https://api.` + baseDomain + `www/relays/all/` +) + type MyIPDetails struct { - Ip string `json:"ip"` + IP string `json:"ip"` Country string `json:"country"` City string `json:"city"` Longitude float64 `json:"longitude"` Latitude float64 `json:"latitude"` - MullvadExitIp bool `json:"mullvad_exit_ip"` - MullvadExitIpHostname string `json:"mullvad_exit_ip_hostname"` + MullvadExitIP bool `json:"mullvad_exit_ip"` + MullvadExitIPHostname string `json:"mullvad_exit_ip_hostname"` MullvadServerType string `json:"mullvad_server_type"` Blacklisted struct { Blacklisted bool `json:"blacklisted"` @@ -21,18 +33,22 @@ type MyIPDetails struct { } type MullvadServer struct { - Hostname string `json:"hostname"` - CountryCode string `json:"country_code"` - CountryName string `json:"country_name"` - CityCode string `json:"city_code"` - CityName string `json:"city_name"` - Active bool `json:"active"` - Owned bool `json:"owned"` - Provider string `json:"provider"` - Ipv4AddrIn string `json:"ipv4_addr_in"` - Ipv6AddrIn string `json:"ipv6_addr_in"` - NetworkPortSpeed int `json:"network_port_speed"` - Pubkey string `json:"pubkey"` - MultihopPort int `json:"multihop_port"` - SocksName string `json:"socks_name"` + Hostname string `json:"hostname"` + CountryCode string `json:"country_code"` + CountryName string `json:"country_name"` + CityCode string `json:"city_code"` + CityName string `json:"city_name"` + Active bool `json:"active"` + Owned bool `json:"owned"` + Provider string `json:"provider"` + Ipv4AddrIn string `json:"ipv4_addr_in"` + Ipv6AddrIn *string `json:"ipv6_addr_in"` + NetworkPortSpeed int `json:"network_port_speed"` + Type string `json:"type"` + StatusMessages []interface{} `json:"status_messages"` + Pubkey string `json:"pubkey,omitempty"` + MultihopPort int `json:"multihop_port,omitempty"` + SocksName string `json:"socks_name,omitempty"` + SshFingerprintSha256 string `json:"ssh_fingerprint_sha256,omitempty"` + SshFingerprintMd5 string `json:"ssh_fingerprint_md5,omitempty"` } diff --git a/check.go b/check.go index c5205da..c4d6df7 100644 --- a/check.go +++ b/check.go @@ -2,17 +2,12 @@ package mullsox import ( "context" - "encoding/json" "fmt" "io" "net/http" "time" ) -const baseEndpoint = "am.i.mullvad.net" -const Ipv4Endpoint = `https://ipv4.` + baseEndpoint -const Ipv6Endpoint = `https://ipv6.` + baseEndpoint - func CheckIP4(ctx context.Context, h *http.Client) (details *MyIPDetails, err error) { return checkIP(ctx, h, false) } @@ -68,6 +63,8 @@ func CheckIP(ctx context.Context, h *http.Client) (v4details *MyIPDetails, v6det v6details = res.details case !res.ipv6: v4details = res.details + default: + panic("malformed result") } finished++ default: @@ -85,9 +82,9 @@ func checkIP(ctx context.Context, h *http.Client, ipv6 bool) (details *MyIPDetai ) switch ipv6 { case true: - target = Ipv6Endpoint + "/json" + target = ipv6Endpoint + "/json" default: - target = Ipv4Endpoint + "/json" + target = ipv4Endpoint + "/json" } req, _ := http.NewRequestWithContext(ctx, "GET", target, nil) resp, err = h.Do(req) @@ -99,6 +96,9 @@ func checkIP(ctx context.Context, h *http.Client, ipv6 bool) (details *MyIPDetai return } cytes, err = io.ReadAll(resp.Body) + if err != nil { + return + } err = json.Unmarshal(cytes, &details) return } diff --git a/check_test.go b/check_test.go index 083b1a6..355a53a 100644 --- a/check_test.go +++ b/check_test.go @@ -2,7 +2,6 @@ package mullsox import ( "context" - "encoding/json" "net/http" "testing" "time" diff --git a/go.mod b/go.mod index 711c7be..cb0b0d8 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,10 @@ module git.tcp.direct/kayos/mullsox go 1.18 + +require github.com/json-iterator/go v1.1.12 + +require ( + github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect + github.com/modern-go/reflect2 v1.0.2 // indirect +) diff --git a/go.sum b/go.sum index e69de29..5ad85e0 100644 --- a/go.sum +++ b/go.sum @@ -0,0 +1,15 @@ +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= +github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= +github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= diff --git a/relays.go b/relays.go new file mode 100644 index 0000000..0c0f152 --- /dev/null +++ b/relays.go @@ -0,0 +1,8 @@ +package mullsox + +import "context" + +func GetRelays(ctx context.Context) (ret chan MullvadServer) { + ret = make(chan MullvadServer) + +}