IP.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892
  1. <?php
  2. namespace App\Utils;
  3. use Arr;
  4. use Cache;
  5. use Exception;
  6. use GeoIp2\Database\Reader;
  7. use GeoIp2\Exception\AddressNotFoundException;
  8. use Http;
  9. use Illuminate\Http\Client\PendingRequest;
  10. use IP2Location\Database;
  11. use ipip\db\City;
  12. use Log;
  13. use MaxMind\Db\Reader\InvalidDatabaseException;
  14. use XdbSearcher;
  15. class IP
  16. {
  17. private const CACHE_TAG = 'IP_INFO'; // 公共常量 / 默认键
  18. private static ?PendingRequest $basicRequest = null;
  19. public static function getClientIP(): ?string
  20. { // 获取访客真实IP
  21. return request()?->ip();
  22. }
  23. public static function getIPInfo(string $ip, ?string $checker = null): array|null|false
  24. { // 获取 IP 信息
  25. if (in_array($ip, ['::1', '127.0.0.1'], true)) {
  26. return false;
  27. }
  28. if ($checker !== null) {
  29. $result = self::IPLookup($ip, [$checker]);
  30. } else {
  31. $cached = Cache::tags(self::CACHE_TAG)->get($ip);
  32. if ($cached && ! empty(array_filter($cached))) {
  33. return $cached;
  34. }
  35. $isIpv4 = self::isIpv4($ip);
  36. if (app()->getLocale() === 'zh_CN') {
  37. $checkers = $isIpv4
  38. ? ['ipApi', 'Baidu', 'baiduBce', 'ipw', 'ipGeoLocation', 'TaoBao', 'speedtest', 'bjjii', 'vore', 'juHe', 'ip2Region', 'IPDB', 'ipwhois', 'pconline']
  39. : ['ipApi', 'Baidu', 'baiduBce', 'ipw', 'ipGeoLocation', 'vore', 'ip2Region'];
  40. } else {
  41. $checkers = ['ipApi', 'IPSB', 'ipinfo', 'ip234', 'ipGeoLocation', 'dbIP', 'IP2Online', 'ipdata', 'ipApiIS', 'ipApiCo', 'ip2Location', 'GeoIP2', 'ipApiCom', 'ipApiIO', 'freeipapi'];
  42. }
  43. $result = self::IPLookup($ip, $checkers);
  44. }
  45. if ($result !== null) {
  46. $result['address'] = implode(' ', Arr::except(array_filter($result), ['isp', 'latitude', 'longitude']));
  47. Cache::tags(self::CACHE_TAG)->put($ip, $result, Day);
  48. }
  49. return $result;
  50. }
  51. private static function IPLookup(string $ip, array $checkers): ?array
  52. {
  53. foreach ($checkers as $checker) {
  54. if (! method_exists(self::class, $checker)) {
  55. continue;
  56. }
  57. try {
  58. $result = call_user_func([self::class, $checker], $ip);
  59. if (is_array($result) && ! empty(array_filter($result))) {
  60. return $result;
  61. }
  62. } catch (Exception $e) {
  63. Log::error("[$checker] IP信息获取报错: ".$e->getMessage());
  64. }
  65. }
  66. return null;
  67. }
  68. private static function isIpv4(string $ip): bool
  69. {
  70. return (bool) filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);
  71. }
  72. public static function getIPGeo(string $ip, ?string $checker = null): array|false
  73. { // 仅获取经纬度
  74. if ($checker !== null) {
  75. $ret = self::IPLookup($ip, [$checker]);
  76. } else {
  77. $ret = self::IPLookup($ip, ['IPSB', 'ipApi', 'ipw', 'ipinfo', 'IP2Online', 'speedtest', 'bjjii', 'Baidu', 'ip234', 'ipdata', 'ipGeoLocation', 'ipApiIS', 'ipApiCo', 'ipApiCom', 'ip2Location', 'ipApiIO', 'ipwhois', 'freeipapi']);
  78. }
  79. if (is_array($ret)) {
  80. return Arr::only($ret, ['latitude', 'longitude']);
  81. }
  82. return false;
  83. }
  84. private static function http(): PendingRequest
  85. { // 统一的HTTP客户端方法
  86. if (! self::$basicRequest) {
  87. self::$basicRequest = Http::timeout(5)->retry(2)->withOptions(['http_errors' => false])->withoutVerifying()->withUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36');
  88. }
  89. return self::$basicRequest;
  90. }
  91. private static function ipApi(string $ip): ?array
  92. { // 开发依据: https://ip-api.com/docs/api:json
  93. $client = self::http()->withHeader('Origin', 'https://members.ip-api.com');
  94. $lang = str_replace('_', '-', app()->getLocale());
  95. $key = config('services.ip.ip-api_key');
  96. if (empty($key)) {
  97. $response = $client->get("https://demo.ip-api.com/json/$ip?fields=582361&key=$key&lang=$lang");
  98. } else {
  99. $response = $client->get("https://pro.ip-api.com/json/$ip?fields=582361&key=$key&lang=$lang");
  100. }
  101. if ($response->ok()) {
  102. $data = $response->json();
  103. if ($data['status'] === 'success' && $data['query'] === $ip) {
  104. return [
  105. 'country' => $data['country'] ?? null,
  106. 'region' => $data['regionName'] ?? null,
  107. 'city' => $data['city'] ?? null,
  108. 'isp' => $data['isp'] ?? null,
  109. 'area' => $data['district'] ?? null,
  110. 'latitude' => $data['lat'] ?? null,
  111. 'longitude' => $data['lon'] ?? null,
  112. ];
  113. }
  114. Log::error('【ip-api.com】ip查询失败:'.($data['message'] ?? 'unknown'));
  115. } else {
  116. Log::error('【ip-api.com】查询无效:'.$ip);
  117. }
  118. return null;
  119. }
  120. private static function Baidu(string $ip): ?array
  121. { // 通过api.map.baidu.com查询IP地址的详细信息,依据 http://lbsyun.baidu.com/index.php?title=webapi/ip-api 开发
  122. $client = self::http();
  123. $key = config('services.ip.baidu_ak');
  124. if (empty($key)) {
  125. return null;
  126. }
  127. $response = $client->get("https://api.map.baidu.com/location/ip?ak=$key&ip=$ip&coor=gcj02");
  128. if (! $response->ok()) {
  129. Log::error('【百度IP库】解析异常:'.$ip);
  130. return null;
  131. }
  132. $message = $response->json();
  133. if ($message['status'] === 0) {
  134. $location = isset($message['address']) ? explode('|', $message['address']) : [];
  135. return [
  136. 'country' => $location[0] ?? null,
  137. 'region' => $message['content']['address_detail']['province'] ?? null,
  138. 'city' => $message['content']['address_detail']['city'] ?? null,
  139. 'isp' => $location[4] ?? null,
  140. 'area' => $message['content']['address_detail']['street'] ?? null,
  141. 'latitude' => $message['content']['point']['y'] ?? null,
  142. 'longitude' => $message['content']['point']['x'] ?? null,
  143. ];
  144. }
  145. Log::warning('【百度IP库】返回错误信息:'.$ip.PHP_EOL.var_export($message, true));
  146. return null;
  147. }
  148. private static function baiduBce(string $ip): ?array
  149. { // 依据 https://qifu.baidu.com/?activeId=SEARCH_IP_ADDRESS&ip=&_frm=aladdin
  150. $client = self::http();
  151. $isIpv4 = self::isIpv4($ip);
  152. $url = $isIpv4
  153. ? "https://qifu-api.baidubce.com/ip/geo/v1/district?ip=$ip"
  154. : "https://qifu-api.baidubce.com/ip/geo/v1/ipv6/district?ip=$ip";
  155. $response = $client->get($url);
  156. if (! $response->ok()) {
  157. Log::error('【baiduBce】查询无效:'.$ip.var_export($response->json(), true));
  158. return null;
  159. }
  160. $data = $response->json();
  161. if ($data && $data['code'] === 'Success' && $data['ip'] === $ip) {
  162. $ipData = $data['data'] ?? null;
  163. if ($ipData) {
  164. return [
  165. 'country' => $ipData['country'] ?? null,
  166. 'region' => $ipData['prov'] ?? null,
  167. 'city' => $ipData['city'] ?? null,
  168. 'isp' => $ipData['isp'] ?: $ipData['owner'] ?? null,
  169. 'area' => $ipData['district'] ?? null,
  170. ];
  171. }
  172. }
  173. Log::error('【baiduBce】IP查询失败:'.($data['msg'] ?? 'unknown'));
  174. return null;
  175. }
  176. private static function ipGeoLocation(string $ip): ?array
  177. { // 开发依据: https://ipgeolocation.io/documentation.html
  178. $client = self::http();
  179. $lang = config('common.language.'.app()->getLocale().'.1');
  180. $response = $client->withHeader('Origin', 'https://ipgeolocation.io')
  181. ->get("https://api.ipgeolocation.io/ipgeo?ip=$ip&fields=country_name,state_prov,district,city,isp,latitude,longitude&lang=$lang");
  182. if (! $response->ok()) {
  183. Log::error('【ipGeoLocation】查询无效:'.$ip.var_export($response->json(), true));
  184. return null;
  185. }
  186. $data = $response->json();
  187. if ($data && $data['ip'] === $ip) {
  188. return [
  189. 'country' => $data['country_name'] ?? null,
  190. 'region' => $data['state_prov'] ?? null,
  191. 'city' => $data['city'] ?? null,
  192. 'isp' => $data['isp'] ?? null,
  193. 'area' => $data['district'] ?? null,
  194. 'latitude' => $data['latitude'] ?? null,
  195. 'longitude' => $data['longitude'] ?? null,
  196. ];
  197. }
  198. Log::error('【ipgeolocation.io】IP查询失败:'.($data ?? 'unknown'));
  199. return null;
  200. }
  201. private static function TaoBao(string $ip): ?array
  202. { // 通过ip.taobao.com查询IP地址的详细信息 依据 https://ip.taobao.com/instructions 开发
  203. $client = self::http();
  204. $response = $client->post("https://ip.taobao.com/outGetIpInfo?ip=$ip&accessKey=alibaba-inc");
  205. if (! $response->ok()) {
  206. Log::error('【淘宝IP库】解析异常:'.$ip);
  207. return null;
  208. }
  209. $message = $response->json();
  210. $data = $message['data'] ?? null;
  211. if ($message['code'] === 0 && $data['ip'] === $ip) {
  212. // 简化三元表达式
  213. $fields = ['country', 'region', 'city', 'isp', 'area'];
  214. $result = [];
  215. foreach ($fields as $field) {
  216. $value = $data[$field] ?? null;
  217. $result[$field] = (isset($value) && strtolower($value) !== 'xx') ? $value : null;
  218. }
  219. return $result;
  220. }
  221. Log::warning('【淘宝IP库】返回错误信息:'.$ip.PHP_EOL.($message['msg'] ?? json_encode($message)));
  222. return null;
  223. }
  224. private static function speedtest(string $ip): ?array
  225. {
  226. $client = self::http();
  227. $response = $client->withHeaders(['Clientectype' => 65, 'Encrypt' => 'true'])->get('https://api-v3.speedtest.cn/ip', ['data' => base64_encode(openssl_encrypt(json_encode(['ip' => $ip], JSON_THROW_ON_ERROR), 'AES-128-CBC', '5ECC5D62140EC099', OPENSSL_RAW_DATA, 'E63EA892A702EEAA'
  228. ))]);
  229. if (! $response->ok()) {
  230. Log::error('【speedtest】查询无效:'.$ip.var_export($response->json(), true));
  231. return null;
  232. }
  233. $data = $response->json();
  234. if ($data['code'] === 0 && $data["'msg'"] === 'ok') {
  235. $ipData = json_decode(openssl_decrypt(base64_decode($data['data']), 'AES-128-CBC', '5ECC5D62140EC099', OPENSSL_RAW_DATA, 'E63EA892A702EEAA'), true, 512, JSON_THROW_ON_ERROR);
  236. if ($ipData['ip'] !== $ip) {
  237. Log::error('【speedtest】IP不一致,查询IP:'.$ip.' 返回IP:'.$ipData['ip'] ?? 'null');
  238. return null;
  239. }
  240. return [
  241. 'country' => $ipData['country'] ?? null,
  242. 'region' => $ipData['province'] ?? null,
  243. 'city' => $ipData['city'] ?? null,
  244. 'isp' => $ipData['isp'] ?: $ipData['operator'] ?? null,
  245. 'area' => $ipData['district'] ?? null,
  246. 'latitude' => $ipData['lat'] ?? null,
  247. 'longitude' => $ipData['lon'] ?? null,
  248. ];
  249. }
  250. Log::error('【speedtest】IP查询失败');
  251. return null;
  252. }
  253. private static function juHe(string $ip): ?array
  254. { // 开发依据: https://www.juhe.cn/docs/api/id/1
  255. $client = self::http();
  256. $response = $client->asForm()->post('https://apis.juhe.cn/ip/Example/query.php', ['IP' => $ip]);
  257. if (! $response->ok()) {
  258. Log::error('【juHe】查询无效:'.$ip.var_export($response->json(), true));
  259. return null;
  260. }
  261. $data = $response->json();
  262. if ($data['resultcode'] === '200' && $data['error_code'] === 0) {
  263. $ipData = $data['result'];
  264. if ($ipData) {
  265. return [
  266. 'country' => $ipData['Country'] ?? null,
  267. 'region' => $ipData['Province'] ?? null,
  268. 'city' => $ipData['City'] ?? null,
  269. 'isp' => $ipData['Isp'] ?? null,
  270. 'area' => $ipData['District'] ?? null,
  271. ];
  272. }
  273. }
  274. return null;
  275. }
  276. private static function ip2Region(string $ip): ?array
  277. { // 通过ip2Region查询IP地址的详细信息 数据库不经常更新
  278. try {
  279. $data = (new XdbSearcher)->search($ip);
  280. } catch (Exception $e) {
  281. Log::error('【ip2Region】错误信息:'.$e->getMessage());
  282. return null;
  283. }
  284. if (! empty($data)) {
  285. $location = explode('|', $data);
  286. return [
  287. 'country' => $location[0] ?? null,
  288. 'region' => $location[2] ?? null,
  289. 'city' => $location[3] ?? null,
  290. 'isp' => $location[4] ?? null,
  291. 'area' => $location[1] ?? null,
  292. ];
  293. }
  294. return null;
  295. }
  296. private static function IPDB(string $ip): array
  297. { // 通过IPDB格式的离线数据查询IP地址的详细信息 来源: https://github.com/metowolf/qqwry.ipdb
  298. $filePath = database_path('qqwry.ipdb');
  299. $location = (new City($filePath))->findMap($ip, 'CN');
  300. return [
  301. 'country' => $location['country_name'] ?? null,
  302. 'region' => $location['region_name'] ?? null,
  303. 'city' => $location['city_name'] ?? null,
  304. 'isp' => $location['isp_domain'] ?? null,
  305. 'area' => null,
  306. ];
  307. }
  308. private static function IPSB(string $ip): ?array
  309. { // 通过api.ip.sb查询IP地址的详细信息
  310. $client = self::http();
  311. try {
  312. $response = $client->post("https://api.ip.sb/geoip/$ip");
  313. if (! $response->ok()) {
  314. Log::warning('[IPSB] 解析'.$ip.'异常: '.$response->body());
  315. return null;
  316. }
  317. $data = $response->json();
  318. if ($data && $data['ip'] && $data['ip'] === $ip) {
  319. return [
  320. 'country' => $data['country'] ?? null,
  321. 'region' => $data['region'] ?? null,
  322. 'city' => $data['city'] ?? null,
  323. 'isp' => $data['organization'] ?? null,
  324. 'area' => null,
  325. 'latitude' => $data['latitude'] ?? null,
  326. 'longitude' => $data['longitude'] ?? null,
  327. ];
  328. }
  329. } catch (Exception $e) {
  330. Log::error('[IPSB] 解析'.$ip.'错误: '.var_export($e->getMessage(), true));
  331. }
  332. return null;
  333. }
  334. private static function ipinfo(string $ip): ?array
  335. { // 开发依据: https://ipinfo.io/account/home
  336. $client = self::http();
  337. $key = config('services.ip.ipinfo_token');
  338. if (empty($key)) {
  339. return null;
  340. }
  341. $response = $client->acceptJson()->get("https://ipinfo.io/$ip?token=$key");
  342. if (! $response->ok()) {
  343. Log::error('【ipinfo】解析异常:'.$ip);
  344. return null;
  345. }
  346. $data = $response->json();
  347. if ($data && $data['ip'] === $ip) {
  348. $location = explode(',', $data['loc'] ?? '');
  349. return [
  350. 'country' => $data['country'] ?? null,
  351. 'region' => $data['region'] ?? null,
  352. 'city' => $data['city'] ?? null,
  353. 'isp' => $data['org'] ?? null,
  354. 'area' => null,
  355. 'latitude' => $location[0] ?? null,
  356. 'longitude' => $location[1] ?? null,
  357. ];
  358. }
  359. return null;
  360. }
  361. private static function ip234(string $ip): ?array
  362. {
  363. $client = self::http();
  364. $response = $client->get("https://ip234.in/search_ip?ip=$ip");
  365. if (! $response->ok()) {
  366. Log::error('【ip234】查询无效:'.$ip.var_export($response->json(), true));
  367. return null;
  368. }
  369. $data = $response->json();
  370. if ($data['code'] === 0) {
  371. $ipData = $data['data'];
  372. if ($ipData && $ipData['ip'] === $ip) {
  373. return [
  374. 'country' => $ipData['country'] ?? null,
  375. 'region' => $ipData['region'] ?? null,
  376. 'city' => $ipData['city'] ?? null,
  377. 'isp' => $ipData['organization'] ?? null,
  378. 'area' => null,
  379. 'latitude' => $ipData['latitude'] ?? null,
  380. 'longitude' => $ipData['longitude'] ?? null,
  381. ];
  382. }
  383. }
  384. Log::error('【ip234】IP查询失败:'.($data['msg'] ?? 'unknown'));
  385. return null;
  386. }
  387. private static function dbIP(string $ip): ?array
  388. { // 开发依据: https://db-ip.com/api/doc.php
  389. $client = self::http();
  390. $response = $client->acceptJson()->get("https://api.db-ip.com/v2/free/$ip");
  391. if (! $response->ok()) {
  392. Log::error('【dbIP】查询无效:'.$ip.var_export($response->json(), true));
  393. return null;
  394. }
  395. $data = $response->json();
  396. if ($data && $data['ipAddress'] === $ip) {
  397. return [
  398. 'country' => $data['countryName'] ?? null,
  399. 'region' => $data['stateProv'] ?? null,
  400. 'city' => $data['city'] ?? null,
  401. 'isp' => null,
  402. 'area' => null,
  403. ];
  404. }
  405. return null;
  406. }
  407. private static function IP2Online(string $ip): ?array
  408. { // 开发依据: https://www.ip2location.io/ip2location-documentation
  409. $client = self::http();
  410. $key = config('services.ip.IP2Location_key');
  411. if (empty($key)) {
  412. $response = $client->acceptJson()->get("https://api.ip2location.io/?ip=$ip");
  413. } else {
  414. $response = $client->acceptJson()->get("https://api.ip2location.io/?key=$key&ip=$ip");
  415. }
  416. if (! $response->ok()) {
  417. Log::error('【IP2Online】查询无效:'.$ip.var_export($response->json(), true));
  418. return null;
  419. }
  420. $data = $response->json();
  421. if ($data && $data['ip'] === $ip) {
  422. return [
  423. 'country' => $data['country_name'] ?? null,
  424. 'region' => $data['region_name'] ?? null,
  425. 'city' => $data['city_name'] ?? null,
  426. 'isp' => $data['as'] ?? null,
  427. 'area' => null,
  428. 'latitude' => $data['latitude'] ?? null,
  429. 'longitude' => $data['longitude'] ?? null,
  430. ];
  431. }
  432. return null;
  433. }
  434. private static function ipdata(string $ip): ?array
  435. { // 开发依据: https://docs.ipdata.co/docs
  436. $client = self::http();
  437. $key = config('services.ip.ipdata_key');
  438. if (empty($key)) {
  439. $response = $client->withHeader('Referer', 'https://ipdata.co/')->get("https://api.ipdata.co/$ip?api-key=dfaeafd1e8192e29db79905207d07059a81161c04fce90b040866b22&fields=ip,city,region,country_name,latitude,longitude,asn");
  440. } else {
  441. $response = $client->get("https://api.ipdata.co/$ip?api-key=$key&fields=ip,city,region,country_name,latitude,longitude,asn");
  442. }
  443. if (! $response->ok()) {
  444. Log::error('【ipdata】查询无效:'.$ip.var_export($response->json(), true));
  445. return null;
  446. }
  447. $data = $response->json();
  448. if ($data && $data['ip'] === $ip) {
  449. return [
  450. 'country' => $data['country_name'] ?? null,
  451. 'region' => $data['region'] ?? null,
  452. 'city' => $data['city'] ?? null,
  453. 'isp' => $data['asn']['name'] ?? null,
  454. 'area' => null,
  455. 'latitude' => $data['latitude'] ?? null,
  456. 'longitude' => $data['longitude'] ?? null,
  457. ];
  458. }
  459. return null;
  460. }
  461. private static function ipApiCo(string $ip): ?array
  462. { // 开发依据: https://ipapi.co/api/
  463. $client = self::http();
  464. $response = $client->get("https://ipapi.co/$ip/json/");
  465. if (! $response->ok()) {
  466. Log::error('【ipApiCo】查询无效:'.$ip.var_export($response->json(), true));
  467. return null;
  468. }
  469. $data = $response->json();
  470. if ($data && $data['ip'] === $ip) {
  471. return [
  472. 'country' => $data['country_name'] ?? null,
  473. 'region' => $data['region'] ?? null,
  474. 'city' => $data['city'] ?? null,
  475. 'isp' => $data['org'] ?? null,
  476. 'area' => null,
  477. 'latitude' => $data['latitude'] ?? null,
  478. 'longitude' => $data['longitude'] ?? null,
  479. ];
  480. }
  481. return null;
  482. }
  483. private static function ip2Location(string $ip): ?array
  484. { // 通过ip2Location查询IP地址的详细信息 来源: https://lite.ip2location.com/database-download
  485. $filePath = database_path('IP2LOCATION-LITE-DB11.IPV6.BIN');
  486. try {
  487. $location = (new Database($filePath, Database::FILE_IO))
  488. ->lookup($ip, [Database::CITY_NAME, Database::REGION_NAME, Database::COUNTRY_NAME, Database::LATITUDE, Database::LONGITUDE]);
  489. return [
  490. 'country' => $location['countryName'] ?? null,
  491. 'region' => $location['regionName'] ?? null,
  492. 'city' => $location['cityName'] ?? null,
  493. 'isp' => null,
  494. 'area' => null,
  495. 'latitude' => $location['latitude'] ?? null,
  496. 'longitude' => $location['longitude'] ?? null,
  497. ];
  498. } catch (Exception $e) {
  499. Log::error('【ip2Location】错误信息:'.$e->getMessage());
  500. }
  501. return null;
  502. }
  503. private static function GeoIP2(string $ip): ?array
  504. { // 通过GeoIP2查询IP地址的详细信息 来源:https://github.com/P3TERX/GeoLite.mmdb/releases
  505. $filePath = database_path('GeoLite2-City.mmdb');
  506. try {
  507. $location = (new Reader($filePath))->city($ip);
  508. return [
  509. 'country' => $location->country->name ?? null,
  510. 'region' => $location->mostSpecificSubdivision->name ?? null,
  511. 'city' => $location->city->name ?? null,
  512. 'isp' => null,
  513. 'area' => null,
  514. 'latitude' => $location->location->latitude ?? null,
  515. 'longitude' => $location->location->longitude ?? null,
  516. ];
  517. } catch (AddressNotFoundException $e) {
  518. Log::error("【GeoIP2】查询失败:$ip ".$e->getMessage());
  519. } catch (InvalidDatabaseException $e) {
  520. Log::error("【GeoIP2】数据库无效:$ip ".$e->getMessage());
  521. } catch (Exception $e) {
  522. Log::error("【GeoIP2】其他错误:$ip ".$e->getMessage());
  523. }
  524. return null;
  525. }
  526. private static function ipApiCom(string $ip): ?array
  527. {
  528. $client = self::http();
  529. $key = config('services.ip.ipApiCom_acess_key');
  530. if (empty($key)) {
  531. return null;
  532. }
  533. $response = $client->get("https://api.ipapi.com/api/$ip?access_key=$key");
  534. if (! $response->ok()) {
  535. Log::error('【ipApiCom】查询无效:'.$ip.var_export($response->json(), true));
  536. return null;
  537. }
  538. $data = $response->json();
  539. if ($data && $data['ip'] === $ip) {
  540. return [
  541. 'country' => $data['country_name'] ?? null,
  542. 'region' => $data['region_name'] ?? null,
  543. 'city' => $data['city'] ?? null,
  544. 'isp' => $data['connection']['isp'] ?? null,
  545. 'area' => null,
  546. 'latitude' => $data['latitude'] ?? null,
  547. 'longitude' => $data['longitude'] ?? null,
  548. ];
  549. }
  550. return null;
  551. }
  552. private static function vore(string $ip): ?array
  553. { // 开发依据: https://api.vore.top/
  554. $client = self::http();
  555. $response = $client->get("https://api.vore.top/api/IPdata?ip=$ip");
  556. if (! $response->ok()) {
  557. Log::error('【vore】查询无效:'.$ip.var_export($response->json(), true));
  558. return null;
  559. }
  560. $data = $response->json();
  561. if ($data['code'] === 200) {
  562. $ipData = $data['ipdata'];
  563. if ($ipData) {
  564. return [
  565. 'country' => $ipData['info1'] ?? null,
  566. 'region' => $ipData['info2'] ?? null,
  567. 'city' => $ipData['info3'] ?? null,
  568. 'isp' => $ipData['isp'] ?? null,
  569. 'area' => null,
  570. ];
  571. }
  572. }
  573. return null;
  574. }
  575. private static function ipw(string $ip): ?array
  576. { // 开发依据: https://ipw.cn/ip/ https://ipw.cn/ipv6/
  577. $client = self::http();
  578. $response = $client->withHeader('Referer', 'https://ipw.cn/')->get('https://rest.ipw.cn/api/aw/v1/ip'.(self::isIpv4($ip) ? 'v4' : 'v6')."?ip=$ip&warning=please-direct-use-please-use-ipplus360.com");
  579. if (! $response->ok()) {
  580. Log::error('【ipw】查询无效:'.$ip.var_export($response->json(), true));
  581. return null;
  582. }
  583. $data = $response->json();
  584. if ($data && $data['code'] === 'Success' && $data['ip'] === $ip) {
  585. $ipData = $data['data'];
  586. if ($ipData) {
  587. return [
  588. 'country' => $ipData['country'] ?? null,
  589. 'region' => $ipData['prov'] ?? null,
  590. 'city' => $ipData['city'] ?? null,
  591. 'isp' => $ipData['isp'] ?? null,
  592. 'area' => $ipData['district'] ?? null,
  593. 'latitude' => $ipData['lat'] ?? null,
  594. 'longitude' => $ipData['lng'] ?? null,
  595. ];
  596. }
  597. }
  598. return null;
  599. }
  600. private static function bjjii(string $ip): ?array
  601. { // 开发依据: https://api.bjjii.com/doc/77
  602. $client = self::http();
  603. $key = config('services.ip.bjjii_key');
  604. if (empty($key)) {
  605. return null;
  606. }
  607. $response = $client->get("https://api.bjjii.com/api/ip/query?key=$key&ip=$ip");
  608. if (! $response->ok()) {
  609. Log::error('【bjjii】查询无效:'.$ip.var_export($response->json(), true));
  610. return null;
  611. }
  612. $data = $response->json();
  613. if ($data['code'] === 200 && $data['data']['ip'] === $ip) {
  614. $ipData = $data['data']['info'];
  615. if ($ipData) {
  616. return [
  617. 'country' => $ipData['nation'] ?? null,
  618. 'region' => $ipData['province'] ?? null,
  619. 'city' => $ipData['city'] ?? null,
  620. 'isp' => $ipData['isp'] ?? null,
  621. 'area' => $ipData['district'] ?? null,
  622. 'latitude' => $ipData['lat'] ?? null,
  623. 'longitude' => $ipData['lng'] ?? null,
  624. ];
  625. }
  626. }
  627. return null;
  628. }
  629. private static function pconline(string $ip): ?array
  630. { // ipv4 only
  631. $client = self::http();
  632. $response = $client->get("https://whois.pconline.com.cn/ipJson.jsp?ip=$ip&json=true");
  633. $data = json_decode(mb_convert_encoding($response->body(), 'UTF-8', 'GBK'), true, 512, JSON_THROW_ON_ERROR);
  634. if (! $response->ok()) {
  635. Log::error('【pconline】查询无效:'.$ip.var_export($data, true));
  636. return null;
  637. }
  638. if ($data && $data['ip'] === $ip) {
  639. return [
  640. 'country' => null,
  641. 'region' => $data['pro'] ?? null,
  642. 'city' => $data['city'] ?? null,
  643. 'isp' => null,
  644. 'area' => $data['region'] ?? null,
  645. ];
  646. }
  647. Log::error('【pconline】IP查询失败:'.($data['msg'] ?? 'unknown'));
  648. return null;
  649. }
  650. private static function ipApiIO(string $ip): ?array
  651. { // 开发依据: https://ip-api.io/
  652. $client = self::http();
  653. $response = $client->get("https://ip-api.io/api/v1/ip/$ip");
  654. if (! $response->ok()) {
  655. Log::error('【ipApiIO】查询无效:'.$ip.var_export($response->json(), true));
  656. return null;
  657. }
  658. $data = $response->json();
  659. if ($data && $data['ip'] === $ip) {
  660. $ipData = $data['location'];
  661. if ($ipData) {
  662. return [
  663. 'country' => $ipData['country'] ?? null,
  664. 'region' => null,
  665. 'city' => $ipData['city'] ?? null,
  666. 'isp' => null,
  667. 'area' => null,
  668. 'latitude' => $ipData['latitude'] ?? null,
  669. 'longitude' => $ipData['longitude'] ?? null,
  670. ];
  671. }
  672. }
  673. return null;
  674. }
  675. private static function ipApiIS(string $ip): ?array
  676. {
  677. $client = self::http();
  678. $response = $client->get("https://api.ipapi.is/?ip=$ip");
  679. if (! $response->ok()) {
  680. Log::error('【ipApiIS】查询无效:'.$ip.var_export($response->json(), true));
  681. return null;
  682. }
  683. $data = $response->json();
  684. if ($data && $data['ip'] === $ip) {
  685. $ipData = $data['location'];
  686. if ($ipData) {
  687. return [
  688. 'country' => $ipData['country'] ?? null,
  689. 'region' => $ipData['state'] ?? null,
  690. 'city' => $ipData['city'] ?? null,
  691. 'isp' => $data['asn']['org'] ?? null,
  692. 'area' => null,
  693. 'latitude' => $ipData['latitude'] ?? null,
  694. 'longitude' => $ipData['longitude'] ?? null,
  695. ];
  696. }
  697. }
  698. return null;
  699. }
  700. private static function freeipapi(string $ip): ?array
  701. {
  702. $client = self::http();
  703. $response = $client->get("https://free.freeipapi.com/api/json/$ip");
  704. if (! $response->ok()) {
  705. Log::error('【freeipapi】查询无效:'.$ip.var_export($response->json(), true));
  706. return null;
  707. }
  708. $data = $response->json();
  709. if ($data && $data['ipAddress'] === $ip) {
  710. return [
  711. 'country' => $data['countryName'] ?? null,
  712. 'region' => $data['regionName'] ?? null,
  713. 'city' => $data['cityName'] ?? null,
  714. 'isp' => $data['asnOrganization'] ?? null,
  715. 'area' => null,
  716. 'latitude' => $data['latitude'] ?? null,
  717. 'longitude' => $data['longitude'] ?? null,
  718. ];
  719. }
  720. return null;
  721. }
  722. private static function ipwhois(string $ip): ?array
  723. {
  724. $client = self::http();
  725. $response = $client->get("https://ipwhois.app/json/$ip?format=json");
  726. if (! $response->ok()) {
  727. Log::error('【ipwhois】查询无效:'.$ip.var_export($response->json(), true));
  728. return null;
  729. }
  730. $data = $response->json();
  731. if ($data && $data['success'] && $data['ip'] === $ip) {
  732. return [
  733. 'country' => $data['country'] ?? null,
  734. 'region' => $data['region'] ?? null,
  735. 'city' => $data['city'] ?? null,
  736. 'isp' => $data['isp'] ?? null,
  737. 'area' => null,
  738. 'latitude' => $data['latitude'] ?? null,
  739. 'longitude' => $data['longitude'] ?? null,
  740. ];
  741. }
  742. return null;
  743. }
  744. }