chore(settings): refactor settings processing (#756)

- Better settings tree structure logged using `qdm12/gotree`
- Read settings from environment variables, then files, then secret files
- Settings methods to default them, merge them and override them
- `DNS_PLAINTEXT_ADDRESS` default changed to `127.0.0.1` to use DoT. Warning added if set to something else.
- `HTTPPROXY_LISTENING_ADDRESS` instead of `HTTPPROXY_PORT` (with retro-compatibility)
This commit is contained in:
Quentin McGaw
2022-01-06 06:40:23 -05:00
committed by GitHub
parent 46738b2934
commit 7d824a5179
275 changed files with 7167 additions and 6328 deletions

View File

@@ -1,17 +1,17 @@
package cyberghost
import (
"github.com/qdm12/gluetun/internal/configuration"
"github.com/qdm12/gluetun/internal/configuration/settings"
"github.com/qdm12/gluetun/internal/constants"
"github.com/qdm12/gluetun/internal/models"
"github.com/qdm12/gluetun/internal/provider/utils"
)
func (c *Cyberghost) GetConnection(selection configuration.ServerSelection) (
func (c *Cyberghost) GetConnection(selection settings.ServerSelection) (
connection models.Connection, err error) {
const port = 443
protocol := constants.UDP
if selection.OpenVPN.TCP {
if *selection.OpenVPN.TCP {
protocol = constants.TCP
}

View File

@@ -3,14 +3,14 @@ package cyberghost
import (
"errors"
"github.com/qdm12/gluetun/internal/configuration"
"github.com/qdm12/gluetun/internal/configuration/settings"
"github.com/qdm12/gluetun/internal/models"
"github.com/qdm12/gluetun/internal/provider/utils"
)
var ErrGroupMismatchesProtocol = errors.New("server group does not match protocol")
func (c *Cyberghost) filterServers(selection configuration.ServerSelection) (
func (c *Cyberghost) filterServers(selection settings.ServerSelection) (
servers []models.CyberghostServer, err error) {
for _, server := range c.servers {
switch {

View File

@@ -4,23 +4,25 @@ import (
"errors"
"testing"
"github.com/qdm12/gluetun/internal/configuration"
"github.com/qdm12/gluetun/internal/configuration/settings"
"github.com/qdm12/gluetun/internal/constants"
"github.com/qdm12/gluetun/internal/models"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func boolPtr(b bool) *bool { return &b }
func Test_Cyberghost_filterServers(t *testing.T) {
t.Parallel()
testCases := map[string]struct {
servers []models.CyberghostServer
selection configuration.ServerSelection
selection settings.ServerSelection
filteredServers []models.CyberghostServer
err error
}{
"no server": {
selection: configuration.ServerSelection{VPN: constants.OpenVPN},
selection: settings.ServerSelection{}.WithDefaults(constants.Cyberghost),
err: errors.New("no server found: for VPN openvpn; protocol udp"),
},
"servers without filter defaults to UDP": {
@@ -30,6 +32,7 @@ func Test_Cyberghost_filterServers(t *testing.T) {
{Country: "c", UDP: true},
{Country: "d", UDP: true},
},
selection: settings.ServerSelection{}.WithDefaults(constants.Cyberghost),
filteredServers: []models.CyberghostServer{
{Country: "c", UDP: true},
{Country: "d", UDP: true},
@@ -42,11 +45,11 @@ func Test_Cyberghost_filterServers(t *testing.T) {
{Country: "c", UDP: true},
{Country: "d", UDP: true},
},
selection: configuration.ServerSelection{
OpenVPN: configuration.OpenVPNSelection{
TCP: true,
selection: settings.ServerSelection{
OpenVPN: settings.OpenVPNSelection{
TCP: boolPtr(true),
},
},
}.WithDefaults(constants.Cyberghost),
filteredServers: []models.CyberghostServer{
{Country: "a", TCP: true},
{Country: "b", TCP: true},
@@ -59,9 +62,9 @@ func Test_Cyberghost_filterServers(t *testing.T) {
{Country: "c", UDP: true},
{Country: "d", UDP: true},
},
selection: configuration.ServerSelection{
selection: settings.ServerSelection{
Countries: []string{"a", "c"},
},
}.WithDefaults(constants.Cyberghost),
filteredServers: []models.CyberghostServer{
{Country: "a", UDP: true},
{Country: "c", UDP: true},
@@ -73,9 +76,9 @@ func Test_Cyberghost_filterServers(t *testing.T) {
{Hostname: "b", UDP: true},
{Hostname: "c", UDP: true},
},
selection: configuration.ServerSelection{
selection: settings.ServerSelection{
Hostnames: []string{"a", "c"},
},
}.WithDefaults(constants.Cyberghost),
filteredServers: []models.CyberghostServer{
{Hostname: "a", UDP: true},
{Hostname: "c", UDP: true},

View File

@@ -1,16 +1,18 @@
package cyberghost
import (
"fmt"
"strconv"
"github.com/qdm12/gluetun/internal/configuration"
"github.com/qdm12/gluetun/internal/configuration/settings"
"github.com/qdm12/gluetun/internal/constants"
"github.com/qdm12/gluetun/internal/models"
"github.com/qdm12/gluetun/internal/openvpn/parse"
"github.com/qdm12/gluetun/internal/provider/utils"
)
func (c *Cyberghost) BuildConf(connection models.Connection,
settings configuration.OpenVPN) (lines []string, err error) {
settings settings.OpenVPN) (lines []string, err error) {
if len(settings.Ciphers) == 0 {
settings.Ciphers = []string{
constants.AES256gcm,
@@ -19,8 +21,9 @@ func (c *Cyberghost) BuildConf(connection models.Connection,
}
}
if settings.Auth == "" {
settings.Auth = constants.SHA256
auth := *settings.Auth
if auth == "" {
auth = constants.SHA256
}
lines = []string{
@@ -28,13 +31,13 @@ func (c *Cyberghost) BuildConf(connection models.Connection,
"nobind",
"tls-exit",
"dev " + settings.Interface,
"verb " + strconv.Itoa(settings.Verbosity),
"verb " + strconv.Itoa(*settings.Verbosity),
// Cyberghost specific
"ping 10",
"remote-cert-tls server",
"auth-user-pass " + constants.OpenVPNAuthConf,
"auth " + settings.Auth,
"auth " + auth,
// Added constant values
"auth-nocache",
@@ -54,27 +57,35 @@ func (c *Cyberghost) BuildConf(connection models.Connection,
lines = append(lines, "explicit-exit-notify")
}
if !settings.Root {
if !*settings.Root {
lines = append(lines, "user "+settings.ProcUser)
lines = append(lines, "persist-tun")
lines = append(lines, "persist-key")
}
if settings.MSSFix > 0 {
lines = append(lines, "mssfix "+strconv.Itoa(int(settings.MSSFix)))
if *settings.MSSFix > 0 {
lines = append(lines, "mssfix "+strconv.Itoa(int(*settings.MSSFix)))
}
if !settings.IPv6 {
if !*settings.IPv6 {
lines = append(lines, `pull-filter ignore "route-ipv6"`)
lines = append(lines, `pull-filter ignore "ifconfig-ipv6"`)
}
lines = append(lines, utils.WrapOpenvpnCA(
constants.CyberghostCertificate)...)
lines = append(lines, utils.WrapOpenvpnCert(
settings.ClientCrt)...)
lines = append(lines, utils.WrapOpenvpnKey(
settings.ClientKey)...)
certData, err := parse.ExtractCert([]byte(*settings.ClientCrt))
if err != nil {
return nil, fmt.Errorf("client cert is not valid: %w", err)
}
lines = append(lines, utils.WrapOpenvpnCert(certData)...)
keyData, err := parse.ExtractPrivateKey([]byte(*settings.ClientKey))
if err != nil {
return nil, fmt.Errorf("client key is not valid: %w", err)
}
lines = append(lines, utils.WrapOpenvpnKey(keyData)...)
lines = append(lines, "")