Chrome和IE获取本机ip地址

Chrome

浏览器输入:chrome://flags/#enable-webrtc-hide-local-ips-with-mdns

把 Anonymize local IPs exposed by WebRTC 设置为 disabled ( 刷新程序,IP正常显示 )

java 复制代码
// 获取本机IP
    async function getUserIp() {
      let ip = '';
      const RTCPeerConnection =
        window.RTCPeerConnection ||
        window.mozRTCPeerConnection ||
        window.webkitRTCPeerConnection;
      if (RTCPeerConnection) {
        const rtc = new RTCPeerConnection();
        await rtc.createDataChannel(''); //创建一个可以发送任意数据的数据通道
        await rtc.createOffer(
          async (offerDesc) => {
            //创建并存储一个sdp数据
            await rtc.setLocalDescription(offerDesc);
          },
          (e) => {
            console.log(e);
          }
        );
        return new Promise((resolve) => {
          rtc.onicecandidate = (evt) => {
            //监听candidate事件
            if (evt.candidate) {
              console.log('evt:', evt.candidate);
              const ip_rule =
                /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/;
              const ip_addr = ip_rule.exec(evt.candidate.candidate)[1];
              console.log('ip_addr:', ip_addr); //打印获取的IP地址
              resolve(ip_addr || '');
            }
          };
        });
      }
      return ip;
    };
    getUserIp().then((ip) => {
    	console.log('===IP===', ip);
	});

IE

java 复制代码
 getIP() {
      var innerIP = null
      var locator = new ActiveXObject("WbemScripting.SWbemLocator");
      var service = locator.ConnectServer("."); //连接本机服务器
      var properties = service.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration where IPEnabled=TRUE");
      //查询使用SQL标准
      var e = new Enumerator(properties);
      for (; !e.atEnd(); e.moveNext()) {
        var p = e.item();
        innerIP = p.IPAddress(0)
        console.log("innerIP" + innerIP)
        if (innerIP != null || innerIP != undefined || innerIP != 'undefined') {
          console.log('===IP==='+innerIP)
        }
      }
    },
相关推荐
JavaGuide44 分钟前
刚刚,阿里版 Codex 上线了匿名模型 HungerG,无限 Token!
前端·后端
zhanlang02171 小时前
HTML 零基础入门到企业实战全套教程【第九章 9.3】移动端 HTML 基础适配入门
前端·html
用户69371750013841 小时前
AI 帮我写了大半年的代码,我却越来越不会写代码了
前端·后端·github
skywalk81631 小时前
把「假成功」赶出工具层:lightharness 两天升级复盘(4×5 模式矩阵 + 工具层 5 个缺陷)
开发语言·前端·人工智能·lightharness
彭亚川Allen2 小时前
从零手搓 RAG:用 Elasticsearch 存储与检索向量
前端·后端·面试
jerrywus2 小时前
15 个可以直接安装的 AI Agent Skills:按场景挑
前端·agent·claude
涛涛ing2 小时前
用了三年微前端,我们终于把它拆成了单体应用
前端
去伪存真3 小时前
《最终版》《打死不改版》全传一遍?RAG 系统文件去重实战
前端
anyup3 小时前
【全端支持】 uni-app x 专用请求库:axios 写法,多实例、拦截器、可中断、上传下载
前端·uni-app·unix
费曼学习法3 小时前
6 个 MCP Server 传输层重写实录:超时率 12% → 0.3%
前端·后端