Mullvad updater (#228)
* Add Mullvad to updater cli * Update hardcoded servers for Mullvad
This commit is contained in:
@@ -1,36 +1,35 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"net"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func stringifyIPs(ips []net.IP) string {
|
||||
ipStrings := make([]string, len(ips))
|
||||
for i := range ips {
|
||||
ipStrings[i] = fmt.Sprintf("{%s}", strings.ReplaceAll(ips[i].String(), ".", ", "))
|
||||
}
|
||||
return strings.Join(ipStrings, ", ")
|
||||
}
|
||||
|
||||
type PIAServer struct {
|
||||
IPs []net.IP `json:"ips"`
|
||||
Region string `json:"region"`
|
||||
}
|
||||
|
||||
func (p *PIAServer) String() string {
|
||||
return fmt.Sprintf("{Region: %q, IPs: []net.IP{%s}}", p.Region, stringifyIPs(p.IPs))
|
||||
return fmt.Sprintf("{Region: %q, IPs: %s}", p.Region, goStringifyIPs(p.IPs))
|
||||
}
|
||||
|
||||
type MullvadServer struct {
|
||||
IPs []net.IP `json:"ips"`
|
||||
IPsV6 []net.IP `json:"ipsv6"`
|
||||
Country string `json:"country"`
|
||||
City string `json:"city"`
|
||||
ISP string `json:"isp"`
|
||||
Owned bool `json:"owned"`
|
||||
}
|
||||
|
||||
func (s *MullvadServer) String() string {
|
||||
return fmt.Sprintf("{Country: %q, City: %q, ISP: %q, Owned: %t, IPs: %s, IPsV6: %s}",
|
||||
s.Country, s.City, s.ISP, s.Owned, goStringifyIPs(s.IPs), goStringifyIPs(s.IPsV6))
|
||||
}
|
||||
|
||||
type WindscribeServer struct {
|
||||
Region string `json:"region"`
|
||||
IPs []net.IP `json:"ips"`
|
||||
@@ -66,3 +65,49 @@ type PurevpnServer struct {
|
||||
City string `json:"city"`
|
||||
IPs []net.IP `json:"ips"`
|
||||
}
|
||||
|
||||
func goStringifyIP(ip net.IP) string {
|
||||
s := fmt.Sprintf("%#v", ip)
|
||||
s = strings.TrimSuffix(strings.TrimPrefix(s, "net.IP{"), "}")
|
||||
fields := strings.Split(s, ", ")
|
||||
isIPv4 := ip.To4() != nil
|
||||
if isIPv4 {
|
||||
fields = fields[len(fields)-4:]
|
||||
}
|
||||
|
||||
// Count leading zeros
|
||||
leadingZeros := 0
|
||||
for i := range fields {
|
||||
if fields[i] == "0x0" {
|
||||
leadingZeros++
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// Remove leading zeros
|
||||
fields = fields[leadingZeros:]
|
||||
|
||||
for i := range fields {
|
||||
// IPv4 is better understood in integer notation, whereas IPv6 is written in hex notation
|
||||
if isIPv4 {
|
||||
hexString := strings.Replace(fields[i], "0x", "", 1)
|
||||
if len(hexString) == 1 {
|
||||
hexString = "0" + hexString
|
||||
}
|
||||
b, _ := hex.DecodeString(hexString)
|
||||
fields[i] = fmt.Sprintf("%d", b[0])
|
||||
}
|
||||
}
|
||||
|
||||
return fmt.Sprintf("net.IP{%s}", strings.Join(fields, ", "))
|
||||
}
|
||||
|
||||
func goStringifyIPs(ips []net.IP) string {
|
||||
ipStrings := make([]string, len(ips))
|
||||
for i := range ips {
|
||||
ipStrings[i] = goStringifyIP(ips[i])
|
||||
ipStrings[i] = strings.TrimPrefix(ipStrings[i], "net.IP")
|
||||
}
|
||||
return "[]net.IP{" + strings.Join(ipStrings, ", ") + "}"
|
||||
}
|
||||
|
||||
141
internal/models/server_test.go
Normal file
141
internal/models/server_test.go
Normal file
@@ -0,0 +1,141 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"net"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func Test_PIAServer_String(t *testing.T) {
|
||||
t.Parallel()
|
||||
testCases := map[string]struct {
|
||||
server PIAServer
|
||||
s string
|
||||
}{
|
||||
"no ips": {
|
||||
server: PIAServer{Region: "a b"},
|
||||
s: `{Region: "a b", IPs: []net.IP{}}`,
|
||||
},
|
||||
"with ips": {
|
||||
server: PIAServer{Region: "a b", IPs: []net.IP{{1, 1, 1, 1}, {2, 2, 2, 2}}},
|
||||
s: `{Region: "a b", IPs: []net.IP{{1, 1, 1, 1}, {2, 2, 2, 2}}}`,
|
||||
},
|
||||
}
|
||||
for name, testCase := range testCases {
|
||||
testCase := testCase
|
||||
t.Run(name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
s := testCase.server.String()
|
||||
assert.Equal(t, testCase.s, s)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_MullvadServer_String(t *testing.T) {
|
||||
t.Parallel()
|
||||
testCases := map[string]struct {
|
||||
server MullvadServer
|
||||
s string
|
||||
}{
|
||||
"example": {
|
||||
server: MullvadServer{
|
||||
IPs: []net.IP{{1, 1, 1, 1}},
|
||||
IPsV6: []net.IP{{0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x0, 0x1}},
|
||||
Country: "That Country",
|
||||
City: "That City",
|
||||
ISP: "not spying on you",
|
||||
Owned: true,
|
||||
},
|
||||
s: `{Country: "That Country", City: "That City", ISP: "not spying on you", Owned: true, IPs: []net.IP{{1, 1, 1, 1}}, IPsV6: []net.IP{{0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x0, 0x1}}}`,
|
||||
},
|
||||
}
|
||||
for name, testCase := range testCases {
|
||||
testCase := testCase
|
||||
t.Run(name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
s := testCase.server.String()
|
||||
assert.Equal(t, testCase.s, s)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_goStringifyIP(t *testing.T) {
|
||||
t.Parallel()
|
||||
testCases := map[string]struct {
|
||||
ip net.IP
|
||||
s string
|
||||
}{
|
||||
"nil ip": {
|
||||
s: "net.IP{net.IP(nil)}",
|
||||
},
|
||||
"empty ip": {
|
||||
ip: net.IP{},
|
||||
s: "net.IP{}",
|
||||
},
|
||||
"ipv4": {
|
||||
ip: net.IP{10, 16, 54, 25},
|
||||
s: "net.IP{10, 16, 54, 25}",
|
||||
},
|
||||
"ipv6": {
|
||||
ip: net.IP{0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x0, 0x1},
|
||||
s: "net.IP{0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x0, 0x1}",
|
||||
},
|
||||
"zeros ipv4": {
|
||||
ip: net.IP{0, 0, 0, 0},
|
||||
s: "net.IP{}",
|
||||
},
|
||||
"zeros ipv46": {
|
||||
ip: net.ParseIP("::"),
|
||||
s: "net.IP{}",
|
||||
},
|
||||
}
|
||||
for name, testCase := range testCases {
|
||||
testCase := testCase
|
||||
t.Run(name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
s := goStringifyIP(testCase.ip)
|
||||
assert.Equal(t, testCase.s, s)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_stringifyIPs(t *testing.T) {
|
||||
t.Parallel()
|
||||
testCases := map[string]struct {
|
||||
ips []net.IP
|
||||
s string
|
||||
}{
|
||||
"nil ips": {
|
||||
s: "[]net.IP{}",
|
||||
},
|
||||
"empty ips": {
|
||||
ips: []net.IP{},
|
||||
s: "[]net.IP{}",
|
||||
},
|
||||
"single ipv4": {
|
||||
ips: []net.IP{{10, 16, 54, 25}},
|
||||
s: "[]net.IP{{10, 16, 54, 25}}",
|
||||
},
|
||||
"single ipv6": {
|
||||
ips: []net.IP{{0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x0, 0x1}},
|
||||
s: "[]net.IP{{0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x0, 0x1}}",
|
||||
},
|
||||
"mix of ips": {
|
||||
ips: []net.IP{
|
||||
{10, 16, 54, 25},
|
||||
{0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x0, 0x1},
|
||||
{0, 0, 0, 0},
|
||||
},
|
||||
s: "[]net.IP{{10, 16, 54, 25}, {0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x0, 0x1}, {}}",
|
||||
},
|
||||
}
|
||||
for name, testCase := range testCases {
|
||||
testCase := testCase
|
||||
t.Run(name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
s := goStringifyIPs(testCase.ips)
|
||||
assert.Equal(t, testCase.s, s)
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user