From ca66000dc47ebd2e1bdb922f7b8970dc343af134 Mon Sep 17 00:00:00 2001 From: Shulhan Date: Sun, 26 Jul 2020 22:34:36 +0700 Subject: all: rename hostsblock to hosts_block --- hosts_block.go | 137 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ hostsblock.go | 137 --------------------------------------------------------- 2 files changed, 137 insertions(+), 137 deletions(-) create mode 100644 hosts_block.go delete mode 100644 hostsblock.go diff --git a/hosts_block.go b/hosts_block.go new file mode 100644 index 0000000..b879d05 --- /dev/null +++ b/hosts_block.go @@ -0,0 +1,137 @@ +// Copyright 2020, Shulhan . All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package rescached + +import ( + "bytes" + "fmt" + "io/ioutil" + "log" + "net/http" + "os" + "path/filepath" + "time" +) + +// +// List of blocked hosts sources. +// +var hostsBlockSources = []*hostsBlock{{ + Name: "pgl.yoyo.org", + URL: `http://pgl.yoyo.org/adservers/serverlist.php?hostformat=hosts&showintro=0&startdate[day]=&startdate[month]=&startdate[year]=&mimetype=plaintext`, +}, { + Name: "www.malwaredomainlist.com", + URL: `http://www.malwaredomainlist.com/hostslist/hosts.txt`, +}, { + Name: "winhelp2002.mvps.org", + URL: `http://winhelp2002.mvps.org/hosts.txt`, +}, { + Name: "someonewhocares.org", + URL: `http://someonewhocares.org/hosts/hosts`, +}} + +type hostsBlock struct { + Name string // Derived from hostname in URL. + URL string + LastUpdated string + IsEnabled bool + lastUpdated time.Time + file string +} + +func (hb *hostsBlock) init(sources []string) { + for _, src := range sources { + if hb.URL == src { + hb.IsEnabled = true + break + } + } + + hb.initLastUpdated() +} + +func (hb *hostsBlock) update() (err error) { + if !hb.isOld() { + return nil + } + + fmt.Printf("hostsBlock %s: updating ...\n", hb.Name) + + res, err := http.Get(hb.URL) + if err != nil { + return fmt.Errorf("hostsBlock.update %q: %w", hb.Name, err) + } + defer func() { + err := res.Body.Close() + if err != nil { + log.Printf("hostsBlock.update %q: %s", hb.Name, err) + } + }() + + body, err := ioutil.ReadAll(res.Body) + if err != nil { + return fmt.Errorf("hostsBlock.update %q: %w", hb.Name, err) + } + + body = bytes.ReplaceAll(body, []byte("\r\n"), []byte("\n")) + + err = ioutil.WriteFile(hb.file, body, 0644) + if err != nil { + return fmt.Errorf("hostsBlock.update %q: %w", hb.Name, err) + } + + hb.initLastUpdated() + + return nil +} + +func (hb *hostsBlock) hide() (err error) { + oldFileName := filepath.Join(dirHosts, hb.Name) + newFileName := filepath.Join(dirHosts, "."+hb.Name) + err = os.Rename(oldFileName, newFileName) + if err != nil { + return err + } + + hb.file = newFileName + + return nil +} + +func (hb *hostsBlock) isOld() bool { + oneWeek := 7 * 24 * time.Hour + lastWeek := time.Now().Add(-1 * oneWeek) + + return hb.lastUpdated.Before(lastWeek) +} + +// +// unhide the hosts block file. +// +func (hb *hostsBlock) unhide() (err error) { + oldFileName := filepath.Join(dirHosts, "."+hb.Name) + newFileName := filepath.Join(dirHosts, hb.Name) + err = os.Rename(oldFileName, newFileName) + if err != nil { + return err + } + + hb.file = newFileName + hb.initLastUpdated() + + return nil +} + +func (hb *hostsBlock) initLastUpdated() { + hb.file = filepath.Join(dirHosts, hb.Name) + fi, err := os.Stat(hb.file) + if err != nil { + hb.IsEnabled = false + return + } + + hb.lastUpdated = fi.ModTime() + hb.LastUpdated = hb.lastUpdated.Format("2006-01-02 15:04:05 MST") +} diff --git a/hostsblock.go b/hostsblock.go deleted file mode 100644 index b879d05..0000000 --- a/hostsblock.go +++ /dev/null @@ -1,137 +0,0 @@ -// Copyright 2020, Shulhan . All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package rescached - -import ( - "bytes" - "fmt" - "io/ioutil" - "log" - "net/http" - "os" - "path/filepath" - "time" -) - -// -// List of blocked hosts sources. -// -var hostsBlockSources = []*hostsBlock{{ - Name: "pgl.yoyo.org", - URL: `http://pgl.yoyo.org/adservers/serverlist.php?hostformat=hosts&showintro=0&startdate[day]=&startdate[month]=&startdate[year]=&mimetype=plaintext`, -}, { - Name: "www.malwaredomainlist.com", - URL: `http://www.malwaredomainlist.com/hostslist/hosts.txt`, -}, { - Name: "winhelp2002.mvps.org", - URL: `http://winhelp2002.mvps.org/hosts.txt`, -}, { - Name: "someonewhocares.org", - URL: `http://someonewhocares.org/hosts/hosts`, -}} - -type hostsBlock struct { - Name string // Derived from hostname in URL. - URL string - LastUpdated string - IsEnabled bool - lastUpdated time.Time - file string -} - -func (hb *hostsBlock) init(sources []string) { - for _, src := range sources { - if hb.URL == src { - hb.IsEnabled = true - break - } - } - - hb.initLastUpdated() -} - -func (hb *hostsBlock) update() (err error) { - if !hb.isOld() { - return nil - } - - fmt.Printf("hostsBlock %s: updating ...\n", hb.Name) - - res, err := http.Get(hb.URL) - if err != nil { - return fmt.Errorf("hostsBlock.update %q: %w", hb.Name, err) - } - defer func() { - err := res.Body.Close() - if err != nil { - log.Printf("hostsBlock.update %q: %s", hb.Name, err) - } - }() - - body, err := ioutil.ReadAll(res.Body) - if err != nil { - return fmt.Errorf("hostsBlock.update %q: %w", hb.Name, err) - } - - body = bytes.ReplaceAll(body, []byte("\r\n"), []byte("\n")) - - err = ioutil.WriteFile(hb.file, body, 0644) - if err != nil { - return fmt.Errorf("hostsBlock.update %q: %w", hb.Name, err) - } - - hb.initLastUpdated() - - return nil -} - -func (hb *hostsBlock) hide() (err error) { - oldFileName := filepath.Join(dirHosts, hb.Name) - newFileName := filepath.Join(dirHosts, "."+hb.Name) - err = os.Rename(oldFileName, newFileName) - if err != nil { - return err - } - - hb.file = newFileName - - return nil -} - -func (hb *hostsBlock) isOld() bool { - oneWeek := 7 * 24 * time.Hour - lastWeek := time.Now().Add(-1 * oneWeek) - - return hb.lastUpdated.Before(lastWeek) -} - -// -// unhide the hosts block file. -// -func (hb *hostsBlock) unhide() (err error) { - oldFileName := filepath.Join(dirHosts, "."+hb.Name) - newFileName := filepath.Join(dirHosts, hb.Name) - err = os.Rename(oldFileName, newFileName) - if err != nil { - return err - } - - hb.file = newFileName - hb.initLastUpdated() - - return nil -} - -func (hb *hostsBlock) initLastUpdated() { - hb.file = filepath.Join(dirHosts, hb.Name) - fi, err := os.Stat(hb.file) - if err != nil { - hb.IsEnabled = false - return - } - - hb.lastUpdated = fi.ModTime() - hb.LastUpdated = hb.lastUpdated.Format("2006-01-02 15:04:05 MST") -} -- cgit v1.3