Sha256: 830f22c1c2fba95c5b03baccc41f8425b44e76da47febd25462339d27c057914

Contents?: true

Size: 673 Bytes

Versions: 2

Compression:

Stored size: 673 Bytes

Contents

require 'ipaddr'
require 'socket'

module Shadowsocks
  class IPDetector
    GFW_LIST_PATH = File.expand_path('../../../data/gfwlist.txt', __FILE__)

    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

    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
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
shadowsocks-0.10 lib/shadowsocks/ip_detector.rb
shadowsocks-0.9 lib/shadowsocks/ip_detector.rb