Skip to content

Commit

Permalink
chore(sources/env): use gosettings/sources/env functions
Browse files Browse the repository at this point in the history
  • Loading branch information
qdm12 committed Jun 14, 2023
1 parent 6506b62 commit 152c6f0
Show file tree
Hide file tree
Showing 13 changed files with 63 additions and 296 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/prometheus/client_model v0.2.0
github.com/qdm12/golibs v0.0.0-20210915134941-19815c6f95fe
github.com/qdm12/goservices v0.1.0-rc2
github.com/qdm12/gosettings v0.3.0
github.com/qdm12/gosettings v0.4.0-rc1
github.com/qdm12/gosplash v0.1.0
github.com/qdm12/gotree v0.0.0-20211231173558-a8b7dce9989e
github.com/qdm12/govalid v0.2.0-rc1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,8 @@ github.com/qdm12/gosettings v0.3.0-rc6 h1:zsGXIIZ5FWsn9LeFBbr7s2/xgeodfvmWliVAFX
github.com/qdm12/gosettings v0.3.0-rc6/go.mod h1:+hHzN8lsE63T01t6SruGzc6xkpvfsZFod/ooDs8FWnQ=
github.com/qdm12/gosettings v0.3.0 h1:YutcgQzVaOB3LuLj+Smtoy90JOH/5B5p2IH3BvV3ra4=
github.com/qdm12/gosettings v0.3.0/go.mod h1:JRV3opOpHvnKlIA29lKQMdYw1WSMVMfHYLLHPHol5ME=
github.com/qdm12/gosettings v0.4.0-rc1 h1:UYA92yyeDPbmZysIuG65yrpZVPtdIoRmtEHft/AyI38=
github.com/qdm12/gosettings v0.4.0-rc1/go.mod h1:JRV3opOpHvnKlIA29lKQMdYw1WSMVMfHYLLHPHol5ME=
github.com/qdm12/gosplash v0.1.0 h1:Sfl+zIjFZFP7b0iqf2l5UkmEY97XBnaKkH3FNY6Gf7g=
github.com/qdm12/gosplash v0.1.0/go.mod h1:+A3fWW4/rUeDXhY3ieBzwghKdnIPFJgD8K3qQkenJlw=
github.com/qdm12/gotree v0.0.0-20211231173558-a8b7dce9989e h1:L1zGR8xaYpbGFO9GjphGHmN91nIzwVp2EO6cauAtwoI=
Expand Down
77 changes: 12 additions & 65 deletions internal/config/sources/env/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,109 +2,56 @@ package env

import (
"fmt"
"net/netip"

"github.com/qdm12/dns/v2/internal/config/settings"
)

func readBlock() (settings settings.Block, err error) {
settings.BlockMalicious, err = envToBoolPtr("BLOCK_MALICIOUS")
func (r *Reader) readBlock() (settings settings.Block, err error) {
settings.BlockMalicious, err = r.env.BoolPtr("BLOCK_MALICIOUS")
if err != nil {
return settings, fmt.Errorf("environment variable BLOCK_MALICIOUS: %w", err)
}

settings.BlockSurveillance, err = envToBoolPtr("BLOCK_SURVEILLANCE")
settings.BlockSurveillance, err = r.env.BoolPtr("BLOCK_SURVEILLANCE")
if err != nil {
return settings, fmt.Errorf("environment variable BLOCK_SURVEILLANCE: %w", err)
}

settings.BlockAds, err = envToBoolPtr("BLOCK_ADS")
settings.BlockAds, err = r.env.BoolPtr("BLOCK_ADS")
if err != nil {
return settings, fmt.Errorf("environment variable BLOCK_ADS: %w", err)
}

settings.RebindingProtection, err = envToBoolPtr("REBINDING_PROTECTION")
settings.RebindingProtection, err = r.env.BoolPtr("REBINDING_PROTECTION")
if err != nil {
return settings, fmt.Errorf("environment variable REBINDING_PROTECTION: %w", err)
}

settings.AllowedHosts = envToCSV("ALLOWED_HOSTNAMES")
settings.AddBlockedHosts = envToCSV("BLOCK_HOSTNAMES")
settings.AllowedHosts = r.env.CSV("ALLOWED_HOSTNAMES")
settings.AddBlockedHosts = r.env.CSV("BLOCK_HOSTNAMES")

settings.AllowedIPs, err = getAllowedIPs()
settings.AllowedIPs, err = r.env.CSVNetipAddresses("ALLOWED_IPS")
if err != nil {
return settings, err
}
settings.AddBlockedIPs, err = getBlockedIPs()
settings.AddBlockedIPs, err = r.env.CSVNetipAddresses("BLOCK_IPS")
if err != nil {
return settings, err
}

settings.AllowedIPPrefixes, err = getAllowedIPPrefixes()
settings.AllowedIPPrefixes, err = r.env.CSVNetipPrefixes("ALLOWED_CIDRS")
if err != nil {
return settings, err
}
settings.AddBlockedIPPrefixes, err = getBlockedIPPrefixes()
settings.AddBlockedIPPrefixes, err = r.env.CSVNetipPrefixes("BLOCK_CIDRS")
if err != nil {
return settings, err
}

settings.RebindingProtection, err = envToBoolPtr("REBINDING_PROTECTION")
settings.RebindingProtection, err = r.env.BoolPtr("REBINDING_PROTECTION")
if err != nil {
return settings, fmt.Errorf("environment variable REBINDING_PROTECTION: %w", err)
}

return settings, nil
}

// getAllowedIPs obtains a list of IPs to unblock from block lists
// from the comma separated list for the environment variable ALLOWED_IPS.
func getAllowedIPs() (ips []netip.Addr, err error) {
ipStrings := envToCSV("ALLOWED_IPS")

ips, err = parseIPStrings(ipStrings)
if err != nil {
return nil, fmt.Errorf("environment variable ALLOWED_IPS: %w", err)
}

return ips, nil
}

// getBlockedIPs obtains a list of IP addresses to block from
// the comma separated list for the environment variable BLOCK_IPS.
func getBlockedIPs() (ips []netip.Addr, err error) {
values := envToCSV("BLOCK_IPS")

ips, err = parseIPStrings(values)
if err != nil {
return nil, fmt.Errorf("environment variable BLOCK_IPS: %w", err)
}

return ips, nil
}

// getAllowedIPPrefixes obtains a list of IP Prefixes to unblock from block lists
// from the comma separated list for the environment variable ALLOWED_CIDRS.
func getAllowedIPPrefixes() (ipPrefixes []netip.Prefix, err error) {
ipPrefixStrings := envToCSV("ALLOWED_CIDRS")

ipPrefixes, err = parseIPPrefixStrings(ipPrefixStrings)
if err != nil {
return nil, fmt.Errorf("environment variable ALLOWED_CIDRS: %w", err)
}

return ipPrefixes, nil
}

// getBlockedIPPrefixes obtains a list of IP networks (CIDR notation) to block from
// the comma separated list for the environment variable BLOCK_CIDRS.
func getBlockedIPPrefixes() (ipPrefixes []netip.Prefix, err error) {
values := envToCSV("BLOCK_CIDRS")

ipPrefixes, err = parseIPPrefixStrings(values)
if err != nil {
return nil, fmt.Errorf("environment variable BLOCK_CIDRS: %w", err)
}

return ipPrefixes, nil
}
12 changes: 5 additions & 7 deletions internal/config/sources/env/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,15 @@ import (
"errors"
"fmt"
"math"
"os"
"strconv"
"strings"

"github.com/qdm12/dns/v2/internal/config/settings"
)

func readCache() (settings settings.Cache, err error) {
settings.Type = strings.ToLower(os.Getenv("CACHE_TYPE"))
func (r *Reader) readCache() (settings settings.Cache, err error) {
settings.Type = r.env.String("CACHE_TYPE")

settings.LRU.MaxEntries, err = getLRUCacheMaxEntries()
settings.LRU.MaxEntries, err = r.getLRUCacheMaxEntries()
if err != nil {
return settings, fmt.Errorf("LRU max entries: %w", err)
}
Expand All @@ -24,8 +22,8 @@ func readCache() (settings settings.Cache, err error) {

var ErrCacheLRUMaxEntries = errors.New("invalid value for max entries of the LRU cache")

func getLRUCacheMaxEntries() (maxEntries uint, err error) {
s := os.Getenv("CACHE_LRU_MAX_ENTRIES")
func (r *Reader) getLRUCacheMaxEntries() (maxEntries uint, err error) {
s := r.env.String("CACHE_LRU_MAX_ENTRIES")
if s == "" {
return 0, nil
}
Expand Down
14 changes: 7 additions & 7 deletions internal/config/sources/env/doh.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@ import (
"github.com/qdm12/dns/v2/internal/config/settings"
)

func readDoH() (settings settings.DoH, err error) {
settings.DoHProviders = envToCSV("DOH_RESOLVERS")
settings.Timeout, err = envToDuration("DOH_TIMEOUT")
func (r *Reader) readDoH() (settings settings.DoH, err error) {
settings.DoHProviders = r.env.CSV("DOH_RESOLVERS")
settings.Timeout, err = r.env.Duration("DOH_TIMEOUT")
if err != nil {
return settings, fmt.Errorf("environment variable DOH_TIMEOUT: %w", err)
}

settings.Self.DoTProviders = envToCSV("DOT_RESOLVERS")
settings.Self.DNSProviders = envToCSV("DNS_FALLBACK_PLAINTEXT_RESOLVERS")
settings.Self.IPv6, err = envToBoolPtr("DOT_CONNECT_IPV6")
settings.Self.DoTProviders = r.env.CSV("DOT_RESOLVERS")
settings.Self.DNSProviders = r.env.CSV("DNS_FALLBACK_PLAINTEXT_RESOLVERS")
settings.Self.IPv6, err = r.env.BoolPtr("DOT_CONNECT_IPV6")
if err != nil {
return settings, fmt.Errorf("environment variable DOT_CONNECT_IPV6: %w", err)
}

settings.Self.Timeout, err = envToDuration("DOT_TIMEOUT")
settings.Self.Timeout, err = r.env.Duration("DOT_TIMEOUT")
if err != nil {
return settings, fmt.Errorf("environment variable DOT_TIMEOUT: %w", err)
}
Expand Down
10 changes: 5 additions & 5 deletions internal/config/sources/env/dot.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import (
"github.com/qdm12/dns/v2/internal/config/settings"
)

func readDoT() (settings settings.DoT, err error) {
settings.DoTProviders = envToCSV("DOT_RESOLVERS")
settings.DNSProviders = envToCSV("DNS_FALLBACK_PLAINTEXT_RESOLVERS")
settings.Timeout, err = envToDuration("DOT_TIMEOUT")
func (r *Reader) readDoT() (settings settings.DoT, err error) {
settings.DoTProviders = r.env.CSV("DOT_RESOLVERS")
settings.DNSProviders = r.env.CSV("DNS_FALLBACK_PLAINTEXT_RESOLVERS")
settings.Timeout, err = r.env.Duration("DOT_TIMEOUT")
if err != nil {
return settings, fmt.Errorf("environment variable DOT_TIMEOUT: %w", err)
}

settings.IPv6, err = envToBoolPtr("DOT_CONNECT_IPV6")
settings.IPv6, err = r.env.BoolPtr("DOT_CONNECT_IPV6")
if err != nil {
return settings, fmt.Errorf("environment variable DOT_CONNECT_IPV6: %w", err)
}
Expand Down
93 changes: 0 additions & 93 deletions internal/config/sources/env/helpers.go

This file was deleted.

73 changes: 0 additions & 73 deletions internal/config/sources/env/helpers_test.go

This file was deleted.

Loading

0 comments on commit 152c6f0

Please sign in to comment.