diff options
| -rw-r--r-- | client.go | 22 | ||||
| -rw-r--r-- | client_test.go | 2 | ||||
| -rw-r--r-- | cmd/resolver/main.go | 2 | ||||
| -rw-r--r-- | cmd/resolver/resolver.go | 42 | ||||
| -rw-r--r-- | environment.go | 4 | ||||
| -rw-r--r-- | httpd.go | 96 |
6 files changed, 83 insertions, 85 deletions
@@ -20,10 +20,10 @@ type Client struct { } // NewClient create new HTTP client that connect to rescached HTTP server. -func NewClient(serverUrl string, insecure bool) (cl *Client) { +func NewClient(serverURL string, insecure bool) (cl *Client) { var ( httpcOpts = libhttp.ClientOptions{ - ServerUrl: serverUrl, + ServerUrl: serverURL, AllowInsecure: insecure, } ) @@ -42,7 +42,7 @@ func (cl *Client) Blockd() (hostBlockd map[string]*Blockd, err error) { resb []byte ) - _, resb, err = cl.Get(httpApiBlockd, nil, nil) + _, resb, err = cl.Get(httpAPIBlockd, nil, nil) if err != nil { return nil, fmt.Errorf("%s: %w", logp, err) } @@ -72,7 +72,7 @@ func (cl *Client) BlockdDisable(blockdName string) (blockd *Blockd, err error) { params.Set(paramNameName, blockdName) - _, resb, err = cl.PostForm(httpApiBlockdDisable, nil, params) + _, resb, err = cl.PostForm(httpAPIBlockdDisable, nil, params) if err != nil { return nil, fmt.Errorf("%s: %w", logp, err) } @@ -101,7 +101,7 @@ func (cl *Client) BlockdEnable(blockdName string) (blockd *Blockd, err error) { params.Set(paramNameName, blockdName) - _, resb, err = cl.PostForm(httpApiBlockdEnable, nil, params) + _, resb, err = cl.PostForm(httpAPIBlockdEnable, nil, params) if err != nil { return nil, fmt.Errorf("%s: %w", logp, err) } @@ -131,7 +131,7 @@ func (cl *Client) BlockdFetch(blockdName string) (blockd *Blockd, err error) { params.Set(paramNameName, blockdName) - _, resb, err = cl.PostForm(httpApiBlockdFetch, nil, params) + _, resb, err = cl.PostForm(httpAPIBlockdFetch, nil, params) if err != nil { return nil, fmt.Errorf("%s: %w", logp, err) } @@ -159,7 +159,7 @@ func (cl *Client) Caches() (answers []*dns.Answer, err error) { resb []byte ) - _, resb, err = cl.Get(httpApiCaches, nil, nil) + _, resb, err = cl.Get(httpAPICaches, nil, nil) if err != nil { return nil, fmt.Errorf("%s: %w", logp, err) } @@ -190,7 +190,7 @@ func (cl *Client) CachesRemove(q string) (listAnswer []*dns.Answer, err error) { params.Set(paramNameName, q) - _, resb, err = cl.Delete(httpApiCaches, nil, params) + _, resb, err = cl.Delete(httpAPICaches, nil, params) if err != nil { return nil, fmt.Errorf("%s: %w", logp, err) } @@ -222,7 +222,7 @@ func (cl *Client) CachesSearch(q string) (listMsg []*dns.Message, err error) { params.Set(paramNameQuery, q) - _, resb, err = cl.Get(httpApiCachesSearch, nil, params) + _, resb, err = cl.Get(httpAPICachesSearch, nil, params) if err != nil { return nil, fmt.Errorf("%s: %w", logp, err) } @@ -248,7 +248,7 @@ func (cl *Client) Env() (env *Environment, err error) { resb []byte ) - _, resb, err = cl.Get(httpApiEnvironment, nil, nil) + _, resb, err = cl.Get(httpAPIEnvironment, nil, nil) if err != nil { return nil, fmt.Errorf("%s: %w", logp, err) } @@ -273,7 +273,7 @@ func (cl *Client) EnvUpdate(envIn *Environment) (envOut *Environment, err error) resb []byte ) - _, resb, err = cl.PostJSON(httpApiEnvironment, nil, envIn) + _, resb, err = cl.PostJSON(httpAPIEnvironment, nil, envIn) if err != nil { return nil, fmt.Errorf("%s: %w", logp, err) } diff --git a/client_test.go b/client_test.go index 8d1d3d9..349b917 100644 --- a/client_test.go +++ b/client_test.go @@ -111,7 +111,7 @@ func TestClient_BlockdFetch(t *testing.T) { err = os.WriteFile(affectedBlockd.fileDisabled, []byte("127.0.0.1 a.block\n"), 0644) }) - expString = "BlockdFetch: 400 httpApiBlockdFetch: unknown hosts block.d name: xxx" + expString = `BlockdFetch: 400 httpAPIBlockdFetch: unknown hosts block.d name: xxx` gotBlockd, err = resc.BlockdFetch("xxx") if err != nil { diff --git a/cmd/resolver/main.go b/cmd/resolver/main.go index 848d679..33ae64d 100644 --- a/cmd/resolver/main.go +++ b/cmd/resolver/main.go @@ -50,7 +50,7 @@ func main() { flag.BoolVar(&rsol.insecure, "insecure", false, "Ignore invalid server certificate.") flag.StringVar(&rsol.nameserver, "ns", "", "Parent name server address using scheme based.") - flag.StringVar(&rsol.rescachedUrl, "server", defRescachedUrl, "Set the rescached HTTP server.") + flag.StringVar(&rsol.rescachedURL, `server`, defRescachedURL, `Set the rescached HTTP server.`) flag.Parse() diff --git a/cmd/resolver/resolver.go b/cmd/resolver/resolver.go index 9561089..4b5e59a 100644 --- a/cmd/resolver/resolver.go +++ b/cmd/resolver/resolver.go @@ -25,7 +25,7 @@ const ( defAttempts = 1 defQueryType = "A" defQueryClass = "IN" - defRescachedUrl = "http://127.0.0.1:5380" + defRescachedURL = `http://127.0.0.1:5380` defResolvConf = "/etc/resolv.conf" defTimeout = 5 * time.Second ) @@ -40,7 +40,7 @@ type resolver struct { sqclass string nameserver string - rescachedUrl string + rescachedURL string qtype dns.RecordType qclass dns.RecordClass @@ -197,28 +197,29 @@ func (rsol *resolver) doCmdEnv(args []string) { var ( resc = rsol.newRescachedClient() - env *rescached.Environment - subCmd string - envJson []byte - err error + err error ) if len(args) == 0 { + var env *rescached.Environment + env, err = resc.Env() if err != nil { log.Fatalf("resolver: %s: %s", rsol.cmd, err) } - envJson, err = json.MarshalIndent(env, "", " ") + var envJSON []byte + + envJSON, err = json.MarshalIndent(env, ``, ` `) if err != nil { log.Fatalf("resolver: %s: %s", rsol.cmd, err) } - fmt.Println(string(envJson)) + fmt.Println(string(envJSON)) return } - subCmd = strings.ToLower(args[0]) + var subCmd = strings.ToLower(args[0]) switch subCmd { case subCmdUpdate: args = args[1:] @@ -241,21 +242,20 @@ func (rsol *resolver) doCmdEnv(args []string) { // doCmdEnvUpdate update the server environment by reading the JSON formatted // environment from file or from stdin. func (rsol *resolver) doCmdEnvUpdate(resc *rescached.Client, fileOrStdin string) (err error) { - var ( - env *rescached.Environment - envJson []byte - ) + var envJSON []byte if fileOrStdin == "-" { - envJson, err = io.ReadAll(os.Stdin) + envJSON, err = io.ReadAll(os.Stdin) } else { - envJson, err = os.ReadFile(fileOrStdin) + envJSON, err = os.ReadFile(fileOrStdin) } if err != nil { return fmt.Errorf("%s %s: %w", cmdEnv, subCmdUpdate, err) } - err = json.Unmarshal(envJson, &env) + var env *rescached.Environment + + err = json.Unmarshal(envJSON, &env) if err != nil { return fmt.Errorf("%s %s: %w", cmdEnv, subCmdUpdate, err) } @@ -265,12 +265,12 @@ func (rsol *resolver) doCmdEnvUpdate(resc *rescached.Client, fileOrStdin string) return fmt.Errorf("%s %s: %w", cmdEnv, subCmdUpdate, err) } - envJson, err = json.MarshalIndent(env, "", " ") + envJSON, err = json.MarshalIndent(env, ``, ` `) if err != nil { return fmt.Errorf("%s %s: %w", cmdEnv, subCmdUpdate, err) } - fmt.Println(string(envJson)) + fmt.Println(string(envJSON)) return nil } @@ -766,10 +766,10 @@ func (rsol *resolver) initSystemResolver() (err error) { // newRescachedClient create new rescached Client. func (rsol *resolver) newRescachedClient() (resc *rescached.Client) { - if len(rsol.rescachedUrl) == 0 { - rsol.rescachedUrl = defRescachedUrl + if len(rsol.rescachedURL) == 0 { + rsol.rescachedURL = defRescachedURL } - resc = rescached.NewClient(rsol.rescachedUrl, rsol.insecure) + resc = rescached.NewClient(rsol.rescachedURL, rsol.insecure) return resc } diff --git a/environment.go b/environment.go index 1068ea9..1a0aef5 100644 --- a/environment.go +++ b/environment.go @@ -34,7 +34,7 @@ const ( keyDebug = "debug" keyFileResolvConf = "file.resolvconf" keyName = "name" - keyUrl = "url" + keyURL = `url` keyCachePruneDelay = "cache.prune_delay" keyCachePruneThreshold = "cache.prune_threshold" @@ -248,7 +248,7 @@ func (env *Environment) save(file string) (in *ini.Ini, err error) { for _, hb = range env.HostBlockd { in.Set(sectionNameBlockd, hb.Name, keyName, hb.Name) - in.Set(sectionNameBlockd, hb.Name, keyUrl, hb.URL) + in.Set(sectionNameBlockd, hb.Name, keyURL, hb.URL) } in.UnsetAll(sectionNameDNS, subNameServer, keyParent) @@ -27,15 +27,15 @@ const ( paramNameType = "type" paramNameValue = "value" - httpApiBlockd = "/api/block.d" - httpApiBlockdDisable = "/api/block.d/disable" - httpApiBlockdEnable = "/api/block.d/enable" - httpApiBlockdFetch = "/api/block.d/fetch" + httpAPIBlockd = `/api/block.d` + httpAPIBlockdDisable = `/api/block.d/disable` + httpAPIBlockdEnable = `/api/block.d/enable` + httpAPIBlockdFetch = `/api/block.d/fetch` - httpApiCaches = "/api/caches" - httpApiCachesSearch = "/api/caches/search" + httpAPICaches = `/api/caches` + httpAPICachesSearch = `/api/caches/search` - httpApiEnvironment = "/api/environment" + httpAPIEnvironment = `/api/environment` apiHostsd = "/api/hosts.d" apiHostsdRR = "/api/hosts.d/rr" @@ -63,10 +63,10 @@ func (srv *Server) httpdRegisterEndpoints() (err error) { err = srv.httpd.RegisterEndpoint(&libhttp.Endpoint{ Method: libhttp.RequestMethodGet, - Path: httpApiBlockd, + Path: httpAPIBlockd, RequestType: libhttp.RequestTypeNone, ResponseType: libhttp.ResponseTypeJSON, - Call: srv.httpApiBlockdList, + Call: srv.httpAPIBlockdList, }) if err != nil { return err @@ -74,10 +74,10 @@ func (srv *Server) httpdRegisterEndpoints() (err error) { err = srv.httpd.RegisterEndpoint(&libhttp.Endpoint{ Method: libhttp.RequestMethodPut, - Path: httpApiBlockd, + Path: httpAPIBlockd, RequestType: libhttp.RequestTypeJSON, ResponseType: libhttp.ResponseTypeJSON, - Call: srv.httpApiBlockdUpdate, + Call: srv.httpAPIBlockdUpdate, }) if err != nil { return err @@ -85,10 +85,10 @@ func (srv *Server) httpdRegisterEndpoints() (err error) { err = srv.httpd.RegisterEndpoint(&libhttp.Endpoint{ Method: libhttp.RequestMethodPost, - Path: httpApiBlockdDisable, + Path: httpAPIBlockdDisable, RequestType: libhttp.RequestTypeForm, ResponseType: libhttp.ResponseTypeJSON, - Call: srv.httpApiBlockdDisable, + Call: srv.httpAPIBlockdDisable, }) if err != nil { return err @@ -96,10 +96,10 @@ func (srv *Server) httpdRegisterEndpoints() (err error) { err = srv.httpd.RegisterEndpoint(&libhttp.Endpoint{ Method: libhttp.RequestMethodPost, - Path: httpApiBlockdEnable, + Path: httpAPIBlockdEnable, RequestType: libhttp.RequestTypeForm, ResponseType: libhttp.ResponseTypeJSON, - Call: srv.httpApiBlockdEnable, + Call: srv.httpAPIBlockdEnable, }) if err != nil { return err @@ -107,10 +107,10 @@ func (srv *Server) httpdRegisterEndpoints() (err error) { err = srv.httpd.RegisterEndpoint(&libhttp.Endpoint{ Method: libhttp.RequestMethodPost, - Path: httpApiBlockdFetch, + Path: httpAPIBlockdFetch, RequestType: libhttp.RequestTypeForm, ResponseType: libhttp.ResponseTypeJSON, - Call: srv.httpApiBlockdFetch, + Call: srv.httpAPIBlockdFetch, }) if err != nil { return err @@ -120,10 +120,10 @@ func (srv *Server) httpdRegisterEndpoints() (err error) { err = srv.httpd.RegisterEndpoint(&libhttp.Endpoint{ Method: libhttp.RequestMethodGet, - Path: httpApiCaches, + Path: httpAPICaches, RequestType: libhttp.RequestTypeQuery, ResponseType: libhttp.ResponseTypeJSON, - Call: srv.httpApiCaches, + Call: srv.httpAPICaches, }) if err != nil { return err @@ -131,10 +131,10 @@ func (srv *Server) httpdRegisterEndpoints() (err error) { err = srv.httpd.RegisterEndpoint(&libhttp.Endpoint{ Method: libhttp.RequestMethodDelete, - Path: httpApiCaches, + Path: httpAPICaches, RequestType: libhttp.RequestTypeQuery, ResponseType: libhttp.ResponseTypeJSON, - Call: srv.httpApiCachesDelete, + Call: srv.httpAPICachesDelete, }) if err != nil { return err @@ -142,10 +142,10 @@ func (srv *Server) httpdRegisterEndpoints() (err error) { err = srv.httpd.RegisterEndpoint(&libhttp.Endpoint{ Method: libhttp.RequestMethodGet, - Path: httpApiCachesSearch, + Path: httpAPICachesSearch, RequestType: libhttp.RequestTypeQuery, ResponseType: libhttp.ResponseTypeJSON, - Call: srv.httpApiCachesSearch, + Call: srv.httpAPICachesSearch, }) if err != nil { return err @@ -155,10 +155,10 @@ func (srv *Server) httpdRegisterEndpoints() (err error) { err = srv.httpd.RegisterEndpoint(&libhttp.Endpoint{ Method: libhttp.RequestMethodGet, - Path: httpApiEnvironment, + Path: httpAPIEnvironment, RequestType: libhttp.RequestTypeJSON, ResponseType: libhttp.ResponseTypeJSON, - Call: srv.httpApiEnvironmentGet, + Call: srv.httpAPIEnvironmentGet, }) if err != nil { return err @@ -166,10 +166,10 @@ func (srv *Server) httpdRegisterEndpoints() (err error) { err = srv.httpd.RegisterEndpoint(&libhttp.Endpoint{ Method: libhttp.RequestMethodPost, - Path: httpApiEnvironment, + Path: httpAPIEnvironment, RequestType: libhttp.RequestTypeJSON, ResponseType: libhttp.ResponseTypeJSON, - Call: srv.httpApiEnvironmentUpdate, + Call: srv.httpAPIEnvironmentUpdate, }) if err != nil { return err @@ -315,7 +315,7 @@ func (srv *Server) httpdRun() { } } -// httpApiBlockdList fetch the list of block.d files. +// httpAPIBlockdList fetch the list of block.d files. // // # Request // @@ -331,10 +331,8 @@ func (srv *Server) httpdRun() { // ... // } // } -func (srv *Server) httpApiBlockdList(_ *libhttp.EndpointRequest) (resBody []byte, err error) { - var ( - res = libhttp.EndpointResponse{} - ) +func (srv *Server) httpAPIBlockdList(_ *libhttp.EndpointRequest) (resBody []byte, err error) { + var res = libhttp.EndpointResponse{} res.Code = http.StatusOK res.Data = srv.env.HostBlockd @@ -343,7 +341,7 @@ func (srv *Server) httpApiBlockdList(_ *libhttp.EndpointRequest) (resBody []byte return resBody, err } -// httpApiBlockdDisable disable the hosts block.d. +// httpAPIBlockdDisable disable the hosts block.d. // // # Request // @@ -359,7 +357,7 @@ func (srv *Server) httpApiBlockdList(_ *libhttp.EndpointRequest) (resBody []byte // { // "data": <Blockd> // } -func (srv *Server) httpApiBlockdDisable(epr *libhttp.EndpointRequest) (resBody []byte, err error) { +func (srv *Server) httpAPIBlockdDisable(epr *libhttp.EndpointRequest) (resBody []byte, err error) { var ( res = libhttp.EndpointResponse{} @@ -392,7 +390,7 @@ func (srv *Server) httpApiBlockdDisable(epr *libhttp.EndpointRequest) (resBody [ return json.Marshal(&res) } -// httpApiBlockdEnable enable the hosts block.d. +// httpAPIBlockdEnable enable the hosts block.d. // // # Request // @@ -408,7 +406,7 @@ func (srv *Server) httpApiBlockdDisable(epr *libhttp.EndpointRequest) (resBody [ // { // "data": <Blockd> // } -func (srv *Server) httpApiBlockdEnable(epr *libhttp.EndpointRequest) (resBody []byte, err error) { +func (srv *Server) httpAPIBlockdEnable(epr *libhttp.EndpointRequest) (resBody []byte, err error) { var ( res = libhttp.EndpointResponse{} @@ -441,7 +439,7 @@ func (srv *Server) httpApiBlockdEnable(epr *libhttp.EndpointRequest) (resBody [] return json.Marshal(&res) } -// httpApiBlockdFetch fetch the latest hosts file from the block.d provider +// httpAPIBlockdFetch fetch the latest hosts file from the block.d provider // based on registered URL. // // # Request @@ -455,9 +453,9 @@ func (srv *Server) httpApiBlockdEnable(epr *libhttp.EndpointRequest) (resBody [] // // On success, the hosts file will be updated and the server will be // restarted. -func (srv *Server) httpApiBlockdFetch(epr *libhttp.EndpointRequest) (resBody []byte, err error) { +func (srv *Server) httpAPIBlockdFetch(epr *libhttp.EndpointRequest) (resBody []byte, err error) { var ( - logp = "httpApiBlockdFetch" + logp = `httpAPIBlockdFetch` res = libhttp.EndpointResponse{} hb *Blockd @@ -495,7 +493,7 @@ func (srv *Server) httpApiBlockdFetch(epr *libhttp.EndpointRequest) (resBody []b return json.Marshal(&res) } -func (srv *Server) httpApiCaches(_ *libhttp.EndpointRequest) (resBody []byte, err error) { +func (srv *Server) httpAPICaches(_ *libhttp.EndpointRequest) (resBody []byte, err error) { var ( res = libhttp.EndpointResponse{} answers = srv.dns.Caches.ExternalLRU() @@ -509,7 +507,7 @@ func (srv *Server) httpApiCaches(_ *libhttp.EndpointRequest) (resBody []byte, er return json.Marshal(&res) } -func (srv *Server) httpApiCachesSearch(epr *libhttp.EndpointRequest) (resBody []byte, err error) { +func (srv *Server) httpAPICachesSearch(epr *libhttp.EndpointRequest) (resBody []byte, err error) { var ( res = libhttp.EndpointResponse{} q = epr.HttpRequest.Form.Get(paramNameQuery) @@ -542,7 +540,7 @@ func (srv *Server) httpApiCachesSearch(epr *libhttp.EndpointRequest) (resBody [] return json.Marshal(&res) } -func (srv *Server) httpApiCachesDelete(epr *libhttp.EndpointRequest) (resBody []byte, err error) { +func (srv *Server) httpAPICachesDelete(epr *libhttp.EndpointRequest) (resBody []byte, err error) { var ( res = libhttp.EndpointResponse{} q = epr.HttpRequest.Form.Get(paramNameName) @@ -567,7 +565,7 @@ func (srv *Server) httpApiCachesDelete(epr *libhttp.EndpointRequest) (resBody [] return json.Marshal(&res) } -// httpApiEnvironmentGet get the current Environment. +// httpAPIEnvironmentGet get the current Environment. // // # Request // @@ -580,7 +578,7 @@ func (srv *Server) httpApiCachesDelete(epr *libhttp.EndpointRequest) (resBody [] // { // "data": <Environment> // } -func (srv *Server) httpApiEnvironmentGet(_ *libhttp.EndpointRequest) (resBody []byte, err error) { +func (srv *Server) httpAPIEnvironmentGet(_ *libhttp.EndpointRequest) (resBody []byte, err error) { var ( res = libhttp.EndpointResponse{} ) @@ -591,7 +589,7 @@ func (srv *Server) httpApiEnvironmentGet(_ *libhttp.EndpointRequest) (resBody [] return json.Marshal(&res) } -// httpApiEnvironmentUpdate update the environment and restart the service. +// httpAPIEnvironmentUpdate update the environment and restart the service. // // # Request // @@ -611,7 +609,7 @@ func (srv *Server) httpApiEnvironmentGet(_ *libhttp.EndpointRequest) (resBody [] // { // "data": <Environment> // } -func (srv *Server) httpApiEnvironmentUpdate(epr *libhttp.EndpointRequest) (resBody []byte, err error) { +func (srv *Server) httpAPIEnvironmentUpdate(epr *libhttp.EndpointRequest) (resBody []byte, err error) { var ( res = libhttp.EndpointResponse{} newEnv = new(Environment) @@ -659,7 +657,7 @@ func (srv *Server) httpApiEnvironmentUpdate(epr *libhttp.EndpointRequest) (resBo return json.Marshal(&res) } -// httpApiBlockdUpdate set the HostsBlock to be enabled or disabled. +// httpAPIBlockdUpdate set the HostsBlock to be enabled or disabled. // // If its status changes to enabled, unhide the hosts block file, populate the // hosts back to caches, and add it to list of hostBlockdFile. @@ -689,9 +687,9 @@ func (srv *Server) httpApiEnvironmentUpdate(epr *libhttp.EndpointRequest) (resBo // ... // } // } -func (srv *Server) httpApiBlockdUpdate(epr *libhttp.EndpointRequest) (resBody []byte, err error) { +func (srv *Server) httpAPIBlockdUpdate(epr *libhttp.EndpointRequest) (resBody []byte, err error) { var ( - logp = "httpApiBlockdUpdate" + logp = `httpAPIBlockdUpdate` res = libhttp.EndpointResponse{} hostBlockd = make(map[string]*Blockd, 0) |
