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 ivpn
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 (i *Ivpn) GetConnection(selection configuration.ServerSelection) (
func (i *Ivpn) GetConnection(selection settings.ServerSelection) (
connection models.Connection, err error) {
port := getPort(selection)
protocol := utils.GetProtocol(selection)
@@ -34,7 +34,7 @@ func (i *Ivpn) GetConnection(selection configuration.ServerSelection) (
return utils.PickConnection(connections, selection, i.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_Ivpn_GetConnection(t *testing.T) {
testCases := map[string]struct {
servers []models.IvpnServer
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.Ivpn),
err: errors.New("no server found: for VPN openvpn; protocol udp"),
},
"no filter": {
servers: []models.IvpnServer{
{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)}, UDP: true},
{VPN: constants.OpenVPN, IPs: []net.IP{net.IPv4(1, 1, 1, 1)}, UDP: true},
{VPN: constants.OpenVPN, IPs: []net.IP{net.IPv4(2, 2, 2, 2)}, UDP: true},
{VPN: constants.OpenVPN, IPs: []net.IP{net.IPv4(3, 3, 3, 3)}, UDP: true},
},
selection: settings.ServerSelection{}.WithDefaults(constants.Ivpn),
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.Ivpn),
servers: []models.IvpnServer{
{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)}, UDP: true},
{VPN: constants.OpenVPN, IPs: []net.IP{net.IPv4(1, 1, 1, 1)}, UDP: true},
{VPN: constants.OpenVPN, IPs: []net.IP{net.IPv4(2, 2, 2, 2)}, UDP: true},
{VPN: constants.OpenVPN, IPs: []net.IP{net.IPv4(3, 3, 3, 3)}, UDP: true},
},
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.Ivpn),
servers: []models.IvpnServer{
{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)}, UDP: true},
{VPN: constants.OpenVPN, Hostname: "a", IPs: []net.IP{net.IPv4(1, 1, 1, 1)}, UDP: true},
{VPN: constants.OpenVPN, Hostname: "b", IPs: []net.IP{net.IPv4(2, 2, 2, 2)}, UDP: true},
{VPN: constants.OpenVPN, Hostname: "a", IPs: []net.IP{net.IPv4(3, 3, 3, 3)}, UDP: true},
},
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 ivpn
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 (i *Ivpn) filterServers(selection configuration.ServerSelection) (
func (i *Ivpn) filterServers(selection settings.ServerSelection) (
servers []models.IvpnServer, err error) {
for _, server := range i.servers {
switch {

View File

@@ -5,105 +5,106 @@ 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_Ivpn_filterServers(t *testing.T) {
t.Parallel()
testCases := map[string]struct {
servers []models.IvpnServer
selection configuration.ServerSelection
selection settings.ServerSelection
filtered []models.IvpnServer
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.Ivpn),
err: errors.New("no server found: for VPN openvpn; protocol udp"),
},
"no filter": {
servers: []models.IvpnServer{
{Hostname: "a", UDP: true},
{Hostname: "b", UDP: true},
{Hostname: "c", UDP: true},
{VPN: constants.OpenVPN, Hostname: "a", UDP: true},
{VPN: constants.OpenVPN, Hostname: "b", UDP: true},
{VPN: constants.OpenVPN, Hostname: "c", UDP: true},
},
selection: settings.ServerSelection{}.WithDefaults(constants.Ivpn),
filtered: []models.IvpnServer{
{Hostname: "a", UDP: true},
{Hostname: "b", UDP: true},
{Hostname: "c", UDP: true},
{VPN: constants.OpenVPN, Hostname: "a", UDP: true},
{VPN: constants.OpenVPN, Hostname: "b", UDP: true},
{VPN: constants.OpenVPN, Hostname: "c", UDP: true},
},
},
"filter by country": {
selection: configuration.ServerSelection{
selection: settings.ServerSelection{
Countries: []string{"b"},
},
}.WithDefaults(constants.Ivpn),
servers: []models.IvpnServer{
{Country: "a", UDP: true},
{Country: "b", UDP: true},
{Country: "c", UDP: true},
{VPN: constants.OpenVPN, Country: "a", UDP: true},
{VPN: constants.OpenVPN, Country: "b", UDP: true},
{VPN: constants.OpenVPN, Country: "c", UDP: true},
},
filtered: []models.IvpnServer{
{Country: "b", UDP: true},
{VPN: constants.OpenVPN, Country: "b", UDP: true},
},
},
"filter by city": {
selection: configuration.ServerSelection{
selection: settings.ServerSelection{
Cities: []string{"b"},
},
}.WithDefaults(constants.Ivpn),
servers: []models.IvpnServer{
{City: "a", UDP: true},
{City: "b", UDP: true},
{City: "c", UDP: true},
{VPN: constants.OpenVPN, City: "a", UDP: true},
{VPN: constants.OpenVPN, City: "b", UDP: true},
{VPN: constants.OpenVPN, City: "c", UDP: true},
},
filtered: []models.IvpnServer{
{City: "b", UDP: true},
{VPN: constants.OpenVPN, City: "b", UDP: true},
},
},
"filter by ISP": {
selection: configuration.ServerSelection{
selection: settings.ServerSelection{
ISPs: []string{"b"},
},
}.WithDefaults(constants.Ivpn),
servers: []models.IvpnServer{
{ISP: "a", UDP: true},
{ISP: "b", UDP: true},
{ISP: "c", UDP: true},
{VPN: constants.OpenVPN, ISP: "a", UDP: true},
{VPN: constants.OpenVPN, ISP: "b", UDP: true},
{VPN: constants.OpenVPN, ISP: "c", UDP: true},
},
filtered: []models.IvpnServer{
{ISP: "b", UDP: true},
{VPN: constants.OpenVPN, ISP: "b", UDP: true},
},
},
"filter by hostname": {
selection: configuration.ServerSelection{
selection: settings.ServerSelection{
Hostnames: []string{"b"},
},
}.WithDefaults(constants.Ivpn),
servers: []models.IvpnServer{
{Hostname: "a", UDP: true},
{Hostname: "b", UDP: true},
{Hostname: "c", UDP: true},
{VPN: constants.OpenVPN, Hostname: "a", UDP: true},
{VPN: constants.OpenVPN, Hostname: "b", UDP: true},
{VPN: constants.OpenVPN, Hostname: "c", UDP: true},
},
filtered: []models.IvpnServer{
{Hostname: "b", UDP: true},
{VPN: constants.OpenVPN, Hostname: "b", UDP: true},
},
},
"filter by protocol": {
selection: configuration.ServerSelection{
OpenVPN: configuration.OpenVPNSelection{
TCP: true,
selection: settings.ServerSelection{
OpenVPN: settings.OpenVPNSelection{
TCP: boolPtr(true),
},
},
}.WithDefaults(constants.Ivpn),
servers: []models.IvpnServer{
{Hostname: "a", UDP: true},
{Hostname: "b", UDP: true, TCP: true},
{Hostname: "c", UDP: true},
{VPN: constants.OpenVPN, Hostname: "a", UDP: true},
{VPN: constants.OpenVPN, Hostname: "b", UDP: true, TCP: true},
{VPN: constants.OpenVPN, Hostname: "c", UDP: true},
},
filtered: []models.IvpnServer{
{Hostname: "b", UDP: true, TCP: true},
{VPN: constants.OpenVPN, Hostname: "b", UDP: true, TCP: true},
},
},
}

View File

@@ -4,14 +4,14 @@ import (
"strconv"
"strings"
"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 (i *Ivpn) 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}
}
@@ -23,7 +23,7 @@ func (i *Ivpn) BuildConf(connection models.Connection,
"nobind",
"tls-exit",
"dev " + settings.Interface,
"verb " + strconv.Itoa(settings.Verbosity),
"verb " + strconv.Itoa(*settings.Verbosity),
// IVPN specific
"ping 5",
@@ -47,25 +47,25 @@ func (i *Ivpn) 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 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 connection.Protocol == constants.UDP {
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.IPv6 {
if !*settings.IPv6 {
lines = append(lines, `pull-filter ignore "route-ipv6"`)
lines = append(lines, `pull-filter ignore "ifconfig-ipv6"`)
}