main.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package main
  2. import (
  3. "errors"
  4. "fmt"
  5. "io/ioutil"
  6. "net"
  7. "net/http"
  8. "strings"
  9. )
  10. var (
  11. lists = []string{
  12. "https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts",
  13. "https://mirror1.malwaredomains.com/files/justdomains",
  14. "http://sysctl.org/cameleon/hosts",
  15. "https://s3.amazonaws.com/lists.disconnect.me/simple_tracking.txt",
  16. "https://s3.amazonaws.com/lists.disconnect.me/simple_ad.txt",
  17. "https://hosts-file.net/ad_servers.txt",
  18. }
  19. invalid = []string{
  20. "localhost", "localhost.localdomain", "local", "broadcasthost",
  21. "ip6-localhost", "ip6-loopback", "ip6-localnet",
  22. "ip6-mcastprefix", "ip6-allnodes", "ip6-allrouters",
  23. "ip6-allhosts",
  24. }
  25. soa = `$TTL 1
  26. @ IN SOA localhost. root.localhost. (
  27. 0 ; serial
  28. 2w ; refresh
  29. 2w ; retry
  30. 2w ; expiry
  31. 2w ) ; negative cache ttl
  32. @ IN NS localhost.
  33. @ IN A 0.0.0.0
  34. `
  35. )
  36. func main() {
  37. fmt.Print(soa)
  38. fmt.Print(generateList())
  39. }
  40. func generateList() (string, error) {
  41. c := make(chan string)
  42. for _, list := range lists {
  43. go handle(list, c)
  44. }
  45. var zones []string
  46. for range lists {
  47. zones = append(zones, <-c)
  48. }
  49. return strings.Join(zones, "\n"), nil
  50. }
  51. func handle(url string, zone chan string) {
  52. body, err := fetch(url)
  53. if err != nil {
  54. return
  55. }
  56. zone <- sanitize(body)
  57. }
  58. func fetch(url string) (string, error) {
  59. req, err := http.Get(url)
  60. if err != nil {
  61. return "", err
  62. }
  63. defer req.Body.Close()
  64. body, err := ioutil.ReadAll(req.Body)
  65. if err != nil {
  66. return "", err
  67. }
  68. return string(body), nil
  69. }
  70. func sanitize(records string) string {
  71. var sanitized []string
  72. for _, line := range strings.Split(records, "\n") {
  73. _, domain, err := parse_record(line)
  74. if err != nil {
  75. continue
  76. }
  77. // Domain is invalid (e.g., "localhost")
  78. if is_invalid(domain) {
  79. continue
  80. }
  81. // Domain is an IP address
  82. if net.ParseIP(domain) != nil {
  83. continue
  84. }
  85. sanitized = append(sanitized, domain + ".\tCNAME\t.\n")
  86. }
  87. return strings.Join(sanitized, "")
  88. }
  89. func parse_record(record string) (string, string, error) {
  90. if len(record) == 0 {
  91. return "", "", errors.New("Empty line")
  92. }
  93. if strings.HasPrefix(record, "#") {
  94. return "", "", errors.New("Comment")
  95. }
  96. words := strings.Fields(record)
  97. // Domain-only list
  98. if len(words) == 1 {
  99. return "", words[0], nil
  100. }
  101. return words[0], words[1], nil
  102. }
  103. func is_invalid(domain string) bool {
  104. for _, i := range invalid {
  105. if domain == i {
  106. return true
  107. }
  108. }
  109. return false
  110. }