diff options
| -rw-r--r-- | blockd.go (renamed from hosts_block.go) | 14 | ||||
| -rw-r--r-- | blockd_test.go (renamed from hosts_block_test.go) | 18 | ||||
| -rw-r--r-- | client.go | 6 | ||||
| -rw-r--r-- | environment.go | 16 | ||||
| -rw-r--r-- | environment_test.go | 10 | ||||
| -rw-r--r-- | httpd.go | 50 | ||||
| -rw-r--r-- | rescached.go | 6 |
7 files changed, 60 insertions, 60 deletions
diff --git a/hosts_block.go b/blockd.go index dc65464..398bb96 100644 --- a/hosts_block.go +++ b/blockd.go @@ -18,7 +18,7 @@ const ( lastUpdatedFormat = "2006-01-02 15:04:05 MST" ) -type hostsBlock struct { +type Blockd struct { lastUpdated time.Time Name string `ini:"::name"` // Derived from hostname in URL. @@ -33,7 +33,7 @@ type hostsBlock struct { } // disable the hosts block by prefixing the file name with single dot. -func (hb *hostsBlock) disable() (err error) { +func (hb *Blockd) disable() (err error) { err = os.Rename(hb.file, hb.fileDisabled) if err != nil { return fmt.Errorf("disable: %w", err) @@ -43,7 +43,7 @@ func (hb *hostsBlock) disable() (err error) { } // enable the hosts block file by removing the dot prefix from file name. -func (hb *hostsBlock) enable() (err error) { +func (hb *Blockd) enable() (err error) { if hb.isFileExist { err = os.Rename(hb.fileDisabled, hb.file) } else { @@ -57,7 +57,7 @@ func (hb *hostsBlock) enable() (err error) { return nil } -func (hb *hostsBlock) init(pathDirBlock string) { +func (hb *Blockd) init(pathDirBlock string) { var ( fi os.FileInfo err error @@ -87,20 +87,20 @@ func (hb *hostsBlock) init(pathDirBlock string) { // isOld will return true if the host file has not been updated since seven // days. -func (hb *hostsBlock) isOld() bool { +func (hb *Blockd) isOld() bool { oneWeek := 7 * 24 * time.Hour lastWeek := time.Now().Add(-1 * oneWeek) return hb.lastUpdated.Before(lastWeek) } -func (hb *hostsBlock) update() (err error) { +func (hb *Blockd) update() (err error) { if !hb.isOld() { return nil } var ( - logp = "hostsBlock.update" + logp = "Blockd.update" res *http.Response body []byte diff --git a/hosts_block_test.go b/blockd_test.go index 18652fc..f1d6abf 100644 --- a/hosts_block_test.go +++ b/blockd_test.go @@ -11,11 +11,11 @@ import ( "github.com/shuLhan/share/lib/test" ) -func TestHostsBlock_init(t *testing.T) { +func TestBlockd_init(t *testing.T) { type testCase struct { desc string - hb hostsBlock - exp hostsBlock + hb Blockd + exp Blockd } var ( @@ -59,10 +59,10 @@ func TestHostsBlock_init(t *testing.T) { cases = []testCase{{ desc: "With hosts block file enabled", - hb: hostsBlock{ + hb: Blockd{ Name: fileEnabled, }, - exp: hostsBlock{ + exp: Blockd{ Name: fileEnabled, lastUpdated: fiEnabled.ModTime(), LastUpdated: fiEnabled.ModTime().Format(lastUpdatedFormat), @@ -73,10 +73,10 @@ func TestHostsBlock_init(t *testing.T) { }, }, { desc: "With hosts block file disabled", - hb: hostsBlock{ + hb: Blockd{ Name: fileDisabled, }, - exp: hostsBlock{ + exp: Blockd{ Name: fileDisabled, lastUpdated: fiDisabled.ModTime(), LastUpdated: fiDisabled.ModTime().Format(lastUpdatedFormat), @@ -86,10 +86,10 @@ func TestHostsBlock_init(t *testing.T) { }, }, { desc: "With hosts block file not exist", - hb: hostsBlock{ + hb: Blockd{ Name: fileNotExist, }, - exp: hostsBlock{ + exp: Blockd{ Name: fileNotExist, file: filepath.Join(pathDirBlock, fileNotExist), fileDisabled: filepath.Join(pathDirBlock, "."+fileNotExist), @@ -40,7 +40,7 @@ func (cl *Client) BlockdDisable(blockdName string) (an interface{}, err error) { res = libhttp.EndpointResponse{} params = url.Values{} - hb *hostsBlock + hb *Blockd resb []byte ) @@ -70,7 +70,7 @@ func (cl *Client) BlockdEnable(blockdName string) (an interface{}, err error) { res = libhttp.EndpointResponse{} params = url.Values{} - hb *hostsBlock + hb *Blockd resb []byte ) @@ -101,7 +101,7 @@ func (cl *Client) BlockdUpdate(blockdName string) (an interface{}, err error) { params = url.Values{} res = libhttp.EndpointResponse{} - hb *hostsBlock + hb *Blockd resb []byte ) diff --git a/environment.go b/environment.go index b5c226c..1dcd2c1 100644 --- a/environment.go +++ b/environment.go @@ -76,8 +76,8 @@ type Environment struct { FileResolvConf string `ini:"rescached::file.resolvconf"` WUIListen string `ini:"rescached::wui.listen"` - HostsBlocks map[string]*hostsBlock `ini:"block.d"` - hostsBlocksFile map[string]*dns.HostsFile + HostBlockd map[string]*Blockd `ini:"block.d"` + hostBlockdFile map[string]*dns.HostsFile // The options for WUI HTTP server. HttpdOptions *libhttp.ServerOptions `json:"-"` @@ -126,8 +126,8 @@ func newEnvironment(dirBase, fileConfig string) *Environment { pathDirZone: filepath.Join(dirBase, dirZone), pathFileCaches: filepath.Join(dirBase, dirCaches, fileCaches), - fileConfig: filepath.Join(dirBase, fileConfig), - hostsBlocksFile: make(map[string]*dns.HostsFile), + fileConfig: filepath.Join(dirBase, fileConfig), + hostBlockdFile: make(map[string]*dns.HostsFile), HttpdOptions: &libhttp.ServerOptions{ Memfs: mfsWww, Address: defWuiAddress, @@ -190,9 +190,9 @@ func (env *Environment) init() (err error) { func (env *Environment) initHostsBlock() { var ( - hb *hostsBlock + hb *Blockd ) - for _, hb = range env.HostsBlocks { + for _, hb = range env.HostBlockd { hb.init(env.pathDirBlock) } } @@ -230,7 +230,7 @@ func (env *Environment) save(file string) (in *ini.Ini, err error) { var ( logp = "save" - hb *hostsBlock + hb *Blockd vstr string ) @@ -247,7 +247,7 @@ func (env *Environment) save(file string) (in *ini.Ini, err error) { in.Set(sectionNameRescached, "", keyDebug, strconv.Itoa(env.Debug)) in.Set(sectionNameRescached, "", keyWUIListen, strings.TrimSpace(env.WUIListen)) - for _, hb = range env.HostsBlocks { + for _, hb = range env.HostBlockd { in.Set(sectionNameBlockd, hb.Name, keyName, hb.Name) in.Set(sectionNameBlockd, hb.Name, keyUrl, hb.URL) } diff --git a/environment_test.go b/environment_test.go index 4c3393e..99d0950 100644 --- a/environment_test.go +++ b/environment_test.go @@ -73,16 +73,16 @@ func TestLoadEnvironment(t *testing.T) { fileConfig: filepath.Join(testDirBase, "/etc/rescached/rescached.cfg"), WUIListen: "127.0.0.1:5381", - HostsBlocks: map[string]*hostsBlock{ - "pgl.yoyo.org": &hostsBlock{ + HostBlockd: map[string]*Blockd{ + "pgl.yoyo.org": &Blockd{ Name: "pgl.yoyo.org", URL: `http://pgl.yoyo.org/adservers/serverlist.php?hostformat=hosts&showintro=0&startdate[day]=&startdate[month]=&startdate[year]=&mimetype=plaintext`, }, - "winhelp2002.mvps.org": &hostsBlock{ + "winhelp2002.mvps.org": &Blockd{ Name: "winhelp2002.mvps.org", URL: `http://winhelp2002.mvps.org/hosts.txt`, }, - "someonewhocares.org": &hostsBlock{ + "someonewhocares.org": &Blockd{ Name: "someonewhocares.org", URL: `http://someonewhocares.org/hosts/hosts`, }, @@ -120,7 +120,7 @@ func TestLoadEnvironment(t *testing.T) { test.Assert(t, "LoadEnvironment", expEnv, gotEnv) - gotEnv.HostsBlocks["test"] = &hostsBlock{ + gotEnv.HostBlockd["test"] = &Blockd{ Name: "test", URL: "http://someurl", } @@ -306,13 +306,13 @@ func (srv *Server) httpApiBlockdDisable(epr *libhttp.EndpointRequest) (resBody [ var ( res = libhttp.EndpointResponse{} - hb *hostsBlock + hb *Blockd hbName string ) hbName = strings.ToLower(epr.HttpRequest.Form.Get(paramNameName)) - hb = srv.env.HostsBlocks[hbName] + hb = srv.env.HostBlockd[hbName] if hb == nil { res.Code = http.StatusBadRequest res.Message = fmt.Sprintf("hosts block.d name not found: %s", hbName) @@ -340,13 +340,13 @@ func (srv *Server) httpApiBlockdEnable(epr *libhttp.EndpointRequest) (resBody [] var ( res = libhttp.EndpointResponse{} - hb *hostsBlock + hb *Blockd hbName string ) hbName = strings.ToLower(epr.HttpRequest.Form.Get(paramNameName)) - hb = srv.env.HostsBlocks[hbName] + hb = srv.env.HostBlockd[hbName] if hb == nil { res.Code = http.StatusBadRequest res.Message = fmt.Sprintf("hosts block.d name not found: %s", hbName) @@ -388,13 +388,13 @@ func (srv *Server) httpApiBlockdUpdate(epr *libhttp.EndpointRequest) (resBody [] logp = "httpApiBlockdUpdate" res = libhttp.EndpointResponse{} - hb *hostsBlock + hb *Blockd hbName string ) hbName = strings.ToLower(epr.HttpRequest.Form.Get(paramNameName)) - hb = srv.env.HostsBlocks[hbName] + hb = srv.env.HostBlockd[hbName] if hb == nil { res.Code = http.StatusBadRequest res.Message = fmt.Sprintf("%s: unknown hosts block.d name: %s", logp, hbName) @@ -562,20 +562,20 @@ func (srv *Server) httpApiEnvironmentUpdate(epr *libhttp.EndpointRequest) (resBo // apiHostsBlockUpdate 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 hostsBlocksFile. +// hosts back to caches, and add it to list of hostBlockdFile. // // If its status changes to disabled, remove the hosts from caches, hide it, -// and remove it from list of hostsBlocksFile. +// and remove it from list of hostBlockdFile. func (srv *Server) apiHostsBlockUpdate(epr *libhttp.EndpointRequest) (resBody []byte, err error) { var ( - res = libhttp.EndpointResponse{} - hostsBlocks = make(map[string]*hostsBlock, 0) + res = libhttp.EndpointResponse{} + hostBlockd = make(map[string]*Blockd, 0) - hbx *hostsBlock - hby *hostsBlock + hbx *Blockd + hby *Blockd ) - err = json.Unmarshal(epr.RequestBody, &hostsBlocks) + err = json.Unmarshal(epr.RequestBody, &hostBlockd) if err != nil { res.Code = http.StatusBadRequest res.Message = err.Error() @@ -584,8 +584,8 @@ func (srv *Server) apiHostsBlockUpdate(epr *libhttp.EndpointRequest) (resBody [] res.Code = http.StatusInternalServerError - for _, hbx = range hostsBlocks { - for _, hby = range srv.env.HostsBlocks { + for _, hbx = range hostBlockd { + for _, hby = range srv.env.HostBlockd { if hbx.Name != hby.Name { continue } @@ -594,13 +594,13 @@ func (srv *Server) apiHostsBlockUpdate(epr *libhttp.EndpointRequest) (resBody [] } if hbx.IsEnabled { - err = srv.hostsBlockEnable(hby) + err = srv.blockdEnable(hby) if err != nil { res.Message = err.Error() return nil, &res } } else { - err = srv.hostsBlockDisable(hby) + err = srv.blockdDisable(hby) if err != nil { res.Message = err.Error() return nil, &res @@ -618,14 +618,14 @@ func (srv *Server) apiHostsBlockUpdate(epr *libhttp.EndpointRequest) (resBody [] } res.Code = http.StatusOK - res.Data = hostsBlocks + res.Data = hostBlockd return json.Marshal(&res) } -func (srv *Server) hostsBlockEnable(hb *hostsBlock) (err error) { +func (srv *Server) blockdEnable(hb *Blockd) (err error) { var ( - logp = "hostsBlockEnable" + logp = "blockdEnable" hfile *dns.HostsFile ) @@ -650,19 +650,19 @@ func (srv *Server) hostsBlockEnable(hb *hostsBlock) (err error) { return fmt.Errorf("%s: %w", logp, err) } - srv.env.hostsBlocksFile[hfile.Name] = hfile + srv.env.hostBlockdFile[hfile.Name] = hfile return nil } -func (srv *Server) hostsBlockDisable(hb *hostsBlock) (err error) { +func (srv *Server) blockdDisable(hb *Blockd) (err error) { var ( - logp = "hostsBlockDisable" + logp = "blockdDisable" hfile *dns.HostsFile ) - hfile = srv.env.hostsBlocksFile[hb.Name] + hfile = srv.env.hostBlockdFile[hb.Name] if hfile == nil { return fmt.Errorf("%s: unknown hosts block: %q", logp, hb.Name) } @@ -674,7 +674,7 @@ func (srv *Server) hostsBlockDisable(hb *hostsBlock) (err error) { return fmt.Errorf("%s: %w", logp, err) } - delete(srv.env.hostsBlocksFile, hfile.Name) + delete(srv.env.hostBlockdFile, hfile.Name) return nil } diff --git a/rescached.go b/rescached.go index 694f6b6..f8caf05 100644 --- a/rescached.go +++ b/rescached.go @@ -63,7 +63,7 @@ func (srv *Server) Start() (err error) { logp = "Start" fcaches *os.File - hb *hostsBlock + hb *Blockd hfile *dns.HostsFile zone *dns.Zone answers []*dns.Answer @@ -99,7 +99,7 @@ func (srv *Server) Start() (err error) { return err } - for _, hb = range srv.env.HostsBlocks { + for _, hb = range srv.env.HostBlockd { if !hb.IsEnabled { continue } @@ -114,7 +114,7 @@ func (srv *Server) Start() (err error) { return fmt.Errorf("%s: %w", logp, err) } - srv.env.hostsBlocksFile[hfile.Name] = hfile + srv.env.hostBlockdFile[hfile.Name] = hfile } srv.env.HostsFiles, err = dns.LoadHostsDir(srv.env.pathDirHosts) |
