2021-08-23 16:13:20 +00:00
|
|
|
package utils
|
|
|
|
|
|
|
|
|
|
import (
|
2022-04-19 14:28:57 +00:00
|
|
|
"fmt"
|
|
|
|
|
|
2022-01-06 06:40:23 -05:00
|
|
|
"github.com/qdm12/gluetun/internal/configuration/settings"
|
2024-03-23 14:56:42 +00:00
|
|
|
"github.com/qdm12/gluetun/internal/constants"
|
2022-04-18 09:15:20 +00:00
|
|
|
"github.com/qdm12/gluetun/internal/constants/vpn"
|
2024-10-23 09:05:32 +00:00
|
|
|
"github.com/qdm12/gluetun/internal/models"
|
2021-08-23 16:13:20 +00:00
|
|
|
)
|
|
|
|
|
|
2024-10-23 09:05:32 +00:00
|
|
|
func getPort(selection settings.ServerSelection, server models.Server,
|
2024-10-11 19:20:48 +00:00
|
|
|
defaultOpenVPNTCP, defaultOpenVPNUDP, defaultWireguard uint16,
|
|
|
|
|
) (port uint16) {
|
2021-08-23 16:13:20 +00:00
|
|
|
switch selection.VPN {
|
2022-04-18 09:15:20 +00:00
|
|
|
case vpn.Wireguard:
|
2022-01-06 06:40:23 -05:00
|
|
|
customPort := *selection.Wireguard.EndpointPort
|
2021-08-23 16:13:20 +00:00
|
|
|
if customPort > 0 {
|
2024-10-23 09:05:32 +00:00
|
|
|
// Note: servers filtering ensures the custom port is within the
|
|
|
|
|
// server ports defined if any is set.
|
2021-08-23 16:13:20 +00:00
|
|
|
return customPort
|
|
|
|
|
}
|
2024-10-23 09:05:32 +00:00
|
|
|
|
|
|
|
|
if len(server.PortsUDP) > 0 {
|
|
|
|
|
defaultWireguard = server.PortsUDP[0]
|
|
|
|
|
}
|
2022-04-19 14:28:57 +00:00
|
|
|
checkDefined("Wireguard", defaultWireguard)
|
2021-08-23 16:13:20 +00:00
|
|
|
return defaultWireguard
|
|
|
|
|
default: // OpenVPN
|
2022-01-06 06:40:23 -05:00
|
|
|
customPort := *selection.OpenVPN.CustomPort
|
2021-08-23 16:13:20 +00:00
|
|
|
if customPort > 0 {
|
2024-10-23 09:05:32 +00:00
|
|
|
// Note: servers filtering ensures the custom port is within the
|
|
|
|
|
// server ports defined if any is set.
|
2021-08-23 16:13:20 +00:00
|
|
|
return customPort
|
|
|
|
|
}
|
2024-03-23 14:56:42 +00:00
|
|
|
if selection.OpenVPN.Protocol == constants.TCP {
|
2024-10-23 09:05:32 +00:00
|
|
|
if len(server.PortsTCP) > 0 {
|
|
|
|
|
defaultOpenVPNTCP = server.PortsTCP[0]
|
|
|
|
|
}
|
2022-04-19 14:28:57 +00:00
|
|
|
checkDefined("OpenVPN TCP", defaultOpenVPNTCP)
|
2021-08-23 16:13:20 +00:00
|
|
|
return defaultOpenVPNTCP
|
|
|
|
|
}
|
2022-04-19 14:28:57 +00:00
|
|
|
|
2024-10-23 09:05:32 +00:00
|
|
|
if len(server.PortsUDP) > 0 {
|
|
|
|
|
defaultOpenVPNUDP = server.PortsUDP[0]
|
|
|
|
|
}
|
2022-04-19 14:28:57 +00:00
|
|
|
checkDefined("OpenVPN UDP", defaultOpenVPNUDP)
|
2021-08-23 16:13:20 +00:00
|
|
|
return defaultOpenVPNUDP
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-04-19 14:28:57 +00:00
|
|
|
|
|
|
|
|
func checkDefined(portName string, port uint16) {
|
|
|
|
|
if port > 0 {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
message := fmt.Sprintf("no default %s port is defined!", portName)
|
|
|
|
|
panic(message)
|
|
|
|
|
}
|