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-05-30 15:21:09 +00:00
|
|
|
selection.ConfFile = 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
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-26 15:16:51 +00:00
|
|
|
selection.CustomPort, err = s.readOpenVPNCustomPort()
|
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-05-30 15:21:09 +00:00
|
|
|
envKey, protocolPtr := s.getEnvWithRetro("OPENVPN_PROTOCOL", []string{"PROTOCOL"})
|
|
|
|
|
if protocolPtr == nil {
|
|
|
|
|
return nil, nil //nolint:nilnil
|
|
|
|
|
}
|
|
|
|
|
protocol := *protocolPtr
|
2022-01-06 06:40:23 -05:00
|
|
|
|
2022-02-09 12:33:24 +00:00
|
|
|
switch strings.ToLower(protocol) {
|
2022-01-06 06:40:23 -05:00
|
|
|
case "":
|
|
|
|
|
return nil, nil //nolint:nilnil
|
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-26 15:16:51 +00:00
|
|
|
func (s *Source) readOpenVPNCustomPort() (customPort *uint16, err error) {
|
2023-05-30 15:21:09 +00:00
|
|
|
key, _ := s.getEnvWithRetro("VPN_ENDPOINT_PORT", []string{"PORT", "OPENVPN_PORT"})
|
|
|
|
|
return env.Uint16Ptr(key)
|
2022-01-06 06:40:23 -05:00
|
|
|
}
|