设为首页收藏本站手机版导航 今天是: 2025-09-03    "抗日胜利纪念日"  
天气与日历 切换到宽版

 找回密码
 立即注册
搜索
热搜: 吉他 seo 大全
查看: 2607|回复: 0

[JavaScript] JS屏蔽指定地区及设备访问代码(含蜘蛛爬虫识别)

[复制链接]
  • 打卡总天数:148

494

主题

34

回帖

24万

积分

管理员

积分
244977

荣誉管理论坛元老本科学士学位百达翡丽奔驰

QQ

皮卡丘 Lv:40
发表于 2022-11-16 10:20:16 来自手机 | 显示全部楼层 |阅读模式 IP:北京
本文介绍如何使用 JavaScript 实现 IP 地域屏蔽 和 设备访问控制,并支持 爬虫(蜘蛛)检测。通过解析用户 IP 地址,可限制特定国家/地区的访问,同时识别访问设备类型(如 PC、手机或爬虫),从而增强网站安全性和访问控制。

JS屏蔽指定地区及设备访问代码(含蜘蛛爬虫识别)

代码灵活调整,可指定屏蔽地区、设备、蜘蛛、用户
以下代码移动设备及蜘蛛可正常访问,北京及运城地区的pc端用户无法访问(百度站长工具抓取测试正常

风险:长期使用可能导致网站降权
  1. <script type="text/javascript">  
  2. (function () {
  3.     const isPC = !/mobile|android|iphone|ipad|phone/i.test(navigator.userAgent.toLowerCase());// 移动设备
  4.     const isSpider = navigator.userAgent.match(/(bot|spider|crawler)/i) ? 1 : 2;// 爬虫
  5.     if (!isPC || isSpider === 1) return; //移动端及蜘蛛访问正常返回页面
  6.     const controller = new AbortController();// 超时控制器
  7.     const timeout = setTimeout(() => controller.abort(), 3000);
  8.     const error_path = "error.html";// 重定向目标  
  9.     function zhi_ding_diqu(province, city){
  10.         return (province.includes('北京') && city.includes('北京')) || (province.includes('山西') && city.includes('运城'));//屏蔽北京和运城
  11.     }
  12.     (async () => {
  13.       try {
  14.         const res = await fetch('https://myip.ipip.net/json', { signal: controller.signal });
  15.         clearTimeout(timeout);
  16.         if (!res.ok) throw new Error('fetch error');
  17.         const { ret, data } = await res.json();
  18.         if (ret === 'ok') {
  19.           const [location, province, city] = data.location;
  20.           if (zhi_ding_diqu(province, city)) {
  21.             window.location.replace(error_path);
  22.           }
  23.         }
  24.       } catch (e) {
  25.         // 上面的方法不能使用自动执行下面的(绵羊优创网本地ip库)
  26.         clearTimeout(timeout);
  27.         console.error(e);
  28.         // 创建并加载脚本
  29.         const script = document.createElement('script');
  30.         script.src = 'http://smy.sheepyc.com/tool/ipku/getipinfo.php?return=ipinfo&format=js';
  31.         script.onload = () => {
  32.             const { province = '', city = '' } = ipinfo || {};
  33.             if (zhi_ding_diqu(province, city)) {
  34.                 window.location.replace(error_path);
  35.             }
  36.         };
  37.         script.onerror = () => console.warn('sheep: ipinfo variable is not defined or is null.');
  38.         document.head.appendChild(script);
  39.       }
  40.     })();
  41.   })();
  42. </script>
复制代码

分享一个简单错误提示页面,可用于跳转后显示:
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>抱歉,站点已停止</title>
  6. <style>
  7. html,body,div,h1,*{margin:0;padding:0;}
  8. body{background-color:#fefefe;color:#333}
  9. .box{width:580px;margin:0 auto;}
  10. h1{font-size:20px;text-align:center;background:url(背景图链接,不需要则删除) no-repeat top center; padding-top:90px;margin-top:30%;font-weight:normal;}
  11. </style>
  12. </head>
  13. <body>
  14. <div class="box"><h1>抱歉!该站点已停止运行!</h1></div>
  15. </body>
  16. </html>
复制代码
为什么写了两个接口判断代码?
默认的ip接口来自网络,若停止更新则不可用,所以增加了判断,若失效则使用绵羊优创网本地ip接口,提升安全性及可用性
为什么不直接使用绵羊优创网本地ip接口?
默认的ip接口有人更新维护ip库,能确保ip保持较新状态,绵羊本地ip库虽然也够用但更新频率低

ip库更新地址:github.com/metowolf/qqwry.dat

(当前绵羊优创网本地ip接口更新时间20250720)

懒得打字嘛,点击右侧快捷回复 【乱回复纯数字纯字母将禁言】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|小黑屋|社区规范|心情|神奇|连连|刮刮|金蛋|兑换|绵羊优创 ( 京ICP备19037745号-2 )|网站地图

公安备案京公网安备11011502037529号

GMT+8, 2025-9-3 16:14 , Processed in 1.209603 second(s), 20 queries , MemCache On.

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表
💗来点音乐😃
专辑封面
歌曲名称
歌手名称
0:00 0:00
顺序播放
歌词加载中...