|
| 1 | +package checks |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net" |
| 6 | + "slices" |
| 7 | + "sort" |
| 8 | + "sync" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/xray-web/web-check-api/checks/clients/ip" |
| 12 | +) |
| 13 | + |
| 14 | +type dnsServer struct { |
| 15 | + Name string |
| 16 | + IP string |
| 17 | +} |
| 18 | + |
| 19 | +var DNS_SERVERS = []dnsServer{ |
| 20 | + {Name: "AdGuard", IP: "176.103.130.130"}, |
| 21 | + {Name: "AdGuard Family", IP: "176.103.130.132"}, |
| 22 | + {Name: "CleanBrowsing Adult", IP: "185.228.168.10"}, |
| 23 | + {Name: "CleanBrowsing Family", IP: "185.228.168.168"}, |
| 24 | + {Name: "CleanBrowsing Security", IP: "185.228.168.9"}, |
| 25 | + {Name: "CloudFlare", IP: "1.1.1.1"}, |
| 26 | + {Name: "CloudFlare Family", IP: "1.1.1.3"}, |
| 27 | + {Name: "Comodo Secure", IP: "8.26.56.26"}, |
| 28 | + {Name: "Google DNS", IP: "8.8.8.8"}, |
| 29 | + {Name: "Neustar Family", IP: "156.154.70.3"}, |
| 30 | + {Name: "Neustar Protection", IP: "156.154.70.2"}, |
| 31 | + {Name: "Norton Family", IP: "199.85.126.20"}, |
| 32 | + {Name: "OpenDNS", IP: "208.67.222.222"}, |
| 33 | + {Name: "OpenDNS Family", IP: "208.67.222.123"}, |
| 34 | + {Name: "Quad9", IP: "9.9.9.9"}, |
| 35 | + {Name: "Yandex Family", IP: "77.88.8.7"}, |
| 36 | + {Name: "Yandex Safe", IP: "77.88.8.88"}, |
| 37 | +} |
| 38 | + |
| 39 | +var knownBlockIPs = []string{ |
| 40 | + "146.112.61.106", |
| 41 | + "185.228.168.10", |
| 42 | + "8.26.56.26", |
| 43 | + "9.9.9.9", |
| 44 | + "208.69.38.170", |
| 45 | + "208.69.39.170", |
| 46 | + "208.67.222.222", |
| 47 | + "208.67.222.123", |
| 48 | + "199.85.126.10", |
| 49 | + "199.85.126.20", |
| 50 | + "156.154.70.22", |
| 51 | + "77.88.8.7", |
| 52 | + "77.88.8.8", |
| 53 | + "::1", |
| 54 | + "2a02:6b8::feed:0ff", |
| 55 | + "2a02:6b8::feed:bad", |
| 56 | + "2a02:6b8::feed:a11", |
| 57 | + "2620:119:35::35", |
| 58 | + "2620:119:53::53", |
| 59 | + "2606:4700:4700::1111", |
| 60 | + "2606:4700:4700::1001", |
| 61 | + "2001:4860:4860::8888", |
| 62 | + "2a0d:2a00:1::", |
| 63 | + "2a0d:2a00:2::", |
| 64 | +} |
| 65 | + |
| 66 | +type Blocklist struct { |
| 67 | + Server string `json:"server"` |
| 68 | + ServerIP string `json:"serverIp"` |
| 69 | + IsBlocked bool `json:"isBlocked"` |
| 70 | +} |
| 71 | + |
| 72 | +type BlockList struct { |
| 73 | + lookup ip.DNSLookup |
| 74 | +} |
| 75 | + |
| 76 | +func NewBlockList(lookup ip.DNSLookup) *BlockList { |
| 77 | + return &BlockList{lookup: lookup} |
| 78 | +} |
| 79 | + |
| 80 | +func (b *BlockList) domainBlocked(ctx context.Context, domain, serverIP string) bool { |
| 81 | + ips, err := b.lookup.DNSLookupIP(ctx, "ip4", domain, serverIP) |
| 82 | + if err != nil { |
| 83 | + // if there's an error, consider it not blocked |
| 84 | + // TODO: return more detailed errors for each server |
| 85 | + return false |
| 86 | + } |
| 87 | + |
| 88 | + return slices.ContainsFunc(ips, func(ip net.IP) bool { |
| 89 | + return slices.Contains(knownBlockIPs, ip.String()) |
| 90 | + }) |
| 91 | +} |
| 92 | + |
| 93 | +func (b *BlockList) BlockedServers(ctx context.Context, domain string) []Blocklist { |
| 94 | + var lock sync.Mutex |
| 95 | + var wg sync.WaitGroup |
| 96 | + limit := make(chan struct{}, 5) |
| 97 | + |
| 98 | + var results []Blocklist |
| 99 | + |
| 100 | + ctx, cancel := context.WithTimeout(ctx, 5*time.Second) |
| 101 | + defer cancel() |
| 102 | + |
| 103 | + for _, server := range DNS_SERVERS { |
| 104 | + wg.Add(1) |
| 105 | + go func(server dnsServer) { |
| 106 | + limit <- struct{}{} |
| 107 | + defer func() { |
| 108 | + <-limit |
| 109 | + wg.Done() |
| 110 | + }() |
| 111 | + |
| 112 | + isBlocked := b.domainBlocked(ctx, domain, server.IP) |
| 113 | + lock.Lock() |
| 114 | + defer lock.Unlock() |
| 115 | + results = append(results, Blocklist{ |
| 116 | + Server: server.Name, |
| 117 | + ServerIP: server.IP, |
| 118 | + IsBlocked: isBlocked, |
| 119 | + }) |
| 120 | + }(server) |
| 121 | + } |
| 122 | + wg.Wait() |
| 123 | + |
| 124 | + sort.Slice(results, func(i, j int) bool { |
| 125 | + return results[i].Server < results[j].Server |
| 126 | + }) |
| 127 | + return results |
| 128 | +} |
0 commit comments