feat: 支持同步配置时选择包含官方/商店版不支持的协议; 同步配置优化

This commit is contained in:
xream 2024-01-18 06:18:05 +08:00
parent 9bb06bf438
commit 228566116d
No known key found for this signature in database
GPG Key ID: 1D2C5225471789F9
4 changed files with 75 additions and 33 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "sub-store", "name": "sub-store",
"version": "2.14.178", "version": "2.14.179",
"description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and ShadowRocket.", "description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and ShadowRocket.",
"main": "src/main.js", "main": "src/main.js",
"scripts": { "scripts": {

View File

@ -40,7 +40,7 @@ async function doSync() {
platform: artifact.platform, platform: artifact.platform,
}); });
files[artifact.name] = { files[encodeURIComponent(artifact.name)] = {
content: output, content: output,
}; };
} }
@ -54,10 +54,9 @@ async function doSync() {
if (artifact.sync) { if (artifact.sync) {
artifact.updated = new Date().getTime(); artifact.updated = new Date().getTime();
// extract real url from gist // extract real url from gist
artifact.url = body.files[artifact.name].raw_url.replace( artifact.url = body.files[
/\/raw\/[^/]*\/(.*)/, encodeURIComponent(artifact.name)
'/raw/$1', ]?.raw_url.replace(/\/raw\/[^/]*\/(.*)/, '/raw/$1');
);
} }
} }

View File

@ -50,20 +50,32 @@ async function restoreArtifacts(_, res) {
throw new Error(`找不到 Sub-Store Gist 文件列表`); throw new Error(`找不到 Sub-Store Gist 文件列表`);
} }
const allArtifacts = $.read(ARTIFACTS_KEY); const allArtifacts = $.read(ARTIFACTS_KEY);
const failed = [];
Object.keys(gist.files).map((key) => { Object.keys(gist.files).map((key) => {
const filename = gist.files[key]?.filename; const filename = gist.files[key]?.filename;
if (filename) { if (filename) {
const artifact = findByName(allArtifacts, filename); if (encodeURIComponent(filename) !== filename) {
if (artifact) { $.error(`文件名 ${filename} 未编码 不保存`);
updateByName(allArtifacts, filename, { failed.push(filename);
...artifact,
url: gist.files[key]?.raw_url,
});
} else { } else {
allArtifacts.push({ const artifact = findByName(allArtifacts, filename);
name: `${filename}`, if (artifact) {
url: gist.files[key]?.raw_url, updateByName(allArtifacts, filename, {
}); ...artifact,
url: gist.files[key]?.raw_url.replace(
/\/raw\/[^/]*\/(.*)/,
'/raw/$1',
),
});
} else {
allArtifacts.push({
name: `${filename}`,
url: gist.files[key]?.raw_url.replace(
/\/raw\/[^/]*\/(.*)/,
'/raw/$1',
),
});
}
} }
} }
}); });
@ -193,9 +205,15 @@ async function deleteArtifact(req, res) {
if (artifact.updated) { if (artifact.updated) {
// delete gist // delete gist
const files = {}; const files = {};
files[artifact.name] = { files[encodeURIComponent(artifact.name)] = {
content: '', content: '',
}; };
if (encodeURIComponent(artifact.name) !== artifact.name) {
files[artifact.name] = {
content: '',
};
}
// 当别的Sub 删了同步订阅 或 gist里面删了 当前设备没有删除 时 无法删除的bug // 当别的Sub 删了同步订阅 或 gist里面删了 当前设备没有删除 时 无法删除的bug
try { try {
await syncToGist(files); await syncToGist(files);

View File

@ -447,23 +447,44 @@ async function syncArtifacts() {
const files = {}; const files = {};
try { try {
const invalid = [];
await Promise.all( await Promise.all(
allArtifacts.map(async (artifact) => { allArtifacts.map(async (artifact) => {
if (artifact.sync && artifact.source) { try {
$.info(`正在同步云配置:${artifact.name}...`); if (artifact.sync && artifact.source) {
const output = await produceArtifact({ $.info(`正在同步云配置:${artifact.name}...`);
type: artifact.type, const output = await produceArtifact({
name: artifact.source, type: artifact.type,
platform: artifact.platform, name: artifact.source,
}); platform: artifact.platform,
produceOpts: {
'include-unsupported-proxy':
artifact.includeUnsupportedProxy,
},
});
files[artifact.name] = { // if (!output || output.length === 0)
content: output, // throw new Error('该配置的结果为空 不进行上传');
};
files[encodeURIComponent(artifact.name)] = {
content: output,
};
}
} catch (e) {
$.error(
`同步配置 ${artifact.name} 发生错误: ${e.message ?? e}`,
);
invalid.push(artifact.name);
} }
}), }),
); );
if (invalid.length > 0) {
throw new Error(
`同步配置 ${invalid.join(', ')} 发生错误 详情请查看日志`,
);
}
const resp = await syncToGist(files); const resp = await syncToGist(files);
const body = JSON.parse(resp.body); const body = JSON.parse(resp.body);
@ -471,10 +492,9 @@ async function syncArtifacts() {
if (artifact.sync) { if (artifact.sync) {
artifact.updated = new Date().getTime(); artifact.updated = new Date().getTime();
// extract real url from gist // extract real url from gist
artifact.url = body.files[artifact.name].raw_url.replace( artifact.url = body.files[
/\/raw\/[^/]*\/(.*)/, encodeURIComponent(artifact.name)
'/raw/$1', ]?.raw_url.replace(/\/raw\/[^/]*\/(.*)/, '/raw/$1');
);
} }
} }
@ -541,6 +561,9 @@ async function syncArtifact(req, res) {
type: artifact.type, type: artifact.type,
name: artifact.source, name: artifact.source,
platform: artifact.platform, platform: artifact.platform,
produceOpts: {
'include-unsupported-proxy': artifact.includeUnsupportedProxy,
},
}); });
$.info( $.info(
@ -550,6 +573,8 @@ async function syncArtifact(req, res) {
2, 2,
)}`, )}`,
); );
// if (!output || output.length === 0)
// throw new Error('该配置的结果为空 不进行上传');
const resp = await syncToGist({ const resp = await syncToGist({
[encodeURIComponent(artifact.name)]: { [encodeURIComponent(artifact.name)]: {
content: output, content: output,
@ -559,11 +584,11 @@ async function syncArtifact(req, res) {
const body = JSON.parse(resp.body); const body = JSON.parse(resp.body);
artifact.url = body.files[ artifact.url = body.files[
encodeURIComponent(artifact.name) encodeURIComponent(artifact.name)
].raw_url.replace(/\/raw\/[^/]*\/(.*)/, '/raw/$1'); ]?.raw_url.replace(/\/raw\/[^/]*\/(.*)/, '/raw/$1');
$.write(allArtifacts, ARTIFACTS_KEY); $.write(allArtifacts, ARTIFACTS_KEY);
success(res, artifact); success(res, artifact);
} catch (err) { } catch (err) {
$.error(`远程配置 ${artifact.name} 发生错误: ${err}`); $.error(`远程配置 ${artifact.name} 发生错误: ${err.message ?? err}`);
failed( failed(
res, res,
new InternalServerError( new InternalServerError(