Skip to content

Commit

Permalink
Perf: preload phishing data source
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed Jan 17, 2025
1 parent 42428e1 commit 96cf0fc
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 6 deletions.
15 changes: 9 additions & 6 deletions Build/lib/get-phishing-domains.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { processHosts } from './parse-filter/hosts';
import { processDomainLists } from './parse-filter/domainlists';
import { processHostsWithPreload } from './parse-filter/hosts';
import { processDomainListsWithPreload } from './parse-filter/domainlists';

import * as tldts from 'tldts-experimental';

Expand Down Expand Up @@ -207,15 +207,18 @@ const processPhihsingDomains = cache(function processPhihsingDomains(domainArr:
temporaryBypass: !isCI || DEBUG_DOMAIN_TO_FIND !== null
});

const downloads = [
...PHISHING_DOMAIN_LISTS_EXTRA.map(entry => processDomainListsWithPreload(...entry)),
...PHISHING_HOSTS_EXTRA.map(entry => processHostsWithPreload(...entry))
];

export function getPhishingDomains(parentSpan: Span) {
return parentSpan.traceChild('get phishing domains').traceAsyncFn(async (span) => {
const domainArr = await span.traceChildAsync('download/parse/merge phishing domains', async (curSpan) => {
const domainArr: string[] = [];

await Promise.all([
...PHISHING_DOMAIN_LISTS_EXTRA.map(entry => processDomainLists(curSpan, ...entry)),
...PHISHING_HOSTS_EXTRA.map(entry => processHosts(curSpan, ...entry))
]).then(domainGroups => domainGroups.forEach(appendArrayInPlaceCurried(domainArr)));
const domainGroups = await Promise.all(downloads.map(task => task(curSpan)));
domainGroups.forEach(appendArrayInPlaceCurried(domainArr));

return domainArr;
});
Expand Down
18 changes: 18 additions & 0 deletions Build/lib/parse-filter/domainlists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,21 @@ export function processDomainLists(
return domainSets;
});
}

export function processDomainListsWithPreload(domainListsUrl: string, mirrors: string[] | null, includeAllSubDomain = false) {
const downloadPromise = fetchAssets(domainListsUrl, mirrors);

return (span: Span) => span.traceChildAsync(`process domainlist: ${domainListsUrl}`, async (span) => {
const text = await span.traceChildPromise('download', downloadPromise);
const domainSets: string[] = [];
const filterRules = text.split('\n');

span.traceChildSync('parse domain list', () => {
for (let i = 0, len = filterRules.length; i < len; i++) {
domainListLineCb(filterRules[i], domainSets, includeAllSubDomain, domainListsUrl);
}
});

return domainSets;
});
}
20 changes: 20 additions & 0 deletions Build/lib/parse-filter/hosts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,23 @@ export function processHosts(
return domainSets;
});
}

export function processHostsWithPreload(hostsUrl: string, mirrors: string[] | null, includeAllSubDomain = false) {
const downloadPromise = fetchAssets(hostsUrl, mirrors);

return (span: Span) => span.traceChildAsync(`process hosts: ${hostsUrl}`, async (span) => {
const text = await span.traceChild('download').tracePromise(downloadPromise);

const domainSets: string[] = [];

const filterRules = text.split('\n');

span.traceChild('parse hosts').traceSyncFn(() => {
for (let i = 0, len = filterRules.length; i < len; i++) {
hostsLineCb(filterRules[i], domainSets, includeAllSubDomain, hostsUrl);
}
});

return domainSets;
});
}

0 comments on commit 96cf0fc

Please sign in to comment.