Skip to content

Commit e0b0062

Browse files
committed
feat: 脚本操作可使用 ProxyUtils.age.encrypt 和 ProxyUtils.age.decrypt 进行 age 加密解密
1 parent 651a837 commit e0b0062

4 files changed

Lines changed: 68 additions & 1 deletion

File tree

backend/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "sub-store",
3-
"version": "2.24.4",
3+
"version": "2.24.5",
44
"description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and Shadowrocket.",
55
"main": "src/main.js",
66
"packageManager": "pnpm@11.0.9",

backend/src/core/proxy-utils/index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import { Buffer } from 'buffer';
33
import rs from '@/utils/rs';
44
import YAML from '@/utils/yaml';
55
import download, { downloadFile } from '@/utils/download';
6+
import {
7+
decryptArmorIfPresent,
8+
encryptArmor,
9+
} from '@/utils/age';
610
import {
711
isIPv4,
812
isIPv6,
@@ -37,6 +41,11 @@ import JSON5 from 'json5';
3741
import { hex_md5 } from '@/vendor/md5';
3842
import SurgeMac_Producer from './producers/surgemac';
3943

44+
const ageUtils = Object.freeze({
45+
encrypt: encryptArmor,
46+
decrypt: decryptArmorIfPresent,
47+
});
48+
4049
function preprocess(raw) {
4150
for (const processor of PROXY_PREPROCESSORS) {
4251
try {
@@ -644,6 +653,7 @@ export const ProxyUtils = {
644653
process: processFn,
645654
processResponse: processResponseFn,
646655
produce,
656+
age: ageUtils,
647657
ipAddress,
648658
getRandomPort,
649659
isIPv4,

backend/src/test/proxy-processors/process-context.spec.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { expect } from 'chai';
22
import { describe, it } from 'mocha';
33

44
import { ProxyUtils } from '@/core/proxy-utils';
5+
import { generateKeyPair } from '@/utils/age';
56

67
describe('Process context control', function () {
78
it('disables later actions by customName from shared context', async function () {
@@ -189,4 +190,42 @@ describe('Process context control', function () {
189190

190191
expect(output.body).to.equal('AC');
191192
});
193+
194+
it('exposes age encrypt/decrypt helpers in script operators', async function () {
195+
this.timeout(10000);
196+
197+
const pair = await generateKeyPair();
198+
const operators = [
199+
{
200+
type: 'Script Operator',
201+
args: {
202+
mode: 'script',
203+
arguments: {
204+
publicKey: pair['age-public-key'],
205+
secretKey: pair['age-secret-key'],
206+
},
207+
content: `async function operator(proxies) {
208+
const encrypted = await ProxyUtils.age.encrypt(
209+
'hello age',
210+
$arguments.publicKey,
211+
);
212+
const decrypted = await ProxyUtils.age.decrypt(
213+
encrypted,
214+
$arguments.secretKey,
215+
);
216+
return proxies.map((proxy) => ({
217+
...proxy,
218+
name: proxy.name + '-' + decrypted,
219+
}));
220+
}`,
221+
},
222+
},
223+
];
224+
225+
const output = await ProxyUtils.process([{ name: 'A' }], operators);
226+
227+
expect(output.map((proxy) => proxy.name)).to.deep.equal([
228+
'A-hello age',
229+
]);
230+
});
192231
});

scripts/demo.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,10 @@ function operator(proxies = [], targetPlatform, context) {
205205
// Gist, // Gist 类
206206
// download, // 内部的下载方法, 见 backend/src/utils/download.js
207207
// downloadFile, // 下载二进制文件, 见 backend/src/utils/download.js
208+
// age: {
209+
// encrypt, // age 加密, 返回 armored 文本
210+
// decrypt, // age 解密, 非 armored 文本会原样返回
211+
// },
208212
// MMDB, // Node.js 环境 可用于模拟 Surge/Loon 的 $utils.ipasn, $utils.ipaso, $utils.geoip. 具体见 https://t.me/zhetengsha/1269
209213
// isValidUUID, // 辅助判断是否为有效的 UUID
210214
// doh, // DNS over HTTPS 解析, 源码见 backend/src/utils/dns.js, 使用参考本项目里调用方式 backend/src/core/proxy-utils/processors/index.js
@@ -241,6 +245,20 @@ function operator(proxies = [], targetPlatform, context) {
241245
// }))
242246
// $server.reuse = config.reuse
243247

248+
// 示例: 使用 ProxyUtils.age.encrypt 加密文本
249+
// const armored = await ProxyUtils.age.encrypt(
250+
// 'hello age',
251+
// 'age1...'
252+
// )
253+
// console.log(armored)
254+
255+
// 示例: 使用 ProxyUtils.age.decrypt 解密 armored 文本
256+
// const plaintext = await ProxyUtils.age.decrypt(
257+
// armored,
258+
// 'AGE-SECRET-KEY-1...'
259+
// )
260+
// console.log(plaintext)
261+
244262
// 1. Surge 输出 WireGuard 完整配置
245263

246264
// let proxies = await produceArtifact({

0 commit comments

Comments
 (0)