mirror of
https://github.com/SukkaW/Surge.git
synced 2025-12-12 01:00:34 +08:00
Fix ruleset processing
This commit is contained in:
parent
33788588f0
commit
16ca72b35f
@ -134,7 +134,6 @@ export const buildRejectDomainSet = task(require.main === module, __filename)(as
|
||||
// exclude keyword when creating trie
|
||||
if (!kwfilter(domain)) {
|
||||
baseTrie.add(domain);
|
||||
|
||||
extraTrie.whitelist(domain);
|
||||
}
|
||||
}
|
||||
@ -149,17 +148,17 @@ export const buildRejectDomainSet = task(require.main === module, __filename)(as
|
||||
});
|
||||
|
||||
// Dedupe domainSets
|
||||
const dudupedDominArray = span.traceChildSync('dedupe from covered subdomain (base)', () => domainsetDeduper(baseTrie));
|
||||
const dedupedDominArray = span.traceChildSync('dedupe from covered subdomain (base)', () => domainsetDeduper(baseTrie));
|
||||
const dudupedDominArrayExtra = span.traceChildSync('dedupe from covered subdomain (extra)', () => domainsetDeduper(extraTrie));
|
||||
|
||||
console.log(`Final size ${dudupedDominArray.length} + ${dudupedDominArrayExtra.length}`);
|
||||
console.log(`Final size ${dedupedDominArray.length} + ${dudupedDominArrayExtra.length}`);
|
||||
|
||||
const {
|
||||
domainMap: domainArrayMainDomainMap,
|
||||
subdomainMap: domainArraySubdomainMap
|
||||
} = span.traceChildSync(
|
||||
'build map for stat and sort',
|
||||
() => buildParseDomainMap(dudupedDominArray.concat(dudupedDominArrayExtra))
|
||||
() => buildParseDomainMap(dedupedDominArray.concat(dudupedDominArrayExtra))
|
||||
);
|
||||
|
||||
// Create reject stats
|
||||
@ -168,7 +167,7 @@ export const buildRejectDomainSet = task(require.main === module, __filename)(as
|
||||
.traceSyncFn(() => {
|
||||
const results = [];
|
||||
results.push('=== base ===');
|
||||
appendArrayInPlace(results, getStatMap(dudupedDominArray, domainArrayMainDomainMap));
|
||||
appendArrayInPlace(results, getStatMap(dedupedDominArray, domainArrayMainDomainMap));
|
||||
results.push('=== extra ===');
|
||||
appendArrayInPlace(results, getStatMap(dudupedDominArrayExtra, domainArrayMainDomainMap));
|
||||
return results;
|
||||
@ -189,7 +188,7 @@ export const buildRejectDomainSet = task(require.main === module, __filename)(as
|
||||
...ADGUARD_FILTERS.map(filter => ` - ${Array.isArray(filter) ? filter[0] : filter}`)
|
||||
],
|
||||
new Date(),
|
||||
span.traceChildSync('sort reject domainset (base)', () => sortDomains(dudupedDominArray, domainArrayMainDomainMap, domainArraySubdomainMap)),
|
||||
span.traceChildSync('sort reject domainset (base)', () => sortDomains(dedupedDominArray, domainArrayMainDomainMap, domainArraySubdomainMap)),
|
||||
'domainset',
|
||||
output('reject', 'domainset')
|
||||
),
|
||||
|
||||
@ -188,72 +188,73 @@ export const createRuleset = (
|
||||
singBoxPath: string,
|
||||
_clashMrsPath?: string
|
||||
]
|
||||
) => parentSpan.traceChild(`create ruleset: ${path.basename(surgePath, path.extname(surgePath))}`).traceAsyncFn(async (childSpan) => {
|
||||
content = processRuleSet(content);
|
||||
const surgeContent = childSpan.traceChildSync('process surge ruleset', () => {
|
||||
let _surgeContent;
|
||||
switch (type) {
|
||||
case 'domainset':
|
||||
_surgeContent = [MARK, ...content];
|
||||
break;
|
||||
case 'ruleset':
|
||||
_surgeContent = [`DOMAIN,${MARK}`, ...content];
|
||||
break;
|
||||
case 'ipcidr':
|
||||
_surgeContent = [`DOMAIN,${MARK}`, ...content.map(i => `IP-CIDR,${i}`)];
|
||||
break;
|
||||
case 'ipcidr6':
|
||||
_surgeContent = [`DOMAIN,${MARK}`, ...content.map(i => `IP-CIDR6,${i}`)];
|
||||
break;
|
||||
default:
|
||||
throw new TypeError(`Unknown type: ${type}`);
|
||||
}
|
||||
) => parentSpan.traceChildAsync(
|
||||
`create ruleset: ${path.basename(surgePath, path.extname(surgePath))}`,
|
||||
async (childSpan) => {
|
||||
const surgeContent = childSpan.traceChildSync('process surge ruleset', () => {
|
||||
let _surgeContent;
|
||||
switch (type) {
|
||||
case 'domainset':
|
||||
_surgeContent = [MARK, ...content];
|
||||
break;
|
||||
case 'ruleset':
|
||||
_surgeContent = [`DOMAIN,${MARK}`, ...processRuleSet(content)];
|
||||
break;
|
||||
case 'ipcidr':
|
||||
_surgeContent = [`DOMAIN,${MARK}`, ...processRuleSet(content.map(i => `IP-CIDR,${i}`))];
|
||||
break;
|
||||
case 'ipcidr6':
|
||||
_surgeContent = [`DOMAIN,${MARK}`, ...processRuleSet(content.map(i => `IP-CIDR6,${i}`))];
|
||||
break;
|
||||
default:
|
||||
throw new TypeError(`Unknown type: ${type}`);
|
||||
}
|
||||
|
||||
return withBannerArray(title, description, date, _surgeContent);
|
||||
});
|
||||
return withBannerArray(title, description, date, _surgeContent);
|
||||
});
|
||||
|
||||
const clashContent = childSpan.traceChildSync('convert incoming ruleset to clash', () => {
|
||||
let _clashContent;
|
||||
switch (type) {
|
||||
case 'domainset':
|
||||
_clashContent = [MARK, ...surgeDomainsetToClashDomainset(content)];
|
||||
break;
|
||||
case 'ruleset':
|
||||
_clashContent = [`DOMAIN,${MARK}`, ...surgeRulesetToClashClassicalTextRuleset(content)];
|
||||
break;
|
||||
case 'ipcidr':
|
||||
case 'ipcidr6':
|
||||
_clashContent = content;
|
||||
break;
|
||||
default:
|
||||
throw new TypeError(`Unknown type: ${type}`);
|
||||
}
|
||||
return withBannerArray(title, description, date, _clashContent);
|
||||
});
|
||||
const singboxContent = childSpan.traceChildSync('convert incoming ruleset to singbox', () => {
|
||||
let _singBoxContent;
|
||||
switch (type) {
|
||||
case 'domainset':
|
||||
_singBoxContent = surgeDomainsetToSingbox([MARK, ...content]);
|
||||
break;
|
||||
case 'ruleset':
|
||||
_singBoxContent = surgeRulesetToSingbox([`DOMAIN,${MARK}`, ...content]);
|
||||
break;
|
||||
case 'ipcidr':
|
||||
case 'ipcidr6':
|
||||
_singBoxContent = ipCidrListToSingbox(content);
|
||||
break;
|
||||
default:
|
||||
throw new TypeError(`Unknown type: ${type}`);
|
||||
}
|
||||
return stringify(_singBoxContent).split('\n');
|
||||
});
|
||||
const clashContent = childSpan.traceChildSync('convert incoming ruleset to clash', () => {
|
||||
let _clashContent;
|
||||
switch (type) {
|
||||
case 'domainset':
|
||||
_clashContent = [MARK, ...surgeDomainsetToClashDomainset(content)];
|
||||
break;
|
||||
case 'ruleset':
|
||||
_clashContent = [`DOMAIN,${MARK}`, ...surgeRulesetToClashClassicalTextRuleset(processRuleSet(content))];
|
||||
break;
|
||||
case 'ipcidr':
|
||||
case 'ipcidr6':
|
||||
_clashContent = content;
|
||||
break;
|
||||
default:
|
||||
throw new TypeError(`Unknown type: ${type}`);
|
||||
}
|
||||
return withBannerArray(title, description, date, _clashContent);
|
||||
});
|
||||
const singboxContent = childSpan.traceChildSync('convert incoming ruleset to singbox', () => {
|
||||
let _singBoxContent;
|
||||
switch (type) {
|
||||
case 'domainset':
|
||||
_singBoxContent = surgeDomainsetToSingbox([MARK, ...processRuleSet(content)]);
|
||||
break;
|
||||
case 'ruleset':
|
||||
_singBoxContent = surgeRulesetToSingbox([`DOMAIN,${MARK}`, ...processRuleSet(content)]);
|
||||
break;
|
||||
case 'ipcidr':
|
||||
case 'ipcidr6':
|
||||
_singBoxContent = ipCidrListToSingbox(content);
|
||||
break;
|
||||
default:
|
||||
throw new TypeError(`Unknown type: ${type}`);
|
||||
}
|
||||
return stringify(_singBoxContent).split('\n');
|
||||
});
|
||||
|
||||
await Promise.all([
|
||||
compareAndWriteFile(childSpan, surgeContent, surgePath),
|
||||
compareAndWriteFile(childSpan, clashContent, clashPath),
|
||||
compareAndWriteFile(childSpan, singboxContent, singBoxPath)
|
||||
]);
|
||||
await Promise.all([
|
||||
compareAndWriteFile(childSpan, surgeContent, surgePath),
|
||||
compareAndWriteFile(childSpan, clashContent, clashPath),
|
||||
compareAndWriteFile(childSpan, singboxContent, singBoxPath)
|
||||
]);
|
||||
|
||||
// if (clashMrsPath) {
|
||||
// if (type === 'domainset') {
|
||||
@ -265,4 +266,5 @@ export const createRuleset = (
|
||||
// });
|
||||
// }
|
||||
// }
|
||||
});
|
||||
}
|
||||
);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user