mirror of
https://github.com/SukkaW/Surge.git
synced 2025-12-12 17:20:35 +08:00
Refactor file output
This commit is contained in:
parent
3e71107c2b
commit
0a6f28dcde
@ -9,9 +9,10 @@ import { output } from './lib/misc';
|
||||
|
||||
const cacheKey = createCacheKey(__filename);
|
||||
|
||||
const url = 'https://raw.githubusercontent.com/felixonmars/dnsmasq-china-list/master/apple.china.conf';
|
||||
export const getAppleCdnDomainsPromise = createMemoizedPromise(() => fsFetchCache.apply(
|
||||
cacheKey('https://raw.githubusercontent.com/felixonmars/dnsmasq-china-list/master/apple.china.conf'),
|
||||
() => parseFelixDnsmasq('https://raw.githubusercontent.com/felixonmars/dnsmasq-china-list/master/apple.china.conf'),
|
||||
cacheKey(url),
|
||||
() => parseFelixDnsmasq(url),
|
||||
{
|
||||
ttl: TTL.THREE_DAYS(),
|
||||
serializer: serializeArray,
|
||||
@ -42,7 +43,7 @@ export const buildAppleCdn = task(require.main === module, __filename)(async (sp
|
||||
new Date(),
|
||||
ruleset,
|
||||
'ruleset',
|
||||
...output('apple_cdn', 'non_ip')
|
||||
output('apple_cdn', 'non_ip')
|
||||
),
|
||||
createRuleset(
|
||||
span,
|
||||
@ -51,7 +52,7 @@ export const buildAppleCdn = task(require.main === module, __filename)(async (sp
|
||||
new Date(),
|
||||
domainset,
|
||||
'domainset',
|
||||
...output('apple_cdn', 'domainset')
|
||||
output('apple_cdn', 'domainset')
|
||||
)
|
||||
]);
|
||||
});
|
||||
|
||||
@ -78,7 +78,7 @@ export const buildCdnDownloadConf = task(require.main === module, __filename)(as
|
||||
new Date(),
|
||||
sortDomains(domainDeduper(cdnDomainsList)),
|
||||
'domainset',
|
||||
...output('cdn', 'domainset')
|
||||
output('cdn', 'domainset')
|
||||
),
|
||||
createRuleset(
|
||||
span,
|
||||
@ -91,7 +91,7 @@ export const buildCdnDownloadConf = task(require.main === module, __filename)(as
|
||||
new Date(),
|
||||
sortDomains(domainDeduper(downloadDomainSet)),
|
||||
'domainset',
|
||||
...output('download', 'domainset')
|
||||
output('download', 'domainset')
|
||||
)
|
||||
]);
|
||||
});
|
||||
|
||||
@ -40,7 +40,7 @@ export const buildChnCidr = task(require.main === module, __filename)(async (spa
|
||||
new Date(),
|
||||
filteredCidr4,
|
||||
'ipcidr',
|
||||
...output('china_ip', 'ip')
|
||||
output('china_ip', 'ip')
|
||||
),
|
||||
createRuleset(
|
||||
span,
|
||||
@ -52,7 +52,7 @@ export const buildChnCidr = task(require.main === module, __filename)(async (spa
|
||||
new Date(),
|
||||
cidr6,
|
||||
'ipcidr6',
|
||||
...output('china_ip_ipv6', 'ip')
|
||||
output('china_ip_ipv6', 'ip')
|
||||
)
|
||||
]);
|
||||
});
|
||||
|
||||
@ -1,12 +1,8 @@
|
||||
import path from 'node:path';
|
||||
import { DOMAINS, PROCESS_NAMES } from '../Source/non_ip/cloudmounter';
|
||||
import { SHARED_DESCRIPTION } from './lib/constants';
|
||||
import { createRuleset } from './lib/create-file';
|
||||
import { task } from './trace';
|
||||
|
||||
const outputSurgeDir = path.resolve(__dirname, '../List');
|
||||
const outputClashDir = path.resolve(__dirname, '../Clash');
|
||||
const outputSingboxDir = path.resolve(__dirname, '../sing-box');
|
||||
import { output } from './lib/misc';
|
||||
|
||||
export const buildCloudMounterRules = task(require.main === module, __filename)(async (span) => {
|
||||
// AND,((SRC-IP,192.168.1.110), (DOMAIN, example.com))
|
||||
@ -32,8 +28,6 @@ export const buildCloudMounterRules = task(require.main === module, __filename)(
|
||||
new Date(),
|
||||
results,
|
||||
'ruleset',
|
||||
path.resolve(outputSurgeDir, 'non_ip', 'cloudmounter.conf'),
|
||||
path.resolve(outputClashDir, 'non_ip', 'cloudmounter.txt'),
|
||||
path.resolve(outputSingboxDir, 'non_ip', 'cloudmounter.json')
|
||||
output('cloudmounter', 'non_ip')
|
||||
);
|
||||
});
|
||||
|
||||
@ -137,9 +137,11 @@ function transformDomainset(parentSpan: Span, sourcePath: string, relativePath:
|
||||
new Date(),
|
||||
deduped,
|
||||
'domainset',
|
||||
path.resolve(outputSurgeDir, relativePath),
|
||||
path.resolve(outputClashDir, `${clashFileBasename}.txt`),
|
||||
path.resolve(outputSingboxDir, `${clashFileBasename}.json`)
|
||||
[
|
||||
path.resolve(outputSurgeDir, relativePath),
|
||||
path.resolve(outputClashDir, `${clashFileBasename}.txt`),
|
||||
path.resolve(outputSingboxDir, `${clashFileBasename}.json`)
|
||||
]
|
||||
);
|
||||
}
|
||||
);
|
||||
@ -175,9 +177,11 @@ async function transformRuleset(parentSpan: Span, sourcePath: string, relativePa
|
||||
new Date(),
|
||||
lines,
|
||||
'ruleset',
|
||||
path.resolve(outputSurgeDir, relativePath),
|
||||
path.resolve(outputClashDir, `${clashFileBasename}.txt`),
|
||||
path.resolve(outputSingboxDir, `${clashFileBasename}.json`)
|
||||
[
|
||||
path.resolve(outputSurgeDir, relativePath),
|
||||
path.resolve(outputClashDir, `${clashFileBasename}.txt`),
|
||||
path.resolve(outputSingboxDir, `${clashFileBasename}.json`)
|
||||
]
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
@ -48,7 +48,7 @@ export const buildDomesticRuleset = task(require.main === module, __filename)(as
|
||||
new Date(),
|
||||
res[0],
|
||||
'ruleset',
|
||||
...output('domestic', 'non_ip')
|
||||
output('domestic', 'non_ip')
|
||||
),
|
||||
createRuleset(
|
||||
span,
|
||||
@ -61,7 +61,7 @@ export const buildDomesticRuleset = task(require.main === module, __filename)(as
|
||||
new Date(),
|
||||
res[1],
|
||||
'ruleset',
|
||||
...output('direct', 'non_ip')
|
||||
output('direct', 'non_ip')
|
||||
),
|
||||
createRuleset(
|
||||
span,
|
||||
@ -74,7 +74,7 @@ export const buildDomesticRuleset = task(require.main === module, __filename)(as
|
||||
new Date(),
|
||||
res[2],
|
||||
'ruleset',
|
||||
...output('lan', 'non_ip')
|
||||
output('lan', 'non_ip')
|
||||
),
|
||||
compareAndWriteFile(
|
||||
span,
|
||||
|
||||
@ -63,6 +63,6 @@ export const buildMicrosoftCdn = task(require.main === module, __filename)(async
|
||||
new Date(),
|
||||
res,
|
||||
'ruleset',
|
||||
...output('microsoft_cdn', 'non_ip')
|
||||
output('microsoft_cdn', 'non_ip')
|
||||
);
|
||||
});
|
||||
|
||||
@ -192,7 +192,7 @@ export const buildRejectDomainSet = task(require.main === module, __filename)(as
|
||||
new Date(),
|
||||
span.traceChildSync('sort reject domainset (base)', () => sortDomains(dudupedDominArray, domainArrayMainDomainMap, domainArraySubdomainMap)),
|
||||
'domainset',
|
||||
...output('reject', 'domainset')
|
||||
output('reject', 'domainset')
|
||||
),
|
||||
createRuleset(
|
||||
span,
|
||||
@ -211,7 +211,7 @@ export const buildRejectDomainSet = task(require.main === module, __filename)(as
|
||||
new Date(),
|
||||
span.traceChildSync('sort reject domainset (extra)', () => sortDomains(dudupedDominArrayExtra, domainArrayMainDomainMap, domainArraySubdomainMap)),
|
||||
'domainset',
|
||||
...output('reject_extra', 'domainset')
|
||||
output('reject_extra', 'domainset')
|
||||
),
|
||||
compareAndWriteFile(
|
||||
span,
|
||||
|
||||
@ -101,6 +101,6 @@ export const buildRejectIPList = task(require.main === module, __filename)(async
|
||||
new Date(),
|
||||
result,
|
||||
'ruleset',
|
||||
...output('reject', 'ip')
|
||||
output('reject', 'ip')
|
||||
);
|
||||
});
|
||||
|
||||
@ -251,6 +251,6 @@ export const buildSpeedtestDomainSet = task(require.main === module, __filename)
|
||||
new Date(),
|
||||
deduped,
|
||||
'domainset',
|
||||
...output('speedtest', 'domainset')
|
||||
output('speedtest', 'domainset')
|
||||
);
|
||||
});
|
||||
|
||||
@ -22,7 +22,7 @@ export const createRulesetForStreamService = (span: Span, fileId: string, title:
|
||||
new Date(),
|
||||
streamServices.flatMap((i) => i.rules),
|
||||
'ruleset',
|
||||
...output(fileId, 'non_ip')
|
||||
output(fileId, 'non_ip')
|
||||
),
|
||||
// IP
|
||||
createRuleset(
|
||||
@ -43,7 +43,7 @@ export const createRulesetForStreamService = (span: Span, fileId: string, title:
|
||||
: []
|
||||
)),
|
||||
'ruleset',
|
||||
...output(fileId, 'ip')
|
||||
output(fileId, 'ip')
|
||||
)
|
||||
]));
|
||||
};
|
||||
|
||||
@ -52,6 +52,6 @@ export const buildTelegramCIDR = task(require.main === module, __filename)(async
|
||||
date,
|
||||
results,
|
||||
'ruleset',
|
||||
...output('telegram', 'ip')
|
||||
output('telegram', 'ip')
|
||||
);
|
||||
});
|
||||
|
||||
@ -17,17 +17,15 @@ const mockDir = path.resolve(__dirname, '../Mock');
|
||||
|
||||
export const downloadMockAssets = task(require.main === module, __filename)((span) => Promise.all(Object.entries(ASSETS_LIST).map(
|
||||
([filename, url]) => span
|
||||
.traceChild(url)
|
||||
.traceAsyncFn(() => fetchWithRetry(url).then(res => {
|
||||
.traceChildAsync(url, () => fetchWithRetry(url).then(res => {
|
||||
const src = path.join(mockDir, filename);
|
||||
if (!res.body) {
|
||||
throw new Error(`Empty body from ${url}`);
|
||||
}
|
||||
|
||||
const writeStream = fs.createWriteStream(src, { encoding: 'utf-8' });
|
||||
return pipeline(
|
||||
Readable.fromWeb(res.body),
|
||||
writeStream
|
||||
fs.createWriteStream(src, 'utf-8')
|
||||
);
|
||||
}))
|
||||
)));
|
||||
|
||||
@ -155,7 +155,12 @@ export const createRuleset = (
|
||||
parentSpan: Span,
|
||||
title: string, description: string[] | readonly string[], date: Date, content: string[],
|
||||
type: 'ruleset' | 'domainset' | 'ipcidr' | 'ipcidr6',
|
||||
surgePath: string, clashPath: string, singBoxPath: string, _clashMrsPath?: string
|
||||
[surgePath, clashPath, singBoxPath, _clashMrsPath]: readonly [
|
||||
surgePath: string,
|
||||
clashPath: string,
|
||||
singBoxPath: string,
|
||||
_clashMrsPath?: string
|
||||
]
|
||||
) => parentSpan.traceChild(`create ruleset: ${path.basename(surgePath, path.extname(surgePath))}`).traceAsyncFn(async (childSpan) => {
|
||||
content = sortRuleSet(content);
|
||||
const surgeContent = childSpan.traceChildSync('process surge ruleset', () => {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user