1,获取内网ip

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
getIP( function (ip) {
console.log(ip);
})
// 192.168.1.191
// 2001::2841:aa90:2843:1983:e4d1:a9b8

function getIP(callback) {
let recode = {};
let RTCPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
// 如果不存在则使用一个iframe绕过
if (!RTCPeerConnection) {
// 因为这里用到了iframe,所以在调用这个方法的script上必须有一个iframe标签
// <iframe id="iframe" sandbox="allow-same-origin" style="display:none;"></iframe>
let win = iframe.contentWindow;
RTCPeerConnection = win.RTCPeerConnection || win.mozRTCPeerConnection || win.webkitRTCPeerConnection;
}

//创建实例,生成连接
let pc = new RTCPeerConnection();

// 匹配字符串中符合ip地址的字段
function handleCandidate(candidate) {
let ip_regexp = /([0-9]{1,3}(\.[0-9]{1,3}){3}|([a-f0-9]{1,4}((:[a-f0-9]{1,4}){7}|:+[a-f0-9]{1,4}){6}))/;
let ip_isMatch = candidate.match(ip_regexp)[1];
if (!recode[ip_isMatch]) {
callback(ip_isMatch);
recode[ip_isMatch] = true;
}
}

//监听icecandidate事件
pc.onicecandidate = (ice) => {
if (ice.candidate) {
handleCandidate(ice.candidate.candidate);
}
};
//建立一个伪数据的通道
pc.createDataChannel('');
pc.createOffer((res) => {
pc.setLocalDescription(res);
}, () => {});

//延迟,让一切都能完成
setTimeout(() => {
let lines = pc.localDescription.sdp.split('\n');
lines.forEach(item => {
if (item.indexOf('a=candidate:') === 0) {
handleCandidate(item);
}
})
}, 1000);
}

//获取浏览器信息
//getBrowserInfo();

function getBrowserInfo(){
var agent = navigator.userAgent.toLowerCase() ;
console.log(agent);
var arr = [];
var system = agent.split(' ')[1].split(' ')[0].split('(')[1];
arr.push(system);
var regStr_edge = /edge\/[\d.]+/gi;
var regStr_ie = /trident\/[\d.]+/gi ;
var regStr_ff = /firefox\/[\d.]+/gi;
var regStr_chrome = /chrome\/[\d.]+/gi ;
var regStr_saf = /safari\/[\d.]+/gi ;
var regStr_opera = /opr\/[\d.]+/gi;
//IE
if(agent.indexOf("trident") > 0){
arr.push(agent.match(regStr_ie)[0].split('/')[0]);
arr.push(agent.match(regStr_ie)[0].split('/')[1]);
return arr;
}
//Edge
if(agent.indexOf('edge') > 0){
arr.push(agent.match(regStr_edge)[0].split('/')[0]);
arr.push(agent.match(regStr_edge)[0].split('/')[1]);
return arr;
}
//firefox
if(agent.indexOf("firefox") > 0){
arr.push(agent.match(regStr_ff)[0].split('/')[0]);
arr.push(agent.match(regStr_ff)[0].split('/')[1]);
return arr;
}
//Opera
if(agent.indexOf("opr")>0){
arr.push(agent.match(regStr_opera)[0].split('/')[0]);
arr.push(agent.match(regStr_opera)[0].split('/')[1]);
return arr;
}
//Safari
if(agent.indexOf("safari") > 0 && agent.indexOf("chrome") < 0){
arr.push(agent.match(regStr_saf)[0].split('/')[0]);
arr.push(agent.match(regStr_saf)[0].split('/')[1]);
return arr;
}
//Chrome
if(agent.indexOf("chrome") > 0){
arr.push(agent.match(regStr_chrome)[0].split('/')[0]);
arr.push(agent.match(regStr_chrome)[0].split('/')[1]);
return arr;
}else{
arr.push('请更换主流浏览器,例如chrome,firefox,opera,safari,IE,Edge!')
return arr;
}
}

2,获取公网ip

1
2
3
4
引入接口文件
<script type="text/javascript" src="http://pv.sohu.com/cityjson?ie=utf-8"></script>
// 返回结果为 var returnCitySN = {"cip": "27.46.86.71", "cid": "440000", "cname": "广东省"};
在下面的js中,通过调用returnCitySN.cip就可以获取 外网的ip