diff options
| author | Shulhan <m.shulhan@gmail.com> | 2020-05-31 06:02:26 +0700 |
|---|---|---|
| committer | Shulhan <m.shulhan@gmail.com> | 2020-07-26 03:48:51 +0700 |
| commit | abea060e735d5da18ab3c4bd0ad5fe489369e0a5 (patch) | |
| tree | db87911e035946edf970f0cc40786026df93dbf0 /environment.go | |
| parent | 8a476fad2b3410de9c42a45f144980de4a5e176d (diff) | |
| download | rescached-abea060e735d5da18ab3c4bd0ad5fe489369e0a5.tar.xz | |
all: add user interface to configure source of hosts block
The user interface, which can be accessed at
http://127.0.0.1:5380/#hostsblock, contains list of hosts block sources
that can be enabled or disabled using check box.
If enabled and the file is outdated, it will fetch the latest list
and store it at /etc/rescached/hosts.d/<name>, where name is the domain
name of source.
Diffstat (limited to 'environment.go')
| -rw-r--r-- | environment.go | 38 |
1 files changed, 32 insertions, 6 deletions
diff --git a/environment.go b/environment.go index 1dfe834..ac72dec 100644 --- a/environment.go +++ b/environment.go @@ -6,9 +6,9 @@ package rescached import ( "fmt" - "io/ioutil" "log" "strconv" + "strings" "github.com/shuLhan/share/lib/debug" "github.com/shuLhan/share/lib/dns" @@ -33,7 +33,11 @@ const ( keyCachePruneDelay = "cache.prune_delay" keyCachePruneThreshold = "cache.prune_threshold" keyDohBehindProxy = "doh.behind_proxy" + keyHostsBlock = "hosts_block" keyHTTPPort = "http.port" + keyIsEnabled = "is_enabled" + keyIsSystem = "is_system" + keyLastUpdated = "last_updated" keyListen = "listen" keyParent = "parent" keyTLSAllowInsecure = "tls.allow_insecure" @@ -50,9 +54,11 @@ const ( // type environment struct { dns.ServerOptions - WuiListen string `ini:"rescached::wui.listen"` - FileResolvConf string `ini:"rescached::file.resolvconf"` - Debug int `ini:"rescached::debug"` + WuiListen string `ini:"rescached::wui.listen"` + FileResolvConf string `ini:"rescached::file.resolvconf"` + Debug int `ini:"rescached::debug"` + HostsBlocksRaw []string `ini:"rescached::hosts_block" json:"-"` + HostsBlocks []*hostsBlock } func loadEnvironment(file string) (env *environment) { @@ -63,19 +69,20 @@ func loadEnvironment(file string) (env *environment) { return env } - cfg, err := ioutil.ReadFile(file) + cfg, err := ini.Open(file) if err != nil { log.Printf("rescached: loadEnvironment %q: %s", file, err) return env } - err = ini.Unmarshal(cfg, env) + err = cfg.Unmarshal(env) if err != nil { log.Printf("rescached: loadEnvironment %q: %s", file, err) return env } env.init() + env.initHostsBlock(cfg) debug.Value = env.Debug return env @@ -107,6 +114,18 @@ func (env *environment) init() { } } +func (env *environment) initHostsBlock(cfg *ini.Ini) { + env.HostsBlocks = hostsBlockSources + + for x, v := range env.HostsBlocksRaw { + env.HostsBlocksRaw[x] = strings.ToLower(v) + } + + for _, hb := range env.HostsBlocks { + hb.init(env.HostsBlocksRaw) + } +} + func (env *environment) loadResolvConf() (ok bool, err error) { rc, err := libnet.NewResolvConf(env.FileResolvConf) if err != nil { @@ -150,6 +169,13 @@ func (env *environment) write(file string) (err error) { in.Set(sectionNameRescached, "", keyFileResolvConf, env.FileResolvConf) in.Set(sectionNameRescached, "", keyDebug, strconv.Itoa(env.Debug)) + in.UnsetAll(sectionNameRescached, "", keyHostsBlock) + for _, hb := range env.HostsBlocks { + if hb.IsEnabled { + in.Add(sectionNameRescached, "", keyHostsBlock, hb.URL) + } + } + in.UnsetAll(sectionNameDNS, subNameServer, keyParent) for _, ns := range env.NameServers { in.Add(sectionNameDNS, subNameServer, keyParent, ns) |
