-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
349 lines (297 loc) · 12.9 KB
/
script.js
File metadata and controls
349 lines (297 loc) · 12.9 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
class IPLookupApp {
constructor() {
this.elements = {
ipInput: document.getElementById('ipInput'),
searchBtn: document.getElementById('searchBtn'),
getCurrentIpBtn: document.getElementById('getCurrentIpBtn'),
results: document.getElementById('results'),
resultsContent: document.getElementById('resultsContent'),
error: document.getElementById('error')
};
this.steamCacheData = {
v4: [],
v6: []
};
this.communitiesTranslation = {};
this.init();
}
init() {
this.bindEvents();
this.setupEnterKey();
this.setupRealTimeValidation();
this.loadSteamCacheData();
}
bindEvents() {
this.elements.searchBtn.addEventListener('click', () => this.searchIP());
this.elements.getCurrentIpBtn.addEventListener('click', () => this.getCurrentIP());
}
setupEnterKey() {
this.elements.ipInput.addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
this.searchIP();
}
});
}
setupRealTimeValidation() {
this.elements.ipInput.addEventListener('input', (e) => {
const ip = e.target.value.trim();
this.validateIPInput(ip);
});
this.elements.ipInput.addEventListener('blur', (e) => {
const ip = e.target.value.trim();
if (ip && !this.isValidIP(ip)) {
this.showInputError('請輸入有效的 IPv4 或 IPv6 地址');
} else {
this.hideInputError();
}
});
}
validateIPInput(ip) {
if (!ip) {
this.hideInputError();
this.elements.ipInput.classList.remove('valid', 'invalid');
return;
}
if (this.isValidIP(ip)) {
this.elements.ipInput.classList.remove('invalid');
this.elements.ipInput.classList.add('valid');
this.hideInputError();
} else {
this.elements.ipInput.classList.remove('valid');
this.elements.ipInput.classList.add('invalid');
}
}
showInputError(message) {
let errorElement = this.elements.ipInput.parentNode.querySelector('.input-error');
if (!errorElement) {
errorElement = document.createElement('div');
errorElement.className = 'input-error';
this.elements.ipInput.parentNode.appendChild(errorElement);
}
errorElement.textContent = message;
errorElement.style.display = 'block';
}
hideInputError() {
const errorElement = this.elements.ipInput.parentNode.querySelector('.input-error');
if (errorElement) {
errorElement.style.display = 'none';
}
}
async loadSteamCacheData() {
try {
console.log('Loading Steam cache data...');
// Load IPv4 data
const v4Response = await fetch('data/steam-cache-v4.json');
if (v4Response.ok) {
this.steamCacheData.v4 = await v4Response.json();
console.log(`Loaded ${this.steamCacheData.v4.length} IPv4 entries`);
}
// Load IPv6 data
const v6Response = await fetch('data/steam-cache-v6.json');
if (v6Response.ok) {
this.steamCacheData.v6 = await v6Response.json();
console.log(`Loaded ${this.steamCacheData.v6.length} IPv6 entries`);
}
// Load communities translation
const communitiesResponse = await fetch('data/communities.json');
if (communitiesResponse.ok) {
const communitiesData = await communitiesResponse.json();
this.communitiesTranslation = {};
communitiesData.forEach(item => {
this.communitiesTranslation[item.community] = item.description;
});
console.log(`Loaded ${Object.keys(this.communitiesTranslation).length} community translations`);
}
console.log('Steam cache data loaded successfully');
} catch (error) {
console.error('Error loading Steam cache data:', error);
}
}
isIPInSteamCache(ip) {
// Determine if it's IPv4 or IPv6
const isIPv6 = ip.includes(':');
const cacheData = isIPv6 ? this.steamCacheData.v6 : this.steamCacheData.v4;
let bestMatch = null;
let longestPrefix = -1;
// Check if IP is in any of the prefixes and find the longest match
for (const entry of cacheData) {
if (this.ipInPrefix(ip, entry.prefix)) {
const prefixLength = parseInt(entry.prefix.split('/')[1]);
if (prefixLength > longestPrefix) {
longestPrefix = prefixLength;
bestMatch = entry;
}
}
}
return bestMatch;
}
ipInPrefix(ip, prefix) {
// Simple prefix matching - for production use, consider using a proper IP library
const [prefixIP, prefixLength] = prefix.split('/');
const prefixLengthNum = parseInt(prefixLength);
if (ip.includes(':')) {
// IPv6 - simplified check
return ip.startsWith(prefixIP.replace(/::.*$/, ''));
} else {
// IPv4 - simplified check
const ipParts = ip.split('.').map(Number);
const prefixParts = prefixIP.split('.').map(Number);
// Check if IP matches the prefix
for (let i = 0; i < Math.floor(prefixLengthNum / 8); i++) {
if (ipParts[i] !== prefixParts[i]) {
return false;
}
}
// Check the remaining bits
const remainingBits = prefixLengthNum % 8;
if (remainingBits > 0) {
const mask = (0xFF << (8 - remainingBits)) & 0xFF;
return (ipParts[Math.floor(prefixLengthNum / 8)] & mask) === (prefixParts[Math.floor(prefixLengthNum / 8)] & mask);
}
return true;
}
}
formatCommunities(communities) {
if (!communities || communities.length === 0) {
return 'N/A';
}
else if (communities.length === 1) {
return '自有網段';
}
return communities.map(community => {
const translation = this.communitiesTranslation[community];
if (translation) {
return `${community} (${translation})`;
} else {
return community;
}
}).join(', ');
}
async getCurrentIP() {
this.setLoading(true, this.elements.getCurrentIpBtn);
this.hideError();
try {
const response = await fetch('https://api.ipify.org?format=json');
const data = await response.json();
this.elements.ipInput.value = data.ip;
this.searchIP();
} catch (error) {
this.showError('Failed to get current IP address. Please try again.');
console.error('Error getting current IP:', error);
} finally {
this.setLoading(false, this.elements.getCurrentIpBtn);
}
}
async searchIP() {
const ip = this.elements.ipInput.value.trim();
if (!ip) {
this.showError('請輸入 IP 地址');
return;
}
if (!this.isValidIP(ip)) {
this.showError('請輸入有效的 IPv4 或 IPv6 地址');
return;
}
this.setLoading(true, this.elements.searchBtn);
this.hideError();
try {
// First check if IP is in Steam cache
const steamCacheEntry = this.isIPInSteamCache(ip);
// Get geolocation data
const response = await fetch(`https://ipapi.co/${ip}/json/`);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const geoData = await response.json();
if (geoData.error) {
throw new Error(geoData.reason || 'Invalid IP address');
}
// Combine Steam cache data with geolocation data
this.displayResults(geoData, steamCacheEntry);
} catch (error) {
this.showError('Failed to get IP information. Please try again.');
console.error('Error searching IP:', error);
} finally {
this.setLoading(false, this.elements.searchBtn);
}
}
isValidIP(ip) {
// Comprehensive IPv4 validation
const ipv4Regex = /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
// Comprehensive IPv6 validation (handles various formats)
const ipv6Regex = /^(?:(?:[0-9a-fA-F]{1,4}:){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}(?:(?: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 ipv4Regex.test(ip) || ipv6Regex.test(ip);
}
displayResults(geoData, steamCacheEntry) {
let resultsHTML = `
<div class="info-item">
<span class="info-label">IP 地址</span>
<span class="info-value">${geoData.ip || 'N/A'}</span>
</div>
<div class="info-item">
<span class="info-label">國家</span>
<span class="info-value">${geoData.country_name || 'N/A'}</span>
</div>
<div class="info-item">
<span class="info-label">網路服務商</span>
<span class="info-value">${geoData.org || 'N/A'}</span>
</div>
`;
// Add Steam cache information if available
if (steamCacheEntry) {
resultsHTML += `
<div class="info-item steam-cache-info">
<span class="info-label">Steam Cache 狀態</span>
<span class="info-value steam-cache-yes">✅ 在服務範圍內</span>
</div>
<div class="info-item">
<span class="info-label">Steam Cache 網段</span>
<span class="info-value">${steamCacheEntry.prefix}</span>
</div>
<div class="info-item">
<span class="info-label">AS 路徑</span>
<span class="info-value">${steamCacheEntry.aspath || '自有網段'}</span>
</div>
<div class="info-item">
<span class="info-label">BGP 社群資訊</span>
<span class="info-value">${this.formatCommunities(steamCacheEntry.communities)}</span>
</div>
`;
} else {
resultsHTML += `
<div class="info-item steam-cache-info steam-cache-no">
<span class="info-label">Steam Cache 狀態</span>
<span class="info-value steam-cache-no">❌ 不在服務範圍內</span>
</div>
`;
}
this.elements.resultsContent.innerHTML = resultsHTML;
this.elements.results.style.display = 'block';
// Scroll to results
this.elements.results.scrollIntoView({ behavior: 'smooth' });
}
setLoading(isLoading, button) {
const btnText = button.querySelector('.btn-text');
const spinner = button.querySelector('.loading-spinner');
if (isLoading) {
button.disabled = true;
if (btnText) btnText.style.display = 'none';
if (spinner) spinner.style.display = 'block';
} else {
button.disabled = false;
if (btnText) btnText.style.display = 'block';
if (spinner) spinner.style.display = 'none';
}
}
showError(message) {
this.elements.error.textContent = message;
this.elements.error.style.display = 'block';
}
hideError() {
this.elements.error.style.display = 'none';
}
}
// Initialize the app when the DOM is loaded
document.addEventListener('DOMContentLoaded', () => {
new IPLookupApp();
});