-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathIPinfo.php
More file actions
434 lines (384 loc) · 13.4 KB
/
IPinfo.php
File metadata and controls
434 lines (384 loc) · 13.4 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
<?php
namespace ipinfo\ipinfo;
require_once __DIR__ . '/Const.php';
use Exception;
use ipinfo\ipinfo\cache\DefaultCache;
use GuzzleHttp\Pool;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Promise;
use Symfony\Component\HttpFoundation\IpUtils;
/**
* Exposes the IPinfo library to client code.
*/
class IPinfo
{
const API_URL = 'https://ipinfo.io';
const COUNTRY_FLAG_URL = 'https://cdn.ipinfo.io/static/images/countries-flags/';
const STATUS_CODE_QUOTA_EXCEEDED = 429;
const REQUEST_TIMEOUT_DEFAULT = 2; // seconds
const CACHE_MAXSIZE = 4096;
const CACHE_TTL = 86400; // 24 hours as seconds
const CACHE_KEY_VSN = '1'; // update when cache vals change for same key.
const COUNTRIES_DEFAULT = COUNTRIES;
const EU_COUNTRIES_DEFAULT = EU;
const COUNTRIES_FLAGS_DEFAULT = FLAGS;
const COUNTRIES_CURRENCIES_DEFAULT = CURRENCIES;
const CONTINENTS_DEFAULT = CONTINENTS;
const BATCH_MAX_SIZE = 1000;
const BATCH_TIMEOUT = 5; // seconds
public $access_token;
public $settings;
public $cache;
public $countries;
public $eu_countries;
public $countries_flags;
public $countries_currencies;
public $continents;
protected $http_client;
public function __construct($access_token = null, $settings = [])
{
$this->access_token = $access_token;
$this->settings = $settings;
/*
Support a timeout first-class, then a `guzzle_opts` key that can
override anything.
*/
$guzzle_opts = [
'http_errors' => false,
'headers' => $this->buildHeaders(),
'timeout' => $settings['timeout'] ?? self::REQUEST_TIMEOUT_DEFAULT
];
if (isset($settings['guzzle_opts'])) {
$guzzle_opts = array_merge($guzzle_opts, $settings['guzzle_opts']);
}
$this->http_client = new Client($guzzle_opts);
$this->countries = $settings['countries'] ?? self::COUNTRIES_DEFAULT;
$this->countries_flags = $settings['countries_flags'] ?? self::COUNTRIES_FLAGS_DEFAULT;
$this->countries_currencies = $settings['countries_currencies'] ?? self::COUNTRIES_CURRENCIES_DEFAULT;
$this->eu_countries = $settings['eu_countries'] ?? self::EU_COUNTRIES_DEFAULT;
$this->continents = $settings['continents'] ?? self::CONTINENTS_DEFAULT;
if (!array_key_exists('cache_disabled', $this->settings) || $this->settings['cache_disabled'] == false) {
if (array_key_exists('cache', $settings)) {
$this->cache = $settings['cache'];
} else {
$maxsize = $settings['cache_maxsize'] ?? self::CACHE_MAXSIZE;
$ttl = $settings['cache_ttl'] ?? self::CACHE_TTL;
$this->cache = new DefaultCache($maxsize, $ttl);
}
} else {
$this->cache = null;
}
}
/**
* Get formatted details for an IP address.
* @param string|null $ip_address IP address to look up.
* @return Details Formatted IPinfo data.
* @throws IPinfoException
*/
public function getDetails($ip_address = null)
{
$response_details = $this->getRequestDetails((string) $ip_address);
return $this->formatDetailsObject($response_details);
}
/**
* Get formatted details for a list of IP addresses.
* @param $urls the array of URLs.
* @param $batchSize default value is set to max value for batch size, which is 1000.
* @param batchTimeout in seconds. Default value is 5 seconds.
* @param filter default value is false.
* @return $results
*/
public function getBatchDetails(
$urls,
$batchSize = 0,
$batchTimeout = self::BATCH_TIMEOUT,
$filter = false
) {
$lookupUrls = [];
$results = [];
// no items?
if (count($urls) == 0) {
return $results;
}
// clip batch size.
if (!is_numeric($batchSize) || $batchSize <= 0 || $batchSize > self::BATCH_MAX_SIZE) {
$batchSize = self::BATCH_MAX_SIZE;
}
// filter out URLs already cached.
if ($this->cache != null) {
foreach ($urls as $url) {
$cachedRes = $this->cache->get($this->cacheKey($url));
if ($cachedRes != null) {
$results[$url] = $cachedRes;
} else {
$lookupUrls[] = $url;
}
}
} else {
$lookupUrls = $urls;
}
// everything cached? exit early.
if (count($lookupUrls) == 0) {
return $results;
}
// prepare each batch & fire it off asynchronously.
$apiUrl = self::API_URL . "/batch";
if ($filter) {
$apiUrl .= '?filter=1';
}
$promises = [];
$totalBatches = ceil(count($lookupUrls) / $batchSize);
for ($i = 0; $i < $totalBatches; $i++) {
$start = $i * $batchSize;
$batch = array_slice($lookupUrls, $start, $batchSize);
$promise = $this->http_client->postAsync($apiUrl, [
'body' => json_encode($batch),
'timeout' => $batchTimeout
])->then(function ($resp) use (&$results) {
$batchResult = json_decode($resp->getBody(), true);
foreach ($batchResult as $k => $v) {
$results[$k] = $v;
}
});
$promises[] = $promise;
}
// wait for all batches to finish.
Promise\Utils::settle($promises)->wait();
// cache any new results.
if ($this->cache != null) {
foreach ($lookupUrls as $url) {
if (array_key_exists($url, $results)) {
$this->cache->set($this->cacheKey($url), $results[$url]);
}
}
}
return $results;
}
public function formatDetailsObject($details = [])
{
$country = $details['country'] ?? null;
$details['country_name'] = $this->countries[$country] ?? null;
$details['is_eu'] = in_array($country, $this->eu_countries);
$details['country_flag'] = $this->countries_flags[$country] ?? null;
$details['country_flag_url'] = self::COUNTRY_FLAG_URL.$country.".svg";
$details['country_currency'] = $this->countries_currencies[$country] ?? null;
$details['continent'] = $this->continents[$country] ?? null;
if (array_key_exists('loc', $details)) {
$coords = explode(',', $details['loc']);
$details['latitude'] = $coords[0];
$details['longitude'] = $coords[1];
} else {
$details['latitude'] = null;
$details['longitude'] = null;
}
return new Details($details);
}
/**
* Get details for a specific IP address.
* @param string $ip_address IP address to query API for.
* @return array IP response data.
* @throws IPinfoException
*/
public function getRequestDetails(string $ip_address)
{
if ($this->isBogon($ip_address)) {
return [
"ip" => $ip_address,
"bogon" => true,
];
}
if ($this->cache != null) {
$cachedRes = $this->cache->get($this->cacheKey($ip_address));
if ($cachedRes != null) {
return $cachedRes;
}
}
$url = self::API_URL;
if ($ip_address) {
$url .= "/$ip_address";
}
try {
$response = $this->http_client->request('GET', $url);
} catch (GuzzleException $e) {
throw new IPinfoException($e->getMessage());
} catch (Exception $e) {
throw new IPinfoException($e->getMessage());
}
if ($response->getStatusCode() == self::STATUS_CODE_QUOTA_EXCEEDED) {
throw new IPinfoException('IPinfo request quota exceeded.');
} elseif ($response->getStatusCode() >= 400) {
throw new IPinfoException('Exception: ' . json_encode([
'status' => $response->getStatusCode(),
'reason' => $response->getReasonPhrase(),
]));
}
$raw_details = json_decode($response->getBody(), true);
if ($this->cache != null) {
$this->cache->set($this->cacheKey($ip_address), $raw_details);
}
return $raw_details;
}
/**
* Get residential proxy information for an IP address.
* @param string $ip_address IP address to look up.
* @return array Resproxy data containing ip, last_seen, percent_days_seen, service.
* @throws IPinfoException
*/
public function getResproxy(string $ip_address)
{
$cacheKey = "resproxy/$ip_address";
if ($this->cache != null) {
$cachedRes = $this->cache->get($this->cacheKey($cacheKey));
if ($cachedRes != null) {
// The cache may modify the 'ip' field for IPv6 normalization,
// but for resproxy the key contains a prefix, so restore original IP
$cachedRes['ip'] = $ip_address;
return $cachedRes;
}
}
$url = self::API_URL . "/resproxy/$ip_address";
try {
$response = $this->http_client->request('GET', $url);
} catch (GuzzleException $e) {
throw new IPinfoException($e->getMessage());
} catch (Exception $e) {
throw new IPinfoException($e->getMessage());
}
if ($response->getStatusCode() == self::STATUS_CODE_QUOTA_EXCEEDED) {
throw new IPinfoException('IPinfo request quota exceeded.');
} elseif ($response->getStatusCode() >= 400) {
throw new IPinfoException(
'Exception: ' .
json_encode([
'status' => $response->getStatusCode(),
'reason' => $response->getReasonPhrase(),
]),
);
}
$details = json_decode($response->getBody(), true);
if ($this->cache != null) {
$this->cache->set($this->cacheKey($cacheKey), $details);
}
return $details;
}
/**
* Gets a URL to a map on https://ipinfo.io/map given a list of IPs (max
* 500,000).
* @param array $ips list of IP addresses to put on the map.
* @return string URL to the map.
*/
public function getMapUrl($ips)
{
$url = sprintf("%s/map?cli=1", self::API_URL);
try {
$response = $this->http_client->request(
'POST',
$url,
[
'json' => $ips
]
);
} catch (GuzzleException $e) {
throw new IPinfoException($e->getMessage());
} catch (Exception $e) {
throw new IPinfoException($e->getMessage());
}
$res = json_decode($response->getBody(), true);
return $res['reportUrl'];
}
/**
* Build headers for API request.
* @return array Headers for API request.
*/
private function buildHeaders()
{
$headers = [
'user-agent' => 'IPinfoClient/PHP/3.3.0',
'accept' => 'application/json',
'content-type' => 'application/json',
];
if ($this->access_token) {
$headers['authorization'] = "Bearer {$this->access_token}";
}
return $headers;
}
/**
* Returns a versioned cache key given a user-input key.
* @param string $k key to transform into a versioned cache key.
* @return string the versioned cache key.
*/
private function cacheKey($k)
{
return sprintf('%s_v%s', $k, self::CACHE_KEY_VSN);
}
/**
* Check if an IP address is a bogon.
*
* @param string $ip The IP address to check
* @return bool True if the IP address is a bogon, false otherwise
*/
public function isBogon($ip)
{
// Check if the IP address is in the range
return IpUtils::checkIp($ip, $this->bogonNetworks);
}
// List of bogon CIDRs.
protected $bogonNetworks = [
"0.0.0.0/8",
"10.0.0.0/8",
"100.64.0.0/10",
"127.0.0.0/8",
"169.254.0.0/16",
"172.16.0.0/12",
"192.0.0.0/24",
"192.0.2.0/24",
"192.168.0.0/16",
"198.18.0.0/15",
"198.51.100.0/24",
"203.0.113.0/24",
"224.0.0.0/4",
"240.0.0.0/4",
"255.255.255.255/32",
"::/128",
"::1/128",
"::ffff:0:0/96",
"::/96",
"100::/64",
"2001:10::/28",
"2001:db8::/32",
"fc00::/7",
"fe80::/10",
"fec0::/10",
"ff00::/8",
"2002::/24",
"2002:a00::/24",
"2002:7f00::/24",
"2002:a9fe::/32",
"2002:ac10::/28",
"2002:c000::/40",
"2002:c000:200::/40",
"2002:c0a8::/32",
"2002:c612::/31",
"2002:c633:6400::/40",
"2002:cb00:7100::/40",
"2002:e000::/20",
"2002:f000::/20",
"2002:ffff:ffff::/48",
"2001::/40",
"2001:0:a00::/40",
"2001:0:7f00::/40",
"2001:0:a9fe::/48",
"2001:0:ac10::/44",
"2001:0:c000::/56",
"2001:0:c000:200::/56",
"2001:0:c0a8::/48",
"2001:0:c612::/47",
"2001:0:c633:6400::/56",
"2001:0:cb00:7100::/56",
"2001:0:e000::/36",
"2001:0:f000::/36",
"2001:0:ffff:ffff::/64"
];
}