-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
368 lines (313 loc) · 9.75 KB
/
background.js
File metadata and controls
368 lines (313 loc) · 9.75 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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
// Background script for IPeed
chrome.runtime.onInstalled.addListener(() => {
// Create context menu
chrome.contextMenus.create({
id: "checkIpLocation",
title: "Check IP Location",
contexts: ["selection"]
});
});
// Handle context menu click
chrome.contextMenus.onClicked.addListener((info, tab) => {
if (info.menuItemId === "checkIpLocation") {
// Check if the tab can receive messages
if (tab.url && !tab.url.startsWith('chrome://') && !tab.url.startsWith('chrome-extension://') && !tab.url.startsWith('edge://') && !tab.url.startsWith('moz-extension://')) {
const selectedText = info.selectionText.trim();
if (selectedText) {
processIP(selectedText, tab.id);
}
}
}
});
// Process IP address
function processIP(text, tabId) {
const ip = extractAndValidateIP(text);
if (ip) {
// Fetch IP location data and open popup
fetchIPLocation(ip, tabId, true);
} else {
// Store error and open popup
chrome.storage.local.set({
lastError: `No valid IP address found in: ${text}`
});
openPopup();
}
}
// Extract and strictly validate IP from text
function extractAndValidateIP(text) {
// First extract potential IPs
const ipRegex = /(?:(?: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]?)/g;
const matches = text.match(ipRegex);
if (!matches) return null;
// Validate each match strictly
for (const match of matches) {
if (isValidIP(match)) {
return match;
}
}
return null;
}
// Strict IP validation - only allows valid IPv4 addresses
function isValidIP(ip) {
// Must match exact IPv4 format
const ipRegex = /^(?:(?: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]?)$/;
if (!ipRegex.test(ip)) {
return false;
}
// Additional validation: split and check each octet
const parts = ip.split('.');
// Must have exactly 4 parts
if (parts.length !== 4) {
return false;
}
// Each part must be a valid number 0-255
for (const part of parts) {
const num = parseInt(part, 10);
// Check for leading zeros (except for "0" itself)
if (part.length > 1 && part[0] === '0') {
return false;
}
// Check range
if (isNaN(num) || num < 0 || num > 255) {
return false;
}
// Ensure the string representation matches the number
if (part !== num.toString()) {
return false;
}
}
return true;
}
// Open popup window
function openPopup() {
chrome.action.openPopup().catch(() => {
// Fallback: popup opening failed
});
}
// Get flag emoji for country code
function getFlagEmoji(countryCode) {
try {
if (!countryCode || typeof countryCode !== 'string' || countryCode.length !== 2) {
return '';
}
const upperCode = countryCode.toUpperCase();
const codePoints = [];
for (let i = 0; i < upperCode.length; i++) {
const char = upperCode[i];
const charCode = char.charCodeAt(0);
if (charCode < 65 || charCode > 90) {
return '';
}
codePoints.push(127397 + charCode);
}
return String.fromCodePoint(...codePoints);
} catch (error) {
console.warn('IPeed: Error generating flag emoji for country code:', countryCode, error.message);
return '';
}
}
// API configurations for different providers
const API_PROVIDERS = [
{
name: 'ipinfo.io',
url: (ip) => `https://ipinfo.io/${ip}/json`,
transform: (data) => {
const [lat, lon] = (data.loc || ',').split(',');
return {
ip: data.ip,
country_name: data.country,
country_code: data.country,
region: data.region,
city: data.city,
latitude: lat ? parseFloat(lat) : null,
longitude: lon ? parseFloat(lon) : null,
timezone: data.timezone,
org: data.org,
postal: data.postal,
provider: 'ipinfo.io'
};
},
validate: (data) => !data.error && data.ip
},
{
name: 'ipapi.co',
url: (ip) => `https://ipapi.co/${ip}/json/`,
transform: (data) => ({
ip: data.ip,
country_name: data.country_name,
country_code: data.country_code,
region: data.region,
city: data.city,
latitude: data.latitude,
longitude: data.longitude,
timezone: data.timezone,
org: data.org,
as: data.asn,
asname: data.org,
postal: data.postal,
currency: data.currency,
languages: data.languages,
provider: 'ipapi.co'
}),
validate: (data) => !data.error && data.ip
},
{
name: 'ip-api.com',
url: (ip) => `https://ip-api.com/json/${ip}?fields=status,message,country,countryCode,region,regionName,city,lat,lon,timezone,isp,org,as,asname,zip,mobile,proxy,hosting,query`,
transform: (data) => ({
ip: data.query,
country_name: data.country,
country_code: data.countryCode,
region: data.regionName,
city: data.city,
latitude: data.lat,
longitude: data.lon,
timezone: data.timezone,
org: data.org || data.isp,
as: data.as,
asname: data.asname,
zip: data.zip,
mobile: data.mobile,
proxy: data.proxy,
hosting: data.hosting,
provider: 'ip-api.com'
}),
validate: (data) => data.status === 'success' && data.query
}
];
// Get next API provider using round-robin rotation
let currentProviderIndex = 0;
function getNextProvider() {
const provider = API_PROVIDERS[currentProviderIndex];
currentProviderIndex = (currentProviderIndex + 1) % API_PROVIDERS.length;
return provider;
}
// Get shuffled providers for fallback
function getShuffledProviders() {
return [...API_PROVIDERS].sort(() => Math.random() - 0.5);
}
// Fetch from a specific provider
async function fetchFromProvider(provider, ip) {
const response = await fetch(provider.url(ip));
if (!response.ok) {
throw new Error(`${provider.name} HTTP error: ${response.status} ${response.statusText}`);
}
const data = await response.json();
if (!provider.validate(data)) {
throw new Error(`${provider.name} returned invalid data: ${data.error || data.message || data.reason || 'Unknown error'}`);
}
return provider.transform(data);
}
// Fetch IP location data with API rotation and fallback
async function fetchIPLocation(ip, tabId, openPopupAfter = false) {
// Final validation before API call
if (!isValidIP(ip)) {
const errorMsg = "Invalid IP address format";
chrome.storage.local.set({
lastError: errorMsg
});
if (openPopupAfter) {
openPopup();
}
return;
}
try {
// Check cache first
const cacheKey = `ip_cache_${ip}`;
const cached = await chrome.storage.local.get(cacheKey);
if (cached[cacheKey] && Date.now() - cached[cacheKey].timestamp < 86400000) { // 24 hour cache
if (openPopupAfter) {
openPopup();
}
return;
}
// Use round-robin for primary attempt, then fallback to shuffled order
const primaryProvider = getNextProvider();
let lastError;
// First try the round-robin selected provider
try {
const data = await fetchFromProvider(primaryProvider, ip);
// Validate that the API returned data for the same IP we requested
if (data.ip !== ip) {
throw new Error(`${primaryProvider.name} returned data for different IP`);
}
// Cache the result
chrome.storage.local.set({
[cacheKey]: {
data: data,
timestamp: Date.now()
}
});
// Open popup to show results
if (openPopupAfter) {
openPopup();
}
// Store in chrome storage for popup
chrome.storage.local.set({
lastIP: ip,
lastData: data
});
return; // Success, exit the function
} catch (error) {
lastError = error;
}
// If primary provider failed, try others in random order
const fallbackProviders = getShuffledProviders().filter(p => p.name !== primaryProvider.name);
for (const provider of fallbackProviders) {
try {
const data = await fetchFromProvider(provider, ip);
// Validate that the API returned data for the same IP we requested
if (data.ip !== ip) {
throw new Error(`${provider.name} returned data for different IP`);
}
// Cache the result
chrome.storage.local.set({
[cacheKey]: {
data: data,
timestamp: Date.now()
}
});
// Open popup to show results
if (openPopupAfter) {
openPopup();
}
// Store in chrome storage for popup
chrome.storage.local.set({
lastIP: ip,
lastData: data
});
return; // Success, exit the function
} catch (error) {
lastError = error;
continue; // Try next provider
}
}
// If we get here, all providers failed
throw new Error(`All API providers failed. Last error: ${lastError?.message || 'Unknown error'}`);
} catch (error) {
// Store error and open popup
chrome.storage.local.set({
lastError: error.message
});
if (openPopupAfter) {
openPopup();
}
}
}
// Handle messages from content script
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === "checkIP") {
// Validate IP before processing
const validatedIP = extractAndValidateIP(request.ip);
if (validatedIP) {
processIP(validatedIP, sender.tab.id);
} else {
chrome.tabs.sendMessage(sender.tab.id, {
action: "showError",
message: "Invalid IP address format"
}).catch(error => {
console.log('Content script not available for validation error:', error.message);
});
}
}
});