ip2location_ruby 8.0.1 → 8.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,200 +1,200 @@
1
- # encoding: utf-8
2
- require 'bindata'
3
- require 'ipaddr'
4
- require 'ip2location_ruby/ip2location_config'
5
- require 'ip2location_ruby/database_config'
6
- require 'ip2location_ruby/i2l_float_data'
7
- require 'ip2location_ruby/i2l_string_data'
8
- require 'ip2location_ruby/i2l_ip_data'
9
- require 'ip2location_ruby/ip2location_record'
10
-
11
- class Ip2location
12
- attr_accessor :record_class4, :record_class6, :v4, :file, :db_index, :count, :base_addr, :ipno, :count, :record, :database, :columns, :ip_version, :ipv4databasecount, :ipv4databaseaddr, :ipv4indexbaseaddr, :ipv6databasecount, :ipv6databaseaddr, :ipv6indexbaseaddr
13
-
14
- def open(url)
15
- self.file = File.open(File.expand_path url, 'rb')
16
- i2l = Ip2locationConfig.read(file)
17
- self.db_index = i2l.databasetype
18
- self.columns = i2l.databasecolumn + 0
19
- self.database = DbConfig.setup_database(self.db_index)
20
- self.ipv4databasecount = i2l.ipv4databasecount
21
- self.ipv4databaseaddr = i2l.ipv4databaseaddr
22
- self.ipv6databasecount = i2l.ipv6databasecount
23
- self.ipv6databaseaddr = i2l.ipv6databaseaddr
24
- self.ipv4indexbaseaddr = i2l.ipv4indexbaseaddr
25
- self.ipv6indexbaseaddr = i2l.ipv6indexbaseaddr
26
- self.record_class4 = (Ip2LocationRecord.init database, 4)
27
- self.record_class6 = (Ip2LocationRecord.init database, 6)
28
- self
29
- end
30
-
31
- def get_all(ip)
32
- ipno = IPAddr.new(ip, Socket::AF_UNSPEC)
33
- self.ip_version = ipno.ipv4? ? 4 : 6
34
- self.v4 = ipno.ipv4?
35
- self.count = ipno.ipv4? ? self.ipv4databasecount + 0 : self.ipv6databasecount + 0
36
- self.base_addr = (ipno.ipv4? ? self.ipv4databaseaddr - 1 : self.ipv6databaseaddr - 1)
37
-
38
- ipnum = ipno.to_i + 0
39
- col_length = columns * 4
40
-
41
- if ipv4indexbaseaddr > 0 || ipv6indexbaseaddr > 0
42
- indexpos = 0
43
- case ip_version
44
- when 4
45
- ipnum1_2 = (ipnum >> 16)
46
- indexpos = ipv4indexbaseaddr + (ipnum1_2 << 3)
47
- when 6
48
- ipnum1 = (ipnum / (2**112))
49
- indexpos = ipv6indexbaseaddr + (ipnum1 << 3)
50
- end
51
- low = read32(indexpos)
52
- high = read32(indexpos + 4)
53
- return self.record = bsearch(low, high, ipnum, self.base_addr, col_length)
54
- else
55
- return self.record = bsearch(0, self.count, ipnum, self.base_addr, col_length)
56
- end
57
- end
58
-
59
- def get_country_short(ip)
60
- rec = get_all(ip)
61
- return rec.country_short
62
- end
63
-
64
- def get_country_long(ip)
65
- rec = get_all(ip)
66
- return rec.country_long
67
- end
68
-
69
- def get_region(ip)
70
- rec = get_all(ip)
71
- return rec.region
72
- end
73
-
74
- def get_city(ip)
75
- rec = get_all(ip)
76
- return rec.city
77
- end
78
-
79
- def get_latitude(ip)
80
- rec = get_all(ip)
81
- return rec.latitude
82
- end
83
-
84
- def get_longitude(ip)
85
- rec = get_all(ip)
86
- return rec.longitude
87
- end
88
-
89
- def get_isp(ip)
90
- rec = get_all(ip)
91
- return rec.isp
92
- end
93
-
94
- def get_domain(ip)
95
- rec = get_all(ip)
96
- return rec.domain
97
- end
98
-
99
- def get_zipcode(ip)
100
- rec = get_all(ip)
101
- return rec.zipcode
102
- end
103
-
104
- def get_timezone(ip)
105
- rec = get_all(ip)
106
- return rec.timezone
107
- end
108
-
109
- def get_netspeed(ip)
110
- rec = get_all(ip)
111
- return rec.netspeed
112
- end
113
-
114
- def get_iddcode(ip)
115
- rec = get_all(ip)
116
- return rec.iddcode
117
- end
118
-
119
- def get_areacode(ip)
120
- rec = get_all(ip)
121
- return rec.areacode
122
- end
123
-
124
- def get_weatherstationcode(ip)
125
- rec = get_all(ip)
126
- return rec.weatherstationcode
127
- end
128
-
129
- def get_weatherstationname(ip)
130
- rec = get_all(ip)
131
- return rec.weatherstationname
132
- end
133
-
134
- def get_mcc(ip)
135
- rec = get_all(ip)
136
- return rec.mcc
137
- end
138
-
139
- def get_mnc(ip)
140
- rec = get_all(ip)
141
- return rec.mnc
142
- end
143
-
144
- def get_mobilebrand(ip)
145
- rec = get_all(ip)
146
- return rec.mobilebrand
147
- end
148
-
149
- def get_elevation(ip)
150
- rec = get_all(ip)
151
- return rec.elevation
152
- end
153
-
154
- def get_usagetype(ip)
155
- rec = get_all(ip)
156
- return rec.usagetype
157
- end
158
-
159
- def bsearch(low, high, ipnum, base_addr, col_length)
160
- while low <= high do
161
- mid = (low + high) >> 1
162
- ip_from, ip_to = get_from_to(mid, base_addr, col_length)
163
- if ipnum >= ip_from && ipnum < ip_to
164
- from_base = ( base_addr + mid * (col_length + (self.v4 ? 0 : 12)))
165
- file.seek(from_base)
166
- if v4
167
- return self.record_class4.read(file)
168
- else
169
- return self.record_class6.read(file)
170
- end
171
- else
172
- if ipnum < ip_from
173
- high = mid - 1
174
- else
175
- low = mid + 1
176
- end
177
- end
178
- end
179
- end
180
-
181
- def get_from_to(mid, base_addr, col_length)
182
- from_base = ( base_addr + mid * (col_length + (v4 ? 0 : 12)))
183
- file.seek(from_base)
184
- ip_from = v4 ? file.read(4).unpack('V').first : readipv6(file)
185
- file.seek(from_base + col_length + (v4 ? 0 : 12))
186
- ip_to = v4 ? file.read(4).unpack('V').first : readipv6(file)
187
- [ip_from, ip_to]
188
- end
189
-
190
- def read32(indexp)
191
- file.seek(indexp - 1)
192
- return file.read(4).unpack('V').first
193
- end
194
-
195
- def readipv6(filer)
196
- parts = filer.read(16).unpack('V*')
197
- return parts[0] + parts[1] * 4294967296 + parts[2] * 4294967296**2 + parts[3] * 4294967296**3
198
- end
199
-
1
+ # encoding: utf-8
2
+ require 'bindata'
3
+ require 'ipaddr'
4
+ require 'ip2location_ruby/ip2location_config'
5
+ require 'ip2location_ruby/database_config'
6
+ require 'ip2location_ruby/i2l_float_data'
7
+ require 'ip2location_ruby/i2l_string_data'
8
+ require 'ip2location_ruby/i2l_ip_data'
9
+ require 'ip2location_ruby/ip2location_record'
10
+
11
+ class Ip2location
12
+ attr_accessor :record_class4, :record_class6, :v4, :file, :db_index, :count, :base_addr, :ipno, :count, :record, :database, :columns, :ip_version, :ipv4databasecount, :ipv4databaseaddr, :ipv4indexbaseaddr, :ipv6databasecount, :ipv6databaseaddr, :ipv6indexbaseaddr
13
+
14
+ def open(url)
15
+ self.file = File.open(File.expand_path url, 'rb')
16
+ i2l = Ip2locationConfig.read(file)
17
+ self.db_index = i2l.databasetype
18
+ self.columns = i2l.databasecolumn + 0
19
+ self.database = DbConfig.setup_database(self.db_index)
20
+ self.ipv4databasecount = i2l.ipv4databasecount
21
+ self.ipv4databaseaddr = i2l.ipv4databaseaddr
22
+ self.ipv6databasecount = i2l.ipv6databasecount
23
+ self.ipv6databaseaddr = i2l.ipv6databaseaddr
24
+ self.ipv4indexbaseaddr = i2l.ipv4indexbaseaddr
25
+ self.ipv6indexbaseaddr = i2l.ipv6indexbaseaddr
26
+ self.record_class4 = (Ip2LocationRecord.init database, 4)
27
+ self.record_class6 = (Ip2LocationRecord.init database, 6)
28
+ self
29
+ end
30
+
31
+ def get_all(ip)
32
+ ipno = IPAddr.new(ip, Socket::AF_UNSPEC)
33
+ self.ip_version = ipno.ipv4? ? 4 : 6
34
+ self.v4 = ipno.ipv4?
35
+ self.count = ipno.ipv4? ? self.ipv4databasecount + 0 : self.ipv6databasecount + 0
36
+ self.base_addr = (ipno.ipv4? ? self.ipv4databaseaddr - 1 : self.ipv6databaseaddr - 1)
37
+
38
+ ipnum = ipno.to_i + 0
39
+ col_length = columns * 4
40
+
41
+ if ipv4indexbaseaddr > 0 || ipv6indexbaseaddr > 0
42
+ indexpos = 0
43
+ case ip_version
44
+ when 4
45
+ ipnum1_2 = (ipnum >> 16)
46
+ indexpos = ipv4indexbaseaddr + (ipnum1_2 << 3)
47
+ when 6
48
+ ipnum1 = (ipnum / (2**112))
49
+ indexpos = ipv6indexbaseaddr + (ipnum1 << 3)
50
+ end
51
+ low = read32(indexpos)
52
+ high = read32(indexpos + 4)
53
+ return self.record = bsearch(low, high, ipnum, self.base_addr, col_length)
54
+ else
55
+ return self.record = bsearch(0, self.count, ipnum, self.base_addr, col_length)
56
+ end
57
+ end
58
+
59
+ def get_country_short(ip)
60
+ rec = get_all(ip)
61
+ return rec.country_short
62
+ end
63
+
64
+ def get_country_long(ip)
65
+ rec = get_all(ip)
66
+ return rec.country_long
67
+ end
68
+
69
+ def get_region(ip)
70
+ rec = get_all(ip)
71
+ return rec.region
72
+ end
73
+
74
+ def get_city(ip)
75
+ rec = get_all(ip)
76
+ return rec.city
77
+ end
78
+
79
+ def get_latitude(ip)
80
+ rec = get_all(ip)
81
+ return rec.latitude
82
+ end
83
+
84
+ def get_longitude(ip)
85
+ rec = get_all(ip)
86
+ return rec.longitude
87
+ end
88
+
89
+ def get_isp(ip)
90
+ rec = get_all(ip)
91
+ return rec.isp
92
+ end
93
+
94
+ def get_domain(ip)
95
+ rec = get_all(ip)
96
+ return rec.domain
97
+ end
98
+
99
+ def get_zipcode(ip)
100
+ rec = get_all(ip)
101
+ return rec.zipcode
102
+ end
103
+
104
+ def get_timezone(ip)
105
+ rec = get_all(ip)
106
+ return rec.timezone
107
+ end
108
+
109
+ def get_netspeed(ip)
110
+ rec = get_all(ip)
111
+ return rec.netspeed
112
+ end
113
+
114
+ def get_iddcode(ip)
115
+ rec = get_all(ip)
116
+ return rec.iddcode
117
+ end
118
+
119
+ def get_areacode(ip)
120
+ rec = get_all(ip)
121
+ return rec.areacode
122
+ end
123
+
124
+ def get_weatherstationcode(ip)
125
+ rec = get_all(ip)
126
+ return rec.weatherstationcode
127
+ end
128
+
129
+ def get_weatherstationname(ip)
130
+ rec = get_all(ip)
131
+ return rec.weatherstationname
132
+ end
133
+
134
+ def get_mcc(ip)
135
+ rec = get_all(ip)
136
+ return rec.mcc
137
+ end
138
+
139
+ def get_mnc(ip)
140
+ rec = get_all(ip)
141
+ return rec.mnc
142
+ end
143
+
144
+ def get_mobilebrand(ip)
145
+ rec = get_all(ip)
146
+ return rec.mobilebrand
147
+ end
148
+
149
+ def get_elevation(ip)
150
+ rec = get_all(ip)
151
+ return rec.elevation
152
+ end
153
+
154
+ def get_usagetype(ip)
155
+ rec = get_all(ip)
156
+ return rec.usagetype
157
+ end
158
+
159
+ def bsearch(low, high, ipnum, base_addr, col_length)
160
+ while low <= high do
161
+ mid = (low + high) >> 1
162
+ ip_from, ip_to = get_from_to(mid, base_addr, col_length)
163
+ if ipnum >= ip_from && ipnum < ip_to
164
+ from_base = ( base_addr + mid * (col_length + (self.v4 ? 0 : 12)))
165
+ file.seek(from_base)
166
+ if v4
167
+ return self.record_class4.read(file)
168
+ else
169
+ return self.record_class6.read(file)
170
+ end
171
+ else
172
+ if ipnum < ip_from
173
+ high = mid - 1
174
+ else
175
+ low = mid + 1
176
+ end
177
+ end
178
+ end
179
+ end
180
+
181
+ def get_from_to(mid, base_addr, col_length)
182
+ from_base = ( base_addr + mid * (col_length + (v4 ? 0 : 12)))
183
+ file.seek(from_base)
184
+ ip_from = v4 ? file.read(4).unpack('V').first : readipv6(file)
185
+ file.seek(from_base + col_length + (v4 ? 0 : 12))
186
+ ip_to = v4 ? file.read(4).unpack('V').first : readipv6(file)
187
+ [ip_from, ip_to]
188
+ end
189
+
190
+ def read32(indexp)
191
+ file.seek(indexp - 1)
192
+ return file.read(4).unpack('V').first
193
+ end
194
+
195
+ def readipv6(filer)
196
+ parts = filer.read(16).unpack('V*')
197
+ return parts[0] + parts[1] * 4294967296 + parts[2] * 4294967296**2 + parts[3] * 4294967296**3
198
+ end
199
+
200
200
  end
@@ -1,35 +1,35 @@
1
- class DbConfig
2
- COLUMNS = {
3
- :COUNTRY => [0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
4
- :REGION => [0, 0, 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3],
5
- :CITY => [0, 0, 0, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4],
6
- :ISP => [0, 0, 3, 0, 5, 0, 7, 5, 7, 0, 8, 0, 9, 0, 9, 0, 9, 0, 9, 7, 9, 0, 9, 7, 9],
7
- :LATITUDE => [0, 0, 0, 0, 0, 5, 5, 0, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5],
8
- :LONGITUDE => [0, 0, 0, 0, 0, 6, 6, 0, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6],
9
- :DOMAIN => [0, 0, 0, 0, 0, 0, 0, 6, 8, 0, 9, 0, 10, 0, 10, 0, 10, 0, 10, 8, 10, 0, 10, 8, 10],
10
- :ZIPCODE => [0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 7, 7, 7, 0, 7, 7, 7, 0, 7, 0, 7, 7, 7, 0, 7],
11
- :TIMEZONE => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 8, 7, 8, 8, 8, 7, 8, 0, 8, 8, 8, 0, 8],
12
- :NETSPEED => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 11, 0, 11,8, 11, 0, 11, 0, 11, 0, 11],
13
- :IDDCODE => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 12, 0, 12, 0, 12, 9, 12, 0, 12],
14
- :AREACODE => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10 ,13 ,0, 13, 0, 13, 10, 13, 0, 13],
15
- :WEATHERSTATIONCODE => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 14, 0, 14, 0, 14, 0, 14],
16
- :WEATHERSTATIONNAME => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 15, 0, 15, 0, 15, 0, 15],
17
- :MCC => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 16, 0, 16, 9, 16],
18
- :MNC => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10,17, 0, 17, 10, 17],
19
- :MOBILEBRAND => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11,18, 0, 18, 11, 18],
20
- :ELEVATION => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11, 19, 0, 19],
21
- :USAGETYPE => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 20]
22
- }
23
-
24
- def self.setup_database(db_index)
25
- # strip all 0 value & downcase keys
26
- cols = COLUMNS.inject({}) {|memo, (key, value)|
27
- (memo[key.to_s.downcase.to_sym] = value[db_index] if value[db_index] > 0)
28
- memo
29
- }
30
- # order by value
31
- col_array = cols.sort_by {|key,value| value}
32
- end
33
-
34
- end
1
+ class DbConfig
2
+ COLUMNS = {
3
+ :COUNTRY => [0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
4
+ :REGION => [0, 0, 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3],
5
+ :CITY => [0, 0, 0, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4],
6
+ :ISP => [0, 0, 3, 0, 5, 0, 7, 5, 7, 0, 8, 0, 9, 0, 9, 0, 9, 0, 9, 7, 9, 0, 9, 7, 9],
7
+ :LATITUDE => [0, 0, 0, 0, 0, 5, 5, 0, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5],
8
+ :LONGITUDE => [0, 0, 0, 0, 0, 6, 6, 0, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6],
9
+ :DOMAIN => [0, 0, 0, 0, 0, 0, 0, 6, 8, 0, 9, 0, 10, 0, 10, 0, 10, 0, 10, 8, 10, 0, 10, 8, 10],
10
+ :ZIPCODE => [0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 7, 7, 7, 0, 7, 7, 7, 0, 7, 0, 7, 7, 7, 0, 7],
11
+ :TIMEZONE => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 8, 7, 8, 8, 8, 7, 8, 0, 8, 8, 8, 0, 8],
12
+ :NETSPEED => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 11, 0, 11,8, 11, 0, 11, 0, 11, 0, 11],
13
+ :IDDCODE => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 12, 0, 12, 0, 12, 9, 12, 0, 12],
14
+ :AREACODE => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10 ,13 ,0, 13, 0, 13, 10, 13, 0, 13],
15
+ :WEATHERSTATIONCODE => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 14, 0, 14, 0, 14, 0, 14],
16
+ :WEATHERSTATIONNAME => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 15, 0, 15, 0, 15, 0, 15],
17
+ :MCC => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 16, 0, 16, 9, 16],
18
+ :MNC => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10,17, 0, 17, 10, 17],
19
+ :MOBILEBRAND => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11,18, 0, 18, 11, 18],
20
+ :ELEVATION => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11, 19, 0, 19],
21
+ :USAGETYPE => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 20]
22
+ }
23
+
24
+ def self.setup_database(db_index)
25
+ # strip all 0 value & downcase keys
26
+ cols = COLUMNS.inject({}) {|memo, (key, value)|
27
+ (memo[key.to_s.downcase.to_sym] = value[db_index] if value[db_index] > 0)
28
+ memo
29
+ }
30
+ # order by value
31
+ col_array = cols.sort_by {|key,value| value}
32
+ end
33
+
34
+ end
35
35