forked from felixonmars/dnsmasq-china-list
-
Notifications
You must be signed in to change notification settings - Fork 0
/
verify.rb
executable file
·301 lines (266 loc) · 9.52 KB
/
verify.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#!/usr/bin/ruby
require 'colorize'
require 'concurrent'
require 'ipaddr'
require 'public_suffix'
require 'resolv'
class ChinaListVerify
def initialize(
dns=nil,
whitelist_file: "ns-whitelist.txt",
blacklist_file: "ns-blacklist.txt",
cdnlist_file: "cdn-testlist.txt",
chnroutes_file: "/usr/share/china_ip_list.txt"
)
@dns = dns
@whitelist = load_list whitelist_file
@blacklist = load_list blacklist_file
@cdnlist = load_list cdnlist_file
@tld_ns = Concurrent::Hash.new
begin
@chnroutes = load_list(chnroutes_file).map { |line| IPAddr.new line }
rescue Errno::ENOENT
puts "Failed to load chnroutes, CDN check disabled".red
@chnroutes = nil
end
end
def load_list(filename)
File.readlines(filename).each do |line|
line if !line.chomp!.empty? and !line.start_with?("#")
end
end
def test_cn_ip(domain, response: nil)
if @chnroutes == nil
raise "chnroutes not loaded"
end
answers = nil
if response != nil && !response.empty?
answers = response.filter_map { |n, r| r if n.to_s == domain && r.class == Resolv::DNS::Resource::IN::A }
end
if answers == nil || answers.empty?
answers = resolve(domain, 'A')
end
answers.each do |answer|
answer = IPAddr.new answer.address.to_s
if @chnroutes.any? { |range| range.include? answer }
return true
end
end
return false
end
def resolve(domain, rdtype="A", server: nil, with_glue: false)
rdtype = Kernel.const_get("Resolv::DNS::Resource::IN::#{rdtype}")
if !server
if !@dns
resolver = Resolv::DNS.new
else
resolver = Resolv::DNS.new(nameserver: @dns)
end
else
server = [server] unless server.is_a? Array
resolver = Resolv::DNS.new(nameserver: server)
end
if !with_glue
resolver.getresources(domain, rdtype)
else
# Workaround for https://github.com/ruby/resolv/issues/27
result = []
glue = []
n0 = Resolv::DNS::Name.create domain
resolver.fetch_resource(domain, rdtype) {|reply, reply_name|
reply.each_resource {|n, ttl, data|
if n0 == n && data.is_a?(rdtype)
result << data
else
glue << [n, data]
end
}
}
return result, glue
end
end
def get_ns_for_tld(tld)
if !@tld_ns.has_key? tld
answers = resolve(tld + ".", "NS")
results = []
answers.each do |answer|
ips = resolve answer.name.to_s
ips.each do |ip|
results << ip.address.to_s
end
end
@tld_ns[tld] = results
end
@tld_ns[tld]
end
def check_whitelist(nameservers)
@whitelist.each { |pattern| nameservers.each {|ns| return pattern if ns.end_with? pattern }}
nil
end
def check_blacklist(nameservers)
@blacklist.each { |pattern| nameservers.each {|ns| return pattern if ns.end_with? pattern }}
nil
end
def check_cdnlist(domain)
test_cn_ip domain
end
def check_domain(domain, enable_cdnlist: true)
nameservers = []
nxdomain = false
begin
tld_ns = get_ns_for_tld(PublicSuffix.parse(domain, ignore_private: true).tld)
rescue PublicSuffix::DomainNotAllowed, PublicSuffix::DomainInvalid
yield nil, "Invalid domain #{domain}"
return nil
end
response, glue = self.resolve(
domain + ".",
'NS',
server: tld_ns,
with_glue: true
)
response.each do |rdata|
begin
nameserver = rdata.name.to_s
if PublicSuffix.valid?(nameserver, ignore_private: true)
nameservers << nameserver
end
if result = check_whitelist(nameservers)
yield true, "NS Whitelist #{result} matched for domain #{domain}" if block_given?
return true
end
rescue NoMethodError => e
puts "Ignoring error: #{e}"
end
end
if enable_cdnlist
@cdnlist.each do |testdomain|
if testdomain == domain or testdomain.end_with? "." + domain
if result = check_cdnlist(testdomain)
yield true, "CDN List matched (#{testdomain}) and verified #{result} for domain #{domain}" if block_given?
return true
end
end
end
# Assuming CDNList for non-TLDs
if domain.count(".") > 1 and PublicSuffix.domain(domain, ignore_private: true) != domain
if result = check_cdnlist(domain)
yield true, "CDN List matched and verified #{result} for domain #{domain}" if block_given?
return true if result
end
end
end
if result = check_blacklist(nameservers)
yield false, "NS Blacklist #{result} matched for domain #{domain}" if block_given?
return false
end
nameservers.each do |nameserver|
if result = test_cn_ip(nameserver, response: glue)
yield true, "NS #{nameserver} verified #{result} for domain #{domain}" if block_given?
return true
end
end
if !nameservers.empty?
yield false, "NS #{nameservers[0]} not verified for domain #{domain}" if block_given?
return false
else
yield nil, "Failed to get correct name server for domain #{domain}" if block_given?
return nil
end
end
def check_domain_verbose(domain, show_green: false, **kwargs)
check_domain(domain, **kwargs) do |result, message|
if result == true
puts message.green if show_green
elsif result == false
puts message.red
else
puts message.yellow
end
end
end
def check_domain_list(domain_list, sample: 30, show_green: false, jobs: Concurrent.processor_count)
domains = load_list domain_list
if sample > 0
domains = domains.sample(sample)
else
domains.shuffle!
end
pool = Concurrent::FixedThreadPool.new(jobs)
domains.each do |domain|
pool.post do
if check_domain_verbose(domain, show_green: show_green)
yield domain if block_given?
end
end
end
pool.shutdown
pool.wait_for_termination
end
end
# Operates on the raw file to preserve commented out lines
def CheckRedundant(lines, disabled_lines, domain)
new_line = "server=/#{domain}/114.114.114.114\n"
disabled_line = "#server=/#{domain}/114.114.114.114"
if lines.include? new_line
puts "Domain already exists: #{domain}"
return false
elsif disabled_lines.any? { |line| line.start_with? disabled_line }
puts "Domain already disabled: #{domain}"
return false
else
# Check for duplicates
test_domain = domain
while test_domain.include? '.'
test_domain = test_domain.partition('.').last
_new_line = "server=/#{test_domain}/114.114.114.114\n"
_disabled_line = "#server=/#{test_domain}/114.114.114.114"
if lines.include? _new_line
puts "Redundant domain already exists: #{test_domain}"
return false
elsif disabled_lines.any? { |line| line.start_with? _disabled_line }
puts "Redundant domain already disabled: #{test_domain}"
return false
end
end
end
return new_line
end
if __FILE__ == $0
require 'optparse'
require 'ostruct'
options = OpenStruct.new
options.file = "accelerated-domains.china.raw.txt"
options.sample = 0
options.verbose = false
options.domain = nil
options.dns = nil
OptionParser.new do |opts|
opts.banner = 'A simple verify library for dnsmasq-china-list'
opts.on("-f", "--file FILE", "File to check") do |f|
options.file = f
end
opts.on("-s", "--sample SAMPLE", Integer, "Verify only a limited sample. Pass 0 to example all entries") do |s|
options.sample = s
end
opts.on("-v", "--[no-]verbose", "Show green results") do |v|
options.verbose = v
end
opts.on("-d", "--domain DOMAIN", "Verify a domain instead of checking a list. Will ignore the other list options.") do |d|
options.domain = d
end
opts.on("-D", "--dns DNS", "Specify a DNS server to use instead of the system default one.") do |d|
options.dns = d
end
opts.on_tail("-h", "--help", "Show this message") do
puts opts
exit
end
end.parse!
v = ChinaListVerify.new options.dns
if options.domain
exit v.check_domain_verbose(options.domain, show_green: options.verbose) == true
else
v.check_domain_list(options.file, sample: options.sample, show_green: options.verbose)
end
end