main.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package main
  2. import (
  3. "errors"
  4. "io/ioutil"
  5. "log"
  6. "net"
  7. "net/http"
  8. "os"
  9. "strings"
  10. )
  11. var (
  12. lists = []string{
  13. "https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts",
  14. "https://mirror1.malwaredomains.com/files/justdomains",
  15. "http://sysctl.org/cameleon/hosts",
  16. "https://zeustracker.abuse.ch/blocklist.php?download=domainblocklist",
  17. "https://s3.amazonaws.com/lists.disconnect.me/simple_tracking.txt",
  18. "https://s3.amazonaws.com/lists.disconnect.me/simple_ad.txt",
  19. "https://hosts-file.net/ad_servers.txt",
  20. }
  21. invalid = []string{
  22. "localhost", "localhost.localdomain", "local", "broadcasthost",
  23. "ip6-localhost", "ip6-loopback", "ip6-localnet",
  24. "ip6-mcastprefix", "ip6-allnodes", "ip6-allrouters",
  25. "ip6-allhosts",
  26. }
  27. zone_file = "blacklist.txt"
  28. )
  29. func main() {
  30. // Open the zone file, and create it if needed
  31. f, err := os.OpenFile(zone_file, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
  32. if err != nil {
  33. log.Fatal(err)
  34. }
  35. defer f.Close()
  36. // Override the current content
  37. if err := f.Truncate(0); err != nil {
  38. log.Fatal(err)
  39. }
  40. // Fetch and parse the zone content from lists
  41. c := make(chan string)
  42. for _, list := range lists {
  43. go handle(list, c)
  44. }
  45. // Write the zones
  46. for range lists {
  47. if _, err = f.WriteString(<-c); err != nil {
  48. log.Fatal(err)
  49. }
  50. }
  51. }
  52. func handle(url string, zone chan string) {
  53. body, err := fetch(url)
  54. if err != nil {
  55. return
  56. }
  57. zone <- sanitize(body)
  58. }
  59. func fetch(url string) (string, error) {
  60. req, err := http.Get(url)
  61. if err != nil {
  62. return "", err
  63. }
  64. defer req.Body.Close()
  65. body, err := ioutil.ReadAll(req.Body)
  66. if err != nil {
  67. return "", err
  68. }
  69. return string(body), nil
  70. }
  71. func sanitize(records string) string {
  72. var sanitized []string
  73. for _, line := range strings.Split(records, "\n") {
  74. ip, domain, err := parse_record(line)
  75. if err != nil {
  76. continue
  77. }
  78. // Domain is invalid (e.g., "localhost")
  79. if is_invalid(domain) {
  80. continue
  81. }
  82. // Domain is an IP address
  83. if net.ParseIP(domain) != nil {
  84. continue
  85. }
  86. // Unify the records
  87. if ip != "127.0.0.1" {
  88. ip = "127.0.0.1"
  89. }
  90. sanitized = append(sanitized, ip + " " + domain)
  91. }
  92. return strings.Join(sanitized, "\n")
  93. }
  94. func parse_record(record string) (string, string, error) {
  95. if len(record) == 0 {
  96. return "", "", errors.New("Empty line")
  97. }
  98. if strings.HasPrefix(record, "#") {
  99. return "", "", errors.New("Comment")
  100. }
  101. words := strings.Fields(record)
  102. // Domain-only list
  103. if len(words) == 1 {
  104. return "", words[0], nil
  105. }
  106. return words[0], words[1], nil
  107. }
  108. func is_invalid(domain string) bool {
  109. for _, i := range invalid {
  110. if domain == i {
  111. return true
  112. }
  113. }
  114. return false
  115. }