IpLimit.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. /**
  3. * 国内ip限制
  4. */
  5. namespace app\common\library;
  6. use think\Env;
  7. use think\Exception;
  8. class IpLimit
  9. {
  10. //白名单账号
  11. private $usernames = [
  12. 'inlude', 'inlude1'
  13. ];
  14. public function validateIp($ip, $username = '')
  15. {
  16. if (in_array($username, $this->usernames)) {
  17. return 1;
  18. }
  19. $tmp = cache('IpLimit:validateIp:' . $ip);
  20. if ($tmp > 0 && $tmp != 3) {
  21. return $tmp;
  22. }
  23. //$tmp 返回值 1 国外 2 国内 3 访问异常
  24. $tmp = $this->tencentIp($ip);
  25. if ($tmp != 3) {
  26. cache('IpLimit:validateIp:' . $ip, $tmp, 86400 * 7);
  27. return $tmp;
  28. }
  29. $tmp = $this->freeapi($ip);
  30. if ($tmp != 3) {
  31. cache('IpLimit:validateIp:' . $ip, $tmp, 86400 * 7);
  32. return $tmp;
  33. }
  34. $tmp = $this->pconline($ip);
  35. if ($tmp != 3) {
  36. cache('IpLimit:validateIp:' . $ip, $tmp, 86400 * 7);
  37. return $tmp;
  38. }
  39. $tmp = $this->ipApi($ip);
  40. if ($tmp != 3) {
  41. cache('IpLimit:validateIp:' . $ip, $tmp, 86400 * 7);
  42. return $tmp;
  43. }
  44. return $tmp;
  45. }
  46. //ip-api
  47. private function ipApi($ip)
  48. {
  49. try {
  50. $url = sprintf('http://ip-api.com/json/%s?lang=zh-CN', $ip);
  51. $result = curl($url);
  52. $res = json_decode($result, true);
  53. if ($res['status'] == 'success') {
  54. if ($res['country'] == '中国') {
  55. return 2;
  56. } else {
  57. return 1;
  58. }
  59. } else {
  60. $this->curlDDIp('ipApi', $ip, $result);
  61. return 3;
  62. }
  63. } catch (Exception $e) {
  64. $this->curlDDIp('ipApi', $ip, $e->getMessage());
  65. return 3;
  66. }
  67. }
  68. //太平洋
  69. public function pconline($ip)
  70. {
  71. try {
  72. $url = sprintf('http://whois.pconline.com.cn/ipJson.jsp?ip=%s&json=true', $ip);
  73. $result = curl($url);
  74. $result = iconv('GBK', "UTF-8//IGNORE", $result);
  75. $res = json_decode($result, true);
  76. if (isset($res['proCode'])) {
  77. if (!in_array($res['proCode'], [110000, 120000, 130000, 140000, 150000, 210000, 220000, 230000, 310000, 320000, 330000,
  78. 340000, 350000, 360000, 370000, 410000, 420000, 430000, 440000, 450000, 460000, 500000, 510000, 520000, 530000,
  79. 540000, 610000, 620000, 630000, 640000, 650000
  80. ])) {
  81. return 2;
  82. } else {
  83. return 1;
  84. }
  85. } else {
  86. $this->curlDDIp('pconline', $ip, $result);
  87. return 3;
  88. }
  89. } catch (Exception $e) {
  90. $this->curlDDIp('pconline', $ip, $e->getMessage());
  91. return 3;
  92. }
  93. }
  94. //BX3BZ-JRKE4-4XOU7-XDFVB-VGEO3-LLBXQ
  95. //3JABZ-46O6D-M6R45-PPVSG-2BNXK-VSFL4
  96. public function tencentIp($ip)
  97. {
  98. try {
  99. $url = sprintf('https://apis.map.qq.com/ws/location/v1/ip?output=json&key=BX3BZ-JRKE4-4XOU7-XDFVB-VGEO3-LLBXQ&ip=%s', $ip);
  100. $result = curl($url);
  101. $res = json_decode($result, true);
  102. if ($res['status'] == 0) {
  103. if ($res['result']['ad_info']['nation'] == '中国') {
  104. return 2;
  105. } else {
  106. return 1;
  107. }
  108. } else {
  109. $url = sprintf('https://apis.map.qq.com/ws/location/v1/ip?output=json&key=3JABZ-46O6D-M6R45-PPVSG-2BNXK-VSFL4&ip=%s', $ip);
  110. $result = curl($url);
  111. $res = json_decode($result, true);
  112. if ($res['status'] == 0) {
  113. if ($res['result']['ad_info']['nation'] == '中国') {
  114. return 2;
  115. } else {
  116. return 1;
  117. }
  118. } else {
  119. $this->curlDDIp('tencentIp', $ip, $result);
  120. return 3;
  121. }
  122. }
  123. } catch (Exception $e) {
  124. $this->curlDDIp('tencentIp', $ip, $e->getMessage());
  125. return 3;
  126. }
  127. }
  128. public function freeapi($ip)
  129. {
  130. try {
  131. $url = sprintf('http://freeapi.ipip.net/%s', $ip);
  132. $result = curl($url);
  133. $res = json_decode($result, true);
  134. if (isset($res[0])) {
  135. if ($res[0] == '中国') {
  136. return 2;
  137. } else {
  138. return 1;
  139. }
  140. } else {
  141. $this->curlDDIp('freeapi', $ip, $result);
  142. return 3;
  143. }
  144. } catch (Exception $e) {
  145. $this->curlDDIp('freeapi', $ip, $e->getMessage());
  146. return 3;
  147. }
  148. }
  149. public function curlDDIp($action, $ip, $msg)
  150. {
  151. $template = 'ip归属地获取失败:' . $action . ' ' . $ip . ' ' . $msg;
  152. $ddurl = Env::get('operat_url');
  153. curlDD($template, $ddurl, true);
  154. }
  155. }