IP.php 29 KB

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