当前位置:网站首页>H5如何获取内网IP和公网IP
H5如何获取内网IP和公网IP
2022-07-17 05:05:00 【昔日_少年】
前端H5开发,获取内网IP和公网IP
可直接打开,简答便捷,重要的事情说三遍
直接使用!
直接使用!
直接使用!
获取内网IP
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<ul>
<li>内网ip</li>
</ul>
<script> function getIPs(callback) {
var ip_dups = {
}; var RTCPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection; var useWebKit = !!window.webkitRTCPeerConnection; if (!RTCPeerConnection) {
var iframe = document.createElement('iframe'); iframe.style.display = 'none'; iframe.sandbox = 'allow-same-origin'; iframe.addEventListener("DOMNodeInserted", function(e) {
e.stopPropagation(); }, false); iframe.addEventListener("DOMNodeInsertedIntoDocument", function(e) {
e.stopPropagation(); }, false); document.body.appendChild(iframe); var win = iframe.contentWindow; RTCPeerConnection = win.RTCPeerConnection || win.mozRTCPeerConnection || win.webkitRTCPeerConnection; useWebKit = !!win.webkitRTCPeerConnection; } var mediaConstraints = {
optional: [{
RtpDataChannels: true }] }; var servers = undefined; if (useWebKit) servers = {
iceServers: [{
urls: "stun:stun.services.mozilla.com" }] }; var pc = new RTCPeerConnection(servers, mediaConstraints); function handleCandidate(candidate) {
var ip_regex = /([0-9]{1,3}(\.[0-9]{1,3}){3})/ var ip_addr = ip_regex.exec(candidate)[1]; if (ip_dups[ip_addr] === undefined) callback(ip_addr); ip_dups[ip_addr] = true; } pc.onicecandidate = function(ice) {
if (ice.candidate) handleCandidate(ice.candidate.candidate); }; pc.createDataChannel(""); pc.createOffer(function(result) {
pc.setLocalDescription(result, function() {
}, function() {
}); }, function() {
}); setTimeout(function() {
var lines = pc.localDescription.sdp.split('\n'); lines.forEach(function(line) {
if (line.indexOf('a=candidate:') === 0) handleCandidate(line); }); }, 1000); } getIPs(function(ip) {
var li = document.createElement("li"); li.textContent = ip; if (ip.match(/^(192\.168\.|169\.254\.|10\.|172\.(1[6-9]|2\d|3[01]))/)) {
//内网ip document.getElementsByTagName("ul")[0].appendChild(li); } }); </script>
</body>
</html>
获取公网IP
通常我们使用搜狐的接口
<!DOCTYPE html>
<html>
<head>
<title>公网ip获取</title>
<meta http-equiv=Content-Type content="text/html; charset=gb2312">
</head>
<body>
<script src="http://pv.sohu.com/cityjson?ie=utf-8"></script>
<script type="text/javascript"> document.write(returnCitySN["cip"] + ',' + returnCitySN["cname"]) </script>
</body>
</html>
边栏推荐
- Interface parameters return encapsulated class result
- Database training 7 [index and creation of data integrity constraints]
- 微信小程序wx.setClipboardData复制文本
- 【Es6】详细解说Set ,Array的常用对象及其他方法(完整版)
- Notes de formation pour la deuxième fois des modèles
- 读论文《SNUNet-CD: A Densely Connected Siamese Network for Change Detection of VHR Images》
- 学习C语言的第四天
- STL容器——set集合的应用
- MySQL optimization
- 使用js实现安居客二级菜单及(注意事项和问题点演示)完整版
猜你喜欢

数据分析与数据挖掘实战案例本地房价预测(716):

微信小程序获取年月日周及早上、中午、晚上

es6新增-Symbol数据类型

Feature extraction of machine learning (digitization and discretization of category features and digitization of text features)

Word2Vec原理及应用与文章相似度(推荐系统方法)

MySQL optimization

HarmonyOS第二次培训笔记

【C语言_复习_学习第二课】什么是进制?进制之间应该如何转换

uni-app 条件编译#ifdef #endif 兼容多个终端

数据库实训7【索引与数据完整性约束的创建】
随机推荐
【C语言—零基础第十四课】变量的作用域与存储类
硬核结构体,暴力解读
滚动轮加载的两种js方法及模态框拖拽归总
STL容器——vector的基本操作
基于cuda10.0的pytorch深度学习环境配置
mysql数据库实验实训6,数据视图(详细)
学习C语言第二天
【C语言—零基础_学习_复习_第四课】数据类型及其运算
Fanoutexchange switch is simple to use
【p5.js】模拟烟花效果-交互媒体设计作业
Interface parameters return encapsulated class result
ModelArts第二次培训笔记
ArcGIS Pro发布服务
645. 错误的集合
Harmonyos third training notes
mysql数据库实验实训5,数据查询yggl数据库查询(详细)
Harmonyos fourth training notes
IDL 读取葵花8(Himawari-8)HSD数据
Elment UI usage
获取URL参数的两种方法及location对象的各项获取方式