mirror of
https://github.com/sub-store-org/Sub-Store.git
synced 2026-11-27 23:13:43 +00:00
feat: 支持分享链接按次数消费并统一 token 鉴权处理(前端>=2.17.22)
This commit is contained in:
4 files changed
+115
-22
No files matched your search
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "sub-store",
|
||||
"version": "2.23.29",
|
||||
"version": "2.23.30",
|
||||
"description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and Shadowrocket.",
|
||||
"main": "src/main.js",
|
||||
"packageManager": "pnpm@11.0.9",
|
||||
|
||||
@@ -93,6 +93,8 @@ function restoreArchivedEntry(entry) {
|
||||
mode: snapshot.mode,
|
||||
expiresIn: snapshot.expiresIn,
|
||||
exp: snapshot.exp,
|
||||
count: snapshot.count,
|
||||
usedCount: snapshot.usedCount,
|
||||
});
|
||||
default:
|
||||
throw new RequestInvalidError(
|
||||
|
||||
@@ -10,7 +10,7 @@ import {
|
||||
syncArtifactItem,
|
||||
} from '@/restful/sync';
|
||||
import { gistBackupAction } from '@/restful/miscs';
|
||||
import { TOKENS_KEY, SETTINGS_KEY } from '@/constants';
|
||||
import { SETTINGS_KEY } from '@/constants';
|
||||
import { startArtifactCronJobs } from '@/utils/artifact-cron';
|
||||
|
||||
import registerSubscriptionRoutes from './subscriptions';
|
||||
@@ -29,6 +29,7 @@ import registerMiscRoutes from './miscs';
|
||||
import registerNodeInfoRoutes from './node-info';
|
||||
import registerParserRoutes from './parser';
|
||||
import registerLogRoutes from './logs';
|
||||
import { consumeShareToken } from './token';
|
||||
|
||||
export default function serve() {
|
||||
let port;
|
||||
@@ -74,16 +75,10 @@ export default function serve() {
|
||||
res.status(405).send('Method not allowed');
|
||||
return;
|
||||
}
|
||||
const tokens = $.read(TOKENS_KEY) || [];
|
||||
const token = tokens.find(
|
||||
(t) =>
|
||||
t.token === req.query.token &&
|
||||
(`/share/${t.type}/${t.name}` === pathname ||
|
||||
pathname.startsWith(
|
||||
`/share/${t.type}/${t.name}/`,
|
||||
)) &&
|
||||
(t.exp == null || t.exp > Date.now()),
|
||||
);
|
||||
const token = consumeShareToken({
|
||||
token: req.query.token,
|
||||
pathname,
|
||||
});
|
||||
if (token) {
|
||||
next();
|
||||
return;
|
||||
@@ -401,14 +396,11 @@ export default function serve() {
|
||||
pathRewrite: async (path, req) => {
|
||||
if (req.method.toLowerCase() !== 'get')
|
||||
throw new Error('Method not allowed');
|
||||
const tokens = $.read(TOKENS_KEY) || [];
|
||||
const token = tokens.find(
|
||||
(t) =>
|
||||
t.token === req.query.token &&
|
||||
t.type === req.params.type &&
|
||||
t.name === req.params.name &&
|
||||
(t.exp == null || t.exp > Date.now()),
|
||||
);
|
||||
const token = consumeShareToken({
|
||||
token: req.query.token,
|
||||
type: req.params.type,
|
||||
name: req.params.name,
|
||||
});
|
||||
if (!token) {
|
||||
const settings = $.read(SETTINGS_KEY);
|
||||
if (
|
||||
|
||||
@@ -91,7 +91,7 @@ function normalizeExpirationMode(mode) {
|
||||
if (mode == null || mode === '') {
|
||||
return undefined;
|
||||
}
|
||||
if (mode === 'duration' || mode === 'datetime') {
|
||||
if (mode === 'duration' || mode === 'datetime' || mode === 'count') {
|
||||
return mode;
|
||||
}
|
||||
throw new RequestInvalidError(
|
||||
@@ -171,6 +171,38 @@ function resolveDurationExpiration(options = {}, { required = false } = {}) {
|
||||
};
|
||||
}
|
||||
|
||||
function resolveCountExpiration(options = {}) {
|
||||
const rawCount = options?.count;
|
||||
if (rawCount == null || rawCount === '') {
|
||||
throw new RequestInvalidError(
|
||||
'INVALID_SHARE_COUNT',
|
||||
`Invalid count option: ${rawCount}`,
|
||||
);
|
||||
}
|
||||
|
||||
const count = Number(rawCount);
|
||||
if (!Number.isSafeInteger(count) || count <= 0) {
|
||||
throw new RequestInvalidError(
|
||||
'INVALID_SHARE_COUNT',
|
||||
`Invalid count option: ${rawCount}`,
|
||||
);
|
||||
}
|
||||
|
||||
const rawUsedCount = options?.usedCount ?? 0;
|
||||
const usedCount = Number(rawUsedCount);
|
||||
if (!Number.isSafeInteger(usedCount) || usedCount < 0 || usedCount > count) {
|
||||
throw new RequestInvalidError(
|
||||
'INVALID_SHARE_USED_COUNT',
|
||||
`Invalid usedCount option: ${rawUsedCount}`,
|
||||
);
|
||||
}
|
||||
|
||||
return {
|
||||
count,
|
||||
usedCount,
|
||||
};
|
||||
}
|
||||
|
||||
function createTokenItem(payload, options = {}) {
|
||||
const type = payload?.type;
|
||||
const name = payload?.name;
|
||||
@@ -243,8 +275,11 @@ function createTokenItem(payload, options = {}) {
|
||||
inferLegacyExpirationMode(options);
|
||||
let durationExpiration = null;
|
||||
let exp;
|
||||
let countExpiration = null;
|
||||
if (expirationMode === 'datetime') {
|
||||
exp = resolveExactExpiration(options);
|
||||
} else if (expirationMode === 'count') {
|
||||
countExpiration = resolveCountExpiration(options);
|
||||
} else {
|
||||
durationExpiration = resolveDurationExpiration(options, {
|
||||
required: expirationMode === 'duration',
|
||||
@@ -269,6 +304,8 @@ function createTokenItem(payload, options = {}) {
|
||||
const normalizedMode =
|
||||
expirationMode === 'datetime'
|
||||
? 'datetime'
|
||||
: expirationMode === 'count'
|
||||
? 'count'
|
||||
: durationExpiration
|
||||
? 'duration'
|
||||
: undefined;
|
||||
@@ -276,6 +313,8 @@ function createTokenItem(payload, options = {}) {
|
||||
delete safePayload.mode;
|
||||
delete safePayload.exp;
|
||||
delete safePayload.expiresIn;
|
||||
delete safePayload.count;
|
||||
delete safePayload.usedCount;
|
||||
const tokenData = {
|
||||
...safePayload,
|
||||
token,
|
||||
@@ -283,6 +322,11 @@ function createTokenItem(payload, options = {}) {
|
||||
...(normalizedMode ? { mode: normalizedMode } : {}),
|
||||
...(normalizedMode === 'datetime'
|
||||
? { exp }
|
||||
: normalizedMode === 'count'
|
||||
? {
|
||||
count: countExpiration.count,
|
||||
usedCount: countExpiration.usedCount,
|
||||
}
|
||||
: durationExpiration
|
||||
? {
|
||||
expiresIn: durationExpiration.rawExpiresIn,
|
||||
@@ -314,6 +358,61 @@ function deleteTokenItem(token, type, name) {
|
||||
return match;
|
||||
}
|
||||
|
||||
function matchesShareToken(item, { token, type, name, pathname }) {
|
||||
return (
|
||||
item.token === token &&
|
||||
(type ? item.type === type : true) &&
|
||||
(name ? item.name === name : true) &&
|
||||
(pathname
|
||||
? `/share/${item.type}/${item.name}` === pathname ||
|
||||
pathname.startsWith(`/share/${item.type}/${item.name}/`)
|
||||
: true)
|
||||
);
|
||||
}
|
||||
|
||||
function isShareTokenUsable(item) {
|
||||
if (item.exp != null && item.exp <= Date.now()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (item.mode === 'count') {
|
||||
const count = Number(item.count);
|
||||
const usedCount = item.usedCount == null ? 0 : Number(item.usedCount);
|
||||
return (
|
||||
Number.isSafeInteger(count) &&
|
||||
count > 0 &&
|
||||
Number.isSafeInteger(usedCount) &&
|
||||
usedCount >= 0 &&
|
||||
usedCount < count
|
||||
);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
function consumeShareToken(query) {
|
||||
const allTokens = $.read(TOKENS_KEY) || [];
|
||||
const tokenIndex = allTokens.findIndex(
|
||||
(item) => matchesShareToken(item, query) && isShareTokenUsable(item),
|
||||
);
|
||||
if (tokenIndex < 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const token = allTokens[tokenIndex];
|
||||
if (token.mode !== 'count') {
|
||||
return token;
|
||||
}
|
||||
|
||||
const nextToken = {
|
||||
...token,
|
||||
usedCount: Number(token.usedCount ?? 0) + 1,
|
||||
};
|
||||
allTokens[tokenIndex] = nextToken;
|
||||
$.write(allTokens, TOKENS_KEY);
|
||||
return nextToken;
|
||||
}
|
||||
|
||||
function shouldArchiveDeletion(mode) {
|
||||
if (mode == null || mode === '' || mode === 'permanent') {
|
||||
return false;
|
||||
@@ -327,4 +426,4 @@ function shouldArchiveDeletion(mode) {
|
||||
);
|
||||
}
|
||||
|
||||
export { createTokenItem, deleteTokenItem };
|
||||
export { createTokenItem, deleteTokenItem, consumeShareToken };
|
||||
Reference in new issue
Block a user