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 windscribe
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 *Windscribe) GetConnection(selection configuration.ServerSelection) (
func (w *Windscribe) GetConnection(selection settings.ServerSelection) (
connection models.Connection, err error) {
port := getPort(selection)
protocol := utils.GetProtocol(selection)
@@ -34,7 +34,7 @@ func (w *Windscribe) 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 = 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_Windscribe_GetConnection(t *testing.T) {
testCases := map[string]struct {
servers []models.WindscribeServer
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.Windscribe),
err: errors.New("no server found: for VPN openvpn; protocol udp"),
},
"no filter": {
servers: []models.WindscribeServer{
{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.Windscribe),
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.Windscribe),
servers: []models.WindscribeServer{
{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.Windscribe),
servers: []models.WindscribeServer{
{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 windscribe
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 *Windscribe) filterServers(selection configuration.ServerSelection) (
func (w *Windscribe) filterServers(selection settings.ServerSelection) (
servers []models.WindscribeServer, err error) {
for _, server := range w.servers {
switch {

View File

@@ -5,7 +5,7 @@ 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"
@@ -17,32 +17,31 @@ func Test_Windscribe_filterServers(t *testing.T) {
testCases := map[string]struct {
servers []models.WindscribeServer
selection configuration.ServerSelection
selection settings.ServerSelection
filtered []models.WindscribeServer
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.Windscribe),
err: errors.New("no server found: for VPN openvpn; protocol udp"),
},
"no filter": {
servers: []models.WindscribeServer{
{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.Windscribe),
filtered: []models.WindscribeServer{
{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.Windscribe),
servers: []models.WindscribeServer{
{VPN: constants.OpenVPN, Hostname: "a"},
{VPN: constants.Wireguard, Hostname: "b"},
@@ -53,42 +52,42 @@ func Test_Windscribe_filterServers(t *testing.T) {
},
},
"filter by region": {
selection: configuration.ServerSelection{
selection: settings.ServerSelection{
Regions: []string{"b"},
},
}.WithDefaults(constants.Windscribe),
servers: []models.WindscribeServer{
{Region: "a"},
{Region: "b"},
{Region: "c"},
{VPN: constants.OpenVPN, Region: "a"},
{VPN: constants.OpenVPN, Region: "b"},
{VPN: constants.OpenVPN, Region: "c"},
},
filtered: []models.WindscribeServer{
{Region: "b"},
{VPN: constants.OpenVPN, Region: "b"},
},
},
"filter by city": {
selection: configuration.ServerSelection{
selection: settings.ServerSelection{
Cities: []string{"b"},
},
}.WithDefaults(constants.Windscribe),
servers: []models.WindscribeServer{
{City: "a"},
{City: "b"},
{City: "c"},
{VPN: constants.OpenVPN, City: "a"},
{VPN: constants.OpenVPN, City: "b"},
{VPN: constants.OpenVPN, City: "c"},
},
filtered: []models.WindscribeServer{
{City: "b"},
{VPN: constants.OpenVPN, City: "b"},
},
},
"filter by hostname": {
selection: configuration.ServerSelection{
selection: settings.ServerSelection{
Hostnames: []string{"b"},
},
}.WithDefaults(constants.Windscribe),
servers: []models.WindscribeServer{
{Hostname: "a"},
{Hostname: "b"},
{Hostname: "c"},
{VPN: constants.OpenVPN, Hostname: "a"},
{VPN: constants.OpenVPN, Hostname: "b"},
{VPN: constants.OpenVPN, Hostname: "c"},
},
filtered: []models.WindscribeServer{
{Hostname: "b"},
{VPN: constants.OpenVPN, Hostname: "b"},
},
},
}

View File

@@ -3,14 +3,14 @@ package windscribe
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 (w *Windscribe) 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 +19,9 @@ func (w *Windscribe) BuildConf(connection models.Connection,
}
}
if settings.Auth == "" {
settings.Auth = constants.SHA512
auth := *settings.Auth
if auth == "" {
auth = constants.SHA512
}
lines = []string{
@@ -28,7 +29,7 @@ func (w *Windscribe) BuildConf(connection models.Connection,
"nobind",
"tls-exit",
"dev " + settings.Interface,
"verb " + strconv.Itoa(settings.Verbosity),
"verb " + strconv.Itoa(*settings.Verbosity),
// Windscribe specific
"ping 10",
@@ -37,7 +38,7 @@ func (w *Windscribe) BuildConf(connection models.Connection,
"key-direction 1",
"reneg-sec 0",
"auth-user-pass " + constants.OpenVPNAuthConf,
"auth " + settings.Auth,
"auth " + auth,
// Added constant values
"auth-nocache",
@@ -57,17 +58,17 @@ func (w *Windscribe) 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"`)
}