Files
gluetun/internal/provider/mullvad.go

136 lines
3.4 KiB
Go
Raw Normal View History

package provider
2020-02-16 19:51:08 +00:00
import (
"fmt"
"strings"
2020-02-16 19:51:08 +00:00
2020-07-26 12:07:06 +00:00
"github.com/qdm12/gluetun/internal/constants"
"github.com/qdm12/gluetun/internal/models"
2020-07-13 23:34:03 +00:00
"github.com/qdm12/golibs/network"
2020-02-16 19:51:08 +00:00
)
type mullvad struct {
servers []models.MullvadServer
}
func newMullvad(servers []models.MullvadServer) *mullvad {
return &mullvad{
servers: servers,
}
}
2020-07-23 01:46:28 +00:00
func (m *mullvad) filterServers(country, city, isp string) (servers []models.MullvadServer) {
allServers := constants.MullvadServers()
for i, server := range allServers {
if len(country) == 0 {
server.Country = ""
}
if len(city) == 0 {
server.City = ""
}
if len(isp) == 0 {
server.ISP = ""
}
if strings.EqualFold(server.Country, country) &&
strings.EqualFold(server.City, city) &&
strings.EqualFold(server.ISP, isp) {
2020-07-23 01:46:28 +00:00
servers = append(servers, allServers[i])
}
}
return servers
}
func (m *mullvad) GetOpenVPNConnections(selection models.ServerSelection) (connections []models.OpenVPNConnection, err error) {
2020-07-23 01:46:28 +00:00
servers := m.filterServers(selection.Country, selection.City, selection.ISP)
2020-02-16 19:51:08 +00:00
if len(servers) == 0 {
return nil, fmt.Errorf("no server found for country %q, city %q and ISP %q", selection.Country, selection.City, selection.ISP)
2020-02-16 19:51:08 +00:00
}
2020-07-23 01:46:28 +00:00
var defaultPort uint16 = 1194
if selection.Protocol == constants.TCP {
defaultPort = 443
}
2020-02-16 19:51:08 +00:00
for _, server := range servers {
port := defaultPort
if selection.CustomPort > 0 {
port = selection.CustomPort
2020-02-16 19:51:08 +00:00
}
for _, IP := range server.IPs {
if selection.TargetIP != nil {
if selection.TargetIP.Equal(IP) {
return []models.OpenVPNConnection{{IP: IP, Port: port, Protocol: selection.Protocol}}, nil
}
} else {
connections = append(connections, models.OpenVPNConnection{IP: IP, Port: port, Protocol: selection.Protocol})
}
2020-02-16 19:51:08 +00:00
}
}
2020-07-23 01:46:28 +00:00
if selection.TargetIP != nil {
return nil, fmt.Errorf("target IP address %q not found in IP addresses", selection.TargetIP)
}
2020-07-23 01:46:28 +00:00
if len(connections) > 64 {
connections = connections[:64]
}
2020-02-16 19:51:08 +00:00
return connections, nil
}
2020-07-13 23:34:03 +00:00
func (m *mullvad) BuildConf(connections []models.OpenVPNConnection, verbosity, uid, gid int, root bool, cipher, auth string, extras models.ExtraConfigOptions) (lines []string) {
if len(cipher) == 0 {
cipher = aes256cbc
}
2020-07-13 23:34:03 +00:00
lines = []string{
2020-02-16 19:51:08 +00:00
"client",
"dev tun",
"nobind",
"persist-key",
"remote-cert-tls server",
// Mullvad specific
"ping 10",
"ping-restart 60",
"sndbuf 524288",
"rcvbuf 524288",
2020-02-16 19:51:08 +00:00
"tls-cipher TLS-DHE-RSA-WITH-AES-256-GCM-SHA384:TLS-DHE-RSA-WITH-AES-256-CBC-SHA",
2020-05-04 12:24:34 +00:00
"tun-ipv6",
"fast-io",
"script-security 2",
2020-02-16 19:51:08 +00:00
// Added constant values
"mute-replay-warnings",
"auth-nocache",
2020-02-16 19:51:08 +00:00
"pull-filter ignore \"auth-token\"", // prevent auth failed loops
"auth-retry nointeract",
"remote-random",
"suppress-timestamps",
2020-02-16 19:51:08 +00:00
// Modified variables
fmt.Sprintf("verb %d", verbosity),
2020-02-16 19:51:08 +00:00
fmt.Sprintf("auth-user-pass %s", constants.OpenVPNAuthConf),
fmt.Sprintf("proto %s", connections[0].Protocol),
fmt.Sprintf("cipher %s", cipher),
2020-02-16 19:51:08 +00:00
}
2020-03-18 23:05:47 +00:00
if !root {
lines = append(lines, "user nonrootuser")
}
2020-02-16 19:51:08 +00:00
for _, connection := range connections {
lines = append(lines, fmt.Sprintf("remote %s %d", connection.IP, connection.Port))
2020-02-16 19:51:08 +00:00
}
lines = append(lines, []string{
"<ca>",
"-----BEGIN CERTIFICATE-----",
constants.MullvadCertificate,
"-----END CERTIFICATE-----",
"</ca>",
"",
}...)
2020-07-13 23:34:03 +00:00
return lines
}
2020-07-13 23:34:03 +00:00
func (m *mullvad) GetPortForward(client network.Client) (port uint16, err error) {
panic("port forwarding is not supported for mullvad")
}