Update log.js

This commit is contained in:
Sefinek 2024-12-25 14:22:28 +01:00
parent 03b1fe55ac
commit 663e038b39
5 changed files with 39 additions and 36 deletions

View file

@ -31,13 +31,13 @@ const fetchBlockedIPs = async () => {
) )
); );
log('log', `Fetched ${events.length} (filtered ${filtered.length}) events from Cloudflare`); log(0, `Fetched ${events.length} (filtered ${filtered.length}) events from Cloudflare`);
return filtered; return filtered;
} else { } else {
throw new Error(`Failed to retrieve data from Cloudflare (status ${status}); ${JSON.stringify(data?.errors)}`); throw new Error(`Failed to retrieve data from Cloudflare (status ${status}); ${JSON.stringify(data?.errors)}`);
} }
} catch (err) { } catch (err) {
log('error', err.response?.data ? `${err.response.status} HTTP ERROR Cloudflare API: ${JSON.stringify(err.response.data, null, 2)}` : `Unknown error with Cloudflare API: ${err.message}`); log(2, err.response?.data ? `${err.response.status} HTTP ERROR Cloudflare API: ${JSON.stringify(err.response.data, null, 2)}` : `Unknown error with Cloudflare API: ${err.message}`);
return null; return null;
} }
}; };
@ -62,19 +62,19 @@ const isIPReportedRecently = (rayId, ip, reportedIPs) => {
const reportIP = async (event, uri, country, hostname, endpoint, cycleErrorCounts) => { const reportIP = async (event, uri, country, hostname, endpoint, cycleErrorCounts) => {
if (!uri) { if (!uri) {
logToCSV(event.rayName, event.clientIP, country, hostname, endpoint, event.userAgent, event.action, 'MISSING_URI'); logToCSV(event.rayName, event.clientIP, country, hostname, endpoint, event.userAgent, event.action, 'MISSING_URI');
log('warn', `Missing URL ${event.clientIP}; URI: ${uri}`); log(1, `Missing URL ${event.clientIP}; URI: ${uri}`);
return false; return false;
} }
if (event.clientIP === clientIp.address) { if (event.clientIP === clientIp.address) {
logToCSV(event.rayName, event.clientIP, country, hostname, endpoint, event.userAgent, event.action, 'YOUR_IP_ADDRESS'); logToCSV(event.rayName, event.clientIP, country, hostname, endpoint, event.userAgent, event.action, 'YOUR_IP_ADDRESS');
log('log', `Your IP address (${event.clientIP}) was unexpectedly received from Cloudflare. URI: ${uri}`); log(0, `Your IP address (${event.clientIP}) was unexpectedly received from Cloudflare. URI: ${uri}`);
return false; return false;
} }
if (uri.length > MAX_URL_LENGTH) { if (uri.length > MAX_URL_LENGTH) {
logToCSV(event.rayName, event.clientIP, country, hostname, endpoint, event.userAgent, event.action, 'URI_TOO_LONG'); logToCSV(event.rayName, event.clientIP, country, hostname, endpoint, event.userAgent, event.action, 'URI_TOO_LONG');
// log('log', `URI too long ${event.clientIP}; Received: ${uri}`); // log(0, `URI too long ${event.clientIP}; Received: ${uri}`);
return false; return false;
} }
@ -86,16 +86,16 @@ const reportIP = async (event, uri, country, hostname, endpoint, cycleErrorCount
}, { headers: headers.ABUSEIPDB }); }, { headers: headers.ABUSEIPDB });
logToCSV(event.rayName, event.clientIP, country, hostname, endpoint, event.userAgent, event.action, 'REPORTED'); logToCSV(event.rayName, event.clientIP, country, hostname, endpoint, event.userAgent, event.action, 'REPORTED');
log('log', `Reported ${event.clientIP}; URI: ${uri}`); log(0, `Reported ${event.clientIP}; URI: ${uri}`);
return true; return true;
} catch (err) { } catch (err) {
if (err.response?.status === 429) { if (err.response?.status === 429) {
logToCSV(event.rayName, event.clientIP, country, hostname, endpoint, event.userAgent, event.action, 'TOO_MANY_REQUESTS'); logToCSV(event.rayName, event.clientIP, country, hostname, endpoint, event.userAgent, event.action, 'TOO_MANY_REQUESTS');
log('log', `429 for ${event.clientIP} (${event.rayName}); Endpoint: ${endpoint}`); log(0, `429 for ${event.clientIP} (${event.rayName}); Endpoint: ${endpoint}`);
cycleErrorCounts.blocked++; cycleErrorCounts.blocked++;
} else { } else {
log('error', `Error ${err.response?.status} while reporting ${event.clientIP}; URI: ${uri}; (${err.response?.data})`); log(2, `Error ${err.response?.status} while reporting ${event.clientIP}; URI: ${uri}; (${err.response?.data})`);
cycleErrorCounts.otherErrors++; cycleErrorCounts.otherErrors++;
} }
@ -104,7 +104,7 @@ const reportIP = async (event, uri, country, hostname, endpoint, cycleErrorCount
}; };
(async () => { (async () => {
log('log', 'Loading data, please wait...'); log(0, 'Loading data, please wait...');
await clientIp.fetchIPAddress(); await clientIp.fetchIPAddress();
// Sefinek API // Sefinek API
@ -117,23 +117,23 @@ const reportIP = async (event, uri, country, hostname, endpoint, cycleErrorCount
try { try {
process.send('ready'); process.send('ready');
} catch (err) { } catch (err) {
log('log', `Failed to send ready signal to parent process. ${err.message}`); log(0, `Failed to send ready signal to parent process. ${err.message}`);
} }
} }
// AbuseIPDB // AbuseIPDB
let cycleId = 1; let cycleId = 1;
while (true) { while (true) {
log('log', `===================== Reporting Cycle No. ${cycleId} =====================`); log(0, `===================== Reporting Cycle No. ${cycleId} =====================`);
const blockedIPEvents = await fetchBlockedIPs(); const blockedIPEvents = await fetchBlockedIPs();
if (!blockedIPEvents) { if (!blockedIPEvents) {
log('warn', 'No events fetched, skipping cycle...'); log(1, 'No events fetched, skipping cycle...');
continue; continue;
} }
const userIp = clientIp.getAddress(); const userIp = clientIp.getAddress();
if (!userIp) log('warn', `Your IP address is missing! Received: ${userIp}`); if (!userIp) log(1, `Your IP address is missing! Received: ${userIp}`);
let cycleImageSkippedCount = 0, cycleProcessedCount = 0, cycleReportedCount = 0, cycleSkippedCount = 0; let cycleImageSkippedCount = 0, cycleProcessedCount = 0, cycleReportedCount = 0, cycleSkippedCount = 0;
const cycleErrorCounts = { blocked: 0, otherErrors: 0 }; const cycleErrorCounts = { blocked: 0, otherErrors: 0 };
@ -143,12 +143,12 @@ const reportIP = async (event, uri, country, hostname, endpoint, cycleErrorCount
cycleProcessedCount++; cycleProcessedCount++;
const ip = event.clientIP; const ip = event.clientIP;
if (ip === userIp) { if (ip === userIp) {
log('log', `The IP address ${ip} belongs to this machine. Ignoring...`); log(0, `The IP address ${ip} belongs to this machine. Ignoring...`);
cycleSkippedCount++; cycleSkippedCount++;
continue; continue;
} }
if (whitelist.endpoints.includes(event.clientRequestPath)) return log('log', `Skipping ${event.clientRequestPath}...`); if (whitelist.endpoints.includes(event.clientRequestPath)) return log(0, `Skipping ${event.clientRequestPath}...`);
const reportedIPs = readReportedIPs(); const reportedIPs = readReportedIPs();
const { recentlyReported } = isIPReportedRecently(event.rayName, ip, reportedIPs); const { recentlyReported } = isIPReportedRecently(event.rayName, ip, reportedIPs);
@ -157,7 +157,7 @@ const reportIP = async (event, uri, country, hostname, endpoint, cycleErrorCount
// const hoursAgo = Math.floor(timeDifference / (1000 * 60 * 60)); // const hoursAgo = Math.floor(timeDifference / (1000 * 60 * 60));
// const minutesAgo = Math.floor((timeDifference % (1000 * 60 * 60)) / (1000 * 60)); // const minutesAgo = Math.floor((timeDifference % (1000 * 60 * 60)) / (1000 * 60));
// const secondsAgo = Math.floor((timeDifference % (1000 * 60)) / 1000); // const secondsAgo = Math.floor((timeDifference % (1000 * 60)) / 1000);
// log('log', `${ip} was ${reason} ${hoursAgo}h ${minutesAgo}m ${secondsAgo}s ago. Skipping...`); // log(0, `${ip} was ${reason} ${hoursAgo}h ${minutesAgo}m ${secondsAgo}s ago. Skipping...`);
// } // }
cycleSkippedCount++; cycleSkippedCount++;
@ -168,7 +168,7 @@ const reportIP = async (event, uri, country, hostname, endpoint, cycleErrorCount
cycleImageSkippedCount++; cycleImageSkippedCount++;
if (!wasImageRequestLogged(ip, reportedIPs)) { if (!wasImageRequestLogged(ip, reportedIPs)) {
if (imageRequestLogged) continue; if (imageRequestLogged) continue;
log('log', 'Skipping image requests in this cycle...'); log(0, 'Skipping image requests in this cycle...');
imageRequestLogged = true; imageRequestLogged = true;
} }
@ -182,15 +182,15 @@ const reportIP = async (event, uri, country, hostname, endpoint, cycleErrorCount
} }
} }
log('log', `- Reported IPs: ${cycleReportedCount}`); log(0, `- Reported IPs: ${cycleReportedCount}`);
log('log', `- Total IPs processed: ${cycleProcessedCount}`); log(0, `- Total IPs processed: ${cycleProcessedCount}`);
log('log', `- Skipped IPs: ${cycleSkippedCount}`); log(0, `- Skipped IPs: ${cycleSkippedCount}`);
log('log', `- Ignored image requests: ${cycleImageSkippedCount}`); log(0, `- Ignored image requests: ${cycleImageSkippedCount}`);
log('log', `- Rate-limits: ${cycleErrorCounts.blocked}`); log(0, `- Rate-limits: ${cycleErrorCounts.blocked}`);
log('log', `- Other errors: ${cycleErrorCounts.otherErrors}`); log(0, `- Other errors: ${cycleErrorCounts.otherErrors}`);
log('log', '===================== End of Reporting Cycle ====================='); log(0, '===================== End of Reporting Cycle =====================');
log('log', `Waiting ${formatDelay(CYCLE_INTERVAL)}...`); log(0, `Waiting ${formatDelay(CYCLE_INTERVAL)}...`);
cycleId++; cycleId++;
await new Promise(resolve => setTimeout(resolve, CYCLE_INTERVAL)); await new Promise(resolve => setTimeout(resolve, CYCLE_INTERVAL));
} }

View file

@ -1,7 +1,10 @@
const levels = { const levels = {
log: '[INFO]', 0: { method: 'log', label: '[INFO]' },
warn: '[WARN]', 1: { method: 'warn', label: '[WARN]' },
error: '[FAIL]', 2: { method: 'error', label: '[FAIL]' },
}; };
module.exports = (level, msg) => console[level](`${levels[level]} ${msg}`); module.exports = (level, msg) => {
const { method, label } = levels[level] || { method: 'log', label: '[N/A]' };
console[method](`${label} ${msg}`);
};

View file

@ -32,7 +32,7 @@ module.exports = async () => {
return true; return true;
}); });
if (!uniqueLogs.length) return log('log', 'No unique IPs to send to Sefinek API'); if (!uniqueLogs.length) return log(0, 'No unique IPs to send to Sefinek API');
try { try {
const res = await axios.post(API_URL, { const res = await axios.post(API_URL, {
@ -47,10 +47,10 @@ module.exports = async () => {
})), })),
}, { headers: { 'Authorization': process.env.SEFINEK_API_SECRET } }); }, { headers: { 'Authorization': process.env.SEFINEK_API_SECRET } });
log('log', `Successfully sent ${uniqueLogs.length} logs to Sefinek API. Status: ${res.status}`); log(0, `Successfully sent ${uniqueLogs.length} logs to Sefinek API. Status: ${res.status}`);
uniqueLogs.forEach(ip => updateSefinekAPIInCSV(ip.rayId, true)); uniqueLogs.forEach(ip => updateSefinekAPIInCSV(ip.rayId, true));
} catch (err) { } catch (err) {
log('error', `Failed to send logs to Sefinek API. Status: ${err.response?.status}. Message: ${err.response?.data?.message || err.stack}`); log(2, `Failed to send logs to Sefinek API. Status: ${err.response?.status}. Message: ${err.response?.data?.message || err.stack}`);
} }
}; };

View file

@ -10,10 +10,10 @@ const fetchIPAddress = async () => {
if (data?.success) { if (data?.success) {
address = data.message; address = data.message;
} else { } else {
log('error', 'Failed to retrieve your IP'); log(2, 'Failed to retrieve your IP');
} }
} catch (err) { } catch (err) {
log('error', 'Error fetching your IP:', err.stack); log(2, 'Error fetching your IP:', err.stack);
} }
}; };

View file

@ -12,7 +12,7 @@ const checkCSVSize = () => {
const stats = fs.statSync(CSV_FILE_PATH); const stats = fs.statSync(CSV_FILE_PATH);
if (stats.size > MAX_CSV_SIZE_BYTES) { if (stats.size > MAX_CSV_SIZE_BYTES) {
fs.writeFileSync(CSV_FILE_PATH, CSV_HEADER); fs.writeFileSync(CSV_FILE_PATH, CSV_HEADER);
log('log', `CSV file size exceeded ${MAX_CSV_SIZE_BYTES / (1024 * 1024)} MB. File has been reset.`); log(0, `CSV file size exceeded ${MAX_CSV_SIZE_BYTES / (1024 * 1024)} MB. File has been reset.`);
} }
}; };
@ -57,7 +57,7 @@ const readReportedIPs = () => {
const updateSefinekAPIInCSV = (rayId, reportedToSefinekAPI) => { const updateSefinekAPIInCSV = (rayId, reportedToSefinekAPI) => {
if (!fs.existsSync(CSV_FILE_PATH)) { if (!fs.existsSync(CSV_FILE_PATH)) {
log('error', 'CSV file does not exist'); log(2, 'CSV file does not exist');
return; return;
} }