mirror of
https://github.com/SukkaW/Surge.git
synced 2025-12-15 18:50:34 +08:00
Parse reject with IP support [skip ci]
This commit is contained in:
parent
74654d1886
commit
b1592051ac
@ -10,12 +10,9 @@ export type TldTsParsed = ReturnType<typeof tldts.parse>;
|
|||||||
/**
|
/**
|
||||||
* Skipped the input non-empty check, the `domain` should not be empty.
|
* Skipped the input non-empty check, the `domain` should not be empty.
|
||||||
*/
|
*/
|
||||||
export function fastNormalizeDomainWithoutWww(domain: string, parsed: TldTsParsed | null = null) {
|
export function fastNormalizeDomainWithoutWwwNoIP(domain: string, parsed: TldTsParsed | null = null) {
|
||||||
// We don't want tldts to call its own "extractHostname" on ip, bail out ip first.
|
// We don't want tldts to call its own "extractHostname" on ip, bail out ip first.
|
||||||
// Now ip has been bailed out, we can safely set normalizeTldtsOpt.detectIp to false.
|
// This function won't run with IP, we can safely set normalizeTldtsOpt.detectIp to false.
|
||||||
if (isProbablyIpv4(domain) || isProbablyIpv6(domain)) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
parsed ??= tldts.parse(domain, normalizeTldtsOpt);
|
parsed ??= tldts.parse(domain, normalizeTldtsOpt);
|
||||||
// Private invalid domain (things like .tor, .dn42, etc)
|
// Private invalid domain (things like .tor, .dn42, etc)
|
||||||
@ -33,6 +30,19 @@ export function fastNormalizeDomainWithoutWww(domain: string, parsed: TldTsParse
|
|||||||
return parsed.hostname;
|
return parsed.hostname;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Skipped the input non-empty check, the `domain` should not be empty.
|
||||||
|
*/
|
||||||
|
export function fastNormalizeDomainWithoutWww(domain: string, parsed: TldTsParsed | null = null) {
|
||||||
|
// We don't want tldts to call its own "extractHostname" on ip, bail out ip first.
|
||||||
|
// Now ip has been bailed out, we can safely set normalizeTldtsOpt.detectIp to false.
|
||||||
|
if (isProbablyIpv4(domain) || isProbablyIpv6(domain)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return fastNormalizeDomainWithoutWwwNoIP(domain, parsed);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Skipped the input non-empty check, the `domain` should not be empty.
|
* Skipped the input non-empty check, the `domain` should not be empty.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@ -6,7 +6,8 @@ import { createRetrieKeywordFilter as createKeywordFilter } from 'foxts/retrie';
|
|||||||
import { looseTldtsOpt } from '../../constants/loose-tldts-opt';
|
import { looseTldtsOpt } from '../../constants/loose-tldts-opt';
|
||||||
import tldts from 'tldts-experimental';
|
import tldts from 'tldts-experimental';
|
||||||
import { NetworkFilter } from '@ghostery/adblocker';
|
import { NetworkFilter } from '@ghostery/adblocker';
|
||||||
import { fastNormalizeDomain, fastNormalizeDomainWithoutWww } from '../normalize-domain';
|
import { fastNormalizeDomain, fastNormalizeDomainWithoutWww, fastNormalizeDomainWithoutWwwNoIP } from '../normalize-domain';
|
||||||
|
import { isProbablyIpv4, isProbablyIpv6 } from 'foxts/is-probably-ip';
|
||||||
|
|
||||||
const enum ParseType {
|
const enum ParseType {
|
||||||
WhiteIncludeSubdomain = 0,
|
WhiteIncludeSubdomain = 0,
|
||||||
@ -14,6 +15,7 @@ const enum ParseType {
|
|||||||
BlackAbsolute = 1,
|
BlackAbsolute = 1,
|
||||||
BlackIncludeSubdomain = 2,
|
BlackIncludeSubdomain = 2,
|
||||||
ErrorMessage = 10,
|
ErrorMessage = 10,
|
||||||
|
BlackIP = 20,
|
||||||
Null = 1000,
|
Null = 1000,
|
||||||
NotParsed = 2000
|
NotParsed = 2000
|
||||||
}
|
}
|
||||||
@ -230,7 +232,15 @@ export function parse($line: string, result: [string, ParseType], includeThirdPa
|
|||||||
&& filter.isPlain() // isPlain() === !isRegex()
|
&& filter.isPlain() // isPlain() === !isRegex()
|
||||||
&& (!filter.isFullRegex())
|
&& (!filter.isFullRegex())
|
||||||
) {
|
) {
|
||||||
const hostname = fastNormalizeDomainWithoutWww(filter.hostname);
|
// We don't want tldts to call its own "extractHostname" on ip, bail out ip first.
|
||||||
|
// Now ip has been bailed out, we can safely set normalizeTldtsOpt.detectIp to false.
|
||||||
|
if (isProbablyIpv4(filter.hostname) || isProbablyIpv6(filter.hostname)) {
|
||||||
|
result[0] = filter.hostname;
|
||||||
|
result[1] = ParseType.BlackIP;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
const hostname = fastNormalizeDomainWithoutWwwNoIP(filter.hostname);
|
||||||
if (!hostname) {
|
if (!hostname) {
|
||||||
result[1] = ParseType.Null;
|
result[1] = ParseType.Null;
|
||||||
return result;
|
return result;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user