forked from client9/ipcat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipcat.rb
46 lines (38 loc) · 839 Bytes
/
ipcat.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
require "ipaddr"
require "csv"
module IPCat
class Datacenters
def initialize file
@starts = Array.new
@ends = Array.new
@urls = Array.new
File.open(file, "r").readlines.map do |line|
add(*CSV.parse_line(line))
end
end
def add start, stop, name, url=nil
@starts << IPAddr.new(start).to_i
@ends << IPAddr.new(stop).to_i
@urls << url
end
def length
@starts.length
end
def find ipstring
ip = IPAddr.new(ipstring).to_i
high = length
low = 0
while high >= low do
probe = ((high+low)/2).floor.to_i
if @starts[probe] > ip
high = probe - 1
elsif @ends[probe] < ip
low = probe + 1
else
return @urls[probe]
end
end
return nil
end
end
end