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 mullvad
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 (m *Mullvad) GetConnection(selection configuration.ServerSelection) (
func (m *Mullvad) GetConnection(selection settings.ServerSelection) (
connection models.Connection, err error) {
port := getPort(selection)
protocol := utils.GetProtocol(selection)
@@ -33,7 +33,7 @@ func (m *Mullvad) GetConnection(selection configuration.ServerSelection) (
return utils.PickConnection(connections, selection, m.randSource)
}
func getPort(selection configuration.ServerSelection) (port uint16) {
func getPort(selection settings.ServerSelection) (port uint16) {
const (
defaultOpenVPNTCP = 443
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,53 +18,55 @@ func Test_Mullvad_GetConnection(t *testing.T) {
testCases := map[string]struct {
servers []models.MullvadServer
selection configuration.ServerSelection
selection settings.ServerSelection
connection models.Connection
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.Mullvad),
err: errors.New("no server found: for VPN openvpn; protocol udp"),
},
"no filter": {
servers: []models.MullvadServer{
{IPs: []net.IP{net.IPv4(1, 1, 1, 1)}},
{IPs: []net.IP{net.IPv4(2, 2, 2, 2)}},
{IPs: []net.IP{net.IPv4(3, 3, 3, 3)}},
{VPN: constants.OpenVPN, IPs: []net.IP{net.IPv4(1, 1, 1, 1)}},
{VPN: constants.OpenVPN, IPs: []net.IP{net.IPv4(2, 2, 2, 2)}},
{VPN: constants.OpenVPN, IPs: []net.IP{net.IPv4(3, 3, 3, 3)}},
},
selection: settings.ServerSelection{}.WithDefaults(constants.Mullvad),
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.Mullvad),
servers: []models.MullvadServer{
{IPs: []net.IP{net.IPv4(1, 1, 1, 1)}},
{IPs: []net.IP{net.IPv4(2, 2, 2, 2)}},
{IPs: []net.IP{net.IPv4(3, 3, 3, 3)}},
{VPN: constants.OpenVPN, IPs: []net.IP{net.IPv4(1, 1, 1, 1)}},
{VPN: constants.OpenVPN, IPs: []net.IP{net.IPv4(2, 2, 2, 2)}},
{VPN: constants.OpenVPN, 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.Mullvad),
servers: []models.MullvadServer{
{Hostname: "a", IPs: []net.IP{net.IPv4(1, 1, 1, 1)}},
{Hostname: "b", IPs: []net.IP{net.IPv4(2, 2, 2, 2)}},
{Hostname: "a", IPs: []net.IP{net.IPv4(3, 3, 3, 3)}},
{VPN: constants.OpenVPN, Hostname: "a", IPs: []net.IP{net.IPv4(1, 1, 1, 1)}},
{VPN: constants.OpenVPN, Hostname: "b", IPs: []net.IP{net.IPv4(2, 2, 2, 2)}},
{VPN: constants.OpenVPN, 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 mullvad
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 (m *Mullvad) filterServers(selection configuration.ServerSelection) (
func (m *Mullvad) filterServers(selection settings.ServerSelection) (
servers []models.MullvadServer, err error) {
for _, server := range m.servers {
switch {
@@ -16,7 +16,7 @@ func (m *Mullvad) filterServers(selection configuration.ServerSelection) (
utils.FilterByPossibilities(server.City, selection.Cities),
utils.FilterByPossibilities(server.ISP, selection.ISPs),
utils.FilterByPossibilities(server.Hostname, selection.Hostnames),
selection.Owned && !server.Owned:
*selection.OwnedOnly && !server.Owned:
default:
servers = append(servers, server)
}

View File

@@ -5,44 +5,45 @@ 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_Mullvad_filterServers(t *testing.T) {
t.Parallel()
testCases := map[string]struct {
servers []models.MullvadServer
selection configuration.ServerSelection
selection settings.ServerSelection
filtered []models.MullvadServer
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.Mullvad),
err: errors.New("no server found: for VPN openvpn; protocol udp"),
},
"no filter": {
servers: []models.MullvadServer{
{Hostname: "a"},
{Hostname: "b"},
{Hostname: "c"},
{VPN: constants.OpenVPN, Hostname: "a"},
{VPN: constants.OpenVPN, Hostname: "b"},
{VPN: constants.OpenVPN, Hostname: "c"},
},
selection: settings.ServerSelection{}.WithDefaults(constants.Mullvad),
filtered: []models.MullvadServer{
{Hostname: "a"},
{Hostname: "b"},
{Hostname: "c"},
{VPN: constants.OpenVPN, Hostname: "a"},
{VPN: constants.OpenVPN, Hostname: "b"},
{VPN: constants.OpenVPN, Hostname: "c"},
},
},
"filter OpenVPN out": {
selection: configuration.ServerSelection{
selection: settings.ServerSelection{
VPN: constants.Wireguard,
},
}.WithDefaults(constants.Mullvad),
servers: []models.MullvadServer{
{VPN: constants.OpenVPN, Hostname: "a"},
{VPN: constants.Wireguard, Hostname: "b"},
@@ -53,68 +54,68 @@ func Test_Mullvad_filterServers(t *testing.T) {
},
},
"filter by country": {
selection: configuration.ServerSelection{
selection: settings.ServerSelection{
Countries: []string{"b"},
},
}.WithDefaults(constants.Mullvad),
servers: []models.MullvadServer{
{Country: "a"},
{Country: "b"},
{Country: "c"},
{VPN: constants.OpenVPN, Country: "a"},
{VPN: constants.OpenVPN, Country: "b"},
{VPN: constants.OpenVPN, Country: "c"},
},
filtered: []models.MullvadServer{
{Country: "b"},
{VPN: constants.OpenVPN, Country: "b"},
},
},
"filter by city": {
selection: configuration.ServerSelection{
selection: settings.ServerSelection{
Cities: []string{"b"},
},
}.WithDefaults(constants.Mullvad),
servers: []models.MullvadServer{
{City: "a"},
{City: "b"},
{City: "c"},
{VPN: constants.OpenVPN, City: "a"},
{VPN: constants.OpenVPN, City: "b"},
{VPN: constants.OpenVPN, City: "c"},
},
filtered: []models.MullvadServer{
{City: "b"},
{VPN: constants.OpenVPN, City: "b"},
},
},
"filter by ISP": {
selection: configuration.ServerSelection{
selection: settings.ServerSelection{
ISPs: []string{"b"},
},
}.WithDefaults(constants.Mullvad),
servers: []models.MullvadServer{
{ISP: "a"},
{ISP: "b"},
{ISP: "c"},
{VPN: constants.OpenVPN, ISP: "a"},
{VPN: constants.OpenVPN, ISP: "b"},
{VPN: constants.OpenVPN, ISP: "c"},
},
filtered: []models.MullvadServer{
{ISP: "b"},
{VPN: constants.OpenVPN, ISP: "b"},
},
},
"filter by hostname": {
selection: configuration.ServerSelection{
selection: settings.ServerSelection{
Hostnames: []string{"b"},
},
}.WithDefaults(constants.Mullvad),
servers: []models.MullvadServer{
{Hostname: "a"},
{Hostname: "b"},
{Hostname: "c"},
{VPN: constants.OpenVPN, Hostname: "a"},
{VPN: constants.OpenVPN, Hostname: "b"},
{VPN: constants.OpenVPN, Hostname: "c"},
},
filtered: []models.MullvadServer{
{Hostname: "b"},
{VPN: constants.OpenVPN, Hostname: "b"},
},
},
"filter by owned": {
selection: configuration.ServerSelection{
Owned: true,
},
selection: settings.ServerSelection{
OwnedOnly: boolPtr(true),
}.WithDefaults(constants.Mullvad),
servers: []models.MullvadServer{
{Hostname: "a"},
{Hostname: "b", Owned: true},
{Hostname: "c"},
{VPN: constants.OpenVPN, Hostname: "a"},
{VPN: constants.OpenVPN, Hostname: "b", Owned: true},
{VPN: constants.OpenVPN, Hostname: "c"},
},
filtered: []models.MullvadServer{
{Hostname: "b", Owned: true},
{VPN: constants.OpenVPN, Hostname: "b", Owned: true},
},
},
}

View File

@@ -3,14 +3,14 @@ package mullvad
import (
"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/provider/utils"
)
func (m *Mullvad) 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.AES256cbc, constants.AES128gcm}
}
@@ -20,7 +20,7 @@ func (m *Mullvad) BuildConf(connection models.Connection,
"nobind",
"tls-exit",
"dev " + settings.Interface,
"verb " + strconv.Itoa(settings.Verbosity),
"verb " + strconv.Itoa(*settings.Verbosity),
"auth-user-pass " + constants.OpenVPNAuthConf,
// Mullvad specific
@@ -44,8 +44,8 @@ func (m *Mullvad) BuildConf(connection models.Connection,
lines = append(lines, utils.CipherLines(settings.Ciphers, settings.Version)...)
if settings.Auth != "" {
lines = append(lines, "auth "+settings.Auth)
if *settings.Auth != "" {
lines = append(lines, "auth "+*settings.Auth)
}
if connection.Protocol == constants.UDP {
@@ -53,22 +53,22 @@ func (m *Mullvad) BuildConf(connection models.Connection,
lines = append(lines, "explicit-exit-notify")
}
if !settings.IPv6 {
if !*settings.IPv6 {
lines = append(lines, `pull-filter ignore "route-ipv6"`)
lines = append(lines, `pull-filter ignore "ifconfig-ipv6"`)
}
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"`)
}