-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathremoveByCriteria.js
More file actions
157 lines (133 loc) · 4.75 KB
/
Copy pathremoveByCriteria.js
File metadata and controls
157 lines (133 loc) · 4.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
import fs from 'node:fs/promises';
import { existsSync } from 'node:fs';
import path, { dirname } from 'node:path';
import { parse } from 'csv-parse/sync';
import { stringify } from 'csv-stringify/sync';
import { fileURLToPath } from 'node:url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const LISTS_DIR = path.join(__dirname, 'lists');
const FILES = {
txt: path.join(LISTS_DIR, 'main.txt'),
csv: path.join(LISTS_DIR, 'details.csv'),
};
/**
* CSV column mapping for criteria types.
* Maps user-friendly names to CSV column names.
*/
const CRITERIA_COLUMNS = {
endpoint: 'Endpoint',
ip: 'IP',
userAgent: 'User-Agent',
};
/**
* Removes matching IPs from the main.txt file.
* @param {string} filePath - Path to the text file
* @param {Set<string>} ipsToRemove - Set of IPs to remove
* @returns {Promise<number>} Number of lines removed
*/
const removeFromTxt = async (filePath, ipsToRemove) => {
if (!existsSync(filePath)) return 0;
try {
const content = await fs.readFile(filePath, 'utf8');
const originalLines = content.split('\n').map(line => line.trim()).filter(Boolean);
const filteredLines = originalLines.filter(line => !ipsToRemove.has(line));
await fs.writeFile(filePath, filteredLines.join('\n') + '\n', 'utf8');
return originalLines.length - filteredLines.length;
} catch (error) {
console.error(`Error removing from TXT: ${error.message}`);
return 0;
}
};
/**
* Removes matching rows from the CSV file based on criteria.
* @param {string} filePath - Path to the CSV file
* @param {string} criteria - Value to match
* @param {string} columnName - Column name to search in
* @returns {Promise<Object>} Object with removed count and IPs
*/
const removeFromCsv = async (filePath, criteria, columnName) => {
if (!existsSync(filePath)) throw new Error(`CSV file not found: ${filePath}`);
const content = await fs.readFile(filePath, 'utf8');
const records = parse(content, { columns: true, skip_empty_lines: true });
const matchingRecords = [];
const remainingRecords = [];
for (const record of records) {
const cellValue = record[columnName];
if (cellValue && cellValue.includes(criteria)) {
matchingRecords.push(record);
} else {
remainingRecords.push(record);
}
}
// Extract unique IPs from matching records
const ipsToRemove = new Set(matchingRecords.map(r => r.IP).filter(Boolean));
if (remainingRecords.length > 0) {
const csvContent = stringify(remainingRecords, { header: true, columns: Object.keys(remainingRecords[0]) });
await fs.writeFile(filePath, csvContent, 'utf8');
} else {
const header = Object.keys(records[0] || {}).join(',') + '\n';
await fs.writeFile(filePath, header, 'utf8');
}
return {
removedCount: matchingRecords.length,
ipsToRemove,
};
};
/**
* Main function to remove entries by criteria.
* @param {string} criteria - Value to search for
* @param {string} criteriaType - Type of criteria (endpoint, ip, userAgent)
*/
export const removeByCriteria = async (criteria, criteriaType) => {
// Validation
if (!criteria || !criteriaType) throw new Error('Both criteria and criteriaType are required parameters');
if (!CRITERIA_COLUMNS[criteriaType]) {
throw new Error(`Invalid criteriaType: ${criteriaType}, valid options: ${Object.keys(CRITERIA_COLUMNS).join(', ')}`);
}
console.log(`Removing entries where ${criteriaType} contains: "${criteria}"`);
try {
const { removedCount, ipsToRemove } = await removeFromCsv(
FILES.csv,
criteria,
CRITERIA_COLUMNS[criteriaType]
);
let txtRemovedCount = 0;
if (ipsToRemove.size > 0) {
txtRemovedCount = await removeFromTxt(FILES.txt, ipsToRemove);
}
// Log results
console.log('──────────────────────────────────────');
console.log('Removal Summary:');
console.log(` CSV rows removed : ${removedCount}`);
console.log(` TXT lines removed : ${txtRemovedCount}`);
console.log(` Unique IPs affected : ${ipsToRemove.size}`);
console.log('──────────────────────────────────────');
if (removedCount === 0) {
console.warn(`No matching entries found for ${criteriaType}: "${criteria}"`);
}
return {
csvRemoved: removedCount,
txtRemoved: txtRemovedCount,
ipsAffected: ipsToRemove.size,
};
} catch (err) {
throw new Error(`Failed to remove by criteria: ${err.message}`, { cause: err });
}
};
/**
* Main execution function.
*/
(async () => {
try {
// Remove by user-agent
// await removeByCriteria('', 'userAgent');
// Remove by IP
await removeByCriteria('', 'ip');
// Remove by endpoint
// await removeByCriteria('', 'endpoint');
} catch (err) {
console.error('[FATAL]', err.message);
process.exit(1);
}
})();