forked from yaodong/bypass-gfw
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupdate_data.rb
executable file
·42 lines (35 loc) · 1.7 KB
/
update_data.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
#! /usr/bin/env ruby
require 'json'
require 'netaddr'
require 'open-uri'
require 'resolv'
blocked = JSON.parse File.read("#{__dir__}/blocked.json")
data_dir = "#{__dir__}/data"
# -------------------------------------------------------------------
# Collect IP ranges from AWS and Cloudflare
# -------------------------------------------------------------------
blocked_ip_ranges = blocked['ip_ranges']
cloudflare_data = open(blocked_ip_ranges['cloudflare']).read.split("\n")
File.write "#{data_dir}/ip_ranges/cloudflare.json", JSON.pretty_generate(cloudflare_data)
aws_data = JSON.parse(open(blocked_ip_ranges['aws']).read)['prefixes'].select{ |p| p['service'] == 'AMAZON' }.map{ |p| p['ip_prefix'] }
File.write "#{data_dir}/ip_ranges/aws.json", JSON.pretty_generate(aws_data)
# -------------------------------------------------------------------
# Collect IP ranges by ASN
# -------------------------------------------------------------------
blocked['networks'].each do |k, n|
raw_data = `whois -h whois.radb.net -- '-i origin #{n}' | grep route:`
ranges = raw_data.split("\n").map { |i| i.split(/\s+/).last }
loop do
length = ranges.count
ranges = NetAddr.merge ranges
break if ranges.count == length
end
File.write "#{data_dir}/ip_ranges/#{k}.json", JSON.pretty_generate(ranges)
end
# -------------------------------------------------------------------
# Fetch IP of blocked domains
# -------------------------------------------------------------------
open_dns = ['208.67.222.222', 443]
dns_server = Resolv::DNS.new nameserver_port: [open_dns]
hosts = Hash[ blocked['domains'].map{ |d| [d, dns_server.getaddress(d).to_s] } ]
File.write "#{data_dir}/domains.json", JSON.pretty_generate(hosts)