-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathip.html
More file actions
137 lines (117 loc) · 5.76 KB
/
Copy pathip.html
File metadata and controls
137 lines (117 loc) · 5.76 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>IP 极速探测工具 (v2.0)</title>
<style>
:root { --primary: #2563eb; --bg: #f8fafc; --success: #059669; }
body { font-family: system-ui, -apple-system, sans-serif; background: var(--bg); color: #1e293b; display: flex; justify-content: center; padding: 20px; }
.card { background: white; padding: 2rem; border-radius: 1rem; width: 100%; max-width: 500px; box-shadow: 0 10px 15px -3px rgba(0,0,0,0.1); }
h2 { margin-top: 0; color: #0f172a; border-bottom: 2px solid var(--primary); padding-bottom: 10px; display: flex; justify-content: space-between; align-items: center; }
.res-item { margin: 1.2rem 0; padding: 1rem; background: #f1f5f9; border-radius: 0.5rem; border-left: 4px solid #cbd5e1; transition: all 0.3s; }
.res-item.active { border-left-color: var(--primary); background: #eff6ff; }
.label { font-size: 0.75rem; color: #64748b; font-weight: 700; text-transform: uppercase; letter-spacing: 0.05em; }
.value { font-family: 'ui-monospace', 'Cascadia Code', monospace; font-size: 1.1rem; color: #334155; margin-top: 4px; word-break: break-all; min-height: 1.5rem; }
.source { font-size: 10px; color: #94a3b8; margin-top: 4px; display: block; }
button { width: 100%; background: var(--primary); color: white; border: none; padding: 14px; border-radius: 0.5rem; font-weight: 600; cursor: pointer; transition: transform 0.1s, opacity 0.2s; }
button:active { transform: scale(0.98); }
.loading { color: #94a3b8; font-style: italic; animation: pulse 1.5s infinite; }
@keyframes pulse { 50% { opacity: 0.5; } }
.tag { font-size: 10px; padding: 2px 6px; border-radius: 4px; background: #e2e8f0; margin-left: 8px; vertical-align: middle; }
</style>
</head>
<body>
<div class="card">
<h2>🌐 IP 极速探测 <span id="status-tag" class="tag">Ready</span></h2>
<div id="box-v4" class="res-item">
<div class="label">公网 IPv4 地址</div>
<div id="v4-val" class="value loading">等待探测...</div>
<span id="v4-src" class="source"></span>
</div>
<div id="box-v6" class="res-item">
<div class="label">公网 IPv6 地址</div>
<div id="v6-val" class="value loading">等待探测...</div>
<span id="v6-src" class="source"></span>
</div>
<button onclick="startProbe()">立即刷新探测</button>
<div style="margin-top: 1rem; font-size: 12px; color: #94a3b8; line-height: 1.5;">
• 采用 <b>WebRTC + API</b> 混合探测模式<br>
• 已针对 iPad/iOS 及私密转发进行兼容优化<br>
• 探测结果仅在本地显示,不进行后台存留
</div>
</div>
<script>
const v4El = document.getElementById('v4-val');
const v6El = document.getElementById('v6-val');
const v4Src = document.getElementById('v4-src');
const v6Src = document.getElementById('v6-src');
function updateUI(type, value, source) {
const el = type === 'v4' ? v4El : v6El;
const srcEl = type === 'v4' ? v4Src : v6Src;
const box = document.getElementById(`box-${type}`);
// 如果已经有结果且是 API 获取的,WebRTC 就不覆盖它(反之亦然,除非新结果更准)
if (el.classList.contains('loading') || !srcEl.innerText.includes('API')) {
el.innerText = value;
el.className = "value";
srcEl.innerText = "检测源: " + source;
box.classList.add('active');
}
}
async function probeByAPI() {
// 使用针对国内优化且支持 CORS 的接口
const apis = [
{ url: 'https://4.ipw.cn/api/ip/myip', type: 'v4' },
{ url: 'https://6.ipw.cn/api/ip/myip', type: 'v6' }
];
apis.forEach(api => {
fetch(api.url)
.then(res => res.text())
.then(ip => {
if (ip) updateUI(api.type, ip.trim(), "HTTP API (高可靠)");
})
.catch(() => {/* 忽略单个失败 */});
});
}
function probeByWebRTC() {
const pc = new RTCPeerConnection({
iceServers: [
{ urls: 'stun:stun.cloudflare.com:3478' },
{ urls: 'stun:stun.l.google.com:19302' }
]
});
pc.createDataChannel("");
pc.createOffer().then(offer => pc.setLocalDescription(offer));
pc.onicecandidate = (ice) => {
if (!ice || !ice.candidate) return;
const cand = ice.candidate.candidate;
// 匹配 IPv4
const v4Match = /([0-9]{1,3}(\.[0-9]{1,3}){3})/.exec(cand);
if (v4Match && !cand.includes("192.168.") && !cand.includes("10.") && !cand.includes("172.")) {
updateUI('v4', v4Match[1], "WebRTC (极速)");
}
// 匹配 IPv6
const v6Match = /([0-9a-fA-F]{1,4}(:[0-9a-fA-F]{1,4}){5,7}|([0-9a-fA-F]{1,4}:){1,7}:|:((:[0-9a-fA-F]{1,4}){1,7}))/.exec(cand);
if (v6Match && !cand.includes("fe80:")) {
updateUI('v6', v6Match[1], "WebRTC (极速)");
}
};
setTimeout(() => pc.close(), 3000);
}
function startProbe() {
// 重置 UI
[v4El, v6El].forEach(el => {
el.innerText = "探测中...";
el.className = "value loading";
});
[v4Src, v6Src].forEach(el => el.innerText = "");
document.querySelectorAll('.res-item').forEach(b => b.classList.remove('active'));
// 并行执行两种探测
probeByWebRTC();
probeByAPI();
}
// 页面加载自动执行
window.onload = startProbe;
</script>
</body>
</html>