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