diff --git a/SmuggleSheild/background.js b/SmuggleSheild/background.js index af977d1..eb834a2 100644 --- a/SmuggleSheild/background.js +++ b/SmuggleSheild/background.js @@ -1,50 +1,88 @@ +const config = { + suspiciousURLPatterns: [ + /data:application\/octet-stream/i, + /blob:/i, + /javascript:/i + ], + suspiciousHeaders: ['content-disposition', 'content-type'], + logRetentionDays: 10, + cacheDurationMs: 5 * 60 * 1000, +}; + +const urlCache = new Map(); + +function debounce(func, delay) { + let timeoutId; + return (...args) => { + clearTimeout(timeoutId); + timeoutId = setTimeout(() => func(...args), delay); + }; +} + +const debouncedLogBlockedContent = debounce(logBlockedContent, 1000); + chrome.runtime.onMessage.addListener((request, sender, sendResponse) => { console.log('Received message:', request); - if (request.action === "logWarning") { - logBlockedContent(sender.tab.url, request.patterns, Date.now()); - console.warn(request.message); - } else if (request.action === "analyzeURL") { - const suspiciousURLPatterns = [ - /data:application\/octet-stream/i, - /blob:/i, - /javascript:/i - ]; - - const isSuspicious = suspiciousURLPatterns.some(pattern => pattern.test(request.url)); - sendResponse({isSuspicious: isSuspicious}); - return true; - } else if (request.action === "exportLogs") { - chrome.storage.local.get(['blockedLogs'], function(result) { - sendResponse({ logs: result.blockedLogs || [] }); - }); - return true; + switch (request.action) { + case "logWarning": + debouncedLogBlockedContent(sender.tab.url, request.patterns, Date.now()); + console.warn(request.message); + break; + case "analyzeURL": + const cachedResult = urlCache.get(request.url); + if (cachedResult && (Date.now() - cachedResult.timestamp < config.cacheDurationMs)) { + sendResponse({isSuspicious: cachedResult.isSuspicious}); + } else { + const isSuspicious = config.suspiciousURLPatterns.some(pattern => pattern.test(request.url)); + urlCache.set(request.url, {isSuspicious, timestamp: Date.now()}); + sendResponse({isSuspicious}); + } + return true; + case "exportLogs": + chrome.storage.local.get(['blockedLogs'], result => { + sendResponse({ logs: result.blockedLogs || [] }); + }); + return true; + case "updateConfig": + updateConfig(request.newConfig); + sendResponse({success: true}); + return true; } }); function logBlockedContent(url, patterns, timestamp) { - chrome.storage.local.get(['blockedLogs'], function(result) { + chrome.storage.local.get(['blockedLogs', 'otherData'], function(result) { let logs = result.blockedLogs || []; logs.push({ url, patterns, timestamp }); - const tenDaysAgo = Date.now() - (10 * 24 * 60 * 60 * 1000); - logs = logs.filter(log => log.timestamp > tenDaysAgo); + const retentionDate = Date.now() - (config.logRetentionDays * 24 * 60 * 60 * 1000); + logs = logs.filter(log => log.timestamp > retentionDate); - chrome.storage.local.set({ blockedLogs: logs }); + chrome.storage.local.set({ + blockedLogs: logs, + otherData: result.otherData // Preserve other data + }, () => { + if (chrome.runtime.lastError) { + console.error('Error saving logs:', chrome.runtime.lastError); + } + }); }); } + +function updateConfig(newConfig) { + Object.assign(config, newConfig); + urlCache.clear(); +} + chrome.webRequest.onHeadersReceived.addListener( (details) => { - const suspiciousHeaders = [ - 'Content-Disposition', - 'Content-Type' - ]; - const hasSuspiciousHeaders = details.responseHeaders.some(header => - suspiciousHeaders.includes(header.name.toLowerCase()) && + config.suspiciousHeaders.includes(header.name.toLowerCase()) && /attachment|application\/octet-stream/i.test(header.value) ); if (hasSuspiciousHeaders) { - chrome.tabs.sendMessage(details.tabId, {action: "suspiciousHeadersDetected"}); + chrome.tabs.sendMessage(details.tabId, {action: "suspiciousHeadersDetected"}) + .catch(error => console.error('Error sending message:', error)); } return {responseHeaders: details.responseHeaders}; @@ -52,3 +90,12 @@ chrome.webRequest.onHeadersReceived.addListener( {urls: [""]}, ["responseHeaders"] ); + +setInterval(() => { + const now = Date.now(); + for (const [url, data] of urlCache.entries()) { + if (now - data.timestamp > config.cacheDurationMs) { + urlCache.delete(url); + } + } +}, config.cacheDurationMs);