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:
@@ -4,7 +4,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"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/extract"
|
||||
@@ -17,7 +17,7 @@ var (
|
||||
)
|
||||
|
||||
// GetConnection gets the connection from the OpenVPN configuration file.
|
||||
func (p *Provider) GetConnection(selection configuration.ServerSelection) (
|
||||
func (p *Provider) GetConnection(selection settings.ServerSelection) (
|
||||
connection models.Connection, err error) {
|
||||
switch selection.VPN {
|
||||
case constants.OpenVPN:
|
||||
@@ -30,9 +30,9 @@ func (p *Provider) GetConnection(selection configuration.ServerSelection) (
|
||||
}
|
||||
|
||||
func getOpenVPNConnection(extractor extract.Interface,
|
||||
selection configuration.ServerSelection) (
|
||||
selection settings.ServerSelection) (
|
||||
connection models.Connection, err error) {
|
||||
_, connection, err = extractor.Data(selection.OpenVPN.ConfFile)
|
||||
_, connection, err = extractor.Data(*selection.OpenVPN.ConfFile)
|
||||
if err != nil {
|
||||
return connection, fmt.Errorf("%w: %s", ErrExtractConnection, err)
|
||||
}
|
||||
@@ -41,9 +41,9 @@ func getOpenVPNConnection(extractor extract.Interface,
|
||||
return connection, nil
|
||||
}
|
||||
|
||||
func getWireguardConnection(selection configuration.ServerSelection) (
|
||||
func getWireguardConnection(selection settings.ServerSelection) (
|
||||
connection models.Connection) {
|
||||
port := getPort(selection.Wireguard.EndpointPort, selection)
|
||||
port := getPort(*selection.Wireguard.EndpointPort, selection)
|
||||
return models.Connection{
|
||||
Type: constants.Wireguard,
|
||||
IP: selection.Wireguard.EndpointIP,
|
||||
@@ -54,6 +54,6 @@ func getWireguardConnection(selection configuration.ServerSelection) (
|
||||
}
|
||||
|
||||
// Port found is overridden by custom port set with `PORT` or `WIREGUARD_ENDPOINT_PORT`.
|
||||
func getPort(foundPort uint16, selection configuration.ServerSelection) (port uint16) {
|
||||
func getPort(foundPort uint16, selection settings.ServerSelection) (port uint16) {
|
||||
return utils.GetPort(selection, foundPort, foundPort, foundPort)
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ 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"
|
||||
@@ -15,8 +15,8 @@ import (
|
||||
var ErrExtractData = errors.New("failed extracting information from custom configuration file")
|
||||
|
||||
func (p *Provider) BuildConf(connection models.Connection,
|
||||
settings configuration.OpenVPN) (lines []string, err error) {
|
||||
lines, _, err = p.extractor.Data(settings.ConfFile)
|
||||
settings settings.OpenVPN) (lines []string, err error) {
|
||||
lines, _, err = p.extractor.Data(*settings.ConfFile)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%w: %s", ErrExtractData, err)
|
||||
}
|
||||
@@ -27,7 +27,7 @@ func (p *Provider) BuildConf(connection models.Connection,
|
||||
}
|
||||
|
||||
func modifyConfig(lines []string, connection models.Connection,
|
||||
settings configuration.OpenVPN) (modified []string) {
|
||||
settings settings.OpenVPN) (modified []string) {
|
||||
// Remove some lines
|
||||
for _, line := range lines {
|
||||
switch {
|
||||
@@ -52,9 +52,9 @@ func modifyConfig(lines []string, connection models.Connection,
|
||||
// Remove values eventually modified
|
||||
len(settings.Ciphers) > 0 && hasPrefixOneOf(line,
|
||||
"cipher ", "ncp-ciphers ", "data-ciphers ", "data-ciphers-fallback "),
|
||||
settings.Auth != "" && strings.HasPrefix(line, "auth "),
|
||||
settings.MSSFix > 0 && strings.HasPrefix(line, "mssfix "),
|
||||
!settings.IPv6 && hasPrefixOneOf(line, "tun-ipv6",
|
||||
*settings.Auth != "" && strings.HasPrefix(line, "auth "),
|
||||
*settings.MSSFix > 0 && strings.HasPrefix(line, "mssfix "),
|
||||
!*settings.IPv6 && hasPrefixOneOf(line, "tun-ipv6",
|
||||
`pull-filter ignore "route-ipv6"`,
|
||||
`pull-filter ignore "ifconfig-ipv6"`):
|
||||
default:
|
||||
@@ -74,21 +74,21 @@ func modifyConfig(lines []string, connection models.Connection,
|
||||
if settings.User != "" {
|
||||
modified = append(modified, "auth-user-pass "+constants.OpenVPNAuthConf)
|
||||
}
|
||||
modified = append(modified, "verb "+strconv.Itoa(settings.Verbosity))
|
||||
modified = append(modified, "verb "+strconv.Itoa(*settings.Verbosity))
|
||||
if len(settings.Ciphers) > 0 {
|
||||
modified = append(modified, utils.CipherLines(settings.Ciphers, settings.Version)...)
|
||||
}
|
||||
if settings.Auth != "" {
|
||||
modified = append(modified, "auth "+settings.Auth)
|
||||
if *settings.Auth != "" {
|
||||
modified = append(modified, "auth "+*settings.Auth)
|
||||
}
|
||||
if settings.MSSFix > 0 {
|
||||
modified = append(modified, "mssfix "+strconv.Itoa(int(settings.MSSFix)))
|
||||
if *settings.MSSFix > 0 {
|
||||
modified = append(modified, "mssfix "+strconv.Itoa(int(*settings.MSSFix)))
|
||||
}
|
||||
if !settings.IPv6 {
|
||||
if !*settings.IPv6 {
|
||||
modified = append(modified, `pull-filter ignore "route-ipv6"`)
|
||||
modified = append(modified, `pull-filter ignore "ifconfig-ipv6"`)
|
||||
}
|
||||
if !settings.Root {
|
||||
if !*settings.Root {
|
||||
modified = append(modified, "user "+settings.ProcUser)
|
||||
modified = append(modified, "persist-tun")
|
||||
modified = append(modified, "persist-key")
|
||||
|
||||
@@ -4,18 +4,23 @@ 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"
|
||||
)
|
||||
|
||||
func boolPtr(b bool) *bool { return &b }
|
||||
func intPtr(n int) *int { return &n }
|
||||
func uint16Ptr(n uint16) *uint16 { return &n }
|
||||
func stringPtr(s string) *string { return &s }
|
||||
|
||||
func Test_modifyConfig(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
testCases := map[string]struct {
|
||||
lines []string
|
||||
settings configuration.OpenVPN
|
||||
settings settings.OpenVPN
|
||||
connection models.Connection
|
||||
modified []string
|
||||
}{
|
||||
@@ -30,14 +35,16 @@ func Test_modifyConfig(t *testing.T) {
|
||||
"keep me here",
|
||||
"auth bla",
|
||||
},
|
||||
settings: configuration.OpenVPN{
|
||||
settings: settings.OpenVPN{
|
||||
User: "user",
|
||||
Ciphers: []string{"cipher"},
|
||||
Auth: "auth",
|
||||
MSSFix: 1000,
|
||||
Auth: stringPtr("auth"),
|
||||
MSSFix: uint16Ptr(1000),
|
||||
Root: boolPtr(false),
|
||||
ProcUser: "procuser",
|
||||
Interface: "tun3",
|
||||
},
|
||||
Verbosity: intPtr(0),
|
||||
}.WithDefaults(constants.Custom),
|
||||
connection: models.Connection{
|
||||
IP: net.IPv4(1, 2, 3, 4),
|
||||
Port: 1194,
|
||||
|
||||
Reference in New Issue
Block a user