Feat: Wireguard support for Ivpn (#584)

This commit is contained in:
Quentin McGaw (desktop)
2021-08-23 16:01:01 +00:00
parent eb6238ee52
commit 06a2d79cb4
14 changed files with 403 additions and 60 deletions

View File

@@ -10,7 +10,7 @@ import (
func (i *Ivpn) GetConnection(selection configuration.ServerSelection) (
connection models.Connection, err error) {
port := getPort(selection)
protocol := getProtocol(selection.OpenVPN.TCP)
protocol := getProtocol(selection)
servers, err := i.filterServers(selection)
if err != nil {
@@ -26,6 +26,7 @@ func (i *Ivpn) GetConnection(selection configuration.ServerSelection) (
Port: port,
Protocol: protocol,
Hostname: server.Hostname,
PubKey: server.WgPubKey, // Wireguard only
}
connections = append(connections, connection)
}
@@ -39,19 +40,29 @@ func (i *Ivpn) GetConnection(selection configuration.ServerSelection) (
}
func getPort(selection configuration.ServerSelection) (port uint16) {
customPort := selection.OpenVPN.CustomPort
if customPort > 0 {
return customPort
switch selection.VPN {
case constants.Wireguard:
customPort := selection.Wireguard.CustomPort
if customPort > 0 {
return customPort
}
const defaultPort = 58237
return defaultPort
default: // OpenVPN
customPort := selection.OpenVPN.CustomPort
if customPort > 0 {
return customPort
}
port = 1194
if selection.OpenVPN.TCP {
port = 443
}
return port
}
port = 1194
if selection.OpenVPN.TCP {
port = 443
}
return port
}
func getProtocol(tcp bool) (protocol string) {
if tcp {
func getProtocol(selection configuration.ServerSelection) (protocol string) {
if selection.VPN == constants.OpenVPN && selection.OpenVPN.TCP {
return constants.TCP
}
return constants.UDP

View File

@@ -130,6 +130,21 @@ func Test_getPort(t *testing.T) {
},
port: 1234,
},
"Wireguard": {
selection: configuration.ServerSelection{
VPN: constants.Wireguard,
},
port: 58237,
},
"Wireguard custom port": {
selection: configuration.ServerSelection{
VPN: constants.Wireguard,
Wireguard: configuration.WireguardSelection{
CustomPort: 1234,
},
},
port: 1234,
},
}
for name, testCase := range testCases {
@@ -148,16 +163,27 @@ func Test_getProtocol(t *testing.T) {
t.Parallel()
testCases := map[string]struct {
tcp bool
protocol string
selection configuration.ServerSelection
protocol string
}{
"UDP": {
"OpenVPN UDP": {
protocol: constants.UDP,
},
"TCP": {
tcp: true,
"OpenVPN TCP": {
selection: configuration.ServerSelection{
VPN: constants.OpenVPN,
OpenVPN: configuration.OpenVPNSelection{
TCP: true,
},
},
protocol: constants.TCP,
},
"Wireguard": {
selection: configuration.ServerSelection{
VPN: constants.Wireguard,
},
protocol: constants.UDP,
},
}
for name, testCase := range testCases {
@@ -165,7 +191,7 @@ func Test_getProtocol(t *testing.T) {
t.Run(name, func(t *testing.T) {
t.Parallel()
protocol := getProtocol(testCase.tcp)
protocol := getProtocol(testCase.selection)
assert.Equal(t, testCase.protocol, protocol)
})

View File

@@ -11,6 +11,7 @@ func (i *Ivpn) filterServers(selection configuration.ServerSelection) (
for _, server := range i.servers {
switch {
case
server.VPN != selection.VPN,
utils.FilterByPossibilities(server.ISP, selection.ISPs),
utils.FilterByPossibilities(server.Country, selection.Countries),
utils.FilterByPossibilities(server.City, selection.Cities),