2020-06-13 14:08:29 -04:00
|
|
|
package provider
|
2020-02-16 19:51:08 +00:00
|
|
|
|
|
|
|
|
import (
|
2020-10-12 10:55:08 -04:00
|
|
|
"context"
|
2020-02-16 19:51:08 +00:00
|
|
|
"fmt"
|
2020-10-12 15:29:58 -04:00
|
|
|
"math/rand"
|
2020-10-12 10:55:08 -04:00
|
|
|
"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"
|
2020-10-12 10:55:08 -04:00
|
|
|
"github.com/qdm12/gluetun/internal/firewall"
|
2020-07-26 12:07:06 +00:00
|
|
|
"github.com/qdm12/gluetun/internal/models"
|
2020-10-12 10:55:08 -04:00
|
|
|
"github.com/qdm12/golibs/logging"
|
2021-01-02 01:57:00 +00:00
|
|
|
"github.com/qdm12/golibs/os"
|
2020-02-16 19:51:08 +00:00
|
|
|
)
|
|
|
|
|
|
2020-08-25 19:38:50 -04:00
|
|
|
type mullvad struct {
|
2020-10-12 15:29:58 -04:00
|
|
|
servers []models.MullvadServer
|
|
|
|
|
randSource rand.Source
|
2020-08-25 19:38:50 -04:00
|
|
|
}
|
2020-06-13 14:08:29 -04:00
|
|
|
|
2020-10-12 15:29:58 -04:00
|
|
|
func newMullvad(servers []models.MullvadServer, timeNow timeNowFunc) *mullvad {
|
2020-08-25 19:38:50 -04:00
|
|
|
return &mullvad{
|
2020-10-12 15:29:58 -04:00
|
|
|
servers: servers,
|
|
|
|
|
randSource: rand.NewSource(timeNow().UnixNano()),
|
2020-08-25 19:38:50 -04:00
|
|
|
}
|
2020-06-13 14:08:29 -04:00
|
|
|
}
|
|
|
|
|
|
2020-10-18 17:15:42 -04:00
|
|
|
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) {
|
2020-08-24 01:53:24 +00:00
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-18 17:15:42 -04:00
|
|
|
servers := m.filterServers(selection.Countries, selection.Cities, selection.ISPs, selection.Owned)
|
2020-10-12 20:21:26 +00:00
|
|
|
if len(servers) == 0 {
|
2020-10-18 17:15:42 -04:00
|
|
|
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
|
|
|
}
|
2020-08-24 01:53:24 +00:00
|
|
|
|
2020-10-12 15:29:58 -04: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
|
|
|
|
2020-10-12 15:29:58 -04: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) {
|
2020-03-26 20:29:32 -04:00
|
|
|
if len(cipher) == 0 {
|
2020-06-13 14:08:29 -04:00
|
|
|
cipher = aes256cbc
|
2020-03-26 20:29:32 -04:00
|
|
|
}
|
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
|
2020-05-29 10:29:07 +00:00
|
|
|
"ping 10",
|
|
|
|
|
"ping-restart 60",
|
2020-03-04 23:52:41 +00:00
|
|
|
"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",
|
2020-06-21 20:21:13 -04:00
|
|
|
"script-security 2",
|
2020-02-16 19:51:08 +00:00
|
|
|
|
|
|
|
|
// Added constant values
|
|
|
|
|
"mute-replay-warnings",
|
2020-02-22 16:33:37 +00:00
|
|
|
"auth-nocache",
|
2020-02-16 19:51:08 +00:00
|
|
|
"pull-filter ignore \"auth-token\"", // prevent auth failed loops
|
|
|
|
|
"auth-retry nointeract",
|
2020-05-29 11:17:14 +00:00
|
|
|
"suppress-timestamps",
|
2020-02-16 19:51:08 +00:00
|
|
|
|
|
|
|
|
// Modified variables
|
2020-02-22 15:48:09 +00:00
|
|
|
fmt.Sprintf("verb %d", verbosity),
|
2020-02-16 19:51:08 +00:00
|
|
|
fmt.Sprintf("auth-user-pass %s", constants.OpenVPNAuthConf),
|
2020-10-12 15:29:58 -04:00
|
|
|
fmt.Sprintf("proto %s", connection.Protocol),
|
|
|
|
|
fmt.Sprintf("remote %s %d", connection.IP, connection.Port),
|
2020-03-26 20:29:32 -04:00
|
|
|
fmt.Sprintf("cipher %s", cipher),
|
2020-02-16 19:51:08 +00:00
|
|
|
}
|
2020-09-26 09:33:24 -04: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
|
2020-06-13 14:08:29 -04:00
|
|
|
}
|
|
|
|
|
|
2020-10-12 10:55:08 -04:00
|
|
|
func (m *mullvad) PortForward(ctx context.Context, client *http.Client,
|
2020-12-29 00:55:31 +00:00
|
|
|
openFile os.OpenFileFunc, pfLogger logging.Logger, gateway net.IP, fw firewall.Configurator,
|
2020-10-12 10:55:08 -04:00
|
|
|
syncState func(port uint16) (pfFilepath models.Filepath)) {
|
2020-06-13 14:08:29 -04:00
|
|
|
panic("port forwarding is not supported for mullvad")
|
|
|
|
|
}
|