Class: Shadowsocks::IPDetector
- Inherits:
-
Object
- Object
- Shadowsocks::IPDetector
- Defined in:
- lib/shadowsocks/ip_detector.rb
Constant Summary collapse
- GFW_LIST_PATH =
File.('../../../data/gfwlist.txt', __FILE__)
Instance Method Summary collapse
- #behind_gfw?(domain) ⇒ Boolean
-
#initialize ⇒ IPDetector
constructor
A new instance of IPDetector.
Constructor Details
#initialize ⇒ IPDetector
Returns a new instance of IPDetector.
9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/shadowsocks/ip_detector.rb', line 9 def initialize @internals = {} @nums = [] @dns_cache = {} lines = File.readlines(GFW_LIST_PATH) lines.each do |line| num = IPAddr.new(line).to_i @nums << num @internals[num.to_s] = line end @nums.sort! end |
Instance Method Details
#behind_gfw?(domain) ⇒ Boolean
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 |
# File 'lib/shadowsocks/ip_detector.rb', line 22 def behind_gfw?(domain) key = Digest::MD5.hexdigest domain if @dns_cache[key] @dns_cache[key] else begin ip = IPSocket::getaddress(domain) ip_num = IPAddr.new(ip).to_i i = @nums.bsearch { |x| x > ip_num } index = @nums.index(i) - 1 r = IPAddr.new(@internals[@nums[index].to_s]).include? ip if @dns_cache.size > 512 @dns_cache.delete @dns_cache.first[0] end @dns_cache[key] = r r rescue Exception false end end end |