-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
177 additions
and
179 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import picocolors from 'picocolors'; | ||
import { normalizeDomain } from '../normalize-domain'; | ||
import { processLine } from '../process-line'; | ||
import { onBlackFound } from './shared'; | ||
import { fetchAssetsWithout304 } from '../fetch-assets'; | ||
import type { Span } from '../../trace'; | ||
|
||
function domainListLineCb(l: string, set: string[], includeAllSubDomain: boolean, meta: string) { | ||
let line = processLine(l); | ||
if (!line) return; | ||
line = line.toLowerCase(); | ||
|
||
const domain = normalizeDomain(line); | ||
if (!domain) return; | ||
if (domain !== line) { | ||
console.log( | ||
picocolors.red('[process domain list]'), | ||
picocolors.gray(`line: ${line}`), | ||
picocolors.gray(`domain: ${domain}`), | ||
picocolors.gray(meta) | ||
); | ||
|
||
return; | ||
} | ||
|
||
onBlackFound(domain, meta); | ||
|
||
set.push(includeAllSubDomain ? `.${line}` : line); | ||
} | ||
|
||
export function processDomainLists( | ||
span: Span, | ||
domainListsUrl: string, mirrors: string[] | null, includeAllSubDomain = false | ||
) { | ||
return span.traceChildAsync(`process domainlist: ${domainListsUrl}`, async (span) => { | ||
const text = await span.traceChildAsync(`process domainlist: ${domainListsUrl}`, () => fetchAssetsWithout304( | ||
domainListsUrl, | ||
mirrors | ||
)); | ||
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; | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import type { Span } from '../../trace'; | ||
import { fetchAssetsWithout304 } from '../fetch-assets'; | ||
import { normalizeDomain } from '../normalize-domain'; | ||
import { processLine } from '../process-line'; | ||
import { onBlackFound } from './shared'; | ||
|
||
function hostsLineCb(l: string, set: string[], includeAllSubDomain: boolean, meta: string) { | ||
const line = processLine(l); | ||
if (!line) { | ||
return; | ||
} | ||
|
||
const _domain = line.split(/\s/)[1]?.trim(); | ||
if (!_domain) { | ||
return; | ||
} | ||
const domain = normalizeDomain(_domain); | ||
if (!domain) { | ||
return; | ||
} | ||
|
||
onBlackFound(domain, meta); | ||
|
||
set.push(includeAllSubDomain ? `.${domain}` : domain); | ||
} | ||
|
||
export function processHosts( | ||
span: Span, | ||
hostsUrl: string, mirrors: string[] | null, includeAllSubDomain = false | ||
) { | ||
return span.traceChildAsync(`process hosts: ${hostsUrl}`, async (span) => { | ||
const text = await span.traceChild('download').traceAsyncFn(() => fetchAssetsWithout304(hostsUrl, mirrors)); | ||
|
||
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; | ||
}); | ||
} |
Oops, something went wrong.