feat: Gist 备份恢复增加 Base64 编码方式

This commit is contained in:
xream
2025-07-18 17:12:51 +08:00
parent 217fdae7f1
commit e81245a5bb
3 changed files with 52 additions and 22 deletions

View File

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

View File

@@ -1,3 +1,4 @@
import { Base64 } from 'js-base64';
import _ from 'lodash';
import express from '@/vendor/express';
import $ from '@/core/app';
@@ -427,6 +428,14 @@ export default function serve() {
$.info(`[BACKEND] downloading data from ${data_url}`);
download(data_url)
.then(async (content) => {
try {
content = JSON.parse(Base64.decode(content));
if (Object.keys(content.settings).length === 0) {
throw new Error(
'备份文件应该至少包含 settings 字段',
);
}
} catch (err) {
try {
content = JSON.parse(content);
if (Object.keys(content.settings).length === 0) {
@@ -442,6 +451,7 @@ export default function serve() {
);
throw new Error('Gist 备份文件校验失败, 无法还原');
}
}
if (data_url_post) {
$.info('[BACKEND] executing post-processing script');
eval(data_url_post);

View File

@@ -1,3 +1,4 @@
import { Base64 } from 'js-base64';
import _ from 'lodash';
import $ from '@/core/app';
import { ENV } from '@/vendor/open-api';
@@ -105,7 +106,7 @@ async function refresh(_, res) {
success(res);
}
async function gistBackupAction(action, keep) {
async function gistBackupAction(action, keep, encode) {
// read token
const { gistToken, syncPlatform } = $.read(SETTINGS_KEY);
if (!gistToken) throw new Error('GitHub Token is required for backup!');
@@ -127,8 +128,16 @@ async function gistBackupAction(action, keep) {
content = $.read('#sub-store');
content = content ? JSON.parse(content) : {};
if ($.env.isNode) content = JSON.parse(JSON.stringify($.cache));
content.settings.gistToken = '恢复后请重新设置 GitHub Token';
if (encode === 'base64') {
content = Base64.encode(
JSON.stringify(content, null, ` `),
);
} else {
content.settings.gistToken =
'恢复后请重新设置 GitHub Token';
content = JSON.stringify(content, null, ` `);
}
$.info(`下载备份, 与本地内容对比...`);
const onlineContent = await gist.download(
GIST_BACKUP_FILE_NAME,
@@ -147,8 +156,12 @@ async function gistBackupAction(action, keep) {
content = $.read('#sub-store');
content = content ? JSON.parse(content) : {};
if ($.env.isNode) content = JSON.parse(JSON.stringify($.cache));
if (encode) {
content = Base64.encode(JSON.stringify(content, null, ` `));
} else {
content.settings.gistToken = '恢复后请重新设置 GitHub Token';
content = JSON.stringify(content, null, ` `);
}
$.info(`上传备份中...`);
try {
await gist.upload({
@@ -165,6 +178,12 @@ async function gistBackupAction(action, keep) {
case 'download':
$.info(`还原备份中...`);
content = await gist.download(GIST_BACKUP_FILE_NAME);
try {
content = JSON.parse(Base64.decode(content));
if (Object.keys(content.settings).length === 0) {
throw new Error('备份文件应该至少包含 settings 字段');
}
} catch (err) {
try {
content = JSON.parse(content);
if (Object.keys(content.settings).length === 0) {
@@ -178,6 +197,7 @@ async function gistBackupAction(action, keep) {
);
throw new Error('Gist 备份文件校验失败, 无法还原');
}
}
if (keep) {
$.info(`保留原有设置 ${keep}`);
keep.split(',').forEach((path) => {
@@ -198,7 +218,7 @@ async function gistBackupAction(action, keep) {
}
}
async function gistBackup(req, res) {
const { action, keep } = req.query;
const { action, keep, encode } = req.query;
// read token
const { gistToken } = $.read(SETTINGS_KEY);
if (!gistToken) {
@@ -211,7 +231,7 @@ async function gistBackup(req, res) {
);
} else {
try {
await gistBackupAction(action, keep);
await gistBackupAction(action, keep, encode);
success(res);
} catch (err) {
$.error(