2020-06-13 14:08:29 -04:00
|
|
|
package provider
|
2020-05-29 07:24:27 -04:00
|
|
|
|
|
|
|
|
import (
|
2020-10-12 10:55:08 -04:00
|
|
|
"context"
|
2020-05-29 07:24:27 -04:00
|
|
|
"fmt"
|
2020-10-12 15:29:58 -04:00
|
|
|
"math/rand"
|
2020-10-12 10:55:08 -04:00
|
|
|
"net"
|
|
|
|
|
"net/http"
|
2021-01-19 02:55:38 +00:00
|
|
|
"strconv"
|
2021-05-10 01:24:46 +00:00
|
|
|
"strings"
|
2020-05-29 07:24:27 -04:00
|
|
|
|
2021-02-06 11:05:50 -05:00
|
|
|
"github.com/qdm12/gluetun/internal/configuration"
|
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-05-29 07:24:27 -04:00
|
|
|
)
|
|
|
|
|
|
2020-08-25 19:38:50 -04:00
|
|
|
type surfshark struct {
|
2020-10-12 15:29:58 -04:00
|
|
|
servers []models.SurfsharkServer
|
|
|
|
|
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 newSurfshark(servers []models.SurfsharkServer, timeNow timeNowFunc) *surfshark {
|
2020-08-25 19:38:50 -04:00
|
|
|
return &surfshark{
|
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
|
|
|
}
|
|
|
|
|
|
2021-05-10 01:24:46 +00:00
|
|
|
func (s *surfshark) filterServers(regions, hostnames []string, protocol string) (servers []models.SurfsharkServer) {
|
2020-08-27 23:06:28 +00:00
|
|
|
for _, server := range s.servers {
|
2020-10-18 17:15:42 -04:00
|
|
|
switch {
|
|
|
|
|
case
|
2021-05-10 01:24:46 +00:00
|
|
|
filterByPossibilities(server.Region, regions),
|
|
|
|
|
filterByPossibilities(server.Hostname, hostnames),
|
|
|
|
|
strings.EqualFold(protocol, "tcp") && !server.TCP,
|
|
|
|
|
strings.EqualFold(protocol, "udp") && !server.UDP:
|
2020-10-18 17:15:42 -04:00
|
|
|
default:
|
|
|
|
|
servers = append(servers, server)
|
2020-05-29 07:24:27 -04:00
|
|
|
}
|
|
|
|
|
}
|
2020-10-18 17:15:42 -04:00
|
|
|
return servers
|
2020-07-23 01:46:28 +00:00
|
|
|
}
|
|
|
|
|
|
2021-05-10 01:24:46 +00:00
|
|
|
func (s *surfshark) notFoundErr(selection configuration.ServerSelection) error {
|
|
|
|
|
message := "for protocol " + selection.Protocol
|
|
|
|
|
|
|
|
|
|
if len(selection.Countries) > 0 {
|
|
|
|
|
message += " + regions " + commaJoin(selection.Regions)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(selection.Hostnames) > 0 {
|
|
|
|
|
message += " + hostnames " + commaJoin(selection.Hostnames)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return fmt.Errorf("%w: %s", errNoServerFound, message)
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-06 11:05:50 -05:00
|
|
|
func (s *surfshark) GetOpenVPNConnection(selection configuration.ServerSelection) (
|
2020-10-20 02:45:28 +00:00
|
|
|
connection models.OpenVPNConnection, err error) {
|
2020-05-29 07:24:27 -04:00
|
|
|
var port uint16
|
|
|
|
|
switch {
|
2020-06-13 14:08:29 -04:00
|
|
|
case selection.Protocol == constants.TCP:
|
2020-05-29 07:24:27 -04:00
|
|
|
port = 1443
|
2020-06-13 14:08:29 -04:00
|
|
|
case selection.Protocol == constants.UDP:
|
2020-05-29 07:24:27 -04:00
|
|
|
port = 1194
|
|
|
|
|
default:
|
2020-10-12 15:29:58 -04:00
|
|
|
return connection, fmt.Errorf("protocol %q is unknown", selection.Protocol)
|
2020-05-29 07:24:27 -04:00
|
|
|
}
|
2020-07-23 01:46:28 +00:00
|
|
|
|
2020-10-12 20:21:26 +00:00
|
|
|
if selection.TargetIP != nil {
|
|
|
|
|
return models.OpenVPNConnection{IP: selection.TargetIP, Port: port, Protocol: selection.Protocol}, nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-10 01:24:46 +00:00
|
|
|
servers := s.filterServers(selection.Regions, selection.Hostnames, selection.Protocol)
|
2020-10-12 20:21:26 +00:00
|
|
|
if len(servers) == 0 {
|
2021-05-10 01:24:46 +00:00
|
|
|
return connection, s.notFoundErr(selection)
|
2020-10-12 20:21:26 +00:00
|
|
|
}
|
|
|
|
|
|
2020-10-12 15:29:58 -04:00
|
|
|
var connections []models.OpenVPNConnection
|
2020-07-23 01:46:28 +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-07-23 01:46:28 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if selection.TargetIP != nil {
|
2020-10-12 15:29:58 -04:00
|
|
|
return connection, fmt.Errorf("target IP %s not found in IP addresses", selection.TargetIP)
|
2020-05-29 07:24:27 -04:00
|
|
|
}
|
2020-07-23 01:46:28 +00:00
|
|
|
|
2020-10-12 15:29:58 -04:00
|
|
|
return pickRandomConnection(connections, s.randSource), nil
|
2020-05-29 07:24:27 -04:00
|
|
|
}
|
|
|
|
|
|
2021-01-19 02:42:16 +00:00
|
|
|
func (s *surfshark) BuildConf(connection models.OpenVPNConnection,
|
2021-02-06 11:05:50 -05:00
|
|
|
username string, settings configuration.OpenVPN) (lines []string) {
|
2021-01-19 02:42:16 +00:00
|
|
|
if len(settings.Cipher) == 0 {
|
2021-04-19 18:00:58 +00:00
|
|
|
settings.Cipher = aes256gcm
|
2020-05-29 07:24:27 -04:00
|
|
|
}
|
2021-01-19 02:42:16 +00:00
|
|
|
if len(settings.Auth) == 0 {
|
|
|
|
|
settings.Auth = "SHA512"
|
2020-05-29 07:24:27 -04:00
|
|
|
}
|
2021-01-19 02:55:38 +00:00
|
|
|
|
|
|
|
|
const defaultMSSFix = 1450
|
|
|
|
|
if settings.MSSFix == 0 {
|
|
|
|
|
settings.MSSFix = defaultMSSFix
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-13 23:34:03 +00:00
|
|
|
lines = []string{
|
2020-05-29 07:24:27 -04:00
|
|
|
"client",
|
|
|
|
|
"dev tun",
|
|
|
|
|
"nobind",
|
|
|
|
|
"persist-key",
|
|
|
|
|
"remote-cert-tls server",
|
2021-02-08 00:05:22 +00:00
|
|
|
"ping 15",
|
2021-01-22 13:30:06 +00:00
|
|
|
"ping-timer-rem",
|
2021-01-22 13:36:56 +00:00
|
|
|
"tls-exit",
|
2020-05-29 07:24:27 -04:00
|
|
|
|
|
|
|
|
// Surfshark specific
|
|
|
|
|
"tun-mtu 1500",
|
|
|
|
|
"tun-mtu-extra 32",
|
2021-01-19 02:55:38 +00:00
|
|
|
"mssfix " + strconv.Itoa(int(settings.MSSFix)),
|
2020-05-29 07:24:27 -04:00
|
|
|
"reneg-sec 0",
|
|
|
|
|
"fast-io",
|
|
|
|
|
"key-direction 1",
|
2020-06-21 20:21:13 -04:00
|
|
|
"script-security 2",
|
2021-02-09 03:03:08 +00:00
|
|
|
"ping-restart 0",
|
2020-05-29 07:24:27 -04:00
|
|
|
|
|
|
|
|
// Added constant values
|
|
|
|
|
"auth-nocache",
|
|
|
|
|
"mute-replay-warnings",
|
|
|
|
|
"pull-filter ignore \"auth-token\"", // prevent auth failed loops
|
2020-12-23 06:46:54 +00:00
|
|
|
"pull-filter ignore \"block-outside-dns\"",
|
2020-05-29 07:24:27 -04:00
|
|
|
"auth-retry nointeract",
|
|
|
|
|
"suppress-timestamps",
|
|
|
|
|
|
|
|
|
|
// Modified variables
|
2021-01-19 02:42:16 +00:00
|
|
|
fmt.Sprintf("verb %d", settings.Verbosity),
|
2020-05-29 07:24:27 -04: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),
|
2021-04-19 00:31:46 +00:00
|
|
|
"data-ciphers-fallback " + settings.Cipher,
|
|
|
|
|
"data-ciphers " + settings.Cipher,
|
2021-01-19 02:42:16 +00:00
|
|
|
fmt.Sprintf("auth %s", settings.Auth),
|
2020-05-29 07:24:27 -04:00
|
|
|
}
|
2021-01-19 02:42:16 +00:00
|
|
|
if !settings.Root {
|
2020-12-27 00:36:39 +00:00
|
|
|
lines = append(lines, "user "+username)
|
2020-05-29 07:24:27 -04:00
|
|
|
}
|
|
|
|
|
lines = append(lines, []string{
|
|
|
|
|
"<ca>",
|
|
|
|
|
"-----BEGIN CERTIFICATE-----",
|
|
|
|
|
constants.SurfsharkCertificate,
|
|
|
|
|
"-----END CERTIFICATE-----",
|
|
|
|
|
"</ca>",
|
|
|
|
|
}...)
|
|
|
|
|
lines = append(lines, []string{
|
|
|
|
|
"<tls-auth>",
|
|
|
|
|
"-----BEGIN OpenVPN Static key V1-----",
|
|
|
|
|
constants.SurfsharkOpenvpnStaticKeyV1,
|
|
|
|
|
"-----END OpenVPN Static key V1-----",
|
|
|
|
|
"</tls-auth>",
|
|
|
|
|
"",
|
|
|
|
|
}...)
|
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 (s *surfshark) 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,
|
2021-02-06 18:31:14 +00:00
|
|
|
syncState func(port uint16) (pfFilepath string)) {
|
2020-06-13 14:08:29 -04:00
|
|
|
panic("port forwarding is not supported for surfshark")
|
|
|
|
|
}
|