Class: Shadowsocks::IPDetector

Inherits:
Object
  • Object
show all
Defined in:
lib/shadowsocks/ip_detector.rb

Constant Summary collapse

GFW_LIST_PATH =
File.expand_path('../../../data/gfwlist.txt', __FILE__)

Instance Method Summary collapse

Constructor Details

#initializeIPDetector

Returns a new instance of IPDetector.



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/shadowsocks/ip_detector.rb', line 8

def initialize
  @internals = {}
  @nums      = []
  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

Returns:

  • (Boolean)


20
21
22
23
24
25
26
27
28
# File 'lib/shadowsocks/ip_detector.rb', line 20

def behind_gfw?(domain)
  ip = IPSocket::getaddress(domain)

  ip_num = IPAddr.new(ip).to_i

  i = @nums.bsearch { |x| x > ip_num }
  index = @nums.index(i) - 1
  IPAddr.new(@internals[@nums[index].to_s]).include? ip
end