Files
gluetun/internal/provider/custom/connection.go
T
Quentin McGaw 4a78989d9d chore: do not use sentinel errors when unneeded
- main reason being it's a burden to always define sentinel errors at global scope, wrap them with `%w` instead of using a string directly
- only use sentinel errors when it has to be checked using `errors.Is`
- replace all usage of these sentinel errors in `fmt.Errorf` with direct strings that were in the sentinel error
- exclude the sentinel error definition requirement from .golangci.yml
- update unit tests to use ContainersError instead of ErrorIs so it stays as a "not a change detector test" without requiring a sentinel error
2026-05-02 03:29:46 +00:00

69 lines
2.0 KiB
Go

package custom
import (
"fmt"
"github.com/qdm12/gluetun/internal/configuration/settings"
"github.com/qdm12/gluetun/internal/constants"
"github.com/qdm12/gluetun/internal/constants/vpn"
"github.com/qdm12/gluetun/internal/models"
)
// GetConnection gets the connection from the OpenVPN configuration file.
func (p *Provider) GetConnection(selection settings.ServerSelection, _ bool) (
connection models.Connection, err error,
) {
switch selection.VPN {
case vpn.OpenVPN:
return getOpenVPNConnection(p.extractor, selection)
case vpn.Wireguard, vpn.AmneziaWg:
return getWireguardConnection(selection), nil
default:
return connection, fmt.Errorf("VPN type not supported for custom provider: %s", selection.VPN)
}
}
func getOpenVPNConnection(extractor Extractor,
selection settings.ServerSelection) (
connection models.Connection, err error,
) {
_, connection, err = extractor.Data(*selection.OpenVPN.ConfFile)
if err != nil {
return connection, fmt.Errorf("extracting connection: %w", err)
}
customPort := *selection.OpenVPN.CustomPort
if customPort > 0 {
connection.Port = customPort
}
// assume all custom provider servers support port forwarding
connection.PortForward = true
if len(selection.Names) > 0 {
// Set the server name for PIA port forwarding code used
// together with the custom provider.
connection.ServerName = selection.Names[0]
}
return connection, nil
}
func getWireguardConnection(selection settings.ServerSelection) (
connection models.Connection,
) {
connection = models.Connection{
Type: vpn.Wireguard,
IP: selection.Wireguard.EndpointIP,
Port: *selection.Wireguard.EndpointPort,
Protocol: constants.UDP,
PubKey: selection.Wireguard.PublicKey,
PortForward: true, // assume all custom provider servers support port forwarding
}
if len(selection.Names) > 0 {
// Set the server name for PIA port forwarding code used
// together with the custom provider.
connection.ServerName = selection.Names[0]
}
return connection
}