Maintenance: Protocol selection as boolean in code
This commit is contained in:
@@ -45,8 +45,9 @@ func (c *cyberghost) filterServers(regions, hostnames []string, group string) (s
|
||||
func (c *cyberghost) GetOpenVPNConnection(selection configuration.ServerSelection) (
|
||||
connection models.OpenVPNConnection, err error) {
|
||||
const httpsPort = 443
|
||||
protocol := tcpBoolToProtocol(selection.TCP)
|
||||
if selection.TargetIP != nil {
|
||||
return models.OpenVPNConnection{IP: selection.TargetIP, Port: httpsPort, Protocol: selection.Protocol}, nil
|
||||
return models.OpenVPNConnection{IP: selection.TargetIP, Port: httpsPort, Protocol: protocol}, nil
|
||||
}
|
||||
|
||||
servers := c.filterServers(selection.Regions, selection.Hostnames, selection.Group)
|
||||
@@ -58,7 +59,7 @@ func (c *cyberghost) GetOpenVPNConnection(selection configuration.ServerSelectio
|
||||
var connections []models.OpenVPNConnection
|
||||
for _, server := range servers {
|
||||
for _, IP := range server.IPs {
|
||||
connections = append(connections, models.OpenVPNConnection{IP: IP, Port: httpsPort, Protocol: selection.Protocol})
|
||||
connections = append(connections, models.OpenVPNConnection{IP: IP, Port: httpsPort, Protocol: protocol})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -28,20 +28,13 @@ func newFastestvpn(servers []models.FastestvpnServer, timeNow timeNowFunc) *fast
|
||||
}
|
||||
}
|
||||
|
||||
func (f *fastestvpn) filterServers(countries, hostnames []string, protocol string) (servers []models.FastestvpnServer) {
|
||||
var tcp, udp bool
|
||||
if protocol == "tcp" {
|
||||
tcp = true
|
||||
} else {
|
||||
udp = true
|
||||
}
|
||||
|
||||
func (f *fastestvpn) filterServers(countries, hostnames []string, tcp bool) (servers []models.FastestvpnServer) {
|
||||
for _, server := range f.servers {
|
||||
switch {
|
||||
case filterByPossibilities(server.Country, countries):
|
||||
case filterByPossibilities(server.Hostname, hostnames):
|
||||
case tcp && !server.TCP:
|
||||
case udp && !server.UDP:
|
||||
case !tcp && !server.UDP:
|
||||
default:
|
||||
servers = append(servers, server)
|
||||
}
|
||||
@@ -50,7 +43,7 @@ func (f *fastestvpn) filterServers(countries, hostnames []string, protocol strin
|
||||
}
|
||||
|
||||
func (f *fastestvpn) notFoundErr(selection configuration.ServerSelection) error {
|
||||
message := "no server found for protocol " + selection.Protocol
|
||||
message := "no server found for protocol " + tcpBoolToProtocol(selection.TCP)
|
||||
|
||||
if len(selection.Hostnames) > 0 {
|
||||
message += " + hostnames " + commaJoin(selection.Hostnames)
|
||||
@@ -66,12 +59,13 @@ func (f *fastestvpn) notFoundErr(selection configuration.ServerSelection) error
|
||||
func (f *fastestvpn) GetOpenVPNConnection(selection configuration.ServerSelection) (
|
||||
connection models.OpenVPNConnection, err error) {
|
||||
var port uint16 = 4443
|
||||
protocol := tcpBoolToProtocol(selection.TCP)
|
||||
|
||||
if selection.TargetIP != nil {
|
||||
return models.OpenVPNConnection{IP: selection.TargetIP, Port: port, Protocol: selection.Protocol}, nil
|
||||
return models.OpenVPNConnection{IP: selection.TargetIP, Port: port, Protocol: protocol}, nil
|
||||
}
|
||||
|
||||
servers := f.filterServers(selection.Countries, selection.Hostnames, selection.Protocol)
|
||||
servers := f.filterServers(selection.Countries, selection.Hostnames, selection.TCP)
|
||||
if len(servers) == 0 {
|
||||
return connection, f.notFoundErr(selection)
|
||||
}
|
||||
@@ -82,7 +76,7 @@ func (f *fastestvpn) GetOpenVPNConnection(selection configuration.ServerSelectio
|
||||
connection := models.OpenVPNConnection{
|
||||
IP: IP,
|
||||
Port: port,
|
||||
Protocol: selection.Protocol,
|
||||
Protocol: protocol,
|
||||
}
|
||||
connections = append(connections, connection)
|
||||
}
|
||||
|
||||
@@ -30,15 +30,15 @@ func newHideMyAss(servers []models.HideMyAssServer, timeNow timeNowFunc) *hideMy
|
||||
}
|
||||
|
||||
func (h *hideMyAss) filterServers(countries, cities, hostnames []string,
|
||||
protocol string) (servers []models.HideMyAssServer) {
|
||||
tcp bool) (servers []models.HideMyAssServer) {
|
||||
for _, server := range h.servers {
|
||||
switch {
|
||||
case
|
||||
filterByPossibilities(server.Country, countries),
|
||||
filterByPossibilities(server.City, cities),
|
||||
filterByPossibilities(server.Hostname, hostnames),
|
||||
protocol == constants.TCP && !server.TCP,
|
||||
protocol == constants.UDP && !server.UDP:
|
||||
tcp && !server.TCP,
|
||||
!tcp && !server.UDP:
|
||||
default:
|
||||
servers = append(servers, server)
|
||||
}
|
||||
@@ -67,7 +67,9 @@ func (h *hideMyAss) notFoundErr(selection configuration.ServerSelection) error {
|
||||
func (h *hideMyAss) GetOpenVPNConnection(selection configuration.ServerSelection) (
|
||||
connection models.OpenVPNConnection, err error) {
|
||||
var defaultPort uint16 = 553
|
||||
if selection.Protocol == constants.TCP {
|
||||
protocol := constants.UDP
|
||||
if selection.TCP {
|
||||
protocol = constants.TCP
|
||||
defaultPort = 8080
|
||||
}
|
||||
port := defaultPort
|
||||
@@ -76,10 +78,14 @@ func (h *hideMyAss) GetOpenVPNConnection(selection configuration.ServerSelection
|
||||
}
|
||||
|
||||
if selection.TargetIP != nil {
|
||||
return models.OpenVPNConnection{IP: selection.TargetIP, Port: port, Protocol: selection.Protocol}, nil
|
||||
return models.OpenVPNConnection{
|
||||
IP: selection.TargetIP,
|
||||
Port: port,
|
||||
Protocol: protocol,
|
||||
}, nil
|
||||
}
|
||||
|
||||
servers := h.filterServers(selection.Countries, selection.Cities, selection.Hostnames, selection.Protocol)
|
||||
servers := h.filterServers(selection.Countries, selection.Cities, selection.Hostnames, selection.TCP)
|
||||
if len(servers) == 0 {
|
||||
return models.OpenVPNConnection{}, h.notFoundErr(selection)
|
||||
}
|
||||
@@ -87,7 +93,11 @@ func (h *hideMyAss) GetOpenVPNConnection(selection configuration.ServerSelection
|
||||
var connections []models.OpenVPNConnection
|
||||
for _, server := range servers {
|
||||
for _, IP := range server.IPs {
|
||||
connections = append(connections, models.OpenVPNConnection{IP: IP, Port: port, Protocol: selection.Protocol})
|
||||
connections = append(connections, models.OpenVPNConnection{
|
||||
IP: IP,
|
||||
Port: port,
|
||||
Protocol: protocol,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -48,8 +48,10 @@ func (m *mullvad) filterServers(countries, cities, hostnames,
|
||||
func (m *mullvad) GetOpenVPNConnection(selection configuration.ServerSelection) (
|
||||
connection models.OpenVPNConnection, err error) {
|
||||
var defaultPort uint16 = 1194
|
||||
if selection.Protocol == constants.TCP {
|
||||
protocol := constants.UDP
|
||||
if selection.TCP {
|
||||
defaultPort = 443
|
||||
protocol = constants.TCP
|
||||
}
|
||||
port := defaultPort
|
||||
if selection.CustomPort > 0 {
|
||||
@@ -57,7 +59,7 @@ func (m *mullvad) GetOpenVPNConnection(selection configuration.ServerSelection)
|
||||
}
|
||||
|
||||
if selection.TargetIP != nil {
|
||||
return models.OpenVPNConnection{IP: selection.TargetIP, Port: port, Protocol: selection.Protocol}, nil
|
||||
return models.OpenVPNConnection{IP: selection.TargetIP, Port: port, Protocol: protocol}, nil
|
||||
}
|
||||
|
||||
servers := m.filterServers(selection.Countries, selection.Cities,
|
||||
@@ -70,7 +72,7 @@ func (m *mullvad) GetOpenVPNConnection(selection configuration.ServerSelection)
|
||||
var connections []models.OpenVPNConnection
|
||||
for _, server := range servers {
|
||||
for _, IP := range server.IPs {
|
||||
connections = append(connections, models.OpenVPNConnection{IP: IP, Port: port, Protocol: selection.Protocol})
|
||||
connections = append(connections, models.OpenVPNConnection{IP: IP, Port: port, Protocol: protocol})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -29,7 +29,7 @@ func newNordvpn(servers []models.NordvpnServer, timeNow timeNowFunc) *nordvpn {
|
||||
}
|
||||
}
|
||||
|
||||
func (n *nordvpn) filterServers(regions, hostnames, names []string, numbers []uint16, protocol string) (
|
||||
func (n *nordvpn) filterServers(regions, hostnames, names []string, numbers []uint16, tcp bool) (
|
||||
servers []models.NordvpnServer) {
|
||||
numbersStr := make([]string, len(numbers))
|
||||
for i := range numbers {
|
||||
@@ -39,8 +39,8 @@ func (n *nordvpn) filterServers(regions, hostnames, names []string, numbers []ui
|
||||
numberStr := fmt.Sprintf("%d", server.Number)
|
||||
switch {
|
||||
case
|
||||
protocol == constants.TCP && !server.TCP,
|
||||
protocol == constants.UDP && !server.UDP,
|
||||
tcp && !server.TCP,
|
||||
!tcp && !server.UDP,
|
||||
filterByPossibilities(server.Region, regions),
|
||||
filterByPossibilities(server.Hostname, hostnames),
|
||||
filterByPossibilities(server.Name, names),
|
||||
@@ -55,7 +55,7 @@ func (n *nordvpn) filterServers(regions, hostnames, names []string, numbers []ui
|
||||
var errNoServerFound = errors.New("no server found")
|
||||
|
||||
func (n *nordvpn) notFoundErr(selection configuration.ServerSelection) error {
|
||||
message := "for protocol " + selection.Protocol
|
||||
message := "for protocol " + tcpBoolToProtocol(selection.TCP)
|
||||
|
||||
if len(selection.Regions) > 0 {
|
||||
message += " + regions " + commaJoin(selection.Regions)
|
||||
@@ -82,29 +82,26 @@ func (n *nordvpn) notFoundErr(selection configuration.ServerSelection) error {
|
||||
|
||||
func (n *nordvpn) GetOpenVPNConnection(selection configuration.ServerSelection) (
|
||||
connection models.OpenVPNConnection, err error) {
|
||||
var port uint16
|
||||
switch {
|
||||
case selection.Protocol == constants.UDP:
|
||||
port = 1194
|
||||
case selection.Protocol == constants.TCP:
|
||||
var port uint16 = 1194
|
||||
protocol := constants.UDP
|
||||
if selection.TCP {
|
||||
port = 443
|
||||
default:
|
||||
return connection, fmt.Errorf("protocol %q is unknown", selection.Protocol)
|
||||
protocol = constants.TCP
|
||||
}
|
||||
|
||||
if selection.TargetIP != nil {
|
||||
return models.OpenVPNConnection{IP: selection.TargetIP, Port: port, Protocol: selection.Protocol}, nil
|
||||
return models.OpenVPNConnection{IP: selection.TargetIP, Port: port, Protocol: protocol}, nil
|
||||
}
|
||||
|
||||
servers := n.filterServers(selection.Regions, selection.Hostnames,
|
||||
selection.Names, selection.Numbers, selection.Protocol)
|
||||
selection.Names, selection.Numbers, selection.TCP)
|
||||
if len(servers) == 0 {
|
||||
return connection, n.notFoundErr(selection)
|
||||
}
|
||||
|
||||
connections := make([]models.OpenVPNConnection, len(servers))
|
||||
for i := range servers {
|
||||
connections[i] = models.OpenVPNConnection{IP: servers[i].IP, Port: port, Protocol: selection.Protocol}
|
||||
connections[i] = models.OpenVPNConnection{IP: servers[i].IP, Port: port, Protocol: protocol}
|
||||
}
|
||||
|
||||
return pickRandomConnection(connections, n.randSource), nil
|
||||
|
||||
@@ -47,15 +47,14 @@ var (
|
||||
|
||||
func (p *pia) getPort(selection configuration.ServerSelection) (port uint16, err error) {
|
||||
if selection.CustomPort == 0 {
|
||||
switch selection.Protocol {
|
||||
case constants.TCP:
|
||||
if selection.TCP {
|
||||
switch selection.EncryptionPreset {
|
||||
case constants.PIAEncryptionPresetNormal:
|
||||
port = 502
|
||||
case constants.PIAEncryptionPresetStrong:
|
||||
port = 501
|
||||
}
|
||||
case constants.UDP:
|
||||
} else {
|
||||
switch selection.EncryptionPreset {
|
||||
case constants.PIAEncryptionPresetNormal:
|
||||
port = 1198
|
||||
@@ -63,38 +62,28 @@ func (p *pia) getPort(selection configuration.ServerSelection) (port uint16, err
|
||||
port = 1197
|
||||
}
|
||||
}
|
||||
|
||||
if port == 0 {
|
||||
return 0, fmt.Errorf(
|
||||
"%w: combination of protocol %q and encryption %q does not yield any port number",
|
||||
ErrInvalidPort, selection.Protocol, selection.EncryptionPreset)
|
||||
}
|
||||
return port, nil
|
||||
}
|
||||
|
||||
port = selection.CustomPort
|
||||
switch selection.Protocol {
|
||||
case constants.TCP:
|
||||
if selection.TCP {
|
||||
switch port {
|
||||
case 80, 110, 443: //nolint:gomnd
|
||||
return port, nil
|
||||
default:
|
||||
return 0, fmt.Errorf("%w: %d for protocol %s",
|
||||
ErrInvalidPort, port, selection.Protocol)
|
||||
}
|
||||
case constants.UDP:
|
||||
switch port {
|
||||
case 53, 1194, 1197, 1198, 8080, 9201: //nolint:gomnd
|
||||
default:
|
||||
return 0, fmt.Errorf("%w: %d for protocol %s",
|
||||
ErrInvalidPort, port, selection.Protocol)
|
||||
return 0, fmt.Errorf("%w: %d for protocol TCP", ErrInvalidPort, port)
|
||||
}
|
||||
}
|
||||
|
||||
return port, nil
|
||||
switch port {
|
||||
case 53, 1194, 1197, 1198, 8080, 9201: //nolint:gomnd
|
||||
return port, nil
|
||||
default:
|
||||
return 0, fmt.Errorf("%w: %d for protocol UDP", ErrInvalidPort, port)
|
||||
}
|
||||
}
|
||||
|
||||
func (p *pia) notFoundErr(regions, hostnames, names []string, protocol string) error {
|
||||
message := "for protocol " + protocol
|
||||
func (p *pia) notFoundErr(regions, hostnames, names []string, tcp bool) error {
|
||||
message := "for protocol " + tcpBoolToProtocol(tcp)
|
||||
|
||||
if len(regions) > 0 {
|
||||
message += " + regions " + commaJoin(regions)
|
||||
@@ -118,15 +107,17 @@ func (p *pia) GetOpenVPNConnection(selection configuration.ServerSelection) (
|
||||
return connection, err
|
||||
}
|
||||
|
||||
protocol := tcpBoolToProtocol(selection.TCP)
|
||||
|
||||
servers := p.servers
|
||||
if selection.TargetIP != nil {
|
||||
connection = models.OpenVPNConnection{IP: selection.TargetIP, Port: port, Protocol: selection.Protocol}
|
||||
connection = models.OpenVPNConnection{IP: selection.TargetIP, Port: port, Protocol: protocol}
|
||||
} else {
|
||||
servers := p.filterServers(selection.Regions, selection.Hostnames,
|
||||
selection.Names, selection.Protocol)
|
||||
selection.Names, selection.TCP)
|
||||
if len(servers) == 0 {
|
||||
return connection, p.notFoundErr(selection.Regions, selection.Hostnames,
|
||||
selection.Names, selection.Protocol)
|
||||
selection.Names, selection.TCP)
|
||||
}
|
||||
|
||||
var connections []models.OpenVPNConnection
|
||||
@@ -135,7 +126,7 @@ func (p *pia) GetOpenVPNConnection(selection configuration.ServerSelection) (
|
||||
connection := models.OpenVPNConnection{
|
||||
IP: ip,
|
||||
Port: port,
|
||||
Protocol: selection.Protocol,
|
||||
Protocol: protocol,
|
||||
}
|
||||
connections = append(connections, connection)
|
||||
}
|
||||
@@ -369,15 +360,15 @@ func (p *pia) PortForward(ctx context.Context, client *http.Client,
|
||||
}
|
||||
}
|
||||
|
||||
func (p *pia) filterServers(regions, hostnames, names []string, protocol string) (
|
||||
func (p *pia) filterServers(regions, hostnames, names []string, tcp bool) (
|
||||
filtered []models.PIAServer) {
|
||||
for _, server := range p.servers {
|
||||
switch {
|
||||
case filterByPossibilities(server.Region, regions),
|
||||
filterByPossibilities(server.Hostname, hostnames),
|
||||
filterByPossibilities(server.ServerName, names),
|
||||
protocol == constants.TCP && !server.TCP,
|
||||
protocol == constants.UDP && !server.UDP:
|
||||
tcp && !server.TCP,
|
||||
!tcp && !server.UDP:
|
||||
default:
|
||||
filtered = append(filtered, server)
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package provider
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"net"
|
||||
@@ -67,17 +68,22 @@ func (p *privado) notFoundErr(countries, regions, cities, hostnames []string) er
|
||||
return fmt.Errorf("%w: %s", errNoServerFound, message)
|
||||
}
|
||||
|
||||
var ErrProtocolUnsupported = errors.New("network protocol is not supported")
|
||||
|
||||
func (p *privado) GetOpenVPNConnection(selection configuration.ServerSelection) (
|
||||
connection models.OpenVPNConnection, err error) {
|
||||
var port uint16 = 1194
|
||||
switch selection.Protocol {
|
||||
case constants.UDP:
|
||||
default:
|
||||
return connection, fmt.Errorf("protocol %q is not supported by Privado", selection.Protocol)
|
||||
const protocol = constants.UDP
|
||||
if selection.TCP {
|
||||
return connection, fmt.Errorf("%w: TCP for provider Privado", ErrProtocolUnsupported)
|
||||
}
|
||||
|
||||
if selection.TargetIP != nil {
|
||||
return models.OpenVPNConnection{IP: selection.TargetIP, Port: port, Protocol: selection.Protocol}, nil
|
||||
return models.OpenVPNConnection{
|
||||
IP: selection.TargetIP,
|
||||
Port: port,
|
||||
Protocol: protocol,
|
||||
}, nil
|
||||
}
|
||||
|
||||
servers := p.filterServers(selection.Countries, selection.Regions,
|
||||
@@ -92,7 +98,7 @@ func (p *privado) GetOpenVPNConnection(selection configuration.ServerSelection)
|
||||
connection := models.OpenVPNConnection{
|
||||
IP: servers[i].IP,
|
||||
Port: port,
|
||||
Protocol: selection.Protocol,
|
||||
Protocol: protocol,
|
||||
Hostname: servers[i].Hostname,
|
||||
}
|
||||
connections[i] = connection
|
||||
|
||||
@@ -43,7 +43,7 @@ func (p *privatevpn) filterServers(countries, cities, hostnames []string) (serve
|
||||
}
|
||||
|
||||
func (p *privatevpn) notFoundErr(selection configuration.ServerSelection) error {
|
||||
message := "no server found for protocol " + selection.Protocol
|
||||
message := "no server found for protocol " + tcpBoolToProtocol(selection.TCP)
|
||||
|
||||
if len(selection.Countries) > 0 {
|
||||
message += " + countries " + commaJoin(selection.Countries)
|
||||
@@ -63,14 +63,20 @@ func (p *privatevpn) notFoundErr(selection configuration.ServerSelection) error
|
||||
func (p *privatevpn) GetOpenVPNConnection(selection configuration.ServerSelection) (
|
||||
connection models.OpenVPNConnection, err error) {
|
||||
var port uint16
|
||||
if selection.Protocol == constants.TCP {
|
||||
protocol := constants.TCP
|
||||
if selection.TCP {
|
||||
port = 443
|
||||
} else {
|
||||
protocol = constants.UDP
|
||||
port = 1194
|
||||
}
|
||||
|
||||
if selection.TargetIP != nil {
|
||||
return models.OpenVPNConnection{IP: selection.TargetIP, Port: port, Protocol: selection.Protocol}, nil
|
||||
return models.OpenVPNConnection{
|
||||
IP: selection.TargetIP,
|
||||
Port: port,
|
||||
Protocol: protocol,
|
||||
}, nil
|
||||
}
|
||||
|
||||
servers := p.filterServers(selection.Countries, selection.Cities, selection.Hostnames)
|
||||
@@ -84,7 +90,7 @@ func (p *privatevpn) GetOpenVPNConnection(selection configuration.ServerSelectio
|
||||
connection := models.OpenVPNConnection{
|
||||
IP: ip,
|
||||
Port: port,
|
||||
Protocol: selection.Protocol,
|
||||
Protocol: protocol,
|
||||
}
|
||||
connections = append(connections, connection)
|
||||
}
|
||||
|
||||
@@ -35,11 +35,13 @@ func (p *protonvpn) GetOpenVPNConnection(selection configuration.ServerSelection
|
||||
return connection, err
|
||||
}
|
||||
|
||||
protocol := tcpBoolToProtocol(selection.TCP)
|
||||
|
||||
if selection.TargetIP != nil {
|
||||
return models.OpenVPNConnection{
|
||||
IP: selection.TargetIP,
|
||||
Port: port,
|
||||
Protocol: selection.Protocol,
|
||||
Protocol: protocol,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -54,7 +56,7 @@ func (p *protonvpn) GetOpenVPNConnection(selection configuration.ServerSelection
|
||||
connections[i] = models.OpenVPNConnection{
|
||||
IP: servers[i].EntryIP,
|
||||
Port: port,
|
||||
Protocol: selection.Protocol,
|
||||
Protocol: protocol,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -139,35 +141,29 @@ func (p *protonvpn) PortForward(ctx context.Context, client *http.Client,
|
||||
|
||||
func (p *protonvpn) getPort(selection configuration.ServerSelection) (port uint16, err error) {
|
||||
if selection.CustomPort == 0 {
|
||||
switch selection.Protocol {
|
||||
case constants.TCP:
|
||||
if selection.TCP {
|
||||
const defaultTCPPort = 443
|
||||
return defaultTCPPort, nil
|
||||
case constants.UDP:
|
||||
const defaultUDPPort = 1194
|
||||
return defaultUDPPort, nil
|
||||
}
|
||||
const defaultUDPPort = 1194
|
||||
return defaultUDPPort, nil
|
||||
}
|
||||
|
||||
port = selection.CustomPort
|
||||
switch selection.Protocol {
|
||||
case constants.TCP:
|
||||
if selection.TCP {
|
||||
switch port {
|
||||
case 443, 5995, 8443: //nolint:gomnd
|
||||
return port, nil
|
||||
default:
|
||||
return 0, fmt.Errorf("%w: %d for protocol %s",
|
||||
ErrInvalidPort, port, selection.Protocol)
|
||||
}
|
||||
case constants.UDP:
|
||||
switch port {
|
||||
case 80, 443, 1194, 4569, 5060: //nolint:gomnd
|
||||
default:
|
||||
return 0, fmt.Errorf("%w: %d for protocol %s",
|
||||
ErrInvalidPort, port, selection.Protocol)
|
||||
return 0, fmt.Errorf("%w: %d for protocol TCP", ErrInvalidPort, port)
|
||||
}
|
||||
}
|
||||
|
||||
return port, nil
|
||||
switch port {
|
||||
case 80, 443, 1194, 4569, 5060: //nolint:gomnd
|
||||
return port, nil
|
||||
default:
|
||||
return 0, fmt.Errorf("%w: %d for protocol UDP", ErrInvalidPort, port)
|
||||
}
|
||||
}
|
||||
|
||||
func (p *protonvpn) filterServers(countries, regions, cities, names, hostnames []string) (
|
||||
@@ -188,7 +184,7 @@ func (p *protonvpn) filterServers(countries, regions, cities, names, hostnames [
|
||||
}
|
||||
|
||||
func (p *protonvpn) notFoundErr(selection configuration.ServerSelection) error {
|
||||
message := "no server found for protocol " + selection.Protocol
|
||||
message := "no server found for protocol " + tcpBoolToProtocol(selection.TCP)
|
||||
|
||||
if len(selection.Countries) > 0 {
|
||||
message += " + countries " + commaJoin(selection.Countries)
|
||||
|
||||
@@ -7,7 +7,6 @@ import (
|
||||
"net"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/configuration"
|
||||
"github.com/qdm12/gluetun/internal/constants"
|
||||
@@ -30,7 +29,7 @@ func newPurevpn(servers []models.PurevpnServer, timeNow timeNowFunc) *purevpn {
|
||||
}
|
||||
|
||||
func (p *purevpn) filterServers(regions, countries, cities, hostnames []string,
|
||||
protocol string) (servers []models.PurevpnServer) {
|
||||
tcp bool) (servers []models.PurevpnServer) {
|
||||
for _, server := range p.servers {
|
||||
switch {
|
||||
case
|
||||
@@ -38,8 +37,8 @@ func (p *purevpn) filterServers(regions, countries, cities, hostnames []string,
|
||||
filterByPossibilities(server.Country, countries),
|
||||
filterByPossibilities(server.City, cities),
|
||||
filterByPossibilities(server.Hostname, hostnames),
|
||||
strings.EqualFold(protocol, "tcp") && !server.TCP,
|
||||
strings.EqualFold(protocol, "udp") && !server.UDP:
|
||||
tcp && !server.TCP,
|
||||
!tcp && !server.UDP:
|
||||
default:
|
||||
servers = append(servers, server)
|
||||
}
|
||||
@@ -49,22 +48,19 @@ func (p *purevpn) filterServers(regions, countries, cities, hostnames []string,
|
||||
|
||||
func (p *purevpn) GetOpenVPNConnection(selection configuration.ServerSelection) (
|
||||
connection models.OpenVPNConnection, err error) {
|
||||
var port uint16
|
||||
switch {
|
||||
case selection.Protocol == constants.UDP:
|
||||
port = 53
|
||||
case selection.Protocol == constants.TCP:
|
||||
var port uint16 = 53
|
||||
protocol := constants.UDP
|
||||
if selection.TCP {
|
||||
port = 80
|
||||
default:
|
||||
return connection, fmt.Errorf("protocol %q is unknown", selection.Protocol)
|
||||
protocol = constants.TCP
|
||||
}
|
||||
|
||||
if selection.TargetIP != nil {
|
||||
return models.OpenVPNConnection{IP: selection.TargetIP, Port: port, Protocol: selection.Protocol}, nil
|
||||
return models.OpenVPNConnection{IP: selection.TargetIP, Port: port, Protocol: protocol}, nil
|
||||
}
|
||||
|
||||
servers := p.filterServers(selection.Regions, selection.Countries,
|
||||
selection.Cities, selection.Hostnames, selection.Protocol)
|
||||
selection.Cities, selection.Hostnames, selection.TCP)
|
||||
if len(servers) == 0 {
|
||||
return connection, fmt.Errorf("no server found for regions %s, countries %s and cities %s",
|
||||
commaJoin(selection.Regions), commaJoin(selection.Countries), commaJoin(selection.Cities))
|
||||
@@ -73,7 +69,7 @@ func (p *purevpn) GetOpenVPNConnection(selection configuration.ServerSelection)
|
||||
var connections []models.OpenVPNConnection
|
||||
for _, server := range servers {
|
||||
for _, IP := range server.IPs {
|
||||
connections = append(connections, models.OpenVPNConnection{IP: IP, Port: port, Protocol: selection.Protocol})
|
||||
connections = append(connections, models.OpenVPNConnection{IP: IP, Port: port, Protocol: protocol})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -7,7 +7,6 @@ import (
|
||||
"net"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/configuration"
|
||||
"github.com/qdm12/gluetun/internal/constants"
|
||||
@@ -29,14 +28,14 @@ func newSurfshark(servers []models.SurfsharkServer, timeNow timeNowFunc) *surfsh
|
||||
}
|
||||
}
|
||||
|
||||
func (s *surfshark) filterServers(regions, hostnames []string, protocol string) (servers []models.SurfsharkServer) {
|
||||
func (s *surfshark) filterServers(regions, hostnames []string, tcp bool) (servers []models.SurfsharkServer) {
|
||||
for _, server := range s.servers {
|
||||
switch {
|
||||
case
|
||||
filterByPossibilities(server.Region, regions),
|
||||
filterByPossibilities(server.Hostname, hostnames),
|
||||
strings.EqualFold(protocol, "tcp") && !server.TCP,
|
||||
strings.EqualFold(protocol, "udp") && !server.UDP:
|
||||
tcp && !server.TCP,
|
||||
!tcp && !server.UDP:
|
||||
default:
|
||||
servers = append(servers, server)
|
||||
}
|
||||
@@ -45,7 +44,7 @@ func (s *surfshark) filterServers(regions, hostnames []string, protocol string)
|
||||
}
|
||||
|
||||
func (s *surfshark) notFoundErr(selection configuration.ServerSelection) error {
|
||||
message := "for protocol " + selection.Protocol
|
||||
message := "for protocol " + tcpBoolToProtocol(selection.TCP)
|
||||
|
||||
if len(selection.Countries) > 0 {
|
||||
message += " + regions " + commaJoin(selection.Regions)
|
||||
@@ -60,21 +59,18 @@ func (s *surfshark) notFoundErr(selection configuration.ServerSelection) error {
|
||||
|
||||
func (s *surfshark) GetOpenVPNConnection(selection configuration.ServerSelection) (
|
||||
connection models.OpenVPNConnection, err error) {
|
||||
var port uint16
|
||||
switch {
|
||||
case selection.Protocol == constants.TCP:
|
||||
var port uint16 = 1194
|
||||
protocol := constants.UDP
|
||||
if selection.TCP {
|
||||
port = 1443
|
||||
case selection.Protocol == constants.UDP:
|
||||
port = 1194
|
||||
default:
|
||||
return connection, fmt.Errorf("protocol %q is unknown", selection.Protocol)
|
||||
protocol = constants.TCP
|
||||
}
|
||||
|
||||
if selection.TargetIP != nil {
|
||||
return models.OpenVPNConnection{IP: selection.TargetIP, Port: port, Protocol: selection.Protocol}, nil
|
||||
return models.OpenVPNConnection{IP: selection.TargetIP, Port: port, Protocol: protocol}, nil
|
||||
}
|
||||
|
||||
servers := s.filterServers(selection.Regions, selection.Hostnames, selection.Protocol)
|
||||
servers := s.filterServers(selection.Regions, selection.Hostnames, selection.TCP)
|
||||
if len(servers) == 0 {
|
||||
return connection, s.notFoundErr(selection)
|
||||
}
|
||||
@@ -82,7 +78,7 @@ func (s *surfshark) GetOpenVPNConnection(selection configuration.ServerSelection
|
||||
var connections []models.OpenVPNConnection
|
||||
for _, server := range servers {
|
||||
for _, IP := range server.IPs {
|
||||
connections = append(connections, models.OpenVPNConnection{IP: IP, Port: port, Protocol: selection.Protocol})
|
||||
connections = append(connections, models.OpenVPNConnection{IP: IP, Port: port, Protocol: protocol})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -7,7 +7,6 @@ import (
|
||||
"net"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/configuration"
|
||||
"github.com/qdm12/gluetun/internal/constants"
|
||||
@@ -30,15 +29,15 @@ func newTorguard(servers []models.TorguardServer, timeNow timeNowFunc) *torguard
|
||||
}
|
||||
|
||||
func (t *torguard) filterServers(countries, cities, hostnames []string,
|
||||
protocol string) (servers []models.TorguardServer) {
|
||||
tcp bool) (servers []models.TorguardServer) {
|
||||
for _, server := range t.servers {
|
||||
switch {
|
||||
case
|
||||
filterByPossibilities(server.Country, countries),
|
||||
filterByPossibilities(server.City, cities),
|
||||
filterByPossibilities(server.Hostname, hostnames),
|
||||
strings.EqualFold(protocol, "tcp") && !server.TCP,
|
||||
strings.EqualFold(protocol, "udp") && !server.UDP:
|
||||
tcp && !server.TCP,
|
||||
!tcp && !server.UDP:
|
||||
default:
|
||||
servers = append(servers, server)
|
||||
}
|
||||
@@ -47,7 +46,7 @@ func (t *torguard) filterServers(countries, cities, hostnames []string,
|
||||
}
|
||||
|
||||
func (t *torguard) notFoundErr(selection configuration.ServerSelection) error {
|
||||
message := "no server found for protocol " + selection.Protocol
|
||||
message := "no server found for protocol " + tcpBoolToProtocol(selection.TCP)
|
||||
|
||||
if len(selection.Countries) > 0 {
|
||||
message += " + countries " + commaJoin(selection.Countries)
|
||||
@@ -71,12 +70,14 @@ func (t *torguard) GetOpenVPNConnection(selection configuration.ServerSelection)
|
||||
port = selection.CustomPort
|
||||
}
|
||||
|
||||
protocol := tcpBoolToProtocol(selection.TCP)
|
||||
|
||||
if selection.TargetIP != nil {
|
||||
return models.OpenVPNConnection{IP: selection.TargetIP, Port: port, Protocol: selection.Protocol}, nil
|
||||
return models.OpenVPNConnection{IP: selection.TargetIP, Port: port, Protocol: protocol}, nil
|
||||
}
|
||||
|
||||
servers := t.filterServers(selection.Countries, selection.Cities,
|
||||
selection.Hostnames, selection.Protocol)
|
||||
selection.Hostnames, selection.TCP)
|
||||
if len(servers) == 0 {
|
||||
return connection, t.notFoundErr(selection)
|
||||
}
|
||||
@@ -87,7 +88,7 @@ func (t *torguard) GetOpenVPNConnection(selection configuration.ServerSelection)
|
||||
connection := models.OpenVPNConnection{
|
||||
IP: ip,
|
||||
Port: port,
|
||||
Protocol: selection.Protocol,
|
||||
Protocol: protocol,
|
||||
}
|
||||
connections = append(connections, connection)
|
||||
}
|
||||
|
||||
@@ -52,3 +52,10 @@ func filterByPossibilities(value string, possibilities []string) (filtered bool)
|
||||
func commaJoin(slice []string) string {
|
||||
return strings.Join(slice, ",")
|
||||
}
|
||||
|
||||
func tcpBoolToProtocol(tcp bool) (protocol string) {
|
||||
if tcp {
|
||||
return "tcp"
|
||||
}
|
||||
return "udp"
|
||||
}
|
||||
|
||||
@@ -7,7 +7,6 @@ import (
|
||||
"net"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/configuration"
|
||||
"github.com/qdm12/gluetun/internal/constants"
|
||||
@@ -29,14 +28,14 @@ func newVyprvpn(servers []models.VyprvpnServer, timeNow timeNowFunc) *vyprvpn {
|
||||
}
|
||||
}
|
||||
|
||||
func (v *vyprvpn) filterServers(regions, hostnames []string, protocol string) (servers []models.VyprvpnServer) {
|
||||
func (v *vyprvpn) filterServers(regions, hostnames []string, tcp bool) (servers []models.VyprvpnServer) {
|
||||
for _, server := range v.servers {
|
||||
switch {
|
||||
case
|
||||
filterByPossibilities(server.Region, regions),
|
||||
filterByPossibilities(server.Hostname, hostnames),
|
||||
strings.EqualFold(protocol, "tcp") && !server.TCP,
|
||||
strings.EqualFold(protocol, "udp") && !server.UDP:
|
||||
tcp && !server.TCP,
|
||||
!tcp && !server.UDP:
|
||||
default:
|
||||
servers = append(servers, server)
|
||||
}
|
||||
@@ -47,20 +46,17 @@ func (v *vyprvpn) filterServers(regions, hostnames []string, protocol string) (s
|
||||
func (v *vyprvpn) GetOpenVPNConnection(selection configuration.ServerSelection) (
|
||||
connection models.OpenVPNConnection, err error) {
|
||||
var port uint16
|
||||
switch {
|
||||
case selection.Protocol == constants.TCP:
|
||||
return connection, fmt.Errorf("TCP protocol not supported by this VPN provider")
|
||||
case selection.Protocol == constants.UDP:
|
||||
port = 443
|
||||
default:
|
||||
return connection, fmt.Errorf("protocol %q is unknown", selection.Protocol)
|
||||
const protocol = constants.TCP
|
||||
if selection.TCP {
|
||||
return connection, fmt.Errorf("%w: TCP for provider VyprVPN",
|
||||
ErrProtocolUnsupported)
|
||||
}
|
||||
|
||||
if selection.TargetIP != nil {
|
||||
return models.OpenVPNConnection{IP: selection.TargetIP, Port: port, Protocol: selection.Protocol}, nil
|
||||
return models.OpenVPNConnection{IP: selection.TargetIP, Port: port, Protocol: protocol}, nil
|
||||
}
|
||||
|
||||
servers := v.filterServers(selection.Regions, selection.Hostnames, selection.Protocol)
|
||||
servers := v.filterServers(selection.Regions, selection.Hostnames, selection.TCP)
|
||||
if len(servers) == 0 {
|
||||
return connection, fmt.Errorf("no server found for region %s", commaJoin(selection.Regions))
|
||||
}
|
||||
@@ -68,7 +64,7 @@ func (v *vyprvpn) GetOpenVPNConnection(selection configuration.ServerSelection)
|
||||
var connections []models.OpenVPNConnection
|
||||
for _, server := range servers {
|
||||
for _, IP := range server.IPs {
|
||||
connections = append(connections, models.OpenVPNConnection{IP: IP, Port: port, Protocol: selection.Protocol})
|
||||
connections = append(connections, models.OpenVPNConnection{IP: IP, Port: port, Protocol: protocol})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -45,20 +45,19 @@ func (w *windscribe) filterServers(regions, cities, hostnames []string) (servers
|
||||
|
||||
//nolint:lll
|
||||
func (w *windscribe) GetOpenVPNConnection(selection configuration.ServerSelection) (connection models.OpenVPNConnection, err error) {
|
||||
var port uint16
|
||||
switch {
|
||||
case selection.CustomPort > 0:
|
||||
port = selection.CustomPort
|
||||
case selection.Protocol == constants.TCP:
|
||||
var port uint16 = 443
|
||||
protocol := constants.UDP
|
||||
if selection.TCP {
|
||||
port = 1194
|
||||
case selection.Protocol == constants.UDP:
|
||||
port = 443
|
||||
default:
|
||||
return connection, fmt.Errorf("protocol %q is unknown", selection.Protocol)
|
||||
protocol = constants.TCP
|
||||
}
|
||||
|
||||
if selection.CustomPort > 0 {
|
||||
port = selection.CustomPort
|
||||
}
|
||||
|
||||
if selection.TargetIP != nil {
|
||||
return models.OpenVPNConnection{IP: selection.TargetIP, Port: port, Protocol: selection.Protocol}, nil
|
||||
return models.OpenVPNConnection{IP: selection.TargetIP, Port: port, Protocol: protocol}, nil
|
||||
}
|
||||
|
||||
servers := w.filterServers(selection.Regions, selection.Cities, selection.Hostnames)
|
||||
@@ -72,7 +71,7 @@ func (w *windscribe) GetOpenVPNConnection(selection configuration.ServerSelectio
|
||||
connection := models.OpenVPNConnection{
|
||||
IP: ip,
|
||||
Port: port,
|
||||
Protocol: selection.Protocol,
|
||||
Protocol: protocol,
|
||||
}
|
||||
connections = append(connections, connection)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user