2022-01-06 06:40:23 -05:00
|
|
|
package env
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
"github.com/qdm12/gluetun/internal/configuration/settings"
|
2023-05-29 20:43:06 +00:00
|
|
|
"github.com/qdm12/gosettings/sources/env"
|
2022-01-29 14:55:56 +00:00
|
|
|
"github.com/qdm12/govalid/binary"
|
2022-01-06 06:40:23 -05:00
|
|
|
)
|
|
|
|
|
|
2022-08-26 15:16:51 +00:00
|
|
|
func (s *Source) readOpenVPN() (
|
2022-01-06 06:40:23 -05:00
|
|
|
openVPN settings.OpenVPN, err error) {
|
|
|
|
|
defer func() {
|
2022-08-15 19:54:58 -04:00
|
|
|
err = unsetEnvKeys([]string{"OPENVPN_KEY", "OPENVPN_CERT",
|
|
|
|
|
"OPENVPN_KEY_PASSPHRASE", "OPENVPN_ENCRYPTED_KEY"}, err)
|
2022-01-06 06:40:23 -05:00
|
|
|
}()
|
|
|
|
|
|
2023-05-29 20:43:06 +00:00
|
|
|
openVPN.Version = env.Get("OPENVPN_VERSION")
|
2022-08-26 15:16:51 +00:00
|
|
|
openVPN.User = s.readOpenVPNUser()
|
|
|
|
|
openVPN.Password = s.readOpenVPNPassword()
|
2023-05-29 20:43:06 +00:00
|
|
|
confFile := env.Get("OPENVPN_CUSTOM_CONFIG")
|
2022-01-06 06:40:23 -05:00
|
|
|
if confFile != "" {
|
|
|
|
|
openVPN.ConfFile = &confFile
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-26 15:16:51 +00:00
|
|
|
ciphersKey, _ := s.getEnvWithRetro("OPENVPN_CIPHERS", "OPENVPN_CIPHER")
|
2022-02-06 22:58:23 +00:00
|
|
|
openVPN.Ciphers = envToCSV(ciphersKey)
|
2022-02-05 22:36:51 +00:00
|
|
|
|
2023-05-29 20:43:06 +00:00
|
|
|
auth := env.Get("OPENVPN_AUTH")
|
2022-01-06 06:40:23 -05:00
|
|
|
if auth != "" {
|
|
|
|
|
openVPN.Auth = &auth
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-24 17:48:45 +00:00
|
|
|
openVPN.Cert = envToStringPtr("OPENVPN_CERT")
|
|
|
|
|
openVPN.Key = envToStringPtr("OPENVPN_KEY")
|
|
|
|
|
openVPN.EncryptedKey = envToStringPtr("OPENVPN_ENCRYPTED_KEY")
|
2022-08-15 19:54:58 -04:00
|
|
|
|
2022-08-26 15:16:51 +00:00
|
|
|
openVPN.KeyPassphrase = s.readOpenVPNKeyPassphrase()
|
2022-08-15 19:54:58 -04:00
|
|
|
|
2022-08-26 15:16:51 +00:00
|
|
|
openVPN.PIAEncPreset = s.readPIAEncryptionPreset()
|
2022-01-06 06:40:23 -05:00
|
|
|
|
|
|
|
|
openVPN.MSSFix, err = envToUint16Ptr("OPENVPN_MSSFIX")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return openVPN, fmt.Errorf("environment variable OPENVPN_MSSFIX: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-26 15:16:51 +00:00
|
|
|
_, openVPN.Interface = s.getEnvWithRetro("VPN_INTERFACE", "OPENVPN_INTERFACE")
|
2022-01-06 06:40:23 -05:00
|
|
|
|
2022-08-26 15:16:51 +00:00
|
|
|
openVPN.ProcessUser, err = s.readOpenVPNProcessUser()
|
2022-01-06 06:40:23 -05:00
|
|
|
if err != nil {
|
2022-01-27 23:34:19 +00:00
|
|
|
return openVPN, err
|
2022-01-06 06:40:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
openVPN.Verbosity, err = envToIntPtr("OPENVPN_VERBOSITY")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return openVPN, fmt.Errorf("environment variable OPENVPN_VERBOSITY: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-29 20:43:06 +00:00
|
|
|
flagsStr := env.Get("OPENVPN_FLAGS")
|
2022-03-31 20:49:01 +00:00
|
|
|
if flagsStr != "" {
|
|
|
|
|
openVPN.Flags = strings.Fields(flagsStr)
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-06 06:40:23 -05:00
|
|
|
return openVPN, nil
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-26 15:16:51 +00:00
|
|
|
func (s *Source) readOpenVPNUser() (user *string) {
|
2022-08-13 16:44:38 +00:00
|
|
|
user = new(string)
|
2022-08-26 15:16:51 +00:00
|
|
|
_, *user = s.getEnvWithRetro("OPENVPN_USER", "USER")
|
2022-08-13 16:44:38 +00:00
|
|
|
if *user == "" {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-06 06:40:23 -05:00
|
|
|
// Remove spaces in user ID to simplify user's life, thanks @JeordyR
|
2022-08-13 16:44:38 +00:00
|
|
|
*user = strings.ReplaceAll(*user, " ", "")
|
|
|
|
|
return user
|
2022-01-06 06:40:23 -05:00
|
|
|
}
|
|
|
|
|
|
2022-08-26 15:16:51 +00:00
|
|
|
func (s *Source) readOpenVPNPassword() (password *string) {
|
2022-08-13 16:44:38 +00:00
|
|
|
password = new(string)
|
2022-08-26 15:16:51 +00:00
|
|
|
_, *password = s.getEnvWithRetro("OPENVPN_PASSWORD", "PASSWORD")
|
2022-08-13 16:44:38 +00:00
|
|
|
if *password == "" {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-06 06:40:23 -05:00
|
|
|
return password
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-26 15:16:51 +00:00
|
|
|
func (s *Source) readOpenVPNKeyPassphrase() (passphrase *string) {
|
2022-08-15 19:54:58 -04:00
|
|
|
passphrase = new(string)
|
2023-05-29 20:43:06 +00:00
|
|
|
*passphrase = env.Get("OPENVPN_KEY_PASSPHRASE")
|
2022-08-15 19:54:58 -04:00
|
|
|
if *passphrase == "" {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return passphrase
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-26 15:16:51 +00:00
|
|
|
func (s *Source) readPIAEncryptionPreset() (presetPtr *string) {
|
|
|
|
|
_, preset := s.getEnvWithRetro(
|
2022-02-05 22:38:03 +00:00
|
|
|
"PRIVATE_INTERNET_ACCESS_OPENVPN_ENCRYPTION_PRESET",
|
|
|
|
|
"PIA_ENCRYPTION", "ENCRYPTION")
|
2022-01-06 06:40:23 -05:00
|
|
|
if preset != "" {
|
|
|
|
|
return &preset
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2022-01-27 23:34:19 +00:00
|
|
|
|
2022-08-26 15:16:51 +00:00
|
|
|
func (s *Source) readOpenVPNProcessUser() (processUser string, err error) {
|
|
|
|
|
key, value := s.getEnvWithRetro("OPENVPN_PROCESS_USER", "OPENVPN_ROOT")
|
2022-01-29 14:55:56 +00:00
|
|
|
if key == "OPENVPN_PROCESS_USER" {
|
|
|
|
|
return value, nil
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-27 23:34:19 +00:00
|
|
|
// Retro-compatibility
|
2022-01-29 14:55:56 +00:00
|
|
|
if value == "" {
|
|
|
|
|
return "", nil
|
|
|
|
|
}
|
|
|
|
|
root, err := binary.Validate(value)
|
2022-01-27 23:34:19 +00:00
|
|
|
if err != nil {
|
2022-01-29 14:55:56 +00:00
|
|
|
return "", fmt.Errorf("environment variable %s: %w", key, err)
|
2022-01-27 23:34:19 +00:00
|
|
|
}
|
2023-05-27 08:52:41 +00:00
|
|
|
if *root {
|
2022-01-29 14:55:56 +00:00
|
|
|
return "root", nil
|
|
|
|
|
}
|
|
|
|
|
const defaultNonRootUser = "nonrootuser"
|
|
|
|
|
return defaultNonRootUser, nil
|
2022-01-27 23:34:19 +00:00
|
|
|
}
|