有一个接口需要内网ip和mac地址这个需求,在网上找了很多参考的,如下:
方法一:使用webRTC,问题:在新版本的chrome 浏览器,找不到#enable-webrtc-hide-local-ips-with-mdns
代码网上找的,仅供参考,因为改不了浏览器配置,所以一直拿不到

getIP(callback) {
                let recode = {};
                let RTCPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
                // 如果不存在则使用一个iframe绕过
                if (!RTCPeerConnection) {
                    // 因为这里用到了iframe,所以在调用这个方法的script上必须有一个iframe标签
                    // <iframe id="iframe" sandbox="allow-same-origin" style="display:none;"></iframe>
                    let win = iframe.contentWindow;
                    RTCPeerConnection = win.RTCPeerConnection || win.mozRTCPeerConnection || win.webkitRTCPeerConnection;
                }

                //创建实例,生成连接
                let pc = new RTCPeerConnection();

                // 匹配字符串中符合ip地址的字段
                function handleCandidate(candidate) {
                    let ip_regexp = /([0-9]{1,3}(\.[0-9]{1,3}){3}|([a-f0-9]{1,4}((:[a-f0-9]{1,4}){7}|:+[a-f0-9]{1,4}){6}))/;
                    console.log(candidate)
                    let ip_isMatch = candidate.match(ip_regexp)[1];
                    if (!recode[ip_isMatch]) {
                        callback(ip_isMatch);
                        recode[ip_isMatch] = true;
                    }
                }

                //监听icecandidate事件
                pc.onicecandidate = (ice) => {
                    if (ice.candidate) {
                        handleCandidate(ice.candidate.candidate);
                    }
                };
                //建立一个伪数据的通道
                pc.createDataChannel('');
                pc.createOffer((res) => {
                    pc.setLocalDescription(res);
                }, () => { });

                //延迟,让一切都能完成
                setTimeout(() => {
                    let lines = pc.localDescription.sdp.split('\n');
                    lines.forEach(item => {
                        if (item.indexOf('a=candidate:') === 0) {
                            handleCandidate(item);
                        }
                    })
                }, 1000);
            },

方法二:通过搜狐查询ip的接口,问题:能获取到,但获取的是局域网ip,不是本机的,不符合需求

<script src="http://pv.sohu.com/cityjson?ie=utf-8"></script>
<script type="text/javascript">
    sessionStorage.setItem('ip', returnCitySN["cip"]);
</script>

方法三:使用ActiveX,但只支持IE浏览器,也不符合需求

getMacAddress() {
                var obj = new ActiveXObject("WbemScripting.SWbemLocator");
                var s = obj.ConnectServer(".");
                var properties = s.ExecQuery("Select * from Win32_NetworkAdapterConfiguration");
                var e = new Enumerator(properties);
                var output;
                while (!e.atEnd()) {
                    e.moveNext();
                    var p = e.item();
                    if (!p) continue;
                    if (p.IPEnabled) return p.MACAddress;
                }
                return "";
            },

方法四:使用node的os模块,问题:获取的是服务器地址,如果是将网站做成桌面软件那种应该是能获取的(猜测,这个没试过)
在vue.config.js添加如下代码

const os=require("os");
var ip=''
var mac = ''
var networkInterfaces=os.networkInterfaces();
for(var i in networkInterfaces){
    for(var j in networkInterfaces[i]){
        if(networkInterfaces[i][j]["family"]==="IPv4" && networkInterfaces[i][j]["mac"]!=="00:00:00:00:00:00" && networkInterfaces[i][j]["address"]!=="127.0.0.1"){
            mac = networkInterfaces[i][j]["mac"]
            ip = networkInterfaces[i][j]["address"]
        }else{
          ip=="127.0.0.1"
        }
    }
}

// 自定义环境变量全局使用
process.env.VUE_APP_MAC=mac
process.env.VUE_APP_IP=ip

总结:
以上方法对我这个项目的需求都不符合,在web端要获取内网ip还是不行,可能是考虑到用户隐私问题

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐