Files
gluetun/internal/models/server.go
Quentin McGaw 049bc5b226 Mullvad updater (#228)
* Add Mullvad to updater cli
* Update hardcoded servers for Mullvad
2020-08-29 13:19:34 -04:00

114 lines
2.5 KiB
Go

package models
import (
"encoding/hex"
"fmt"
"net"
"strings"
)
type PIAServer struct {
IPs []net.IP `json:"ips"`
Region string `json:"region"`
}
func (p *PIAServer) String() string {
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"`
}
type SurfsharkServer struct {
Region string `json:"region"`
IPs []net.IP `json:"ips"`
}
type CyberghostServer struct {
Region string `json:"region"`
Group string `json:"group"`
IPs []net.IP `json:"ips"`
}
type VyprvpnServer struct {
Region string `json:"region"`
IPs []net.IP `json:"ips"`
}
type NordvpnServer struct { //nolint:maligned
Region string `json:"region"`
Number uint16 `json:"number"`
IP net.IP `json:"ip"`
TCP bool `json:"tcp"`
UDP bool `json:"udp"`
}
type PurevpnServer struct {
Region string `json:"region"`
Country string `json:"country"`
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, ", ") + "}"
}