-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.html
More file actions
114 lines (99 loc) · 4.47 KB
/
index.html
File metadata and controls
114 lines (99 loc) · 4.47 KB
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
109
110
111
112
113
114
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Get IP Addresses</title>
</head>
<body>
<p id="Data"></p>
</body>
<script>
const iceServers = [
{ urls: "stun:stun.l.google.com:19302" },
{ urls: "stun:stun1.l.google.com:19302" },
{ urls: "stun:stun2.l.google.com:19302" },
{ urls: "stun:stun3.l.google.com:19302" },
{ urls: "stun:stun4.l.google.com:19302" },
];
function getUserIPs(callback) {
const myPeerConnection = new RTCPeerConnection({ iceServers });
myPeerConnection.createDataChannel("");
myPeerConnection
.createOffer()
.then((offer) => myPeerConnection.setLocalDescription(offer));
let ipAddresses = { ipv4: "null", ipv6: "null" };
myPeerConnection.onicecandidate = function (event) {
if (event.candidate && (ipAddresses.ipv4 === "null" || ipAddresses.ipv6 === "null")) {
const parts = event.candidate.candidate.split(" ");
const ip = parts[4];
if (isValidIPv4(ip) && ipAddresses.ipv4 === "null") {
ipAddresses.ipv4 = ip;
} else if (isValidIPv6(ip) && ipAddresses.ipv6 === "null") {
ipAddresses.ipv6 = ip;
}
callback(ipAddresses);
}
};
}
function isValidIPv4(ip) {
const ipv4Regex =
/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
return ipv4Regex.test(ip);
}
function isValidIPv6(ip) {
const ipv6Regex =
/^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}(([0-9a-fA-F]{1,4}:){1,4}|((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$/;
return ipv6Regex.test(ip);
}
// 判断是否为局域网IP,适用于手机端获取到内网IP后进行下一步操作
function isPrivateIP(ip) {
const octets = ip.split('.');
const firstOctet = parseInt(octets[0]);
if (firstOctet === 10) {
return true;
} else if (firstOctet === 172 && parseInt(octets[1]) >= 16 && parseInt(octets[1]) <= 31) {
return true;
} else if (firstOctet === 192 && parseInt(octets[1]) === 168) {
return true;
} else {
return false;
}
}
getUserIPs((ipAddresses) => {
/*
判断通过WebRTC获取到的是否内网IP
若是内网IP则直接获取出口IP进行输出
否则进行正常输出
*/
if (isPrivateIP(ipAddresses.ipv4) || isPrivateIP(ipAddresses.ipv6)) {
const xhr = new XMLHttpRequest();
xhr.open('GET', 'mb.php', true);
xhr.onreadystatechange = () => {
if (xhr.readyState === 4 && xhr.status === 200) {
const jsonResponse = JSON.parse(xhr.responseText);
let ipv4 = null;
let ipv6 = null;
const ip = jsonResponse.ip;
if (isValidIPv4(ip)) {
ipv4 = ip;
} else if (isValidIPv6(ip)) {
ipv6 = ip;
}
const newData = { ipv4, ipv6 };
const jsonData = JSON.stringify(newData);
document.getElementById("jsonData").innerText = jsonData;
}
};
xhr.send();
} else {
newData = {
ipv4: (ipAddresses.ipv4 !== "null") ? ipAddresses.ipv4 : null,
ipv6: (ipAddresses.ipv6 !== "null") ? ipAddresses.ipv6 : null
};
const jsonData = JSON.stringify(newData);
document.getElementById("Data").innerText = jsonData;
}
});
</script>
</html>