-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIp.php
More file actions
231 lines (206 loc) · 8.84 KB
/
Ip.php
File metadata and controls
231 lines (206 loc) · 8.84 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
<?php
namespace TypechoPlugin\Access;
use Typecho\Plugin\Exception;
use Widget\Options;
if (!defined('__TYPECHO_ROOT_DIR__')) {
exit;
}
/**
* 地址解析类
*/
class Ip
{
private static array $cached = [];
/**
* @throws Exception
*/
public static function find(string $ip): array
{
if (empty($ip)) {
return [
'status' => 'failure',
'error' => 'IP地址为空',
'country' => null,
'region' => null,
'city' => null,
];
}
$nip = gethostbyname($ip);
$ipdot = explode('.', $nip);
if ($ipdot[0] < 0 || $ipdot[0] > 255 || count($ipdot) !== 4) {
return [
'status' => 'failure',
'error' => 'IP地址不合法',
'country' => null,
'region' => null,
'city' => null,
];
}
if (isset(self::$cached[$nip])) {
return self::$cached[$nip];
}
$config = Options::alloc()->plugin(basename(__DIR__));
$token = $config->isToken ?? '';
if ($token !== '') {
$result = self::queryIpInfo($nip, $token);
} else {
$result = self::queryKeyCdn($nip);
}
if ($result['status'] === 'success') {
self::$cached[$nip] = $result;
}
return $result;
}
/**
* 通过 IPinfo 接口查询地址详情
* 接口分为 Lite 和 Core 两种,分别为免费、付费接口
*
* @throws Exception
*/
private static function queryIpInfo(string $ip, string $token): array
{
$config = Options::alloc()->plugin(basename(__DIR__));
$isPaid = $config->isPaid ?? '0';
if ($isPaid === '1') {
$officialUrl = 'https://api.ipinfo.io/lookup/';
} else {
$officialUrl = 'https://api.ipinfo.io/lite/';
}
$url = $officialUrl . $ip . '?token=' . $token;
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4685.0 Safari/537.36');
$proxy = $config->socks5Host ?? '';
if ($proxy !== '') {
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5_HOSTNAME);
$auth = $config->socks5Auth ?? '';
if ($auth !== '') {
curl_setopt($ch, CURLOPT_PROXYUSERPWD, $auth);
}
}
$body = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($body === false) {
return ['status' => 'failure', 'error' => '请求返回失败', 'country' => null, 'region' => null, 'city' => null];
}
if ($httpCode !== 200) {
$err = json_decode($body, true);
$msg = $err['error'] ?? ('HTTP ' . $httpCode);
return ['status' => 'failure', 'error' => $msg, 'country' => null, 'region' => null, 'city' => null];
}
$json = json_decode($body, true);
if (!is_array($json) || empty($json['ip'])) {
return ['status' => 'failure', 'error' => '响应数据异常', 'country' => null, 'region' => null, 'city' => null];
}
if (isset($json['bogon']) && $json['bogon'] === true) {
return ['status' => 'success', 'error' => '保留地址区段', 'country' => null, 'region' => null, 'city' => null];
}
if ($isPaid === '1') {
return [
'status' => 'success',
'ip' => $json['ip'],
'city' => $json['geo']['city'] ?? '',
'region' => $json['geo']['region'] ?? '',
'regionCode' => $json['geo']['region_code'] ?? '',
'country' => $json['geo']['country'] ?? '',
'countryCode' => $json['geo']['country_code'] ?? '',
'continent' => $json['geo']['continent'] ?? '',
'continentCode' => $json['geo']['continent_code'] ?? '',
'latitude' => $json['geo']['latitude'] ?? '',
'longitude' => $json['geo']['longitude'] ?? '',
'timezone' => $json['geo']['timezone'] ?? '',
'postalCode' => $json['geo']['postal_code'] ?? '',
'asn' => $json['as']['asn'] ?? '',
'asName' => $json['as']['name'] ?? '',
'asDomain' => $json['as']['domain'] ?? '',
'asType' => $json['as']['type'] ?? '',
'isAnonymous' => $json['is_anonymous'] ?? '',
'isAnycast' => $json['is_anycast'] ?? '',
'isHosting' => $json['is_hosting'] ?? '',
'isMobile' => $json['is_mobile'] ?? '',
'isSatellite' => $json['is_satellite'] ?? '',
];
} elseif ($isPaid === '0') {
return [
'status' => 'success',
'ip' => $json['ip'],
'asn' => $json['asn'] ?? '',
'as_name' => $json['as_name'] ?? '',
'as_domain' => $json['as_domain'] ?? '',
'countryCode' => $json['country_code'] ?? '',
'country' => $json['country'] ?? '',
'continentCode' => $json['continent_code'] ?? '',
'continent' => $json['continent'] ?? '',
];
} else {
return ['status' => 'failure', 'error' => '未知错误', 'country' => null, 'region' => null, 'city' => null];
}
}
/**
* 通过 KeyCDN 接口查询(免费 fallback 接口)
*
* @throws Exception
*/
private static function queryKeyCdn(string $ip): array
{
if (!filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) {
return ['status' => 'success', 'error' => '保留地址区段', 'country' => null, 'region' => null, 'city' => null];
}
$config = Options::alloc()->plugin(basename(__DIR__));
$url = 'https://tools.keycdn.com/geo.json?host=' . $ip;
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_USERAGENT, 'keycdn-tools:https://www.bing.com');
$proxy = $config->socks5Host ?? '';
if ($proxy !== '') {
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5_HOSTNAME);
$auth = $config->socks5Auth ?? '';
if ($auth !== '') {
curl_setopt($ch, CURLOPT_PROXYUSERPWD, $auth);
}
}
$body = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($body === false) {
return ['status' => 'failure', 'error' => '请求返回失败', 'country' => null, 'region' => null, 'city' => null];
}
if ($httpCode !== 200) {
$err = json_decode($body, true);
$msg = $err['error'] ?? ('HTTP ' . $httpCode);
return ['status' => 'failure', 'error' => $msg, 'country' => null, 'region' => null, 'city' => null];
}
$json = json_decode($body, true);
if (!is_array($json) || ($json['status'] ?? '') !== 'success') {
return ['status' => 'failure', 'error' => '响应数据异常', 'country' => null, 'region' => null, 'city' => null];
}
$geo = $json['data']['geo'] ?? [];
return [
'status' => 'success',
'host' => $geo['host'] ?? null,
'ip' => $geo['ip'] ?? $ip,
'asn' => $geo['asn'] ?? null,
'isp' => $geo['isp'] ?? null,
'country' => $geo['country_name'] ?? null,
'countryCode' => $geo['country_code'] ?? null,
'region' => $geo['region_name'] ?? null,
'regionCode' => $geo['region_code'] ?? null,
'city' => $geo['city'] ?? null,
'zip' => $geo['postal_code'] ?? null,
'continent' => $geo['continent_name'] ?? null,
'continentCode' => $geo['continent_code'] ?? null,
'latitude' => $geo['latitude'] ?? null,
'longitude' => $geo['longitude'] ?? null,
'metroCode' => $geo['metro_code'] ?? null,
'timezone' => $geo['timezone'] ?? null,
'datetime' => $geo['datetime'] ?? null,
];
}
}