瀏覽器獲取用戶內(nèi)網(wǎng)IP

var uip= '';

getIPs(addIP);? //調(diào)用獲取IP函數(shù)呕诉,傳入回調(diào)函數(shù)

function addIP(ip) {

? ? uip = ip;

}

function getIPs(callback) {

? ? var ip_dups = {};

? ? //compatibility for firefox and chrome

? ? var RTCPeerConnection = window.RTCPeerConnection ||

? ? ? ? ? ? window.mozRTCPeerConnection ||

? ? ? ? ? ? window.webkitRTCPeerConnection;

? ? var useWebKit = !!window.webkitRTCPeerConnection;

? ? //bypass naive webrtc blocking using an iframe

? ? if (!RTCPeerConnection) {

? ? ? ? //NOTE: you need to have an iframe in the page right above the script tag

? ? ? ? //

? ? ? ? //<iframe id="iframe" sandbox="allow-same-origin" style="display: none"></iframe>

? ? ? ? //<script>...getIPs called in here...

? ? ? ? //

? ? ? ? var iframe = document.getElementById("iframe");

? ? ? ? var win = iframe.contentWindow;

? ? ? ? if (typeof (eval(win.RTCPeerConnection)) === "function") {

? ? ? ? ? ? RTCPeerConnection = win.RTCPeerConnection;

? ? ? ? } else if (typeof (eval(win.mozRTCPeerConnection)) === "function") {

? ? ? ? ? ? RTCPeerConnection = win.mozRTCPeerConnection;

? ? ? ? } else if (typeof (eval(win.webkitRTCPeerConnection)) === "function") {

? ? ? ? ? ? RTCPeerConnection = win.webkitRTCPeerConnection;

? ? ? ? } else {

? ? ? ? ? ? return "";

? ? ? ? }

? ? ? ? useWebKit = !!win.webkitRTCPeerConnection;

? ? }

? ? //minimal requirements for data connection

? ? var mediaConstraints = {

? ? ? ? optional: [{RtpDataChannels: true}]

? ? };

? ? var servers = {iceServers: [{urls: "stun:stun.services.mozilla.com"}]};

? ? //construct a new RTCPeerConnection

? ? var pc = new RTCPeerConnection(servers, mediaConstraints);

? ? function handleCandidate(candidate) {

? ? ? ? //match just the IP address

? ? ? ? var ip_regex = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/

? ? ? ? var ip_addr = ip_regex.exec(candidate)[1];

? ? ? ? //remove duplicates

? ? ? ? if (ip_dups[ip_addr] === undefined)

? ? ? ? ? ? callback(ip_addr);

? ? ? ? ip_dups[ip_addr] = true;

? ? }

? ? //listen for candidate events

? ? pc.onicecandidate = function (ice) {

? ? ? ? //skip non-candidate events

? ? ? ? if (ice.candidate)

? ? ? ? ? ? handleCandidate(ice.candidate.candidate);

? ? };

? ? //create a bogus data channel

? ? pc.createDataChannel("");

? ? //create an offer sdp

? ? pc.createOffer(function (result) {

? ? ? ? //trigger the stun server request

? ? ? ? pc.setLocalDescription(result, function () {}, function () {});

? ? }, function () {});

? ? //wait for a while to let everything done

? ? setTimeout(function () {

? ? ? ? //read candidate info from local description

? ? ? ? var lines = pc.localDescription.sdp.split('\n');

? ? ? ? lines.forEach(function (line) {

? ? ? ? ? ? if (line.indexOf('c=IN') === 0)

? ? ? ? ? ? ? ? handleCandidate(line);

? ? ? ? });

? ? }, 1000);

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末酌住,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子镊绪,更是在濱河造成了極大的恐慌诱建,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,807評論 6 518
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件鞭缭,死亡現(xiàn)場離奇詭異,居然都是意外死亡魏颓,警方通過查閱死者的電腦和手機(jī)岭辣,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,284評論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來甸饱,“玉大人沦童,你說我怎么就攤上這事√净埃” “怎么了偷遗?”我有些...
    開封第一講書人閱讀 169,589評論 0 363
  • 文/不壞的土叔 我叫張陵,是天一觀的道長驼壶。 經(jīng)常有香客問我氏豌,道長,這世上最難降的妖魔是什么辅柴? 我笑而不...
    開封第一講書人閱讀 60,188評論 1 300
  • 正文 為了忘掉前任箩溃,我火速辦了婚禮,結(jié)果婚禮上碌嘀,老公的妹妹穿的比我還像新娘涣旨。我一直安慰自己,他們只是感情好股冗,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,185評論 6 398
  • 文/花漫 我一把揭開白布霹陡。 她就那樣靜靜地躺著,像睡著了一般止状。 火紅的嫁衣襯著肌膚如雪烹棉。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,785評論 1 314
  • 那天怯疤,我揣著相機(jī)與錄音浆洗,去河邊找鬼。 笑死集峦,一個胖子當(dāng)著我的面吹牛伏社,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播塔淤,決...
    沈念sama閱讀 41,220評論 3 423
  • 文/蒼蘭香墨 我猛地睜開眼摘昌,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了高蜂?” 一聲冷哼從身側(cè)響起聪黎,我...
    開封第一講書人閱讀 40,167評論 0 277
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎备恤,沒想到半個月后稿饰,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體锦秒,經(jīng)...
    沈念sama閱讀 46,698評論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,767評論 3 343
  • 正文 我和宋清朗相戀三年湘纵,在試婚紗的時候發(fā)現(xiàn)自己被綠了脂崔。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,912評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡梧喷,死狀恐怖砌左,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情铺敌,我是刑警寧澤汇歹,帶...
    沈念sama閱讀 36,572評論 5 351
  • 正文 年R本政府宣布,位于F島的核電站偿凭,受9級特大地震影響产弹,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜弯囊,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,254評論 3 336
  • 文/蒙蒙 一痰哨、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧匾嘱,春花似錦斤斧、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,746評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至悬垃,卻和暖如春游昼,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背尝蠕。 一陣腳步聲響...
    開封第一講書人閱讀 33,859評論 1 274
  • 我被黑心中介騙來泰國打工烘豌, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人看彼。 一個月前我還...
    沈念sama閱讀 49,359評論 3 379
  • 正文 我出身青樓扇谣,卻偏偏與公主長得像,于是被迫代替她去往敵國和親闲昭。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,922評論 2 361

推薦閱讀更多精彩內(nèi)容