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,12 +1,12 @@
package wevpn
import (
"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"
)
func (w *Wevpn) GetConnection(selection configuration.ServerSelection) (
func (w *Wevpn) GetConnection(selection settings.ServerSelection) (
connection models.Connection, err error) {
port := getPort(selection)
protocol := utils.GetProtocol(selection)
@@ -32,7 +32,7 @@ func (w *Wevpn) GetConnection(selection configuration.ServerSelection) (
return utils.PickConnection(connections, selection, w.randSource)
}
func getPort(selection configuration.ServerSelection) (port uint16) {
func getPort(selection settings.ServerSelection) (port uint16) {
const (
defaultOpenVPNTCP = 1195
defaultOpenVPNUDP = 1194

View File

@@ -6,7 +6,7 @@ import (
"net"
"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"
@@ -18,14 +18,14 @@ func Test_Wevpn_GetConnection(t *testing.T) {
testCases := map[string]struct {
servers []models.WevpnServer
selection configuration.ServerSelection
selection settings.ServerSelection
connection models.Connection
err error
}{
"no server available": {
selection: configuration.ServerSelection{
selection: settings.ServerSelection{
VPN: constants.OpenVPN,
},
}.WithDefaults(constants.Wevpn),
err: errors.New("no server found: for VPN openvpn; protocol udp"),
},
"no filter": {
@@ -34,37 +34,41 @@ func Test_Wevpn_GetConnection(t *testing.T) {
{UDP: true, IPs: []net.IP{net.IPv4(2, 2, 2, 2)}},
{UDP: true, IPs: []net.IP{net.IPv4(3, 3, 3, 3)}},
},
selection: settings.ServerSelection{}.WithDefaults(constants.Wevpn),
connection: models.Connection{
Type: constants.OpenVPN,
IP: net.IPv4(1, 1, 1, 1),
Port: 1194,
Protocol: constants.UDP,
},
},
"target IP": {
selection: configuration.ServerSelection{
selection: settings.ServerSelection{
TargetIP: net.IPv4(2, 2, 2, 2),
},
}.WithDefaults(constants.Wevpn),
servers: []models.WevpnServer{
{UDP: true, IPs: []net.IP{net.IPv4(1, 1, 1, 1)}},
{UDP: true, IPs: []net.IP{net.IPv4(2, 2, 2, 2)}},
{UDP: true, IPs: []net.IP{net.IPv4(3, 3, 3, 3)}},
},
connection: models.Connection{
Type: constants.OpenVPN,
IP: net.IPv4(2, 2, 2, 2),
Port: 1194,
Protocol: constants.UDP,
},
},
"with filter": {
selection: configuration.ServerSelection{
selection: settings.ServerSelection{
Hostnames: []string{"b"},
},
}.WithDefaults(constants.Wevpn),
servers: []models.WevpnServer{
{UDP: true, Hostname: "a", IPs: []net.IP{net.IPv4(1, 1, 1, 1)}},
{UDP: true, Hostname: "b", IPs: []net.IP{net.IPv4(2, 2, 2, 2)}},
{UDP: true, Hostname: "a", IPs: []net.IP{net.IPv4(3, 3, 3, 3)}},
},
connection: models.Connection{
Type: constants.OpenVPN,
IP: net.IPv4(2, 2, 2, 2),
Port: 1194,
Protocol: constants.UDP,

View File

@@ -1,12 +1,12 @@
package wevpn
import (
"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"
)
func (w *Wevpn) filterServers(selection configuration.ServerSelection) (
func (w *Wevpn) filterServers(selection settings.ServerSelection) (
servers []models.WevpnServer, err error) {
for _, server := range w.servers {
switch {

View File

@@ -5,27 +5,27 @@ import (
"math/rand"
"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_Wevpn_filterServers(t *testing.T) {
t.Parallel()
testCases := map[string]struct {
servers []models.WevpnServer
selection configuration.ServerSelection
selection settings.ServerSelection
filtered []models.WevpnServer
err error
}{
"no server available": {
selection: configuration.ServerSelection{
VPN: constants.OpenVPN,
},
err: errors.New("no server found: for VPN openvpn; protocol udp"),
selection: settings.ServerSelection{}.WithDefaults(constants.Wevpn),
err: errors.New("no server found: for VPN openvpn; protocol udp"),
},
"no filter": {
servers: []models.WevpnServer{
@@ -33,6 +33,7 @@ func Test_Wevpn_filterServers(t *testing.T) {
{Hostname: "b", UDP: true},
{Hostname: "c", UDP: true},
},
selection: settings.ServerSelection{}.WithDefaults(constants.Wevpn),
filtered: []models.WevpnServer{
{Hostname: "a", UDP: true},
{Hostname: "b", UDP: true},
@@ -40,9 +41,9 @@ func Test_Wevpn_filterServers(t *testing.T) {
},
},
"filter by protocol": {
selection: configuration.ServerSelection{
OpenVPN: configuration.OpenVPNSelection{TCP: true},
},
selection: settings.ServerSelection{
OpenVPN: settings.OpenVPNSelection{TCP: boolPtr(true)},
}.WithDefaults(constants.Wevpn),
servers: []models.WevpnServer{
{Hostname: "a", UDP: true},
{Hostname: "b", TCP: true},
@@ -53,9 +54,9 @@ func Test_Wevpn_filterServers(t *testing.T) {
},
},
"filter by city": {
selection: configuration.ServerSelection{
selection: settings.ServerSelection{
Cities: []string{"b"},
},
}.WithDefaults(constants.Wevpn),
servers: []models.WevpnServer{
{City: "a", UDP: true},
{City: "b", UDP: true},
@@ -66,9 +67,9 @@ func Test_Wevpn_filterServers(t *testing.T) {
},
},
"filter by hostname": {
selection: configuration.ServerSelection{
selection: settings.ServerSelection{
Hostnames: []string{"b"},
},
}.WithDefaults(constants.Wevpn),
servers: []models.WevpnServer{
{Hostname: "a", UDP: true},
{Hostname: "b", UDP: true},

View File

@@ -1,22 +1,25 @@
package wevpn
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 (w *Wevpn) 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}
}
if settings.Auth == "" {
settings.Auth = constants.SHA512
auth := *settings.Auth
if auth == "" {
auth = constants.SHA512
}
lines = []string{
@@ -24,7 +27,7 @@ func (w *Wevpn) BuildConf(connection models.Connection,
"nobind",
"tls-exit",
"dev " + settings.Interface,
"verb " + strconv.Itoa(settings.Verbosity),
"verb " + strconv.Itoa(*settings.Verbosity),
// Wevpn specific
"ping 30",
@@ -32,7 +35,7 @@ func (w *Wevpn) BuildConf(connection models.Connection,
"redirect-gateway def1 bypass-dhcp",
"reneg-sec 0",
"auth-user-pass " + constants.OpenVPNAuthConf,
"auth " + settings.Auth,
"auth " + auth,
// Added constant values
"auth-nocache",
@@ -52,25 +55,29 @@ func (w *Wevpn) BuildConf(connection models.Connection,
lines = append(lines, utils.CipherLines(settings.Ciphers, settings.Version)...)
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, "tun-ipv6")
} else {
lines = append(lines, `pull-filter ignore "route-ipv6"`)
lines = append(lines, `pull-filter ignore "ifconfig-ipv6"`)
}
lines = append(lines, utils.WrapOpenvpnKey(
settings.ClientKey)...)
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, utils.WrapOpenvpnCA(
constants.WevpnCA)...)
lines = append(lines, utils.WrapOpenvpnCert(