diff options
| author | Shulhan <ms@kilabit.info> | 2022-04-16 01:36:20 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2022-04-16 18:32:36 +0700 |
| commit | 8947cc8b6a9751b3cae3d4d22fdf3ac154f77dfd (patch) | |
| tree | 7251c221bc3d4a324a6bdaca7a215d75bbb310ca | |
| parent | dfc441b32458d204dae6498867f08e7da483c815 (diff) | |
| download | rescached-8947cc8b6a9751b3cae3d4d22fdf3ac154f77dfd.tar.xz | |
all: apply suggestions from linter
List of changes,
* Remove unused constants keyIsEnabled, keyIsSystem, and keyLastUpdated.
* Use the method String on instance of Duration instead of fmt.Sprintf.
* Replace any usage of io/ioutil package with its replacement.
* Check for error from calling Environment.init and Zone.Add.
* Prefix all returned error on hostsBlock.update method.
* Add "lint" task as part of default target, build.
| -rw-r--r-- | Makefile | 4 | ||||
| -rw-r--r-- | environment.go | 11 | ||||
| -rw-r--r-- | hosts_block.go | 30 | ||||
| -rw-r--r-- | httpd.go | 26 |
4 files changed, 45 insertions, 26 deletions
@@ -33,7 +33,7 @@ DIR_BIN=/usr/bin DIR_MAN=/usr/share/man DIR_RESCACHED=/usr/share/rescached -build: test memfs_generate.go +build: lint test memfs_generate.go mkdir -p _bin/$(GOOS)_$(GOARCH) CGO_ENABLED=$(CGO_ENABLED) GOOS=$(GOOS) GOARCH=$(GOARCH) \ go build $(DEBUG) -o _bin/$(GOOS)_$(GOARCH)/ ./cmd/... @@ -52,7 +52,7 @@ test.prof: -memprofile $(MEM_PROF) ./... lint: - -golangci-lint run --enable-all ./... + -golangci-lint run ./... memfs_generate.go: .FORCE go run ./cmd/rescached embed diff --git a/environment.go b/environment.go index b8f2729..a4d7923 100644 --- a/environment.go +++ b/environment.go @@ -36,9 +36,6 @@ const ( keyDohBehindProxy = "doh.behind_proxy" keyHostsBlock = "hosts_block" keyHTTPPort = "http.port" - keyIsEnabled = "is_enabled" - keyIsSystem = "is_system" - keyLastUpdated = "last_updated" keyListen = "listen" keyParent = "parent" keyWUIListen = "wui.listen" @@ -90,7 +87,7 @@ func LoadEnvironment(fileConfig string) (env *Environment, err error) { env.fileConfig = fileConfig if len(fileConfig) == 0 { - env.init() + _ = env.init() return env, nil } @@ -266,10 +263,8 @@ func (env *Environment) save(file string) (in *ini.Ini, err error) { in.Set(sectionNameDNS, subNameServer, keyDohBehindProxy, fmt.Sprintf("%t", env.ServerOptions.DoHBehindProxy)) - in.Set(sectionNameDNS, subNameServer, keyCachePruneDelay, - fmt.Sprintf("%s", env.ServerOptions.PruneDelay)) - in.Set(sectionNameDNS, subNameServer, keyCachePruneThreshold, - fmt.Sprintf("%s", env.ServerOptions.PruneThreshold)) + in.Set(sectionNameDNS, subNameServer, keyCachePruneDelay, env.ServerOptions.PruneDelay.String()) + in.Set(sectionNameDNS, subNameServer, keyCachePruneThreshold, env.ServerOptions.PruneThreshold.String()) return in, nil } diff --git a/hosts_block.go b/hosts_block.go index c80bc9a..4a829be 100644 --- a/hosts_block.go +++ b/hosts_block.go @@ -6,7 +6,7 @@ package rescached import ( "bytes" "fmt" - "io/ioutil" + "io" "log" "net/http" "os" @@ -58,29 +58,37 @@ func (hb *hostsBlock) update() (err error) { return nil } - fmt.Printf("hostsBlock %s: updating ...\n", hb.Name) + var ( + logp = "hostsBlock.update" - res, err := http.Get(hb.URL) + res *http.Response + body []byte + errClose error + ) + + fmt.Printf("%s %s: updating ...\n", logp, hb.Name) + + res, err = http.Get(hb.URL) if err != nil { - return fmt.Errorf("hostsBlock.update %q: %w", hb.Name, err) + return fmt.Errorf("%s %s: %w", logp, hb.Name, err) } defer func() { - err := res.Body.Close() - if err != nil { - log.Printf("hostsBlock.update %q: %s", hb.Name, err) + errClose = res.Body.Close() + if errClose != nil { + log.Printf("%s %q: %s", logp, hb.Name, err) } }() - body, err := ioutil.ReadAll(res.Body) + body, err = io.ReadAll(res.Body) if err != nil { - return fmt.Errorf("hostsBlock.update %q: %w", hb.Name, err) + return fmt.Errorf("%s %q: %w", logp, hb.Name, err) } body = bytes.ReplaceAll(body, []byte("\r\n"), []byte("\n")) - err = ioutil.WriteFile(hb.file, body, 0644) + err = os.WriteFile(hb.file, body, 0644) if err != nil { - return fmt.Errorf("hostsBlock.update %q: %w", hb.Name, err) + return fmt.Errorf("%s %q: %w", logp, hb.Name, err) } hb.initLastUpdated() @@ -325,6 +325,7 @@ func (srv *Server) httpdAPIGetEnvironment(epr *libhttp.EndpointRequest) (resBody func (srv *Server) httpdAPIPostEnvironment(epr *libhttp.EndpointRequest) (resBody []byte, err error) { var ( + logp = "httpdAPIPostEnvironment" res = libhttp.EndpointResponse{} newOpts = new(Environment) ) @@ -342,7 +343,12 @@ func (srv *Server) httpdAPIPostEnvironment(epr *libhttp.EndpointRequest) (resBod return nil, &res } - newOpts.init() + err = newOpts.init() + if err != nil { + res.Code = http.StatusInternalServerError + res.Message = fmt.Sprintf("%s: %s", logp, err) + return nil, &res + } fmt.Printf("new options: %+v\n", newOpts) @@ -474,17 +480,22 @@ func (srv *Server) hostsBlockEnable(hb *hostsBlock) (err error) { } func (srv *Server) hostsBlockDisable(hb *hostsBlock) (err error) { - var hfile *dns.HostsFile + var ( + logp = "hostsBlockDisable" + + hfile *dns.HostsFile + ) + hfile = srv.env.HostsFiles[hb.Name] if hfile == nil { - return fmt.Errorf("unknown hosts block: %q", hb.Name) + return fmt.Errorf("%s: unknown hosts block: %q", logp, hb.Name) } srv.dns.RemoveLocalCachesByNames(hfile.Names()) err = hb.hide() if err != nil { - return err + return fmt.Errorf("%s: %w", logp, err) } delete(srv.env.HostsFiles, hb.Name) @@ -872,7 +883,12 @@ func (srv *Server) apiZoneRRCreate(epr *libhttp.EndpointRequest) (resbody []byte } // Update the Zone file. - zoneFile.Add(rr) + err = zoneFile.Add(rr) + if err != nil { + res.Message = err.Error() + return nil, &res + } + err = zoneFile.Save() if err != nil { res.Message = err.Error() |
