ip3country 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/country_lookup.rb +120 -0
- data/lib/ip_supalite.table +0 -0
- metadata +45 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 94fd1cf27d601488aa2a86d61d88243cbf6aba92254c82f074a0969c369ecf6a
|
4
|
+
data.tar.gz: f5ecba1f4bc05be39c36756d38a1bc28561d2e73ab1d19449a61e8baf6924f62
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7ee452330166f8ef4522401641e3376ef5df78f3d24e00704e2791e68b5115c91c58d4f75144e0b11d7003e6dc7f4277e7ff084fe1db46f1d1c3aee4c918f134
|
7
|
+
data.tar.gz: 60af77a3f9975fa3955b910aedc1b20fc0c3927e135e130aa333f0b103acd79d02fb95e62256fd1f447c2f84d9b69aaa73f1c284ed59ae936a4f3a749fd39f3d
|
@@ -0,0 +1,120 @@
|
|
1
|
+
module CountryLookup
|
2
|
+
|
3
|
+
class Lookup
|
4
|
+
def initialize
|
5
|
+
initialize_from_file
|
6
|
+
end
|
7
|
+
|
8
|
+
def lookup_ip_string(ip_string)
|
9
|
+
if ip_string.nil? || ip_string.empty?
|
10
|
+
return nil
|
11
|
+
end
|
12
|
+
components = ip_string.split('.')
|
13
|
+
if components.length < 4
|
14
|
+
return nil
|
15
|
+
end
|
16
|
+
|
17
|
+
ip_number = components[0].to_i * 16777216 +
|
18
|
+
components[1].to_i * 65536 +
|
19
|
+
components[2].to_i * 256 +
|
20
|
+
components[3].to_i
|
21
|
+
|
22
|
+
self.lookup_ip_number(ip_number)
|
23
|
+
end
|
24
|
+
|
25
|
+
def lookup_ip_number(ip_number)
|
26
|
+
index = self.binary_search(ip_number)
|
27
|
+
cc = @country_codes[index]
|
28
|
+
if cc == '--'
|
29
|
+
return nil
|
30
|
+
end
|
31
|
+
cc
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
def binary_search(value)
|
37
|
+
min = 0
|
38
|
+
max = @ip_ranges.length - 1
|
39
|
+
while min < max do
|
40
|
+
mid = (min + max) >> 1
|
41
|
+
if @ip_ranges[mid] <= value
|
42
|
+
min = mid + 1
|
43
|
+
else
|
44
|
+
max = mid
|
45
|
+
end
|
46
|
+
end
|
47
|
+
min
|
48
|
+
end
|
49
|
+
|
50
|
+
|
51
|
+
# The binary is packed as follows:
|
52
|
+
# c1.c2.c3.....**: Country code look up table, terminated by **
|
53
|
+
#
|
54
|
+
# n1.c: if n is < 240, c is country code index
|
55
|
+
# 242.n2.n3.c: if n >= 240 but < 65536. n2 being lower order byte
|
56
|
+
# 243.n2.n3.n4.c: if n >= 65536. n2 being lower order byte
|
57
|
+
def initialize_from_file
|
58
|
+
unless @country_codes.nil? || @country_codes.length == 0
|
59
|
+
return
|
60
|
+
end
|
61
|
+
@country_codes = Array.new
|
62
|
+
@ip_ranges = Array.new
|
63
|
+
@country_table = Array.new
|
64
|
+
|
65
|
+
File.open(File.dirname(__FILE__) + '/ip_supalite.table') do |file|
|
66
|
+
until file.eof?
|
67
|
+
c1 = file.read(1)
|
68
|
+
c2 = file.read(1)
|
69
|
+
|
70
|
+
@country_table.push(c1 + c2)
|
71
|
+
if (c1 == '*')
|
72
|
+
break
|
73
|
+
end
|
74
|
+
end
|
75
|
+
last_end_range = 0
|
76
|
+
until file.eof?
|
77
|
+
count = 0
|
78
|
+
n1 = file.read(1).ord
|
79
|
+
if n1 < 240
|
80
|
+
count = n1
|
81
|
+
elsif n1 == 242
|
82
|
+
n2 = file.read(1).ord
|
83
|
+
n3 = file.read(1).ord
|
84
|
+
count = n2 | (n3 << 8)
|
85
|
+
elsif n1 == 243
|
86
|
+
n2 = file.read(1).ord
|
87
|
+
n3 = file.read(1).ord
|
88
|
+
n4 = file.read(1).ord
|
89
|
+
count = n2 | (n3 << 8) | (n4 << 16)
|
90
|
+
end
|
91
|
+
last_end_range += count * 256
|
92
|
+
cc = file.read(1).ord
|
93
|
+
|
94
|
+
@ip_ranges.push(last_end_range)
|
95
|
+
@country_codes.push(@country_table[cc])
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
def self.initialize
|
102
|
+
@lookup = Lookup.new
|
103
|
+
return nil
|
104
|
+
end
|
105
|
+
|
106
|
+
def self.lookup_ip_string(ip_string)
|
107
|
+
if @lookup.nil?
|
108
|
+
@lookup = Lookup.new
|
109
|
+
end
|
110
|
+
@lookup.lookup_ip_string(ip_string)
|
111
|
+
end
|
112
|
+
|
113
|
+
def self.lookup_ip_number(ip_number)
|
114
|
+
if @lookup.nil?
|
115
|
+
@lookup = Lookup.new
|
116
|
+
end
|
117
|
+
@lookup.lookup_ip_number(ip_number)
|
118
|
+
end
|
119
|
+
|
120
|
+
end
|
Binary file
|
metadata
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ip3country
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Statsig, Inc
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-06-21 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: A ruby, zero-dependency, super small version of IP2Location LITE country
|
14
|
+
lookups
|
15
|
+
email: support@statsig.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/country_lookup.rb
|
21
|
+
- lib/ip_supalite.table
|
22
|
+
homepage: https://rubygems.org/gems/ip3country
|
23
|
+
licenses:
|
24
|
+
- ISC
|
25
|
+
metadata: {}
|
26
|
+
post_install_message:
|
27
|
+
rdoc_options: []
|
28
|
+
require_paths:
|
29
|
+
- lib
|
30
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - ">="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
requirements: []
|
41
|
+
rubygems_version: 3.2.3
|
42
|
+
signing_key:
|
43
|
+
specification_version: 4
|
44
|
+
summary: ip3country for ruby
|
45
|
+
test_files: []
|