2022-01-06 06:40:23 -05:00
|
|
|
package env
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
2022-02-09 12:33:24 +00:00
|
|
|
"strings"
|
2022-01-06 06:40:23 -05:00
|
|
|
|
|
|
|
|
"github.com/qdm12/gluetun/internal/configuration/settings"
|
|
|
|
|
"github.com/qdm12/gluetun/internal/constants"
|
2023-05-29 20:43:06 +00:00
|
|
|
"github.com/qdm12/gosettings/sources/env"
|
2022-01-06 06:40:23 -05:00
|
|
|
)
|
|
|
|
|
|
2022-08-26 15:16:51 +00:00
|
|
|
func (s *Source) readOpenVPNSelection() (
|
2022-01-06 06:40:23 -05:00
|
|
|
selection settings.OpenVPNSelection, err error) {
|
2023-06-01 08:22:55 +00:00
|
|
|
selection.ConfFile = s.env.Get("OPENVPN_CUSTOM_CONFIG", env.ForceLowercase(false))
|
2022-01-07 15:12:49 +00:00
|
|
|
|
2022-08-26 15:16:51 +00:00
|
|
|
selection.TCP, err = s.readOpenVPNProtocol()
|
2022-01-06 06:40:23 -05:00
|
|
|
if err != nil {
|
|
|
|
|
return selection, err
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-05 16:25:52 +00:00
|
|
|
selection.CustomPort, err = s.env.Uint16Ptr("VPN_ENDPOINT_PORT",
|
|
|
|
|
env.RetroKeys("PORT", "OPENVPN_PORT"))
|
2022-01-06 06:40:23 -05:00
|
|
|
if err != nil {
|
|
|
|
|
return selection, err
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-26 15:16:51 +00:00
|
|
|
selection.PIAEncPreset = s.readPIAEncryptionPreset()
|
2022-01-06 06:40:23 -05:00
|
|
|
|
|
|
|
|
return selection, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var ErrOpenVPNProtocolNotValid = errors.New("OpenVPN protocol is not valid")
|
|
|
|
|
|
2022-08-26 15:16:51 +00:00
|
|
|
func (s *Source) readOpenVPNProtocol() (tcp *bool, err error) {
|
2023-06-05 16:25:52 +00:00
|
|
|
const currentKey = "OPENVPN_PROTOCOL"
|
|
|
|
|
envKey := firstKeySet(s.env, "PROTOCOL", currentKey)
|
|
|
|
|
switch envKey {
|
|
|
|
|
case "":
|
2023-05-30 15:21:09 +00:00
|
|
|
return nil, nil //nolint:nilnil
|
2023-06-05 16:25:52 +00:00
|
|
|
case currentKey:
|
|
|
|
|
default: // Retro compatibility
|
|
|
|
|
s.handleDeprecatedKey(envKey, currentKey)
|
2023-05-30 15:21:09 +00:00
|
|
|
}
|
2022-01-06 06:40:23 -05:00
|
|
|
|
2023-06-05 16:25:52 +00:00
|
|
|
protocol := s.env.String(envKey)
|
2022-02-09 12:33:24 +00:00
|
|
|
switch strings.ToLower(protocol) {
|
2022-01-06 06:40:23 -05:00
|
|
|
case constants.UDP:
|
2023-05-30 13:02:10 +00:00
|
|
|
return ptrTo(false), nil
|
2022-01-06 06:40:23 -05:00
|
|
|
case constants.TCP:
|
2023-05-30 13:02:10 +00:00
|
|
|
return ptrTo(true), nil
|
2022-01-06 06:40:23 -05:00
|
|
|
default:
|
|
|
|
|
return nil, fmt.Errorf("environment variable %s: %w: %s",
|
|
|
|
|
envKey, ErrOpenVPNProtocolNotValid, protocol)
|
|
|
|
|
}
|
|
|
|
|
}
|