64 lines
1.8 KiB
TypeScript
64 lines
1.8 KiB
TypeScript
|
import * as plugins from './plugins.js';
|
||
|
import * as paths from './paths.js';
|
||
|
import * as helpers from './helpers.js';
|
||
|
|
||
|
export interface IFeodoTrackerData {
|
||
|
ID: string;
|
||
|
Dateadded: string;
|
||
|
DestinationIP: string;
|
||
|
DestinationPort: string;
|
||
|
Malware: string;
|
||
|
Reporter: string;
|
||
|
}
|
||
|
|
||
|
|
||
|
export class FeodoTracker {
|
||
|
private static readonly FEODO_TRACKER_API_URL: string = 'https://feodotracker.abuse.ch/downloads/ipblocklist_recommended.txt';
|
||
|
|
||
|
public async getData(): Promise<IFeodoTrackerData[]> {
|
||
|
plugins.smartfile.fs.ensureDirSync(paths.feodoTrackerTmp);
|
||
|
const txtPath = plugins.path.join(paths.feodoTrackerTmp, 'feodo.txt');
|
||
|
|
||
|
const response = await plugins.nodeFetch(FeodoTracker.FEODO_TRACKER_API_URL, {
|
||
|
...(helpers.findProxy() ? {
|
||
|
agent: helpers.getAgent(),
|
||
|
} : {})
|
||
|
});
|
||
|
if (!response.ok) {
|
||
|
throw new Error(`HTTP error! status: ${response.status}`);
|
||
|
}
|
||
|
|
||
|
await new Promise((resolve, reject) => {
|
||
|
const fileStream = plugins.fs.createWriteStream(txtPath);
|
||
|
// @ts-ignore
|
||
|
const readable = plugins.stream.Readable.from(response.body);
|
||
|
plugins.stream.pipeline(readable, fileStream, (err) => {
|
||
|
if (err) reject(err);
|
||
|
else resolve(null);
|
||
|
});
|
||
|
});
|
||
|
|
||
|
let data: IFeodoTrackerData[] = [];
|
||
|
await new Promise((resolve, reject) => {
|
||
|
plugins.stream.pipeline(
|
||
|
plugins.fs.createReadStream(txtPath),
|
||
|
plugins.csv({
|
||
|
headers: ['ID', 'Dateadded', 'DestinationIP', 'DestinationPort', 'Malware', 'Reporter'],
|
||
|
mapValues: ({ header, value }) => value.trim()
|
||
|
}),
|
||
|
(err) => {
|
||
|
if (err) reject(err);
|
||
|
}
|
||
|
)
|
||
|
.on('data', (row) => {
|
||
|
data.push(row);
|
||
|
})
|
||
|
.on('end', resolve)
|
||
|
.on('error', reject);
|
||
|
});
|
||
|
|
||
|
plugins.smartfile.fs.removeSync(paths.feodoTrackerTmp);
|
||
|
return data;
|
||
|
}
|
||
|
}
|