mirror of
https://github.com/sub-store-org/Sub-Store.git
synced 2026-11-27 06:43:08 +00:00
feat: 优化 Gist 逻辑
This commit is contained in:
5 files changed
+135
-10
No files matched your search
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "sub-store",
|
||||
"version": "2.44.1",
|
||||
"version": "2.44.2",
|
||||
"description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and Shadowrocket.",
|
||||
"main": "src/main.js",
|
||||
"packageManager": "pnpm@11.0.9",
|
||||
|
||||
@@ -266,7 +266,7 @@ async function gistBackupAction(
|
||||
`Gist backup action: ${action}, keep: ${keep}, token strategy: ${queryTokenStrategy}, settings token strategy: ${settings.gistDownloadTokenStrategy}, final token strategy: ${tokenStrategy}, encode: ${encode}, settings encode: ${settings.gistUpload}, final encoding: ${encoding}`,
|
||||
);
|
||||
switch (action) {
|
||||
case 'upload':
|
||||
case 'upload': {
|
||||
let backupContent;
|
||||
try {
|
||||
const keepAgeSecretKey = isAgeGistBackupEncoding(encoding);
|
||||
@@ -323,13 +323,11 @@ async function gistBackupAction(
|
||||
$.info(`上传备份中...`);
|
||||
await $.wait(100);
|
||||
try {
|
||||
await gist.upload(
|
||||
{
|
||||
[GIST_BACKUP_FILE_NAME]: {
|
||||
content: uploadContent,
|
||||
},
|
||||
}
|
||||
);
|
||||
await gist.upload({
|
||||
[GIST_BACKUP_FILE_NAME]: {
|
||||
content: uploadContent,
|
||||
},
|
||||
});
|
||||
|
||||
$.info(`上传备份完成`);
|
||||
} catch (err) {
|
||||
@@ -346,7 +344,6 @@ async function gistBackupAction(
|
||||
await $.wait(500);
|
||||
const downloadedContent = await gist.download(
|
||||
GIST_BACKUP_FILE_NAME,
|
||||
gists,
|
||||
);
|
||||
|
||||
const onlineContent = await decryptGistBackupContent(
|
||||
@@ -382,6 +379,7 @@ async function gistBackupAction(
|
||||
throw err;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 'download': {
|
||||
$.info(`还原备份中...`);
|
||||
content = await gist.download(GIST_BACKUP_FILE_NAME);
|
||||
|
||||
@@ -105,6 +105,11 @@ async function updateSettings(req, res) {
|
||||
...settings,
|
||||
...req.body,
|
||||
};
|
||||
for (const key of ['githubUser', 'gistToken']) {
|
||||
if (typeof newSettings[key] === 'string') {
|
||||
newSettings[key] = newSettings[key].trim();
|
||||
}
|
||||
}
|
||||
if (isPlainObject(req.body?.appearanceSetting)) {
|
||||
newSettings.appearanceSetting = {
|
||||
...(isPlainObject(settings?.appearanceSetting)
|
||||
|
||||
@@ -119,6 +119,74 @@ describe('Gist backup age encryption', function () {
|
||||
expect(backup.settings.syncTime).to.be.a('number');
|
||||
});
|
||||
|
||||
for (const encoding of ['base64', 'plaintext', 'age']) {
|
||||
it(`accepts ${encoding} backups when upload fails but verification matches`, async function () {
|
||||
const pair = await generateKeyPair();
|
||||
let uploadedContent;
|
||||
const downloadedFiles = [];
|
||||
|
||||
installState({
|
||||
settings: {
|
||||
gistToken: 'token',
|
||||
gistUpload: encoding,
|
||||
syncTime: 123,
|
||||
[AGE_SECRET_KEY]: pair[AGE_SECRET_KEY],
|
||||
},
|
||||
subs: [],
|
||||
});
|
||||
|
||||
Gist.prototype.download = async (filename) => {
|
||||
downloadedFiles.push(filename);
|
||||
return uploadedContent ?? 'old backup';
|
||||
};
|
||||
Gist.prototype.upload = async (files) => {
|
||||
uploadedContent = files[GIST_BACKUP_FILE_NAME].content;
|
||||
throw new Error('Upload response lost');
|
||||
};
|
||||
|
||||
await gistBackupAction('upload');
|
||||
|
||||
expect(uploadedContent).to.be.a('string');
|
||||
expect(downloadedFiles).to.deep.equal([
|
||||
GIST_BACKUP_FILE_NAME,
|
||||
GIST_BACKUP_FILE_NAME,
|
||||
]);
|
||||
expect($.cache.settings.syncTime).to.be.greaterThan(123);
|
||||
});
|
||||
}
|
||||
|
||||
it('restores syncTime and rejects the upload error when verification differs', async function () {
|
||||
const uploadError = new Error('Upload failed');
|
||||
let downloadCount = 0;
|
||||
|
||||
installState({
|
||||
settings: {
|
||||
gistToken: 'token',
|
||||
syncTime: 123,
|
||||
},
|
||||
subs: [],
|
||||
});
|
||||
|
||||
Gist.prototype.download = async () => {
|
||||
downloadCount++;
|
||||
return 'old backup';
|
||||
};
|
||||
Gist.prototype.upload = async () => {
|
||||
throw uploadError;
|
||||
};
|
||||
|
||||
let error;
|
||||
try {
|
||||
await gistBackupAction('upload');
|
||||
} catch (caught) {
|
||||
error = caught;
|
||||
}
|
||||
|
||||
expect(error).to.equal(uploadError);
|
||||
expect(downloadCount).to.equal(2);
|
||||
expect($.cache.settings.syncTime).to.equal(123);
|
||||
});
|
||||
|
||||
it('replaces matching unencrypted online backups in age mode', async function () {
|
||||
const pair = await generateKeyPair();
|
||||
const initialContent = {
|
||||
|
||||
@@ -8,6 +8,7 @@ import registerSettingsRoutes, {
|
||||
shouldRefreshArtifactStoreForSettingsPatch,
|
||||
} from '@/restful/settings';
|
||||
import { AGE_SECRET_KEY, generateKeyPair } from '@/utils/age';
|
||||
import Gist from '@/utils/gist';
|
||||
|
||||
function createRouteApp() {
|
||||
const handlers = new Map();
|
||||
@@ -119,10 +120,12 @@ describe('settings routes', function () {
|
||||
describe('validated settings', function () {
|
||||
const originalRead = $.read.bind($);
|
||||
const originalWrite = $.write.bind($);
|
||||
const originalLocate = Gist.prototype.locate;
|
||||
|
||||
afterEach(function () {
|
||||
$.read = originalRead;
|
||||
$.write = originalWrite;
|
||||
Gist.prototype.locate = originalLocate;
|
||||
});
|
||||
|
||||
async function patchSettings(initialSettings, body) {
|
||||
@@ -148,6 +151,57 @@ describe('settings routes', function () {
|
||||
};
|
||||
}
|
||||
|
||||
it('trims GitHub credentials before saving and refreshing the artifact store', async function () {
|
||||
let authorization;
|
||||
Gist.prototype.locate = async function () {
|
||||
authorization = this.headers.Authorization;
|
||||
return { html_url: 'https://gist.example.com/artifacts' };
|
||||
};
|
||||
|
||||
const { res, settings } = await patchSettings(
|
||||
{},
|
||||
{
|
||||
githubUser: ' \t\nexample-user\u3000 ',
|
||||
gistToken: ' \r\nexample-token\t\u00a0',
|
||||
},
|
||||
);
|
||||
|
||||
expect(res.body.status).to.equal('success');
|
||||
expect(settings.githubUser).to.equal('example-user');
|
||||
expect(settings.gistToken).to.equal('example-token');
|
||||
expect(res.body.data.githubUser).to.equal('example-user');
|
||||
expect(res.body.data.gistToken).to.equal('example-token');
|
||||
expect(authorization).to.equal('token example-token');
|
||||
});
|
||||
|
||||
it('saves whitespace-only GitHub credentials as empty without a Gist request', async function () {
|
||||
let locateCalls = 0;
|
||||
Gist.prototype.locate = async () => {
|
||||
locateCalls++;
|
||||
};
|
||||
|
||||
const { res, settings } = await patchSettings(
|
||||
{ githubUser: 'old-user', gistToken: 'old-token' },
|
||||
{ githubUser: ' \t\n\u3000', gistToken: '\r\n \u00a0' },
|
||||
);
|
||||
|
||||
expect(res.body.status).to.equal('success');
|
||||
expect(settings.githubUser).to.equal('');
|
||||
expect(settings.gistToken).to.equal('');
|
||||
expect(locateCalls).to.equal(0);
|
||||
});
|
||||
|
||||
it('does not add omitted GitHub credentials on unrelated patches', async function () {
|
||||
const { res, settings } = await patchSettings(
|
||||
{},
|
||||
{ logsMaxCount: 2 },
|
||||
);
|
||||
|
||||
expect(res.body.status).to.equal('success');
|
||||
expect(settings).not.to.have.property('githubUser');
|
||||
expect(settings).not.to.have.property('gistToken');
|
||||
});
|
||||
|
||||
it('does not validate existing age-secret-key on unrelated patches', async function () {
|
||||
const { res, settings } = await patchSettings(
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user