From 9eb81694c735fc0d581114a4d540f0e3f75170f8 Mon Sep 17 00:00:00 2001 From: Philipp Kunz Date: Tue, 1 Aug 2023 13:01:48 +0200 Subject: [PATCH] fix(core): update --- test/test.ts | 2 +- ts/00_commitinfo_data.ts | 2 +- ts/abuse.ch.classes.feodotracker.ts | 67 ++++++++++++----------------- 3 files changed, 30 insertions(+), 41 deletions(-) diff --git a/test/test.ts b/test/test.ts index 4e3434a..1cb09f6 100644 --- a/test/test.ts +++ b/test/test.ts @@ -17,7 +17,7 @@ tap.test('should deal with FeodoTracker data', async () => { const feodoTracker = new abuseCh.FeodoTracker(); const data = await feodoTracker.getData(); console.log(data.length); - console.log(data[0]); + console.log(data[1]); }); tap.start(); diff --git a/ts/00_commitinfo_data.ts b/ts/00_commitinfo_data.ts index 9b71610..2c5d795 100644 --- a/ts/00_commitinfo_data.ts +++ b/ts/00_commitinfo_data.ts @@ -3,6 +3,6 @@ */ export const commitinfo = { name: '@apiclient.xyz/abuse.ch', - version: '1.0.9', + version: '1.0.10', description: 'an unofficial client to retrieve abuse.ch data' } diff --git a/ts/abuse.ch.classes.feodotracker.ts b/ts/abuse.ch.classes.feodotracker.ts index bd903b1..71ddd97 100644 --- a/ts/abuse.ch.classes.feodotracker.ts +++ b/ts/abuse.ch.classes.feodotracker.ts @@ -3,61 +3,50 @@ 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; + ip_address: string; + port: number; + status: string; + hostname: string | null; + as_number: number; + as_name: string; + country: string; + first_seen: string; + last_online: string; + malware: string; } - export class FeodoTracker { - private static readonly FEODO_TRACKER_API_URL: string = 'https://feodotracker.abuse.ch/downloads/ipblocklist_recommended.txt'; + private static readonly FEODO_TRACKER_API_URL: string = 'https://feodotracker.abuse.ch/downloads/ipblocklist.json'; public async getData(): Promise { - 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); - }); - }); + const data: IFeodoTrackerData[] = await response.json(); - 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); - }); + // Ensure the data is an array and has the expected structure + if (!Array.isArray(data) || !data.every(item => + typeof item.ip_address === 'string' && + typeof item.port === 'number' && + (typeof item.hostname === 'string' || item.hostname === null) && + typeof item.as_number === 'number' && + typeof item.as_name === 'string' && + typeof item.country === 'string' && + typeof item.first_seen === 'string' && + typeof item.last_online === 'string' && + typeof item.malware === 'string' + )) { + throw new Error(`Invalid data structure!`); + } - plugins.smartfile.fs.removeSync(paths.feodoTrackerTmp); return data; } } +