-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathip.php
More file actions
81 lines (79 loc) · 3.05 KB
/
ip.php
File metadata and controls
81 lines (79 loc) · 3.05 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
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Workerman\Worker;
use Workerman\Connection\TcpConnection;
use Workerman\Protocols\Http\Request;
use Workerman\Protocols\Http\Response;
use SebastianBergmann\Timer\Timer;
use GeoIp2\Database\Reader;
$ip_worker = new Worker("http://0.0.0.0:2335");
$ip_worker->count = 1;
$ip_worker->name = 'ip';
$ip_worker->onWorkerStart = function (Worker $worker) {
global $city_reader, $asn_reader;
$city_reader = new Reader('/usr/local/share/GeoIP/GeoLite2-City.mmdb');
$asn_reader = new Reader('/usr/local/share/GeoIP/GeoLite2-ASN.mmdb');
};
$ip_worker->onMessage = function (TcpConnection $connection, Request $request) {
global $city_reader, $asn_reader;
$timer = new Timer;
$timer->start();
$ip = ($request->header('X-Real-IP')) ?
$request->header('X-Real-IP') : $connection->getRemoteIp();
//if($request->path() == '/favicon.ico') $connection->close(new Response(204));
if ($request->header('x-vercel-id')) {
list($cdn, ) = explode('::', $request->header('x-vercel-id'));
} else if ($request->header('CF-RAY')) {
list(, $cdn) = explode('-', $request->header('CF-RAY'));
$ip = $request->header('CF-Connecting-IP');
}
switch ($request->path()) {
case '/ip':
$info = $ip;
break;
case '/asn':
try {
$info = $asn_reader->asn($ip)->autonomousSystemNumber;
} catch (GeoIp2\Exception\AddressNotFoundException $e) {
echo "The address is not in the database.\n";
}
break;
case '/country':
try {
$info = $city_reader->city($ip)->country->isoCode;
} catch (GeoIp2\Exception\AddressNotFoundException $e) {
echo "The address is not in the database.\n";
}
break;
case '/ua':
$info = $request->header()['user-agent'];
break;
default:
try {
$isoCode = $city_reader->city($ip)->country->isoCode;
$city_name = $city_reader->city($ip)->country->name;
$asn_num = $asn_reader->asn($ip)->autonomousSystemNumber;
$asn_org = $asn_reader->asn($ip)->autonomousSystemOrganization;
} catch (GeoIp2\Exception\AddressNotFoundException $e) {
echo "The address is not in the database.\n";
}
$info = sprintf(
"%s\n%s / %s\nAS%s / %s\n\n%s\n%s",
$ip,
$isoCode,
$city_name,
$asn_num,
$asn_org,
$request->header()['user-agent'],
$cdn
);
}
$response = new Response(200, [
'X-Powered-By' => 'Workerman ' . Worker::VERSION,
'Connection' => 'close',
'Content-Security-Policy' => "img-src 'none'",
'Content-Type' => 'text/plain; charset=UTF-8',
], $info . "\n");
$response->header('Server-Timing', $timer->stop()->asMilliseconds());
$connection->close($response);
};