-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathipinfo.rb
More file actions
208 lines (166 loc) · 5.95 KB
/
ipinfo.rb
File metadata and controls
208 lines (166 loc) · 5.95 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# frozen_string_literal: true
require 'cgi'
require 'ipaddr'
require 'ipinfo/adapter'
require 'ipinfo/cache/default_cache'
require 'ipinfo/errors'
require 'ipinfo/response'
require 'ipinfo/version'
require 'json'
require_relative 'ipinfo/ipAddressMatcher'
require_relative 'ipinfo/countriesData'
module IPinfo
include CountriesData
DEFAULT_CACHE_MAXSIZE = 4096
DEFAULT_CACHE_TTL = 60 * 60 * 24
RATE_LIMIT_MESSAGE = 'To increase your limits, please review our ' \
'paid plans at https://ipinfo.io/pricing'
# Base URL to get country flag image link.
# "PK" -> "https://cdn.ipinfo.io/static/images/countries-flags/PK.svg"
COUNTRY_FLAGS_URL = "https://cdn.ipinfo.io/static/images/countries-flags/"
class << self
def create(access_token = nil, settings = {})
IPinfo.new(access_token, settings)
end
end
end
class IPinfo::IPinfo
include IPinfo
attr_accessor :access_token, :countries, :httpc
def initialize(access_token = nil, settings = {})
@access_token = access_token
prepare_http_client(settings.fetch('http_client', nil))
maxsize = settings.fetch('maxsize', DEFAULT_CACHE_MAXSIZE)
ttl = settings.fetch('ttl', DEFAULT_CACHE_TTL)
@cache = settings.fetch('cache', DefaultCache.new(ttl, maxsize))
@countries = settings.fetch('countries', DEFAULT_COUNTRY_LIST)
@eu_countries = settings.fetch('eu_countries', DEFAULT_EU_COUNTRIES_LIST)
@countries_flags = settings.fetch('countries_flags', DEFAULT_COUNTRIES_FLAG_LIST)
@countries_currencies = settings.fetch('countries_currencies', DEFAULT_COUNTRIES_CURRENCIES_LIST)
@continents = settings.fetch('continents', DEFAULT_CONTINENT_LIST)
end
def details(ip_address = nil)
details_base(ip_address, :v4)
end
def details_v6(ip_address = nil)
details_base(ip_address, :v6)
end
def resproxy(ip_address)
cache_key_str = "resproxy:#{ip_address}"
res = @cache.get(cache_key(cache_key_str))
return Response.new(res) unless res.nil?
response = @httpc.get("/resproxy/#{CGI.escape(ip_address)}", :v4)
if response.status.eql?(429)
raise RateLimitError,
RATE_LIMIT_MESSAGE
end
details = JSON.parse(response.body, symbolize_names: true)
@cache.set(cache_key(cache_key_str), details)
Response.new(details)
end
def get_map_url(ips)
if !ips.kind_of?(Array)
return JSON.generate({:error => 'Invalid input. Array required!'})
end
if ips.length > 500000
return JSON.generate({:error => 'No more than 500,000 ips allowed!'})
end
json_ips = JSON.generate({:ips => ips})
res = @httpc.post('/tools/map', json_ips)
obj = JSON.parse(res.body)
obj['reportUrl']
end
def batch_requests(url_array, api_token)
result = Hash.new
lookup_ips = []
url_array.each { |url|
ip = @cache.get(cache_key(url))
unless ip.nil?
result.store(url, ip)
else
lookup_ips << url
end
}
if lookup_ips.empty?
return result
end
begin
lookup_ips.each_slice(1000){ |ips|
json_arr = JSON.generate(lookup_ips)
res = @httpc.post("/batch?token=#{api_token}", json_arr, 5)
raise StandardError, "Request Quota Exceeded" if res.status == 429
data = JSON.parse(res.body)
data.each { |key, val|
@cache.set(cache_key(key), val)
}
result.merge!(data)
}
rescue StandardError => e
return e.message
end
result
end
protected
def prepare_http_client(httpc = nil)
@httpc = Adapter.new(access_token, httpc || :net_http)
end
private
def request_details(ip_address = nil, host_type)
if isBogon(ip_address)
details = {}
details[:ip] = ip_address
details[:bogon] = true
details[:ip_address] = IPAddr.new(ip_address)
return details
end
res = @cache.get(cache_key(ip_address))
return res unless res.nil?
response = @httpc.get(escape_path(ip_address), host_type)
if response.status.eql?(429)
raise RateLimitError,
RATE_LIMIT_MESSAGE
end
details = JSON.parse(response.body, symbolize_names: true)
@cache.set(cache_key(ip_address), details)
details
end
def details_base(ip_address, host_type)
details = request_details(ip_address, host_type)
if details.key? :country
details[:country_name] =
@countries.fetch(details.fetch(:country), nil)
details[:is_eu] =
@eu_countries.include?(details.fetch(:country))
details[:country_flag] =
@countries_flags.fetch(details.fetch(:country), nil)
details[:country_currency] =
@countries_currencies.fetch(details.fetch(:country), nil)
details[:continent] =
@continents.fetch(details.fetch(:country), nil)
details[:country_flag_url] = COUNTRY_FLAGS_URL + details.fetch(:country) + ".svg"
end
if details.key? :ip
details[:ip_address] =
IPAddr.new(details.fetch(:ip))
end
if details.key? :loc
loc = details.fetch(:loc).split(',')
details[:latitude] = loc[0]
details[:longitude] = loc[1]
end
Response.new(details)
end
def isBogon(ip)
if ip.nil?
return false
end
matcher_object = IpAddressMatcher.new(ip)
matcher_object.matches
end
def escape_path(ip)
ip ? "/#{CGI.escape(ip)}" : '/'
end
def cache_key(ip)
"1:#{ip}"
end
end