在浏览器中获取本地IP地址的非服务器端方法? [英] Non-server side method of getting local IP address in browser?

查看:118
本文介绍了在浏览器中获取本地IP地址的非服务器端方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为LAN上的用户提供一种向网络管理员(我")注册"的方法,而不必要么a)在我的计算机上托管一个页面b)在中央服务器上托管脚本(因为它只是一个路由器,而不是真正的可靠HTTP服务器)或c)注册动态域,以使用前两个域中的任何一个,并避免将URL发送到指向本地IP的链接的混乱.

I am trying to provide a way for users on my LAN to "register" with the Network admin (me) without having to either a) host a page on my computer b) host a script on the central server (since it is only a router, not really a solid HTTP server) or c) sign up for a Dynamic Domain in order to either either of the first two and avoid the confusion of sending out a URL to a link to a local IP.

有没有一种简单的方法可以通过客户端脚本在屏幕上显示本地IP地址?我在想,也许我可以拥有一个iframe,该iframe指向路径中带有一些javascript的某个通用网址,这样我就可以让用户转到非本地站点,并且iframe会弹出他们的IP地址,然后可以在远程主页面中输入表单.

Is there a simple way to display the local IP address on screen via a client-side script? I'm thinking maybe I could have an iframe that points to some generic url with some javascript in the path, so that I can have the users go to a non-local site, and the iframe would pop up with their IP address that they can then enter into a form in the main remote page.

如果其他所有方法均失败,是否有办法让他们查找跨平台且不涉及使用命令行的IP(我认为第一个(即使不可能)可能比第二个更现实) ).

If all else fails, is there a way for them to look up their IP that is cross-platform and doesn't involve using the command line (I think the first, even if impossible, is probably more realistic than the second).

推荐答案

感谢Webrtc,可以从javascript访问本地IP.

Thankfully to Webrtc it is possible to access local IP from javascript.

看看: http://net.ipcalf.com/(源)

(在chrome中有效)

( It works in chrome )

但是从Javascript访问本地IP可能是一个隐私和安全性问题.

However accessing local IP from Javascript could be a privacy and security issue.

以及下面的简单示例,然后是net.ipcalf.com

and below simpler example then net.ipcalf.com

以及 jsfiddle

var RTCPeerConnection = window.webkitRTCPeerConnection || window.mozRTCPeerConnection;

var configuration = { "iceServers": [] };
var pc;
var localIP;

if(RTCPeerConnection){  

    pc = new RTCPeerConnection(configuration);
    pc.onicecandidate = function (evt) {  
        if (evt.candidate) { 
            if (!localIP) { 
                localIP = getIpFromString(  evt.candidate.candidate );
                console.log(localIP);
            }
        }
    };

     pc.createOffer(function (offerDesc) {;
        pc.setLocalDescription(offerDesc);                                      
    }, function (e) { console.warn("offer failed", e); });

    function getIpFromString(a)
    {
        var r = a.match(/\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b/);
        return r[0];
    }

} else {
 //browser doesn't support webrtc   
}

这篇关于在浏览器中获取本地IP地址的非服务器端方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆