Files
gluetun/internal/provider/mullvad.go

135 lines
3.7 KiB
Go
Raw Normal View History

package provider
2020-02-16 19:51:08 +00:00
import (
"context"
2020-02-16 19:51:08 +00:00
"fmt"
"math/rand"
"net"
"net/http"
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/firewall"
2020-07-26 12:07:06 +00:00
"github.com/qdm12/gluetun/internal/models"
"github.com/qdm12/gluetun/internal/os"
"github.com/qdm12/golibs/logging"
2020-02-16 19:51:08 +00:00
)
type mullvad struct {
servers []models.MullvadServer
randSource rand.Source
}
func newMullvad(servers []models.MullvadServer, timeNow timeNowFunc) *mullvad {
return &mullvad{
servers: servers,
randSource: rand.NewSource(timeNow().UnixNano()),
}
}
func (m *mullvad) filterServers(countries, cities, isps []string, owned bool) (servers []models.MullvadServer) {
for _, server := range m.servers {
switch {
case
filterByPossibilities(server.Country, countries),
filterByPossibilities(server.City, cities),
filterByPossibilities(server.ISP, isps),
owned && !server.Owned:
default:
servers = append(servers, server)
2020-07-23 01:46:28 +00:00
}
}
return servers
}
2020-10-20 02:45:28 +00:00
func (m *mullvad) GetOpenVPNConnection(selection models.ServerSelection) (
connection models.OpenVPNConnection, err error) {
var defaultPort uint16 = 1194
if selection.Protocol == constants.TCP {
defaultPort = 443
}
2020-10-12 20:21:26 +00:00
port := defaultPort
if selection.CustomPort > 0 {
port = selection.CustomPort
}
if selection.TargetIP != nil {
return models.OpenVPNConnection{IP: selection.TargetIP, Port: port, Protocol: selection.Protocol}, nil
}
servers := m.filterServers(selection.Countries, selection.Cities, selection.ISPs, selection.Owned)
2020-10-12 20:21:26 +00:00
if len(servers) == 0 {
return connection, fmt.Errorf("no server found for countries %s, cities %s, ISPs %s and owned %t",
commaJoin(selection.Countries), commaJoin(selection.Cities), commaJoin(selection.ISPs), selection.Owned)
2020-10-12 20:21:26 +00:00
}
var connections []models.OpenVPNConnection
2020-02-16 19:51:08 +00:00
for _, server := range servers {
for _, IP := range server.IPs {
2020-10-12 20:21:26 +00:00
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
return pickRandomConnection(connections, m.randSource), nil
2020-02-16 19:51:08 +00:00
}
2020-10-20 02:45:28 +00:00
func (m *mullvad) BuildConf(connection models.OpenVPNConnection,
2020-12-27 00:36:39 +00:00
verbosity int, username string, 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
"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",
"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", connection.Protocol),
fmt.Sprintf("remote %s %d", connection.IP, connection.Port),
fmt.Sprintf("cipher %s", cipher),
2020-02-16 19:51:08 +00:00
}
if extras.OpenVPNIPv6 {
lines = append(lines, "tun-ipv6")
} else {
lines = append(lines, `pull-filter ignore "route-ipv6"`)
lines = append(lines, `pull-filter ignore "ifconfig-ipv6"`)
}
2020-03-18 23:05:47 +00:00
if !root {
2020-12-27 00:36:39 +00:00
lines = append(lines, "user "+username)
2020-03-18 23:05:47 +00:00
}
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
}
func (m *mullvad) PortForward(ctx context.Context, client *http.Client,
openFile os.OpenFileFunc, pfLogger logging.Logger, gateway net.IP, fw firewall.Configurator,
syncState func(port uint16) (pfFilepath models.Filepath)) {
panic("port forwarding is not supported for mullvad")
}