2020-06-13 14:08:29 -04:00
|
|
|
package provider
|
2020-02-16 19:51:08 +00:00
|
|
|
|
|
|
|
|
import (
|
2020-06-13 14:08:29 -04:00
|
|
|
"context"
|
2020-02-16 19:51:08 +00:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
|
|
"github.com/qdm12/golibs/files"
|
2020-06-13 14:08:29 -04:00
|
|
|
"github.com/qdm12/golibs/logging"
|
2020-02-16 19:51:08 +00:00
|
|
|
"github.com/qdm12/private-internet-access-docker/internal/constants"
|
|
|
|
|
"github.com/qdm12/private-internet-access-docker/internal/models"
|
|
|
|
|
)
|
|
|
|
|
|
2020-06-13 14:08:29 -04:00
|
|
|
type mullvad struct {
|
|
|
|
|
fileManager files.FileManager
|
|
|
|
|
logger logging.Logger
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func newMullvad(fileManager files.FileManager, logger logging.Logger) *mullvad {
|
|
|
|
|
return &mullvad{
|
|
|
|
|
fileManager: fileManager,
|
|
|
|
|
logger: logger.WithPrefix("Mullvad configurator: "),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *mullvad) GetOpenVPNConnections(selection models.ServerSelection) (connections []models.OpenVPNConnection, err error) {
|
|
|
|
|
servers := constants.MullvadServerFilter(selection.Country, selection.City, selection.ISP)
|
2020-02-16 19:51:08 +00:00
|
|
|
if len(servers) == 0 {
|
2020-06-13 14:08:29 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
for _, server := range servers {
|
|
|
|
|
port := server.DefaultPort
|
2020-06-13 14:08:29 -04:00
|
|
|
if selection.CustomPort > 0 {
|
|
|
|
|
port = selection.CustomPort
|
2020-02-16 19:51:08 +00:00
|
|
|
}
|
|
|
|
|
for _, IP := range server.IPs {
|
2020-06-13 14:08:29 -04:00
|
|
|
if selection.TargetIP != nil {
|
|
|
|
|
if selection.TargetIP.Equal(IP) {
|
|
|
|
|
return []models.OpenVPNConnection{{IP: IP, Port: port, Protocol: selection.Protocol}}, nil
|
2020-03-18 23:49:40 +00:00
|
|
|
}
|
|
|
|
|
} else {
|
2020-06-13 14:08:29 -04:00
|
|
|
connections = append(connections, models.OpenVPNConnection{IP: IP, Port: port, Protocol: selection.Protocol})
|
2020-03-18 23:49:40 +00:00
|
|
|
}
|
2020-02-16 19:51:08 +00:00
|
|
|
}
|
|
|
|
|
}
|
2020-06-13 14:08:29 -04:00
|
|
|
if selection.TargetIP != nil {
|
|
|
|
|
return nil, fmt.Errorf("target IP address %q not found in IP addresses", selection.TargetIP)
|
2020-03-18 23:49:40 +00:00
|
|
|
}
|
2020-02-16 19:51:08 +00:00
|
|
|
return connections, nil
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-13 14:08:29 -04:00
|
|
|
func (m *mullvad) BuildConf(connections []models.OpenVPNConnection, verbosity, uid, gid int, root bool, cipher, auth string, extras models.ExtraConfigOptions) (err error) {
|
2020-02-16 19:51:08 +00:00
|
|
|
if len(connections) == 0 {
|
|
|
|
|
return fmt.Errorf("at least one connection string is expected")
|
|
|
|
|
}
|
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-02-16 19:51:08 +00:00
|
|
|
lines := []string{
|
|
|
|
|
"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
|
|
|
"tun-ipv6",
|
|
|
|
|
"fast-io",
|
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",
|
|
|
|
|
"remote-random",
|
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),
|
|
|
|
|
fmt.Sprintf("proto %s", string(connections[0].Protocol)),
|
2020-03-26 20:29:32 -04:00
|
|
|
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.String(), connection.Port))
|
|
|
|
|
}
|
|
|
|
|
lines = append(lines, []string{
|
|
|
|
|
"<ca>",
|
|
|
|
|
"-----BEGIN CERTIFICATE-----",
|
|
|
|
|
constants.MullvadCertificate,
|
|
|
|
|
"-----END CERTIFICATE-----",
|
|
|
|
|
"</ca>",
|
|
|
|
|
"",
|
|
|
|
|
}...)
|
2020-06-13 14:08:29 -04:00
|
|
|
return m.fileManager.WriteLinesToFile(string(constants.OpenVPNConf), lines, files.Ownership(uid, gid), files.Permissions(0400))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *mullvad) GetPortForward() (port uint16, err error) {
|
|
|
|
|
panic("port forwarding is not supported for mullvad")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *mullvad) WritePortForward(filepath models.Filepath, port uint16, uid, gid int) (err error) {
|
|
|
|
|
panic("port forwarding is not supported for mullvad")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *mullvad) AllowPortForwardFirewall(ctx context.Context, device models.VPNDevice, port uint16) (err error) {
|
|
|
|
|
panic("port forwarding is not supported for mullvad")
|
2020-02-16 19:51:08 +00:00
|
|
|
}
|