Feat: WeVPN support (#591)
This commit is contained in:
@@ -28,6 +28,7 @@ import (
|
||||
"github.com/qdm12/gluetun/internal/provider/torguard"
|
||||
"github.com/qdm12/gluetun/internal/provider/vpnunlimited"
|
||||
"github.com/qdm12/gluetun/internal/provider/vyprvpn"
|
||||
"github.com/qdm12/gluetun/internal/provider/wevpn"
|
||||
"github.com/qdm12/gluetun/internal/provider/windscribe"
|
||||
"github.com/qdm12/golibs/logging"
|
||||
)
|
||||
@@ -85,6 +86,8 @@ func New(provider string, allServers models.AllServers, timeNow func() time.Time
|
||||
return vpnunlimited.New(allServers.VPNUnlimited.Servers, randSource)
|
||||
case constants.Vyprvpn:
|
||||
return vyprvpn.New(allServers.Vyprvpn.Servers, randSource)
|
||||
case constants.Wevpn:
|
||||
return wevpn.New(allServers.Wevpn.Servers, randSource)
|
||||
case constants.Windscribe:
|
||||
return windscribe.New(allServers.Windscribe.Servers, randSource)
|
||||
default:
|
||||
|
||||
43
internal/provider/wevpn/connection.go
Normal file
43
internal/provider/wevpn/connection.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package wevpn
|
||||
|
||||
import (
|
||||
"github.com/qdm12/gluetun/internal/configuration"
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
"github.com/qdm12/gluetun/internal/provider/utils"
|
||||
)
|
||||
|
||||
func (w *Wevpn) GetConnection(selection configuration.ServerSelection) (
|
||||
connection models.Connection, err error) {
|
||||
port := getPort(selection)
|
||||
protocol := utils.GetProtocol(selection)
|
||||
|
||||
servers, err := w.filterServers(selection)
|
||||
if err != nil {
|
||||
return connection, err
|
||||
}
|
||||
|
||||
var connections []models.Connection
|
||||
for _, server := range servers {
|
||||
for _, IP := range server.IPs {
|
||||
connection := models.Connection{
|
||||
Type: selection.VPN,
|
||||
IP: IP,
|
||||
Port: port,
|
||||
Protocol: protocol,
|
||||
}
|
||||
connections = append(connections, connection)
|
||||
}
|
||||
}
|
||||
|
||||
return utils.PickConnection(connections, selection, w.randSource)
|
||||
}
|
||||
|
||||
func getPort(selection configuration.ServerSelection) (port uint16) {
|
||||
const (
|
||||
defaultOpenVPNTCP = 1195
|
||||
defaultOpenVPNUDP = 1194
|
||||
defaultWireguard = 0 // Wireguard not supported
|
||||
)
|
||||
return utils.GetPort(selection, defaultOpenVPNTCP,
|
||||
defaultOpenVPNUDP, defaultWireguard)
|
||||
}
|
||||
96
internal/provider/wevpn/connection_test.go
Normal file
96
internal/provider/wevpn/connection_test.go
Normal file
@@ -0,0 +1,96 @@
|
||||
package wevpn
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"math/rand"
|
||||
"net"
|
||||
"testing"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/configuration"
|
||||
"github.com/qdm12/gluetun/internal/constants"
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func Test_Wevpn_GetConnection(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
testCases := map[string]struct {
|
||||
servers []models.WevpnServer
|
||||
selection configuration.ServerSelection
|
||||
connection models.Connection
|
||||
err error
|
||||
}{
|
||||
"no server available": {
|
||||
selection: configuration.ServerSelection{
|
||||
VPN: constants.OpenVPN,
|
||||
},
|
||||
err: errors.New("no server found: for VPN openvpn; protocol udp"),
|
||||
},
|
||||
"no filter": {
|
||||
servers: []models.WevpnServer{
|
||||
{UDP: true, IPs: []net.IP{net.IPv4(1, 1, 1, 1)}},
|
||||
{UDP: true, IPs: []net.IP{net.IPv4(2, 2, 2, 2)}},
|
||||
{UDP: true, IPs: []net.IP{net.IPv4(3, 3, 3, 3)}},
|
||||
},
|
||||
connection: models.Connection{
|
||||
IP: net.IPv4(1, 1, 1, 1),
|
||||
Port: 1194,
|
||||
Protocol: constants.UDP,
|
||||
},
|
||||
},
|
||||
"target IP": {
|
||||
selection: configuration.ServerSelection{
|
||||
TargetIP: net.IPv4(2, 2, 2, 2),
|
||||
},
|
||||
servers: []models.WevpnServer{
|
||||
{UDP: true, IPs: []net.IP{net.IPv4(1, 1, 1, 1)}},
|
||||
{UDP: true, IPs: []net.IP{net.IPv4(2, 2, 2, 2)}},
|
||||
{UDP: true, IPs: []net.IP{net.IPv4(3, 3, 3, 3)}},
|
||||
},
|
||||
connection: models.Connection{
|
||||
IP: net.IPv4(2, 2, 2, 2),
|
||||
Port: 1194,
|
||||
Protocol: constants.UDP,
|
||||
},
|
||||
},
|
||||
"with filter": {
|
||||
selection: configuration.ServerSelection{
|
||||
Hostnames: []string{"b"},
|
||||
},
|
||||
servers: []models.WevpnServer{
|
||||
{UDP: true, Hostname: "a", IPs: []net.IP{net.IPv4(1, 1, 1, 1)}},
|
||||
{UDP: true, Hostname: "b", IPs: []net.IP{net.IPv4(2, 2, 2, 2)}},
|
||||
{UDP: true, Hostname: "a", IPs: []net.IP{net.IPv4(3, 3, 3, 3)}},
|
||||
},
|
||||
connection: models.Connection{
|
||||
IP: net.IPv4(2, 2, 2, 2),
|
||||
Port: 1194,
|
||||
Protocol: constants.UDP,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for name, testCase := range testCases {
|
||||
testCase := testCase
|
||||
t.Run(name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
randSource := rand.NewSource(0)
|
||||
|
||||
m := New(testCase.servers, randSource)
|
||||
|
||||
connection, err := m.GetConnection(testCase.selection)
|
||||
|
||||
if testCase.err != nil {
|
||||
require.Error(t, err)
|
||||
assert.Equal(t, testCase.err.Error(), err.Error())
|
||||
} else {
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
assert.Equal(t, testCase.connection, connection)
|
||||
})
|
||||
}
|
||||
}
|
||||
27
internal/provider/wevpn/filter.go
Normal file
27
internal/provider/wevpn/filter.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package wevpn
|
||||
|
||||
import (
|
||||
"github.com/qdm12/gluetun/internal/configuration"
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
"github.com/qdm12/gluetun/internal/provider/utils"
|
||||
)
|
||||
|
||||
func (w *Wevpn) filterServers(selection configuration.ServerSelection) (
|
||||
servers []models.WevpnServer, err error) {
|
||||
for _, server := range w.servers {
|
||||
switch {
|
||||
case
|
||||
utils.FilterByProtocol(selection, server.TCP, server.UDP),
|
||||
utils.FilterByPossibilities(server.City, selection.Cities),
|
||||
utils.FilterByPossibilities(server.Hostname, selection.Hostnames):
|
||||
default:
|
||||
servers = append(servers, server)
|
||||
}
|
||||
}
|
||||
|
||||
if len(servers) == 0 {
|
||||
return nil, utils.NoServerFoundError(selection)
|
||||
}
|
||||
|
||||
return servers, nil
|
||||
}
|
||||
104
internal/provider/wevpn/filter_test.go
Normal file
104
internal/provider/wevpn/filter_test.go
Normal file
@@ -0,0 +1,104 @@
|
||||
package wevpn
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"math/rand"
|
||||
"testing"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/configuration"
|
||||
"github.com/qdm12/gluetun/internal/constants"
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func Test_Wevpn_filterServers(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
testCases := map[string]struct {
|
||||
servers []models.WevpnServer
|
||||
selection configuration.ServerSelection
|
||||
filtered []models.WevpnServer
|
||||
err error
|
||||
}{
|
||||
"no server available": {
|
||||
selection: configuration.ServerSelection{
|
||||
VPN: constants.OpenVPN,
|
||||
},
|
||||
err: errors.New("no server found: for VPN openvpn; protocol udp"),
|
||||
},
|
||||
"no filter": {
|
||||
servers: []models.WevpnServer{
|
||||
{Hostname: "a", UDP: true},
|
||||
{Hostname: "b", UDP: true},
|
||||
{Hostname: "c", UDP: true},
|
||||
},
|
||||
filtered: []models.WevpnServer{
|
||||
{Hostname: "a", UDP: true},
|
||||
{Hostname: "b", UDP: true},
|
||||
{Hostname: "c", UDP: true},
|
||||
},
|
||||
},
|
||||
"filter by protocol": {
|
||||
selection: configuration.ServerSelection{
|
||||
OpenVPN: configuration.OpenVPNSelection{TCP: true},
|
||||
},
|
||||
servers: []models.WevpnServer{
|
||||
{Hostname: "a", UDP: true},
|
||||
{Hostname: "b", TCP: true},
|
||||
{Hostname: "c", UDP: true},
|
||||
},
|
||||
filtered: []models.WevpnServer{
|
||||
{Hostname: "b", TCP: true},
|
||||
},
|
||||
},
|
||||
"filter by city": {
|
||||
selection: configuration.ServerSelection{
|
||||
Cities: []string{"b"},
|
||||
},
|
||||
servers: []models.WevpnServer{
|
||||
{City: "a", UDP: true},
|
||||
{City: "b", UDP: true},
|
||||
{City: "c", UDP: true},
|
||||
},
|
||||
filtered: []models.WevpnServer{
|
||||
{City: "b", UDP: true},
|
||||
},
|
||||
},
|
||||
"filter by hostname": {
|
||||
selection: configuration.ServerSelection{
|
||||
Hostnames: []string{"b"},
|
||||
},
|
||||
servers: []models.WevpnServer{
|
||||
{Hostname: "a", UDP: true},
|
||||
{Hostname: "b", UDP: true},
|
||||
{Hostname: "c", UDP: true},
|
||||
},
|
||||
filtered: []models.WevpnServer{
|
||||
{Hostname: "b", UDP: true},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for name, testCase := range testCases {
|
||||
testCase := testCase
|
||||
t.Run(name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
randSource := rand.NewSource(0)
|
||||
|
||||
w := New(testCase.servers, randSource)
|
||||
|
||||
servers, err := w.filterServers(testCase.selection)
|
||||
|
||||
if testCase.err != nil {
|
||||
require.Error(t, err)
|
||||
assert.Equal(t, testCase.err.Error(), err.Error())
|
||||
} else {
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
assert.Equal(t, testCase.filtered, servers)
|
||||
})
|
||||
}
|
||||
}
|
||||
84
internal/provider/wevpn/openvpnconf.go
Normal file
84
internal/provider/wevpn/openvpnconf.go
Normal file
@@ -0,0 +1,84 @@
|
||||
package wevpn
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/configuration"
|
||||
"github.com/qdm12/gluetun/internal/constants"
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
"github.com/qdm12/gluetun/internal/provider/utils"
|
||||
)
|
||||
|
||||
func (w *Wevpn) BuildConf(connection models.Connection,
|
||||
settings configuration.OpenVPN) (lines []string, err error) {
|
||||
if settings.Cipher == "" {
|
||||
settings.Cipher = constants.AES256gcm
|
||||
}
|
||||
|
||||
if settings.Auth == "" {
|
||||
settings.Auth = constants.SHA512
|
||||
}
|
||||
|
||||
lines = []string{
|
||||
"client",
|
||||
"nobind",
|
||||
"tls-exit",
|
||||
"dev " + settings.Interface,
|
||||
"verb " + strconv.Itoa(settings.Verbosity),
|
||||
|
||||
// Wevpn specific
|
||||
"ping 30",
|
||||
"remote-cert-tls server",
|
||||
"redirect-gateway def1 bypass-dhcp",
|
||||
"reneg-sec 0",
|
||||
"auth-user-pass " + constants.OpenVPNAuthConf,
|
||||
"auth " + settings.Auth,
|
||||
|
||||
// Added constant values
|
||||
"auth-nocache",
|
||||
"mute-replay-warnings",
|
||||
"pull-filter ignore \"auth-token\"", // prevent auth failed loops
|
||||
"auth-retry nointeract",
|
||||
"suppress-timestamps",
|
||||
|
||||
// Modified variables
|
||||
connection.OpenVPNProtoLine(),
|
||||
connection.OpenVPNRemoteLine(),
|
||||
}
|
||||
|
||||
if connection.Protocol == constants.UDP {
|
||||
lines = append(lines, "explicit-exit-notify")
|
||||
}
|
||||
|
||||
lines = append(lines, utils.CipherLines(settings.Cipher, settings.Version)...)
|
||||
|
||||
if !settings.Root {
|
||||
lines = append(lines, "user "+settings.ProcUser)
|
||||
lines = append(lines, "persist-tun")
|
||||
lines = append(lines, "persist-key")
|
||||
}
|
||||
|
||||
if settings.MSSFix > 0 {
|
||||
lines = append(lines, "mssfix "+strconv.Itoa(int(settings.MSSFix)))
|
||||
}
|
||||
|
||||
if settings.IPv6 {
|
||||
lines = append(lines, "tun-ipv6")
|
||||
} else {
|
||||
lines = append(lines, `pull-filter ignore "route-ipv6"`)
|
||||
lines = append(lines, `pull-filter ignore "ifconfig-ipv6"`)
|
||||
}
|
||||
|
||||
lines = append(lines, utils.WrapOpenvpnKey(
|
||||
settings.ClientKey)...)
|
||||
lines = append(lines, utils.WrapOpenvpnCA(
|
||||
constants.WevpnCA)...)
|
||||
lines = append(lines, utils.WrapOpenvpnCert(
|
||||
constants.WevpnCertificate)...)
|
||||
lines = append(lines, utils.WrapOpenvpnTLSCrypt(
|
||||
constants.WevpnOpenvpnStaticKeyV1)...)
|
||||
|
||||
lines = append(lines, "")
|
||||
|
||||
return lines, nil
|
||||
}
|
||||
23
internal/provider/wevpn/provider.go
Normal file
23
internal/provider/wevpn/provider.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package wevpn
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/constants"
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
"github.com/qdm12/gluetun/internal/provider/utils"
|
||||
)
|
||||
|
||||
type Wevpn struct {
|
||||
servers []models.WevpnServer
|
||||
randSource rand.Source
|
||||
utils.NoPortForwarder
|
||||
}
|
||||
|
||||
func New(servers []models.WevpnServer, randSource rand.Source) *Wevpn {
|
||||
return &Wevpn{
|
||||
servers: servers,
|
||||
randSource: randSource,
|
||||
NoPortForwarder: utils.NewNoPortForwarding(constants.Wevpn),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user