mirror of
https://github.com/sub-store-org/Sub-Store.git
synced 2026-11-27 06:43:08 +00:00
feat: SCP 环境支持分享功能 (#672)
SCP 默认显示分享功能。脚本环境优先使用安全随机数签发 token,生成失败时回退到 nanoid/non-secure。版本更新至 2.44.15。
This commit is contained in:
1 parent
2e7a63c6f4
commit
7f2de2f33b
6 files changed
+115
-9
No files matched your search
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "sub-store",
|
||||
"version": "2.44.14",
|
||||
"version": "2.44.15",
|
||||
"description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and Shadowrocket.",
|
||||
"main": "src/main.js",
|
||||
"packageManager": "pnpm@11.0.9",
|
||||
|
||||
@@ -15,8 +15,12 @@ import {
|
||||
applyAgeOutputEncryption,
|
||||
resolveShareAgeConfig,
|
||||
} from '@/restful/age-output';
|
||||
import { findShareToken } from '@/restful/token';
|
||||
import {
|
||||
consumeShareTokenForRequest,
|
||||
findShareToken,
|
||||
} from '@/restful/token';
|
||||
import { maskAgeSecretInUrl } from '@/utils/age';
|
||||
import { ENV } from '@/vendor/open-api';
|
||||
|
||||
function getMihomoExternalOptions(query) {
|
||||
const useMihomoExternal = query.target === 'SurgeMac';
|
||||
@@ -86,6 +90,22 @@ async function downloadSubscription(req, res) {
|
||||
const { name } = req.params;
|
||||
const isShareRoute = req.path?.startsWith('/share/');
|
||||
|
||||
if (
|
||||
ENV().isSubsCheckPro &&
|
||||
isShareRoute &&
|
||||
!consumeShareTokenForRequest(req, 'sub')
|
||||
) {
|
||||
failed(
|
||||
res,
|
||||
new RequestInvalidError(
|
||||
'INVALID_SHARE_TOKEN',
|
||||
'分享令牌错误、已过期或已用尽',
|
||||
),
|
||||
404,
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
const {
|
||||
useMihomoExternal,
|
||||
mihomoMerge,
|
||||
@@ -492,6 +512,22 @@ async function downloadSubscription(req, res) {
|
||||
async function downloadCollection(req, res) {
|
||||
const { name } = req.params;
|
||||
|
||||
if (
|
||||
ENV().isSubsCheckPro &&
|
||||
req.path?.startsWith('/share/') &&
|
||||
!consumeShareTokenForRequest(req, 'col')
|
||||
) {
|
||||
failed(
|
||||
res,
|
||||
new RequestInvalidError(
|
||||
'INVALID_SHARE_TOKEN',
|
||||
'分享令牌错误、已过期或已用尽',
|
||||
),
|
||||
404,
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
const {
|
||||
useMihomoExternal,
|
||||
mihomoMerge,
|
||||
|
||||
@@ -22,9 +22,13 @@ import {
|
||||
applyAgeOutputEncryption,
|
||||
resolveShareAgeConfig,
|
||||
} from '@/restful/age-output';
|
||||
import { findShareToken } from '@/restful/token';
|
||||
import {
|
||||
consumeShareTokenForRequest,
|
||||
findShareToken,
|
||||
} from '@/restful/token';
|
||||
import { maskAgeSecretInUrl, normalizeAgePublicKeyConfig } from '@/utils/age';
|
||||
import { normalizeEditorLanguageConfig } from '@/utils/editor-language';
|
||||
import { ENV } from '@/vendor/open-api';
|
||||
import {
|
||||
isMihomoConfigFile,
|
||||
normalizeFileConfig,
|
||||
@@ -61,6 +65,22 @@ async function getFile(req, res, next) {
|
||||
let { name } = req.params;
|
||||
const reqUA = req.headers['user-agent'] || req.headers['User-Agent'];
|
||||
const isShareRoute = req.path?.startsWith('/share/');
|
||||
|
||||
if (
|
||||
ENV().isSubsCheckPro &&
|
||||
isShareRoute &&
|
||||
!consumeShareTokenForRequest(req, 'file')
|
||||
) {
|
||||
failed(
|
||||
res,
|
||||
new RequestInvalidError(
|
||||
'INVALID_SHARE_TOKEN',
|
||||
'分享令牌错误、已过期或已用尽',
|
||||
),
|
||||
404,
|
||||
);
|
||||
return;
|
||||
}
|
||||
$.info(`正在下载文件:${name}\n请求 User-Agent: ${reqUA}`);
|
||||
let {
|
||||
url,
|
||||
|
||||
@@ -125,7 +125,7 @@ export default function register($app) {
|
||||
function getEnv(req, res) {
|
||||
env.feature = env.feature || {};
|
||||
env.feature.archive = true;
|
||||
if (req.query.share) {
|
||||
if (ENV().isSubsCheckPro || req.query.share) {
|
||||
env.feature.share = true;
|
||||
}
|
||||
res.set('Content-Type', 'application/json;charset=UTF-8').send(
|
||||
|
||||
@@ -11,6 +11,9 @@ import { insertByPosition } from '@/utils/database';
|
||||
import { getCreateItemPosition } from '@/utils/create-item-position';
|
||||
import { archiveShare } from '@/utils/archive';
|
||||
import { normalizeAgePublicKeyConfig } from '@/utils/age';
|
||||
import ms from 'ms';
|
||||
import { customAlphabet, urlAlphabet } from 'nanoid';
|
||||
import { nanoid as nonSecureNanoid } from 'nanoid/non-secure';
|
||||
|
||||
export default function register($app) {
|
||||
if (!$.read(TOKENS_KEY)) $.write([], TOKENS_KEY);
|
||||
@@ -22,6 +25,21 @@ export default function register($app) {
|
||||
$app.route('/api/tokens').get(getAllTokens);
|
||||
}
|
||||
|
||||
function generateShareToken() {
|
||||
if (ENV().isNode) {
|
||||
// The Node bundle inherits nanoid's browser entry from the first build pass.
|
||||
const nanoid = eval(`require("nanoid")`);
|
||||
return nanoid.customAlphabet(nanoid.urlAlphabet)();
|
||||
}
|
||||
|
||||
try {
|
||||
return customAlphabet(urlAlphabet)();
|
||||
} catch {
|
||||
// shortcut: Math.random tokens can be predictable, remove when script hosts have working Web Crypto.
|
||||
return nonSecureNanoid();
|
||||
}
|
||||
}
|
||||
|
||||
function deleteToken(req, res) {
|
||||
try {
|
||||
let { token } = req.params;
|
||||
@@ -59,7 +77,7 @@ function getAllTokens(req, res) {
|
||||
}
|
||||
|
||||
async function signToken(req, res) {
|
||||
if (!ENV().isNode) {
|
||||
if (!ENV().isNode && !ENV().isSubsCheckPro) {
|
||||
return failed(
|
||||
res,
|
||||
new RequestInvalidError(
|
||||
@@ -157,7 +175,6 @@ function resolveDurationExpiration(options = {}, { required = false } = {}) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const ms = eval(`require("ms")`);
|
||||
const expiresIn = ms(rawExpiresIn);
|
||||
if (expiresIn == null || isNaN(expiresIn) || expiresIn <= 0) {
|
||||
throw new RequestInvalidError(
|
||||
@@ -372,11 +389,10 @@ function createTokenItem(payload, options = {}) {
|
||||
exp = durationExpiration?.exp;
|
||||
}
|
||||
|
||||
const nanoid = eval(`require("nanoid")`);
|
||||
const tokens = $.read(TOKENS_KEY) || [];
|
||||
if (!token) {
|
||||
do {
|
||||
token = nanoid.customAlphabet(nanoid.urlAlphabet)();
|
||||
token = generateShareToken();
|
||||
} while (
|
||||
tokens.find(
|
||||
(item) =>
|
||||
@@ -504,6 +520,28 @@ function consumeShareToken(query) {
|
||||
return nextToken;
|
||||
}
|
||||
|
||||
function consumeShareTokenForRequest(req, type) {
|
||||
const query = {
|
||||
token: req.query?.token,
|
||||
type,
|
||||
name: req.params?.name,
|
||||
};
|
||||
if (
|
||||
req.subStoreShareToken &&
|
||||
req.subStoreShareToken.token === query.token &&
|
||||
req.subStoreShareToken.type === type &&
|
||||
req.subStoreShareToken.name === query.name
|
||||
) {
|
||||
return req.subStoreShareToken;
|
||||
}
|
||||
|
||||
const token = consumeShareToken(query);
|
||||
if (token) {
|
||||
req.subStoreShareToken = token;
|
||||
}
|
||||
return token;
|
||||
}
|
||||
|
||||
function findShareToken(query) {
|
||||
const allTokens = $.read(TOKENS_KEY) || [];
|
||||
return (
|
||||
@@ -526,4 +564,10 @@ function shouldArchiveDeletion(mode) {
|
||||
);
|
||||
}
|
||||
|
||||
export { createTokenItem, deleteTokenItem, consumeShareToken, findShareToken };
|
||||
export {
|
||||
createTokenItem,
|
||||
deleteTokenItem,
|
||||
consumeShareToken,
|
||||
consumeShareTokenForRequest,
|
||||
findShareToken,
|
||||
};
|
||||
Vendored
+6
@@ -14,6 +14,11 @@ const isShadowRocket = 'undefined' !== typeof $rocket;
|
||||
|
||||
const isLanceX = 'undefined' != typeof $native;
|
||||
const isGUIforCores = typeof $Plugins !== 'undefined';
|
||||
const isSubsCheckPro =
|
||||
isLoon &&
|
||||
typeof $loon === 'object' &&
|
||||
$loon !== null &&
|
||||
$loon.isSubsCheckPro === true;
|
||||
import { Base64 } from 'js-base64';
|
||||
|
||||
function isPlainObject(obj) {
|
||||
@@ -405,6 +410,7 @@ export function ENV() {
|
||||
isEgern,
|
||||
isLanceX,
|
||||
isGUIforCores,
|
||||
isSubsCheckPro,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user