Files
gluetun/internal/provider/vyprvpn.go

135 lines
3.6 KiB
Go
Raw Normal View History

2020-07-13 08:04:35 -04:00
package provider
import (
"context"
2020-07-13 08:04:35 -04:00
"fmt"
"math/rand"
"net"
"net/http"
2021-01-19 02:55:38 +00:00
"strconv"
2020-07-13 08:04:35 -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"
"github.com/qdm12/gluetun/internal/firewall"
2020-07-26 12:07:06 +00:00
"github.com/qdm12/gluetun/internal/models"
"github.com/qdm12/golibs/logging"
2021-01-02 01:57:00 +00:00
"github.com/qdm12/golibs/os"
2020-07-13 08:04:35 -04:00
)
type vyprvpn struct {
servers []models.VyprvpnServer
randSource rand.Source
}
2020-07-13 08:04:35 -04:00
func newVyprvpn(servers []models.VyprvpnServer, timeNow timeNowFunc) *vyprvpn {
return &vyprvpn{
servers: servers,
randSource: rand.NewSource(timeNow().UnixNano()),
}
2020-07-13 08:04:35 -04:00
}
func (v *vyprvpn) filterServers(regions []string) (servers []models.VyprvpnServer) {
for _, server := range v.servers {
switch {
case
filterByPossibilities(server.Region, regions):
default:
servers = append(servers, server)
2020-07-13 08:04:35 -04:00
}
}
return servers
2020-07-23 01:46:28 +00:00
}
2021-02-06 11:05:50 -05:00
func (v *vyprvpn) GetOpenVPNConnection(selection configuration.ServerSelection) (
2020-10-20 02:45:28 +00:00
connection models.OpenVPNConnection, err error) {
2020-07-13 08:04:35 -04:00
var port uint16
switch {
case selection.Protocol == constants.TCP:
return connection, fmt.Errorf("TCP protocol not supported by this VPN provider")
2020-07-13 08:04:35 -04:00
case selection.Protocol == constants.UDP:
port = 443
default:
return connection, fmt.Errorf("protocol %q is unknown", selection.Protocol)
2020-07-13 08:04:35 -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
}
servers := v.filterServers(selection.Regions)
2020-10-12 20:21:26 +00:00
if len(servers) == 0 {
return connection, fmt.Errorf("no server found for region %s", commaJoin(selection.Regions))
2020-10-12 20:21:26 +00: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
}
2020-07-13 08:04:35 -04:00
}
2020-07-23 01:46:28 +00:00
return pickRandomConnection(connections, v.randSource), nil
2020-07-13 08:04:35 -04:00
}
func (v *vyprvpn) BuildConf(connection models.OpenVPNConnection,
2021-02-06 11:05:50 -05:00
username string, settings configuration.OpenVPN) (lines []string) {
if len(settings.Cipher) == 0 {
settings.Cipher = aes256cbc
2020-07-13 08:04:35 -04:00
}
if len(settings.Auth) == 0 {
settings.Auth = "SHA256"
2020-07-13 08:04:35 -04:00
}
2020-07-13 23:34:03 +00:00
lines = []string{
2020-07-13 08:04:35 -04:00
"client",
"dev tun",
"nobind",
"persist-key",
"remote-cert-tls server",
"ping 10",
"ping-exit 60",
"ping-timer-rem",
2021-01-22 13:36:56 +00:00
"tls-exit",
2020-07-13 08:04:35 -04:00
// Vyprvpn specific
"comp-lzo",
// "verify-x509-name lu1.vyprvpn.com name",
2020-10-20 02:45:28 +00:00
"tls-cipher TLS-ECDHE-RSA-WITH-AES-256-GCM-SHA384:TLS-DHE-RSA-WITH-AES-256-CBC-SHA256:TLS-DHE-RSA-WITH-AES-256-CBC-SHA", //nolint:lll
2020-07-13 08:04:35 -04:00
// Added constant values
"auth-nocache",
"mute-replay-warnings",
"pull-filter ignore \"auth-token\"", // prevent auth failed loops
"auth-retry nointeract",
"suppress-timestamps",
// Modified variables
fmt.Sprintf("verb %d", settings.Verbosity),
2020-07-13 08:04:35 -04:00
fmt.Sprintf("auth-user-pass %s", constants.OpenVPNAuthConf),
fmt.Sprintf("proto %s", connection.Protocol),
fmt.Sprintf("remote %s %d", connection.IP, connection.Port),
"data-ciphers-fallback " + settings.Cipher,
"data-ciphers " + settings.Cipher,
fmt.Sprintf("auth %s", settings.Auth),
2020-07-13 08:04:35 -04:00
}
if !settings.Root {
2020-12-27 00:36:39 +00:00
lines = append(lines, "user "+username)
2020-07-13 08:04:35 -04:00
}
2021-01-19 02:55:38 +00:00
if settings.MSSFix > 0 {
lines = append(lines, "mssfix "+strconv.Itoa(int(settings.MSSFix)))
}
2020-07-13 08:04:35 -04:00
lines = append(lines, []string{
"<ca>",
"-----BEGIN CERTIFICATE-----",
constants.VyprvpnCertificate,
"-----END CERTIFICATE-----",
"</ca>",
}...)
2020-07-13 23:34:03 +00:00
return lines
2020-07-13 08:04:35 -04:00
}
func (v *vyprvpn) PortForward(ctx context.Context, client *http.Client,
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-07-13 08:04:35 -04:00
panic("port forwarding is not supported for vyprvpn")
}