-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathindex.ts
493 lines (427 loc) · 15.5 KB
/
index.ts
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
import * as fs from 'fs';
import * as zod from 'zod';
import { consola } from 'consola';
import { stringify } from 'csv-stringify/sync';
const WHOTRACKSME_INPUT_PATH = 'source/whotracksme.json';
const WHOTRACKSME_COMPANIES_INPUT_PATH = 'source/whotracksme_companies.json';
const COMPANIES_INPUT_PATH = 'source/companies.json';
const TRACKERS_INPUT_PATH = 'source/trackers.json';
const VPN_SERVICES_INPUT_PATH = 'source/vpn_services.json';
const WHOTRACKSME_OUTPUT_PATH = 'dist/whotracksme.json';
const COMPANIES_OUTPUT_PATH = 'dist/companies.json';
const TRACKERS_OUTPUT_PATH = 'dist/trackers.json';
const TRACKERS_CSV_OUTPUT_PATH = 'dist/trackers.csv';
const VPN_SERVICES_OUTPUT_PATH = 'dist/vpn_services.json';
/**
* Schema parser for the companies JSON file.
*/
const companiesJSONSchema = zod.object({
timeUpdated: zod.string(),
companies: zod.record(
zod.object({
name: zod.string(),
// FIXME: Does the string value always have to be a full URL?
// FIXME: Can value be null?
websiteUrl: zod.string().or(zod.null()),
// FIXME: Can value be null?
description: zod.string().or(zod.null()),
source: zod.string().optional(),
}).strict(),
),
}).strict();
/**
* Schema type of the companies JSON file.
*/
type CompaniesJSON = zod.infer<typeof companiesJSONSchema>;
/**
* Schema parser for the trackers JSON file.
*/
const trackersJSONSchema = zod.object({
timeUpdated: zod.string(),
categories: zod.record(zod.string()),
trackers: zod.record(
zod.object({
name: zod.string(),
categoryId: zod.number().optional(),
// FIXME: Can value be null?
url: zod.string().or(zod.null()),
companyId: zod.string().or(zod.null()),
source: zod.string().optional(),
}).strict(),
),
trackerDomains: zod.record(zod.string()),
}).strict();
/**
* Schema type of the trackers JSON file.
*/
type TrackersJSON = zod.infer<typeof trackersJSONSchema>;
/**
* Schema parser for the VPN service.
*/
const vpnServiceSchema = zod.object({
service_id: zod.string(),
service_name: zod.string(),
categories: zod.array(zod.string()),
domains: zod.array(zod.string()),
icon_domain: zod.string(),
modified_time: zod.string().optional(),
}).strict();
/**
* Schema type of the VPN service.
*/
type VpnService = zod.infer<typeof vpnServiceSchema>;
/**
* Schema parser for the VPN services JSON file.
*/
const vpnServicesJSONSchema = vpnServiceSchema.array();
/**
* Schema type of the VPN services JSON file.
*/
type VpnServicesJSON = zod.infer<typeof vpnServicesJSONSchema>;
/**
* Reads and parse JSON file from source.
*
* @param source Source file path.
* @returns JSON data.
* @throws an error if the JSON file is invalid or does not exist.
*/
function readJSON(source: string): unknown {
consola.info(`Reading ${source} file`);
return JSON.parse(fs.readFileSync(source, 'utf8'));
}
/**
* Writes JSON file to destination.
*
* @param destination Destination file path.
* @param data JSON data to write.
*
* @throws an error if the JSON file is invalid or destination does not exist.
*/
function writeJSON<T>(destination: string, data: T): void {
consola.info(`Writing ${destination} file`);
fs.writeFileSync(destination, `${JSON.stringify(data, null, '\t')}\n`);
}
/**
* Reads and parse the companies JSON.
*
* @param source Source JSON path.
* @returns parsed companies JSON data.
*/
function readCompaniesJSON(source: string): CompaniesJSON {
const data = readJSON(source);
return companiesJSONSchema.parse(data);
}
/**
* Reads and parse the trackers JSON.
*
* @param source Source JSON path.
* @returns parsed trackers JSON data.
*/
function readTrackersJSON(source: string): TrackersJSON {
const data = readJSON(source);
return trackersJSONSchema.parse(data);
}
/**
* Reads and parse the vpn services JSON.
*
* @param source Source JSON path.
* @returns parsed services JSON data.
*/
function readVpnServicesJSON(source: string): VpnServicesJSON {
const data = readJSON(source);
return vpnServicesJSONSchema.parse(data);
}
/**
* Sorts the records alphabetically by key or value.
*
* @param record The record to sort.
* @param sortByKey Flag specifying whether to sort by key or value. Defaults sort by key (true).
* @returns New sorted record.
*/
function sortRecordsAlphabetically<T>(
record: Record<string, T>,
sortByKey = true,
): Record<string, T> {
return Object.fromEntries(Object.entries(record).sort(([aKey, aValue], [bKey, bValue]) => {
const a = sortByKey ? aKey : aValue;
const b = sortByKey ? bKey : bValue;
if (a < b) {
return -1;
}
if (a > b) {
return 1;
}
return 0;
}));
}
type Companies = CompaniesJSON['companies'];
/**
* Creates new companies data by merging whotracksme companies data with AdGuard companies data.
* Adguard data marked as source: 'AdGuard'.
*
* @param whotrackmeCompanies Whotracksme companies data.
* @param adguardCompanies AdGuard companies data.
* @returns merged companies data.
*/
function buildCompanies(
whotrackmeCompanies: Companies,
adguardCompanies: Companies,
): Companies {
// clone the whotracksme companies data
const merged = { ...whotrackmeCompanies };
Object.entries(adguardCompanies).forEach(([id, company]) => {
// Overriding whotracksme info with the companies defined in the source
// companies file.
// Also, indicate, that the company came from AdGuard data.
merged[id] = company;
merged[id].source = 'AdGuard';
});
return sortRecordsAlphabetically(merged);
}
type TrackersCategories = TrackersJSON['categories'];
/**
* Creates new categories data by merging whotracksme companies data with AdGuard companies data.
*
* @param whotracksmeTrackersCategories
* @param adguardTrackersCategories
* @returns merged categories data.
*/
function buildTrackersCategories(
whotracksmeTrackersCategories: TrackersCategories,
adguardTrackersCategories: TrackersCategories,
): TrackersCategories {
return sortRecordsAlphabetically({
...whotracksmeTrackersCategories,
...adguardTrackersCategories,
});
}
type Trackers = TrackersJSON['trackers'];
/**
* Creates new trackers data by merging whotracksme trackers data with AdGuard trackers data.
* Also validates the company reference for each tracker.
*
* @param whotracksmeTrackers Whotracksme trackers data.
* @param adguardTrackers AdGuard trackers data.
* @param companies Merged companies data.
* @returns merged trackers data.
* @throws an error if at least one company reference is invalid.
*/
function buildTrackers(
whotracksmeTrackers: Trackers,
adguardTrackers: Trackers,
companies: Companies,
): Trackers {
const merged = { ...whotracksmeTrackers };
Object.entries(adguardTrackers).forEach(([id, tracker]) => {
// Overriding whotracksme info with the companies defined in the source
// companies file.
// Also, indicate, that the company came from AdGuard data.
merged[id] = tracker;
merged[id].source = 'AdGuard';
});
// Validate the company reference and exit immediately if it's wrong.
Object.entries(merged).forEach(([id, tracker]) => {
if (tracker.companyId === null) {
consola.warn(`Tracker ${id} has no company ID, consider adding it`);
} else if (!companies[tracker.companyId]) {
throw new Error(`Tracker ${id} has an invalid company ID: ${tracker.companyId}`);
}
});
return sortRecordsAlphabetically(merged);
}
type TrackersDomains = TrackersJSON['trackerDomains'];
/**
* Creates new tracker domains data by merging whotracksme trackers
* domains data with AdGuard trackers domains data.
* Also validates the tracker reference for each tracker domain.
*
* @param whotracksmeTrackersDomains Whotracksme trackers domains data.
* @param adguardTrackersDomains AdGuard trackers domains data.
* @param trackers Merged trackers data.
* @returns merged tracker domains data.
* @throws an error if at least one tracker reference is invalid.
*/
function buildTrackersDomains(
whotracksmeTrackersDomains: TrackersDomains,
adguardTrackersDomains: TrackersDomains,
trackers: Trackers,
): TrackersDomains {
const merged = { ...whotracksmeTrackersDomains, ...adguardTrackersDomains };
// Validate the tracker domains and exit immediately if it's wrong.
Object.entries(merged).forEach(([domain, trackerId]) => {
// Make sure that the tracker ID is valid.
if (!trackers[trackerId]) {
throw new Error(`Tracker domain ${domain} has an invalid tracker ID: ${trackerId}`);
}
});
return sortRecordsAlphabetically(merged, false);
}
/**
* Formats the current date and time in the format 'YYYY-MM-DD HH:MM'.
* @returns The current date and time in the format 'YYYY-MM-DD HH:MM'.
*/
function getCurrentDateTime(): string {
const now = new Date();
const day = String(now.getDate()).padStart(2, '0');
const month = String(now.getMonth() + 1).padStart(2, '0');
const year = now.getFullYear();
const hours = String(now.getHours()).padStart(2, '0');
const minutes = String(now.getMinutes()).padStart(2, '0');
return `${year}-${month}-${day} ${hours}:${minutes}`;
}
/**
* Updates the modified_time field in the vpn services JSON file.
* If a service is not present in the upToDateMap, it will be added with the current date.
* If service was changed, the modified_time field will be updated to the current date.
* @param vpnInput The updated vpn services JSON file.
* @param vpnOutput The previously recorded vpn services JSON file.
* @returns The updated vpn services JSON file with the modified_time field updated, if necessary.
*/
function updateVpnServicesJSONDate(
vpnInput: VpnServicesJSON,
vpnOutput: VpnServicesJSON,
): VpnServicesJSON {
const timeUpdated = getCurrentDateTime();
// Create maps of services based on their id
const upToDateMap: { [key: string]: VpnService } = vpnOutput.reduce((map, obj) => ({
...map,
[obj.service_id]: obj,
}), {});
const updatedMap: { [key: string]: VpnService } = vpnInput.reduce((map, obj) => ({
...map,
[obj.service_id]: obj,
}), {});
// Iterate over keys in updatedMap
Object.keys(updatedMap).forEach((key) => {
// Check if the key exists in upToDateMap
if (!upToDateMap[key]) {
upToDateMap[key] = {
...updatedMap[key],
modified_time: timeUpdated,
};
} else {
let shouldUpdateDate = false;
// Check values
Object.keys(updatedMap[key]).forEach((k) => {
// if the value is a string, compare the strings
if (typeof updatedMap[key][k as keyof VpnService] === 'string') {
if (
updatedMap[key][k as keyof VpnService]
!== upToDateMap[key][k as keyof VpnService]) {
shouldUpdateDate = true;
}
} else if (Array.isArray(updatedMap[key][k as keyof VpnService])) {
// if the value is an array, compare the arrays
const updatedArray = updatedMap[key][k as keyof VpnService] as any[];
const upToDateArray = upToDateMap[key][k as keyof VpnService] as any[];
if (!updatedArray.every((element, index) => element === upToDateArray[index])) {
shouldUpdateDate = true;
}
}
});
if (shouldUpdateDate) {
upToDateMap[key] = {
...updatedMap[key],
modified_time: timeUpdated,
};
}
}
});
return Object.values(sortRecordsAlphabetically(upToDateMap));
}
/**
* Builds the trackers CSV file in the following form:
* domain;tracker_id;category_id
*
* This CSV file is used in the ETL process of AdGuard DNS (for data enrichment),
* any changes to its format should be reflected in the ETL code as well.
*
* @param trackers Merge trackers data.
* @param trackersDomains Merged tracker domains data.
* @returns CSV file content.
* @throws an error if at least one tracker reference is invalid.
*/
function buildTrackersCSV(
trackers: Trackers,
trackersDomains: TrackersDomains,
): string {
// Init with the header.
let csv = 'domain;tracker_id;category_id\n';
Object.entries(trackersDomains).forEach(([domain, trackerId]) => {
const tracker = trackers[trackerId];
if (!tracker) {
throw new Error(`Tracker domain ${domain} has an invalid tracker ID: ${trackerId}`);
}
const { categoryId } = tracker;
if (typeof categoryId !== 'undefined') {
const csvRow = stringify([[domain, trackerId, categoryId]], {
delimiter: ';',
quoted_match: ',',
});
csv += csvRow;
} else {
consola.warn(`Tracker ${trackerId} has no category ID, consider adding it`);
}
});
return csv;
}
try {
consola.info('Start building the companies DB');
const timeUpdated = new Date().toISOString();
const whotrackmeTrackersJSON = readTrackersJSON(WHOTRACKSME_INPUT_PATH);
const adguardTrackersJSON = readTrackersJSON(TRACKERS_INPUT_PATH);
const whotrackmeCompaniesJSON = readCompaniesJSON(WHOTRACKSME_COMPANIES_INPUT_PATH);
const adguardCompaniesJSON = readCompaniesJSON(COMPANIES_INPUT_PATH);
writeJSON<TrackersJSON>(WHOTRACKSME_OUTPUT_PATH, {
...whotrackmeTrackersJSON,
timeUpdated,
});
consola.info(`Building ${COMPANIES_OUTPUT_PATH} file`);
const companies = buildCompanies(
whotrackmeCompaniesJSON.companies,
adguardCompaniesJSON.companies,
);
writeJSON<CompaniesJSON>(COMPANIES_OUTPUT_PATH, {
timeUpdated,
companies,
});
consola.info(`Building ${TRACKERS_OUTPUT_PATH} file`);
const categories = buildTrackersCategories(
whotrackmeTrackersJSON.categories,
adguardTrackersJSON.categories,
);
const trackers = buildTrackers(
whotrackmeTrackersJSON.trackers,
adguardTrackersJSON.trackers,
companies,
);
const trackerDomains = buildTrackersDomains(
whotrackmeTrackersJSON.trackerDomains,
adguardTrackersJSON.trackerDomains,
trackers,
);
writeJSON<TrackersJSON>(TRACKERS_OUTPUT_PATH, {
timeUpdated,
categories,
trackers,
trackerDomains,
});
consola.info(`Building ${TRACKERS_CSV_OUTPUT_PATH} file`);
const csv = buildTrackersCSV(trackers, trackerDomains);
fs.writeFileSync(TRACKERS_CSV_OUTPUT_PATH, csv);
// previously recorded VPN services JSON
const upToDateVpnServicesJSON = readVpnServicesJSON(VPN_SERVICES_OUTPUT_PATH);
// VPN services JSON with new records/updates
const vpnServicesJSON = readVpnServicesJSON(VPN_SERVICES_INPUT_PATH);
// modified VPN services JSON with updated modified_time field
// if service was updated or added
const updatedVpnServicesJSON = updateVpnServicesJSONDate(
vpnServicesJSON,
upToDateVpnServicesJSON,
);
writeJSON<VpnServicesJSON>(VPN_SERVICES_OUTPUT_PATH, updatedVpnServicesJSON);
consola.info('Finished building the companies DB');
} catch (ex) {
consola.error(`Error while building the companies DB: ${ex}`);
process.exit(1);
}