我需要使用 JavaScript 以某种方式检索客户端的 IP 地址;没有服务器端代码,甚至没有 SSI。
但是,我不反对使用免费的 3rd 方脚本/服务。
我需要使用 JavaScript 以某种方式检索客户端的 IP 地址;没有服务器端代码,甚至没有 SSI。
但是,我不反对使用免费的 3rd 方脚本/服务。
我将使用可以返回 JSON 的 Web 服务(与 jQuery 一起使事情更简单)。以下是我可以找到的所有活动的免费 IP 查找服务以及它们返回的信息。如果您知道其他人,请添加评论,我会更新此答案。
let apiKey = '1be9a6884abd4c3ea143b59ca317c6b2';
$.getJSON('https://ipgeolocation.abstractapi.com/v1/?api_key=' + apiKey, function(data) {
console.log(JSON.stringify(data, null, 2));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
限制:
// Base
let apiKey = 'd9e53816d07345139c58d0ea733e3870';
$.getJSON('https://api.bigdatacloud.net/data/ip-geolocation?key=' + apiKey, function(data) {
console.log(JSON.stringify(data, null, 2));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
// Base + Confidence Area
let apiKey = 'd9e53816d07345139c58d0ea733e3870';
$.getJSON('https://api.bigdatacloud.net/data/ip-geolocation-with-confidence?key=' + apiKey, function(data) {
console.log(JSON.stringify(data, null, 2));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
// Base + Confidence Area + Hazard Report
let apiKey = 'd9e53816d07345139c58d0ea733e3870';
$.getJSON('https://api.bigdatacloud.net/data/ip-geolocation-full?key=' + apiKey, function(data) {
console.log(JSON.stringify(data, null, 2));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
限制:
$.get('https://www.cloudflare.com/cdn-cgi/trace', function(data) {
// Convert key-value pairs to JSON
// https://stackoverflow.com/a/39284735/452587
data = data.trim().split('\n').reduce(function(obj, pair) {
pair = pair.split('=');
return obj[pair[0]] = pair[1], obj;
}, {});
console.log(data);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
限制:
试试看: https : //api.db-ip.com/v2/free/self
$.getJSON('https://api.db-ip.com/v2/free/self', function(data) {
console.log(JSON.stringify(data, null, 2));
});
返回:
{
"ipAddress": "116.12.250.1",
"continentCode": "AS",
"continentName": "Asia",
"countryCode": "SG",
"countryName": "Singapore",
"city": "Singapore (Queenstown Estate)"
}
限制:
Origin
请求头试试看: http : //gd.geobytes.com/GetCityDetails
$.getJSON('http://gd.geobytes.com/GetCityDetails?callback=?', function(data) {
console.log(JSON.stringify(data, null, 2));
});
返回:
{
"geobytesforwarderfor": "",
"geobytesremoteip": "116.12.250.1",
"geobytesipaddress": "116.12.250.1",
"geobytescertainty": "99",
"geobytesinternet": "SA",
"geobytescountry": "Saudi Arabia",
"geobytesregionlocationcode": "SASH",
"geobytesregion": "Ash Sharqiyah",
"geobytescode": "SH",
"geobyteslocationcode": "SASHJUBA",
"geobytescity": "Jubail",
"geobytescityid": "13793",
"geobytesfqcn": "Jubail, SH, Saudi Arabia",
"geobyteslatitude": "27.004999",
"geobyteslongitude": "49.660999",
"geobytescapital": "Riyadh ",
"geobytestimezone": "+03:00",
"geobytesnationalitysingular": "Saudi Arabian ",
"geobytespopulation": "22757092",
"geobytesnationalityplural": "Saudis",
"geobytesmapreference": "Middle East ",
"geobytescurrency": "Saudi Riyal",
"geobytescurrencycode": "SAR",
"geobytestitle": "Saudi Arabia"
}
限制:
$.getJSON('https://json.geoiplookup.io/?callback=?', function(data) {
console.log(JSON.stringify(data, null, 2));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
限制:
试试看: http : //www.geoplugin.net/json.gp
$.getJSON('http://www.geoplugin.net/json.gp', function(data) {
console.log(JSON.stringify(data, null, 2));
});
返回:
{
"geoplugin_request": "116.12.250.1",
"geoplugin_status": 200,
"geoplugin_credit": "Some of the returned data includes GeoLite data created by MaxMind, available from <a href=\\'http://www.maxmind.com\\'>http://www.maxmind.com</a>.",
"geoplugin_city": "Singapore",
"geoplugin_region": "Singapore (general)",
"geoplugin_areaCode": "0",
"geoplugin_dmaCode": "0",
"geoplugin_countryCode": "SG",
"geoplugin_countryName": "Singapore",
"geoplugin_continentCode": "AS",
"geoplugin_latitude": "1.2931",
"geoplugin_longitude": "103.855797",
"geoplugin_regionCode": "00",
"geoplugin_regionName": "Singapore (general)",
"geoplugin_currencyCode": "SGD",
"geoplugin_currencySymbol": "$",
"geoplugin_currencySymbol_UTF8": "$",
"geoplugin_currencyConverter": 1.4239
}
限制:
$.get('https://api.hackertarget.com/geoip/?q=116.12.250.1', function(data) {
// Convert key-value pairs to JSON
// https://stackoverflow.com/a/39284735/452587
data = data.trim().split('\n').reduce(function(obj, pair) {
pair = pair.split(': ');
return obj[pair[0]] = pair[1], obj;
}, {});
console.log(data);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
限制:
试试看: https : //ipapi.co/json/
$.getJSON('https://ipapi.co/json/', function(data) {
console.log(JSON.stringify(data, null, 2));
});
返回:
{
"ip": "116.12.250.1",
"city": "Singapore",
"region": "Central Singapore Community Development Council",
"country": "SG",
"country_name": "Singapore",
"postal": null,
"latitude": 1.2855,
"longitude": 103.8565,
"timezone": "Asia/Singapore"
}
限制:
Origin
请求头试试看: http : //ip-api.com/json
$.getJSON('http://ip-api.com/json', function(data) {
console.log(JSON.stringify(data, null, 2));
});
返回:
{
"as": "AS3758 SingNet",
"city": "Singapore",
"country": "Singapore",
"countryCode": "SG",
"isp": "SingNet Pte Ltd",
"lat": 1.2931,
"lon": 103.8558,
"org": "Singapore Telecommunications",
"query": "116.12.250.1",
"region": "01",
"regionName": "Central Singapore Community Development Council",
"status": "success",
"timezone": "Asia/Singapore",
"zip": ""
}
限制:
let apiKey = 'be0f755b93290b4c100445d77533d291763a417c75524e95e07819ad';
$.getJSON('https://api.ipdata.co?api-key=' + apiKey, function(data) {
console.log(JSON.stringify(data, null, 2));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
限制:
let apiKey = '50e887ce-e3bb-4f00-a9b9-667597db5539';
$.getJSON('https://ipfind.co/me?auth=' + apiKey, function(data) {
console.log(JSON.stringify(data, null, 2));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
限制:
let apiKey = 'f8e0b361e8f4405c94613ab534959fdf';
$.getJSON('https://api.ipgeolocation.io/ipgeo?apiKey=' + apiKey, function(data) {
console.log(JSON.stringify(data, null, 2));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
限制:
$.getJSON('https://api.ipify.org?format=jsonp&callback=?', function(data) {
console.log(JSON.stringify(data, null, 2));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
限制:
let apiKey = '25864308b6a77fd90f8bf04b3021a48c1f2fb302a676dd3809054bc1b07f5b42';
$.getJSON('https://api.ipinfodb.com/v3/ip-city/?format=json&key=' + apiKey, function(data) {
console.log(JSON.stringify(data, null, 2));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
限制:
$.getJSON('https://ipinfo.io/json', function(data) {
console.log(JSON.stringify(data, null, 2));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
限制:
$.getJSON('https://api.ipregistry.co/?key=tryout', function(data) {
console.log(JSON.stringify(data, null, 2));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
限制:
试试看: http : //api.ipstack.com/ <ip_address> ?access_key= <your_api_key>
$.getJSON('http://api.ipstack.com/<ip_address>?access_key=<your_api_key>', function(data) {
console.log(JSON.stringify(data, null, 2));
});
返回:
{
"ip": "116.12.250.1",
"type": "ipv4",
"continent_code": "AS",
"continent_name": "Asia",
"country_code": "SG",
"country_name": "Singapore",
"region_code": "01",
"region_name": "Central Singapore Community Development Council",
"city": "Singapore",
"zip": null,
"latitude": 1.2931,
"longitude": 103.8558,
"location": {
"geoname_id": 1880252,
"capital": "Singapore",
"languages": [
{
"code": "en",
"name": "English",
"native": "English"
},
{
"code": "ms",
"name": "Malay",
"native": "Bahasa Melayu"
},
{
"code": "ta",
"name": "Tamil",
"native": "தமிழ்"
},
{
"code": "zh",
"name": "Chinese",
"native": "中文"
}
],
"country_flag": "http://assets.ipstack.com/flags/sg.svg",
"country_flag_emoji": "🇸🇬",
"country_flag_emoji_unicode": "U+1F1F8 U+1F1EC",
"calling_code": "65",
"is_eu": false
}
}
限制:
$.getJSON('https://jsonip.com/', function(data) {
console.log(JSON.stringify(data, null, 2));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
限制:
试试看: http : //ip.jsontest.com/
$.getJSON('http://ip.jsontest.com/', function(data) {
console.log(JSON.stringify(data, null, 2));
});
返回:
{
"ip": "116.12.250.1"
}
限制:
let apiKey = 'ed5ebbeba257b8f262a6a9bbc0ec678e';
$.getJSON('https://api.snoopi.io/116.12.250.1?apikey=' + apiKey, function(data) {
console.log(JSON.stringify(data, null, 2));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
限制:
使用现代浏览器,您可以使用本机Fetch API而不是依赖 jQuery 的$.getJSON()
. 下面是一个例子:
let apiKey = '1be9a6884abd4c3ea143b59ca317c6b2';
// Make the request
fetch('https://ipgeolocation.abstractapi.com/v1/?api_key=' + apiKey)
// Extract JSON body content from HTTP response
.then(response => response.json())
// Do something with the JSON data
.then(data => {
console.log(JSON.stringify(data, null, 2))
});
Origin
请求标头(StackOverflow 代码段被迫使用 https 并Origin: null
在请求标头中包含)。Origin
请求标头”正如最近一个新的 Github 存储库webrtc-ip 所示,您现在可以使用 WebRTC泄漏用户的公共IP 地址。可悲的是,由于逐渐转向 mDNS(至少对于 WebRTC),此泄漏不适用于私有 IP,在此处完全解释。但是,这是一个有效的演示:
getIPs().then(res => document.write(res.join('\n')))
<script src="https://cdn.jsdelivr.net/gh/joeymalvinni/webrtc-ip/dist/bundle.dev.js"></script>
可以在此处找到此存储库的已编译源代码。
(以前)最终更新
此解决方案将不再有效,因为浏览器正在修复 webrtc 泄漏:有关该问题的更多信息,请阅读另一个问题:RTCIceCandidate 不再返回 IP
更新:我一直想制作一个最小/丑化版本的代码,所以这里是一个 ES6 Promise 代码:
var findIP = new Promise(r=>{var w=window,a=new (w.RTCPeerConnection||w.mozRTCPeerConnection||w.webkitRTCPeerConnection)({iceServers:[]}),b=()=>{};a.createDataChannel("");a.createOffer(c=>a.setLocalDescription(c,b,b),b);a.onicecandidate=c=>{try{c.candidate.candidate.match(/([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/g).forEach(r)}catch(e){}}})
/*Usage example*/
findIP.then(ip => document.write('your ip: ', ip)).catch(e => console.error(e))
注意:如果您想要用户的所有 IP(可能更多取决于他的网络),则此新的缩小代码将仅返回单个 IP,请使用原始代码...
感谢WebRTC,在支持 WebRTC 的浏览器中获取本地 IP 非常容易(至少现在是这样)。我修改了源代码,减少了行数,没有提出任何令人震惊的请求,因为您只需要本地 IP,而不是公共 IP,以下代码适用于最新的 Firefox 和 Chrome,只需运行代码段并自行检查:
function findIP(onNewIP) { // onNewIp - your listener function for new IPs
var myPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection; //compatibility for firefox and chrome
var pc = new myPeerConnection({iceServers: []}),
noop = function() {},
localIPs = {},
ipRegex = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/g,
key;
function ipIterate(ip) {
if (!localIPs[ip]) onNewIP(ip);
localIPs[ip] = true;
}
pc.createDataChannel(""); //create a bogus data channel
pc.createOffer(function(sdp) {
sdp.sdp.split('\n').forEach(function(line) {
if (line.indexOf('candidate') < 0) return;
line.match(ipRegex).forEach(ipIterate);
});
pc.setLocalDescription(sdp, noop, noop);
}, noop); // create offer and set local description
pc.onicecandidate = function(ice) { //listen for candidate events
if (!ice || !ice.candidate || !ice.candidate.candidate || !ice.candidate.candidate.match(ipRegex)) return;
ice.candidate.candidate.match(ipRegex).forEach(ipIterate);
};
}
var ul = document.createElement('ul');
ul.textContent = 'Your IPs are: '
document.body.appendChild(ul);
function addIP(ip) {
console.log('got ip: ', ip);
var li = document.createElement('li');
li.textContent = ip;
ul.appendChild(li);
}
findIP(addIP);
<h1> Demo retrieving Client IP using WebRTC </h1>
这里发生的事情是,我们正在创建一个虚拟的对等连接,并且为了让远程对等与我们联系,我们通常会相互交换冰候选。并读取冰候选(从本地会话描述和 onIceCandidateEvent)我们可以知道用户的 IP。
我从哪里获取代码 -->源代码
你可以,通过服务器端使用 JSONP 中继它
在谷歌上搜索时,在此处找到了 SO Can I perform a DNS lookup (hostname to IP address) using client-side Javascript?
<script type="application/javascript">
function getip(json){
alert(json.ip); // alerts the ip address
}
</script>
<script type="application/javascript" src="http://www.telize.com/jsonip?callback=getip"></script>
注意: telize.com API 已于2015 年 11 月 15 日永久关闭。
这里的大多数答案“解决”了对服务器端代码的需求... 这是一种完全有效的技术,除非您确实需要在不访问服务器的情况下获取 IP 地址。
传统上,如果没有某种插件,这是不可能的(即使那样,如果您在 NAT 路由器后面,您可能会得到错误的IP 地址),但是随着 WebRTC 的出现,实际上可以做到这一点.. .如果您的目标浏览器支持 WebRTC(目前:Firefox、Chrome 和 Opera)。
有关如何使用 WebRTC 检索有用的客户端 IP 地址的详细信息,请阅读 mido 的回答。
你不能。你得问服务员。