Drop in jsoniter, lint formatting, prepare for endpoint retrieval

This commit is contained in:
kayos@tcp.direct 2022-06-30 07:57:07 -07:00
parent 3f85ef852d
commit a6a55a0ace
Signed by: kayos
GPG Key ID: 4B841471B4BEE979
6 changed files with 70 additions and 25 deletions

50
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"`
}

@ -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
}

@ -2,7 +2,6 @@ package mullsox
import (
"context"
"encoding/json"
"net/http"
"testing"
"time"

7
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
)

15
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=

8
relays.go Normal file

@ -0,0 +1,8 @@
package mullsox
import "context"
func GetRelays(ctx context.Context) (ret chan MullvadServer) {
ret = make(chan MullvadServer)
}