main.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package main
  2. import (
  3. "errors"
  4. "fmt"
  5. "io/ioutil"
  6. "log"
  7. "net"
  8. "net/http"
  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. http.HandleFunc("/", handler)
  31. log.Fatal(http.ListenAndServe(":8080", nil))
  32. }
  33. func handler(w http.ResponseWriter, r *http.Request) {
  34. list, err := generateList()
  35. if err != nil {
  36. // Use a non-HTTP error so that the consumer do not use a
  37. // malformed list.
  38. log.Print(err.Error())
  39. return
  40. }
  41. fmt.Fprint(w, list)
  42. }
  43. func generateList() (string, error) {
  44. c := make(chan string)
  45. for _, list := range lists {
  46. go handle(list, c)
  47. }
  48. var zones []string
  49. for range lists {
  50. zones = append(zones, <-c)
  51. }
  52. return strings.Join(zones, "\n"), nil
  53. }
  54. func handle(url string, zone chan string) {
  55. body, err := fetch(url)
  56. if err != nil {
  57. return
  58. }
  59. zone <- sanitize(body)
  60. }
  61. func fetch(url string) (string, error) {
  62. req, err := http.Get(url)
  63. if err != nil {
  64. return "", err
  65. }
  66. defer req.Body.Close()
  67. body, err := ioutil.ReadAll(req.Body)
  68. if err != nil {
  69. return "", err
  70. }
  71. return string(body), nil
  72. }
  73. func sanitize(records string) string {
  74. var sanitized []string
  75. for _, line := range strings.Split(records, "\n") {
  76. ip, domain, err := parse_record(line)
  77. if err != nil {
  78. continue
  79. }
  80. // Domain is invalid (e.g., "localhost")
  81. if is_invalid(domain) {
  82. continue
  83. }
  84. // Domain is an IP address
  85. if net.ParseIP(domain) != nil {
  86. continue
  87. }
  88. // Unify the records
  89. if ip != "127.0.0.1" {
  90. ip = "127.0.0.1"
  91. }
  92. sanitized = append(sanitized, ip + " " + domain)
  93. }
  94. return strings.Join(sanitized, "\n")
  95. }
  96. func parse_record(record string) (string, string, error) {
  97. if len(record) == 0 {
  98. return "", "", errors.New("Empty line")
  99. }
  100. if strings.HasPrefix(record, "#") {
  101. return "", "", errors.New("Comment")
  102. }
  103. words := strings.Fields(record)
  104. // Domain-only list
  105. if len(words) == 1 {
  106. return "", words[0], nil
  107. }
  108. return words[0], words[1], nil
  109. }
  110. func is_invalid(domain string) bool {
  111. for _, i := range invalid {
  112. if domain == i {
  113. return true
  114. }
  115. }
  116. return false
  117. }