-
Notifications
You must be signed in to change notification settings - Fork 0
/
fake-cf-map.php
70 lines (68 loc) · 2.56 KB
/
fake-cf-map.php
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
<?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:2339");
$ip_worker->count = 1;
$ip_worker->name = 'fake-cf-map';
$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':
$info = $asn_reader->asn($ip)->autonomousSystemNumber;
break;
case '/country':
$info = $city_reader->city($ip)->country->isoCode;
break;
case '/ua':
$info = $request->header()['user-agent'];
break;
default:
$arr = [
'ip' => '127.0.0.1',
'ip_version' => 1,
'protocol' => 'udp',
'dnssec' => true,
'edns' => 0,
'client_subnet' => -1,
'qname_minimization' => false,
'isp' => [
'asn' => $asn_reader->asn($ip)->autonomousSystemNumber,
'name' => $asn_reader->asn($ip)->autonomousSystemOrganization.' with Crazy DNS'
]
];
$info = json_encode($arr);
}
$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);
};
Worker::runAll();