一、JS獲取IP地址
JavaScript是一種廣泛使用的腳本語言,可用於在Web瀏覽器中編寫交互式網頁。在Web開發中,獲取IP地址是一項非常重要的任務,因為它可以幫助開發人員確定用戶的位置。在JavaScript中,可以使用以下代碼來獲取用戶的IP地址:
function getIPAddress(onNewIP) {
var myPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
var pc = new myPeerConnection({iceServers: []}),
noop = function(){};
pc.createDataChannel("");
pc.createOffer(pc.setLocalDescription.bind(pc), noop);
pc.onicecandidate = function(ice) {
if (ice && ice.candidate && ice.candidate.candidate) {
var myIP = ice.candidate.candidate.match(/(d{1,3}.d{1,3}.d{1,3}.d{1,3})/)[0];
onNewIP(myIP);
}
};
}
這段代碼用於創建一個RTCPeerConnection對象來獲取用戶的IP地址。它首先檢查瀏覽器是否支持RTCPeerConnection對象,然後創建一個新的RTCPeerConnection對象,並通過createDataChannel()函數創建一個數據通道。接下來,通過createOffer()函數創建一個offer,然後通過setLocalDescription()函數將本地SDP描述設置為這個offer。最後,當獲取到一個ICE candidate時,將通過正則表達式匹配出IP地址,並通過回調函數返回IP地址。
二、JS獲取IPv6地址
IPv6是下一代IP協議,可以提供更多的地址空間和更好的安全性。在JavaScript中,可以使用以下代碼來獲取用戶的IPv6地址:
function getIPv6Address(onNewIPv6) {
var myPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
var pc = new myPeerConnection({iceServers: []}),
noop = function(){};
pc.createDataChannel("");
pc.createOffer(pc.setLocalDescription.bind(pc), noop);
pc.onicecandidate = function(ice) {
if (ice && ice.candidate && ice.candidate.candidate) {
var myIPv6 = ice.candidate.candidate.match(/([0-9a-fA-F]{1,4}(:[0-9a-fA-F]{1,4}){7})/)[0];
onNewIPv6(myIPv6);
}
};
}
這段代碼與獲取IP地址的代碼類似,但是我們需要使用不同的正則表達式來匹配IPv6地址。在這個例子中,我們使用了一個匹配IPv6地址的正則表達式:([0-9a-fA-F]{1,4}(:[0-9a-fA-F]{1,4}){7})。
三、JS獲取IP和端口
在JavaScript中,可以使用以下代碼來獲取用戶的IP和端口:
function getIPAndPort(onNewIPAndPort) {
var myPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
var pc = new myPeerConnection({iceServers: []}),
noop = function(){};
pc.createDataChannel("");
pc.createOffer(pc.setLocalDescription.bind(pc), noop);
pc.onicecandidate = function(ice) {
if (ice && ice.candidate && ice.candidate.candidate) {
var myIPAndPort = ice.candidate.candidate.match(/(d{1,3}.d{1,3}.d{1,3}.d{1,3}):([0-9]+)/)[0];
onNewIPAndPort(myIPAndPort);
}
};
}
這段代碼類似於獲取IP地址的代碼,但是我們需要對正則表達式進行改變以匹配IP和端口的組合。在這個例子中,我們使用了如下正則表達式:(d{1,3}.d{1,3}.d{1,3}.d{1,3}):([0-9]+)。
四、JS獲取IP和地區
在JavaScript中,可以通過調用第三方API或使用數據庫來獲取用戶的地理位置信息,並與獲取的IP地址進行匹配。以下是使用第三方API獲取IP和地區信息的代碼:
function getIPAndRegion(onNewIPAndRegion) {
var xhr = new XMLHttpRequest();
xhr.open("GET", "https://api.ipify.org?format=json", true);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
var result = JSON.parse(xhr.responseText);
var myIP = result.ip;
var xhr2 = new XMLHttpRequest();
xhr2.open("GET", "https://ipapi.co/" + myIP + "/json/", true);
xhr2.onreadystatechange = function() {
if (xhr2.readyState == 4 && xhr2.status == 200) {
var result2 = JSON.parse(xhr2.responseText);
var myRegion = result2.region;
onNewIPAndRegion(myIP + ", " + myRegion);
}
};
xhr2.send();
}
};
xhr.send();
}
這段代碼首先使用XMLHttpRequest對象向https://api.ipify.org發送一個GET請求,以獲取用戶的IP地址。接下來,使用另一個XMLHttpRequest對象向https://ipapi.co發送一個GET請求,以根據IP地址獲取用戶的地區信息。最後,將IP地址和地區信息一起返回給回調函數。
五、JS獲取IP地理位置
在JavaScript中,可以使用第三方API或自己的數據庫來獲取用戶的地理位置信息。以下是使用第三方API獲取IP地理位置的代碼:
function getIPGeolocation(onNewIPGeolocation) {
var xhr = new XMLHttpRequest();
xhr.open("GET", "https://api.ipify.org?format=json", true);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
var result = JSON.parse(xhr.responseText);
var myIP = result.ip;
var xhr2 = new XMLHttpRequest();
xhr2.open("GET", "https://ipapi.co/" + myIP + "/json/", true);
xhr2.onreadystatechange = function() {
if (xhr2.readyState == 4 && xhr2.status == 200) {
var result2 = JSON.parse(xhr2.responseText);
var myLocation = result2.city + ", " + result2.region_code + ", " + result2.country_name;
onNewIPGeolocation(myLocation);
}
};
xhr2.send();
}
};
xhr.send();
}
這段代碼與獲取IP和地區的代碼類似,但是它返回了用戶的地理位置信息,而不僅僅是地區信息。
六、JS獲取IP地址的方法
除了使用RTCPeerConnection對象外,還可以使用其他方法來獲取IP地址。以下是使用WebRTC API中的RTCDataChannel來獲取IP地址的代碼:
function getIPAddressWithRTCDataChannel(onNewIP) {
var pc = new RTCPeerConnection({iceServers: []}),
noop = function(){};
pc.createDataChannel("");
pc.createOffer(pc.setLocalDescription.bind(pc), noop);
pc.onicecandidate = function(ice) {
if (ice && ice.candidate && ice.candidate.candidate) {
var myIP = ice.candidate.candidate.match(/(d{1,3}.d{1,3}.d{1,3}.d{1,3})/)[0];
onNewIP(myIP);
pc.onicecandidate = noop;
}
};
}
此外,還可以使用第三方庫,如geoip2.js和ipify.js來獲取IP地址、地理位置和ASN(Autonomous System Number,自治系統號)信息。以下是使用geoip2.js和ipify.js獲取IP地址和地理位置的代碼:
// Load the necessary libraries
// Get the IP address
ipify(function(err, data) {
var myIP = data.ip;
// Get the geolocation
geoip2.city(myIP, function(result) {
var myCity = result.city.names.en;
var myRegion = result.subdivisions[0].iso_code;
var myCountry = result.country.names.en;
var myLocation = myCity + ", " + myRegion + ", " + myCountry;
onNewIPAndGeolocation(myIP + ", " + myLocation);
});
});
七、JS獲取當前IP
在JavaScript中,可以使用以下代碼來獲取當前網絡連接的IP地址:
function getCurrentIP(onNewIP) {
var myScript = document.createElement("script");
myScript.src = "https://jsonip.com/?callback=getIP";
document.body.appendChild(myScript);
window.getIP = function(json) {
onNewIP(json.ip);
};
}
這段代碼通過向https://jsonip.com發送一個JSONP請求來獲取當前的IP地址。
八、JS獲取本機IP地址
在JavaScript中,可以通過WebSocket對象獲取本地IP地址:
function getLocalIPAddress(onNewIP) {
var mySocket = new WebSocket("wss://websocket.org");
mySocket.onopen = function() {
mySocket.send("hello");
};
mySocket.onerror = function() {
onNewIP("error");
};
mySocket.onmessage = function(msg) {
mySocket.close();
var myIP = msg.data;
onNewIP(myIP);
};
}
這段代碼創建一個WebSocket對象並連接到https://websocket.org。當連接打開時,它發送了一個“hello”消息,並等待接收到的消息來返回本地IP地址。請注意,這段代碼要求WebSocket服務器返回客戶端的IP地址。
九、JS獲取本地IP
在JavaScript中,可以使用Navigator對象獲取本地IP地址:
function getLocalIPAddressWithNavigator(onNewIP) {
window.RTCPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
var pc = new RTCPeerConnection({iceServers:[]}),
noop = function(){};
pc.createDataChannel("");
pc.createOffer(pc.setLocalDescription.bind(pc), noop);
pc.onicecandidate = function(ice) {
if (!ice || !ice.candidate)
return;
var myIP = /^candidate:.+ (\S+) \d+ typ/.exec(ice.candidate.candidate)[1];
onNewIP(myIP);
pc.onicecandidate = noop;
};
}
這段代碼使用Navigator對象和RTCPeerConnection對象來獲取本地IP地址。它首先檢查瀏覽器是否支持RTCPeerConnection對象,然後創建一個新的RTCPeerConnection對象,並通過createDataChannel()函數創建一個數據通道。接下來,通過createOffer()函數創建一個offer,然後通過setLocalDescription()函數將本地SDP描述設置為這個offer。最後,當獲取到一個ICE candidate時,將使用正則表達式獲取本地IP地址,並通過回調函數返回IP地址。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/247094.html