diff --git a/backend/package.json b/backend/package.json index 8b4a3fe8..e95b1dc6 100644 --- a/backend/package.json +++ b/backend/package.json @@ -1,6 +1,6 @@ { "name": "sub-store", - "version": "2.26.0", + "version": "2.27.0", "description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and Shadowrocket.", "main": "src/main.js", "packageManager": "pnpm@11.0.9", diff --git a/backend/src/restful/archives.js b/backend/src/restful/archives.js index 1287c981..d9e2c562 100644 --- a/backend/src/restful/archives.js +++ b/backend/src/restful/archives.js @@ -92,6 +92,8 @@ function restoreArchivedEntry(entry) { return createTokenItem(snapshot, { mode: snapshot.mode, expiresIn: snapshot.expiresIn, + expiresValue: snapshot.expiresValue, + expiresUnit: snapshot.expiresUnit, exp: snapshot.exp, count: snapshot.count, usedCount: snapshot.usedCount, diff --git a/backend/src/restful/token.js b/backend/src/restful/token.js index c49b7043..33ee4ca7 100644 --- a/backend/src/restful/token.js +++ b/backend/src/restful/token.js @@ -168,10 +168,63 @@ function resolveDurationExpiration(options = {}, { required = false } = {}) { return { rawExpiresIn, + expiresIn, exp: Date.now() + expiresIn, }; } +const DURATION_INPUT_UNIT_MS = { + day: 24 * 60 * 60 * 1000, + month: 30 * 24 * 60 * 60 * 1000, + season: 90 * 24 * 60 * 60 * 1000, + year: 365 * 24 * 60 * 60 * 1000, +}; + +function resolveDurationInput(options = {}, durationExpiration) { + const rawExpiresValue = options?.expiresValue; + const rawExpiresUnit = options?.expiresUnit; + const hasExpiresValue = rawExpiresValue != null && rawExpiresValue !== ''; + const hasExpiresUnit = rawExpiresUnit != null && rawExpiresUnit !== ''; + if (!hasExpiresValue && !hasExpiresUnit) { + return null; + } + + const expiresValue = Number(rawExpiresValue); + if (!Number.isFinite(expiresValue) || expiresValue <= 0) { + throw new RequestInvalidError( + 'INVALID_EXPIRES_VALUE', + `Invalid expiresValue option: ${rawExpiresValue}`, + ); + } + + const expiresUnit = + typeof rawExpiresUnit === 'string' ? rawExpiresUnit.trim() : ''; + if (!['day', 'month', 'season', 'year'].includes(expiresUnit)) { + throw new RequestInvalidError( + 'INVALID_EXPIRES_UNIT', + `Invalid expiresUnit option: ${rawExpiresUnit}`, + ); + } + + const normalizedExpiresValue = Number(expiresValue.toFixed(2)); + const expectedExpiresIn = + normalizedExpiresValue * DURATION_INPUT_UNIT_MS[expiresUnit]; + if ( + durationExpiration && + Math.abs(durationExpiration.expiresIn - expectedExpiresIn) > 1 + ) { + throw new RequestInvalidError( + 'INVALID_DURATION_INPUT', + `expiresValue/expiresUnit do not match expiresIn option: ${rawExpiresValue}${expiresUnit} != ${options?.expiresIn}`, + ); + } + + return { + expiresValue: String(normalizedExpiresValue), + expiresUnit, + }; +} + function resolveCountExpiration(options = {}) { const rawCount = options?.count; if (rawCount == null || rawCount === '') { @@ -275,6 +328,7 @@ function createTokenItem(payload, options = {}) { normalizeExpirationMode(options?.mode) ?? inferLegacyExpirationMode(options); let durationExpiration = null; + let durationInput = null; let exp; let countExpiration = null; if (expirationMode === 'datetime') { @@ -285,6 +339,9 @@ function createTokenItem(payload, options = {}) { durationExpiration = resolveDurationExpiration(options, { required: expirationMode === 'duration', }); + if (durationExpiration) { + durationInput = resolveDurationInput(options, durationExpiration); + } exp = durationExpiration?.exp; } @@ -314,6 +371,8 @@ function createTokenItem(payload, options = {}) { delete safePayload.mode; delete safePayload.exp; delete safePayload.expiresIn; + delete safePayload.expiresValue; + delete safePayload.expiresUnit; delete safePayload.count; delete safePayload.usedCount; normalizeAgePublicKeyConfig(safePayload); @@ -332,6 +391,7 @@ function createTokenItem(payload, options = {}) { : durationExpiration ? { expiresIn: durationExpiration.rawExpiresIn, + ...(durationInput || {}), exp, } : {}), diff --git a/backend/src/test/restful/archive.spec.js b/backend/src/test/restful/archive.spec.js index 22c55c6f..c4192ce8 100644 --- a/backend/src/test/restful/archive.spec.js +++ b/backend/src/test/restful/archive.spec.js @@ -378,6 +378,76 @@ describe('archive routes', function () { expect(state[ARCHIVES_KEY]).to.deep.equal([]); }); + it('preserves duration share input metadata on restore', async function () { + state[SUBS_KEY] = [ + { + name: 'shared-sub', + source: 'remote', + process: [], + }, + ]; + state[TOKENS_KEY] = [ + { + token: 'duration-input', + type: 'sub', + name: 'shared-sub', + displayName: 'Duration Input Share', + mode: 'duration', + expiresIn: '35d', + expiresValue: '35', + expiresUnit: 'day', + }, + ]; + + const deleteHandler = getHandler( + registerTokenRoutes, + 'DELETE', + '/api/token/:token', + ); + const deleteRes = createResponse('/api/token/:token'); + + deleteHandler( + { + params: { token: 'duration-input' }, + query: { + mode: 'archive', + type: 'sub', + name: 'shared-sub', + }, + }, + deleteRes, + ); + + expect(deleteRes.body.status).to.equal('success'); + expect(state[TOKENS_KEY]).to.deep.equal([]); + expect(state[ARCHIVES_KEY]).to.have.length(1); + + const restoreHandler = getHandler( + registerArchiveRoutes, + 'POST', + '/api/archives/:id/restore', + ); + const restoreRes = createResponse('/api/archives/:id/restore'); + + restoreHandler( + { + params: { id: state[ARCHIVES_KEY][0].id }, + query: {}, + }, + restoreRes, + ); + + expect(restoreRes.body.status).to.equal('success'); + expect(state[TOKENS_KEY]).to.have.length(1); + expect(state[TOKENS_KEY][0].token).to.equal('duration-input'); + expect(state[TOKENS_KEY][0].mode).to.equal('duration'); + expect(state[TOKENS_KEY][0].expiresIn).to.equal('35d'); + expect(state[TOKENS_KEY][0].expiresValue).to.equal('35'); + expect(state[TOKENS_KEY][0].expiresUnit).to.equal('day'); + expect(state[TOKENS_KEY][0].exp).to.be.a('number'); + expect(state[ARCHIVES_KEY]).to.deep.equal([]); + }); + it('restores legacy exp-only share tokens as exact-datetime shares', async function () { state[SUBS_KEY] = [ { diff --git a/backend/src/test/restful/token.spec.js b/backend/src/test/restful/token.spec.js index 3dd83461..3aa0a92d 100644 --- a/backend/src/test/restful/token.spec.js +++ b/backend/src/test/restful/token.spec.js @@ -234,6 +234,100 @@ describe('token routes', function () { expect(token.exp).to.be.a('number'); }); + it('stores original duration input for frontend restoration', function () { + const token = createTokenItem( + { + type: 'sub', + name: 'shared-sub', + token: 'duration-input', + }, + { + mode: 'duration', + expiresIn: '35d', + expiresValue: '35', + expiresUnit: 'day', + }, + ); + + expect(token.mode).to.equal('duration'); + expect(token.expiresIn).to.equal('35d'); + expect(token.expiresValue).to.equal('35'); + expect(token.expiresUnit).to.equal('day'); + expect(state[TOKENS_KEY][0].expiresValue).to.equal('35'); + expect(state[TOKENS_KEY][0].expiresUnit).to.equal('day'); + }); + + it('accepts season duration input when it matches expiresIn', function () { + const token = createTokenItem( + { + type: 'sub', + name: 'shared-sub', + token: 'duration-season', + }, + { + mode: 'duration', + expiresIn: '90d', + expiresValue: '1', + expiresUnit: 'season', + }, + ); + + expect(token.mode).to.equal('duration'); + expect(token.expiresIn).to.equal('90d'); + expect(token.expiresValue).to.equal('1'); + expect(token.expiresUnit).to.equal('season'); + expect(state[TOKENS_KEY][0].expiresValue).to.equal('1'); + expect(state[TOKENS_KEY][0].expiresUnit).to.equal('season'); + }); + + it('rejects invalid duration input metadata', function () { + for (const options of [ + { + mode: 'duration', + expiresIn: '35d', + expiresValue: '0', + expiresUnit: 'day', + code: 'INVALID_EXPIRES_VALUE', + }, + { + mode: 'duration', + expiresIn: '35d', + expiresValue: '35', + expiresUnit: 'week', + code: 'INVALID_EXPIRES_UNIT', + }, + { + mode: 'duration', + expiresIn: '1d', + expiresValue: '2', + expiresUnit: 'year', + code: 'INVALID_DURATION_INPUT', + }, + ]) { + let capturedError; + + try { + createTokenItem( + { + type: 'sub', + name: 'shared-sub', + token: `duration-input-${options.code}`, + }, + options, + ); + } catch (error) { + capturedError = error; + } + + expect(capturedError).to.include({ + type: 'RequestInvalidError', + code: options.code, + }); + } + + expect(state[TOKENS_KEY]).to.deep.equal([]); + }); + it('infers legacy exact datetime behavior when mode is omitted but exp exists', function () { const exp = Date.now() - 5 * 60 * 1000; @@ -264,6 +358,8 @@ describe('token routes', function () { mode: 'datetime', exp: Date.now() + 60 * 1000, expiresIn: '1d', + expiresValue: '35', + expiresUnit: 'day', }, {}, ); @@ -271,9 +367,13 @@ describe('token routes', function () { expect(token).to.not.have.property('mode'); expect(token).to.not.have.property('exp'); expect(token).to.not.have.property('expiresIn'); + expect(token).to.not.have.property('expiresValue'); + expect(token).to.not.have.property('expiresUnit'); expect(state[TOKENS_KEY][0]).to.not.have.property('mode'); expect(state[TOKENS_KEY][0]).to.not.have.property('exp'); expect(state[TOKENS_KEY][0]).to.not.have.property('expiresIn'); + expect(state[TOKENS_KEY][0]).to.not.have.property('expiresValue'); + expect(state[TOKENS_KEY][0]).to.not.have.property('expiresUnit'); }); it('preserves age-public-key in share token payload', async function () {