Skip to content

Commit

Permalink
chore: simplify provider GetConnection
Browse files Browse the repository at this point in the history
  • Loading branch information
qdm12 committed Apr 20, 2022
1 parent 306d849 commit 0c0f166
Show file tree
Hide file tree
Showing 36 changed files with 243 additions and 707 deletions.
6 changes: 6 additions & 0 deletions internal/configuration/settings/openvpnselection.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ func (o OpenVPNSelection) validate(vpnProvider string) (err error) {
case providers.Perfectprivacy:
allowedTCP = []uint16{44, 443, 4433}
allowedUDP = []uint16{44, 443, 4433}
case providers.PrivateInternetAccess:
allowedTCP = []uint16{80, 110, 443}
allowedUDP = []uint16{53, 1194, 1197, 1198, 8080, 9201}
case providers.Protonvpn:
allowedTCP = []uint16{443, 5995, 8443}
allowedUDP = []uint16{80, 443, 1194, 4569, 5060}
case providers.Wevpn:
allowedTCP = []uint16{53, 1195, 1199, 2018}
allowedUDP = []uint16{80, 1194, 1198}
Expand Down
15 changes: 6 additions & 9 deletions internal/provider/custom/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/qdm12/gluetun/internal/constants/vpn"
"github.com/qdm12/gluetun/internal/models"
"github.com/qdm12/gluetun/internal/openvpn/extract"
"github.com/qdm12/gluetun/internal/provider/utils"
)

var (
Expand Down Expand Up @@ -37,23 +36,21 @@ func getOpenVPNConnection(extractor extract.Interface,
return connection, fmt.Errorf("cannot extract connection: %w", err)
}

connection.Port = getPort(connection.Port, selection)
customPort := *selection.OpenVPN.CustomPort
if customPort > 0 {
connection.Port = customPort
}

return connection, nil
}

func getWireguardConnection(selection settings.ServerSelection) (
connection models.Connection) {
port := getPort(*selection.Wireguard.EndpointPort, selection)
return models.Connection{
Type: vpn.Wireguard,
IP: selection.Wireguard.EndpointIP,
Port: port,
Port: *selection.Wireguard.EndpointPort,
Protocol: constants.UDP,
PubKey: selection.Wireguard.PublicKey,
}
}

// Port found is overridden by custom port set with `VPN_ENDPOINT_PORT`.
func getPort(foundPort uint16, selection settings.ServerSelection) (port uint16) {
return utils.GetPort(selection, foundPort, foundPort, foundPort)
}
28 changes: 2 additions & 26 deletions internal/provider/cyberghost/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,12 @@ package cyberghost

import (
"github.com/qdm12/gluetun/internal/configuration/settings"
"github.com/qdm12/gluetun/internal/constants"
"github.com/qdm12/gluetun/internal/models"
"github.com/qdm12/gluetun/internal/provider/utils"
)

func (c *Cyberghost) GetConnection(selection settings.ServerSelection) (
connection models.Connection, err error) {
const port = 443
protocol := constants.UDP
if *selection.OpenVPN.TCP {
protocol = constants.TCP
}

servers, err := utils.FilterServers(c.servers, 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, c.randSource)
defaults := utils.NewConnectionDefaults(443, 443, 0) //nolint:gomnd
return utils.GetConnection(c.servers, selection, defaults, c.randSource)
}
35 changes: 2 additions & 33 deletions internal/provider/expressvpn/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,6 @@ import (

func (p *Provider) GetConnection(selection settings.ServerSelection) (
connection models.Connection, err error) {
port := getPort(selection)
protocol := utils.GetProtocol(selection)

servers, err := utils.FilterServers(p.servers, 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,
Hostname: server.Hostname,
}
connections = append(connections, connection)
}
}

return utils.PickConnection(connections, selection, p.randSource)
}

func getPort(selection settings.ServerSelection) (port uint16) {
const (
defaultOpenVPNTCP = 0
defaultOpenVPNUDP = 1195
defaultWireguard = 0
)
return utils.GetPort(selection, defaultOpenVPNTCP,
defaultOpenVPNUDP, defaultWireguard)
defaults := utils.NewConnectionDefaults(0, 1195, 0) //nolint:gomnd
return utils.GetConnection(p.servers, selection, defaults, p.randSource)
}
2 changes: 1 addition & 1 deletion internal/provider/expressvpn/connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func Test_Provider_GetConnection(t *testing.T) {
"no server available": {
selection: settings.ServerSelection{}.WithDefaults(providers.Expressvpn),
errWrapped: utils.ErrNoServerFound,
errMessage: "no server found: for VPN openvpn; protocol udp",
errMessage: "cannot filter servers: no server found: for VPN openvpn; protocol udp",
},
"no filter": {
servers: []models.Server{
Expand Down
28 changes: 2 additions & 26 deletions internal/provider/fastestvpn/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,12 @@ package fastestvpn

import (
"github.com/qdm12/gluetun/internal/configuration/settings"
"github.com/qdm12/gluetun/internal/constants"
"github.com/qdm12/gluetun/internal/models"
"github.com/qdm12/gluetun/internal/provider/utils"
)

func (f *Fastestvpn) GetConnection(selection settings.ServerSelection) (
connection models.Connection, err error) {
const port = 4443
protocol := constants.UDP
if *selection.OpenVPN.TCP {
protocol = constants.TCP
}

servers, err := utils.FilterServers(f.servers, 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, f.randSource)
defaults := utils.NewConnectionDefaults(4443, 4443, 0) //nolint:gomnd
return utils.GetConnection(f.servers, selection, defaults, f.randSource)
}
33 changes: 2 additions & 31 deletions internal/provider/hidemyass/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,12 @@ package hidemyass

import (
"github.com/qdm12/gluetun/internal/configuration/settings"
"github.com/qdm12/gluetun/internal/constants"
"github.com/qdm12/gluetun/internal/models"
"github.com/qdm12/gluetun/internal/provider/utils"
)

func (h *HideMyAss) GetConnection(selection settings.ServerSelection) (
connection models.Connection, err error) {
var port uint16 = 553
protocol := constants.UDP
if *selection.OpenVPN.TCP {
protocol = constants.TCP
port = 8080
}

if *selection.OpenVPN.CustomPort > 0 {
port = *selection.OpenVPN.CustomPort
}

servers, err := utils.FilterServers(h.servers, 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, h.randSource)
defaults := utils.NewConnectionDefaults(8080, 553, 0) //nolint:gomnd
return utils.GetConnection(h.servers, selection, defaults, h.randSource)
}
26 changes: 2 additions & 24 deletions internal/provider/ipvanish/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,12 @@ package ipvanish

import (
"github.com/qdm12/gluetun/internal/configuration/settings"
"github.com/qdm12/gluetun/internal/constants"
"github.com/qdm12/gluetun/internal/models"
"github.com/qdm12/gluetun/internal/provider/utils"
)

func (i *Ipvanish) GetConnection(selection settings.ServerSelection) (
connection models.Connection, err error) {
const port = 443
const protocol = constants.UDP

servers, err := utils.FilterServers(i.servers, 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,
Hostname: server.Hostname,
}
connections = append(connections, connection)
}
}

return utils.PickConnection(connections, selection, i.randSource)
defaults := utils.NewConnectionDefaults(0, 443, 0) //nolint:gomnd
return utils.GetConnection(i.servers, selection, defaults, i.randSource)
}
36 changes: 2 additions & 34 deletions internal/provider/ivpn/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,6 @@ import (

func (i *Ivpn) GetConnection(selection settings.ServerSelection) (
connection models.Connection, err error) {
port := getPort(selection)
protocol := utils.GetProtocol(selection)

servers, err := utils.FilterServers(i.servers, 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,
Hostname: server.Hostname,
PubKey: server.WgPubKey, // Wireguard only
}
connections = append(connections, connection)
}
}

return utils.PickConnection(connections, selection, i.randSource)
}

func getPort(selection settings.ServerSelection) (port uint16) {
const (
defaultOpenVPNTCP = 443
defaultOpenVPNUDP = 1194
defaultWireguard = 58237
)
return utils.GetPort(selection, defaultOpenVPNTCP,
defaultOpenVPNUDP, defaultWireguard)
defaults := utils.NewConnectionDefaults(443, 1194, 58237) //nolint:gomnd
return utils.GetConnection(i.servers, selection, defaults, i.randSource)
}
2 changes: 1 addition & 1 deletion internal/provider/ivpn/connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func Test_Ivpn_GetConnection(t *testing.T) {
"no server available": {
selection: settings.ServerSelection{}.WithDefaults(providers.Ivpn),
errWrapped: utils.ErrNoServerFound,
errMessage: "no server found: for VPN openvpn; protocol udp",
errMessage: "cannot filter servers: no server found: for VPN openvpn; protocol udp",
},
"no filter": {
servers: []models.Server{
Expand Down
39 changes: 2 additions & 37 deletions internal/provider/mullvad/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,6 @@ import (

func (m *Mullvad) GetConnection(selection settings.ServerSelection) (
connection models.Connection, err error) {
port := getPort(selection)
protocol := utils.GetProtocol(selection)

servers, err := utils.FilterServers(m.servers, selection)
if err != nil {
return connection, err
}

connections := make([]models.Connection, 0, len(servers))
for _, server := range servers {
for _, IP := range server.IPs {
if IP.To4() == nil {
// do not use IPv6 connections for now
continue
}
connection := models.Connection{
Type: selection.VPN,
IP: IP,
Port: port,
Protocol: protocol,
PubKey: server.WgPubKey, // Wireguard only
}
connections = append(connections, connection)
}
}

return utils.PickConnection(connections, selection, m.randSource)
}

func getPort(selection settings.ServerSelection) (port uint16) {
const (
defaultOpenVPNTCP = 443
defaultOpenVPNUDP = 1194
defaultWireguard = 51820
)
return utils.GetPort(selection, defaultOpenVPNTCP,
defaultOpenVPNUDP, defaultWireguard)
defaults := utils.NewConnectionDefaults(443, 1194, 51820) //nolint:gomnd
return utils.GetConnection(m.servers, selection, defaults, m.randSource)
}
3 changes: 2 additions & 1 deletion internal/provider/mullvad/connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func Test_Mullvad_GetConnection(t *testing.T) {
"no server available": {
selection: settings.ServerSelection{}.WithDefaults(providers.Mullvad),
errWrapped: utils.ErrNoServerFound,
errMessage: "no server found: for VPN openvpn; protocol udp",
errMessage: "cannot filter servers: no server found: for VPN openvpn; protocol udp",
},
"no filter": {
servers: []models.Server{
Expand Down Expand Up @@ -72,6 +72,7 @@ func Test_Mullvad_GetConnection(t *testing.T) {
Type: vpn.OpenVPN,
IP: net.IPv4(2, 2, 2, 2),
Port: 1194,
Hostname: "b",
Protocol: constants.UDP,
},
},
Expand Down

0 comments on commit 0c0f166

Please sign in to comment.