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:
5
internal/configuration/sources/files/health.go
Normal file
5
internal/configuration/sources/files/health.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package files
|
||||
|
||||
import "github.com/qdm12/gluetun/internal/configuration/settings"
|
||||
|
||||
func (r *Reader) ReadHealth() (settings settings.Health, err error) { return settings, nil }
|
||||
31
internal/configuration/sources/files/helpers.go
Normal file
31
internal/configuration/sources/files/helpers.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package files
|
||||
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
)
|
||||
|
||||
// ReadFromFile reads the content of the file as a string.
|
||||
// It returns a nil string pointer if the file does not exist.
|
||||
func ReadFromFile(filepath string) (s *string, err error) {
|
||||
file, err := os.Open(filepath)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return nil, nil //nolint:nilnil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
b, err := io.ReadAll(file)
|
||||
if err != nil {
|
||||
_ = file.Close()
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := file.Close(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
content := string(b)
|
||||
return &content, nil
|
||||
}
|
||||
22
internal/configuration/sources/files/openvpn.go
Normal file
22
internal/configuration/sources/files/openvpn.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package files
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/configuration/settings"
|
||||
"github.com/qdm12/gluetun/internal/constants"
|
||||
)
|
||||
|
||||
func (r *Reader) readOpenVPN() (settings settings.OpenVPN, err error) {
|
||||
settings.ClientKey, err = ReadFromFile(constants.ClientKey)
|
||||
if err != nil {
|
||||
return settings, fmt.Errorf("cannot read client key: %w", err)
|
||||
}
|
||||
|
||||
settings.ClientCrt, err = ReadFromFile(constants.ClientCertificate)
|
||||
if err != nil {
|
||||
return settings, fmt.Errorf("cannot read client certificate: %w", err)
|
||||
}
|
||||
|
||||
return settings, nil
|
||||
}
|
||||
28
internal/configuration/sources/files/reader.go
Normal file
28
internal/configuration/sources/files/reader.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package files
|
||||
|
||||
import (
|
||||
"github.com/qdm12/gluetun/internal/configuration/settings"
|
||||
"github.com/qdm12/gluetun/internal/configuration/sources"
|
||||
)
|
||||
|
||||
var _ sources.Source = (*Reader)(nil)
|
||||
|
||||
type Reader struct{}
|
||||
|
||||
func New() *Reader {
|
||||
return &Reader{}
|
||||
}
|
||||
|
||||
func (r *Reader) Read() (settings settings.Settings, err error) {
|
||||
settings.VPN, err = r.readVPN()
|
||||
if err != nil {
|
||||
return settings, err
|
||||
}
|
||||
|
||||
settings.System, err = r.readSystem()
|
||||
if err != nil {
|
||||
return settings, err
|
||||
}
|
||||
|
||||
return settings, nil
|
||||
}
|
||||
10
internal/configuration/sources/files/system.go
Normal file
10
internal/configuration/sources/files/system.go
Normal file
@@ -0,0 +1,10 @@
|
||||
package files
|
||||
|
||||
import (
|
||||
"github.com/qdm12/gluetun/internal/configuration/settings"
|
||||
)
|
||||
|
||||
func (r *Reader) readSystem() (system settings.System, err error) {
|
||||
// TODO timezone from /etc/localtime
|
||||
return system, nil
|
||||
}
|
||||
16
internal/configuration/sources/files/vpn.go
Normal file
16
internal/configuration/sources/files/vpn.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package files
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/configuration/settings"
|
||||
)
|
||||
|
||||
func (r *Reader) readVPN() (vpn settings.VPN, err error) {
|
||||
vpn.OpenVPN, err = r.readOpenVPN()
|
||||
if err != nil {
|
||||
return vpn, fmt.Errorf("cannot read OpenVPN settings: %w", err)
|
||||
}
|
||||
|
||||
return vpn, nil
|
||||
}
|
||||
Reference in New Issue
Block a user