feat: Surge/Surfboard 处理不合法的节点名

This commit is contained in:
xream committed 2026-11-16 15:49:33 +08:00
1 parent 7f2de2f33b
commit 2a173b88dc
5 files changed
+53 -7

No files matched your search

+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "sub-store",
"version": "2.44.15",
"version": "2.44.16",
"description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and Shadowrocket.",
"main": "src/main.js",
"packageManager": "pnpm@11.0.9",
+17 -1
View File
@@ -422,6 +422,13 @@ function normalizeResponse(response = {}) {
return normalized;
}
function sanitizeSurgeProxyName(name) {
return name
.replace(/[=,]/g, '')
.replace(/(^|\s)(?:[#;]|\/\/)+/g, '$1')
.trim();
}
function produce(proxies, targetPlatform, type, opts = {}) {
const producer = PROXY_PRODUCERS[targetPlatform];
if (!producer) {
@@ -429,6 +436,9 @@ function produce(proxies, targetPlatform, type, opts = {}) {
}
const normalizedTarget = String(targetPlatform).toLowerCase();
const isSurgeStyleTarget = ['surge', 'surgemac', 'surfboard'].includes(
normalizedTarget,
);
// filter unsupported proxies
proxies = proxies.filter((proxy) => {
@@ -542,6 +552,9 @@ function produce(proxies, targetPlatform, type, opts = {}) {
proxies = proxies.map((proxy) => {
proxy._resolved = proxy.resolved;
if (isSurgeStyleTarget && typeof proxy.name === 'string') {
proxy.name = sanitizeSurgeProxyName(proxy.name);
}
if (!isNotBlank(proxy.name)) {
proxy.name = `${proxy.type} ${proxy.server}:${proxy.port}`;
}
@@ -604,7 +617,10 @@ function produce(proxies, targetPlatform, type, opts = {}) {
list.push(
SurgeMac_Producer().produce(
{
name: opts._merged.name,
name:
sanitizeSurgeProxyName(
String(opts._merged.name ?? ''),
) || 'mihomo merged',
type: 'external',
udp: true,
exec: opts._merged.exec,
@@ -34,7 +34,6 @@ export default function Surfboard_Producer() {
`Platform ${targetPlatform} does not support network ${proxy.network} with http upgrade`,
);
}
proxy.name = proxy.name.replace(/=|,/g, '');
switch (proxy.type) {
case 'ss':
return shadowsocks(proxy);
@@ -174,7 +174,6 @@ export default function Surge_Producer() {
`Platform ${targetPlatform} does not support network ${proxy.network} with http upgrade`,
);
}
proxy.name = proxy.name.replace(/=|,/g, '');
if (proxy.ports) {
proxy.ports = String(proxy.ports);
}
+35 -3
View File
@@ -1123,6 +1123,35 @@ describe('Proxy text producers', function () {
);
});
it('removes comment markers from Surge and Surfboard proxy names', function () {
const names = [
['HK=, #1 /=,/Backup ;VIP', 'HK 1 Backup VIP'],
[' // #JP', 'JP'],
['JP#2//A;B / C', 'JP#2//A;B / C'],
['#//;', 'http example.com:443'],
];
const targets = [
'Surge',
'surge',
'SurgeMac',
'Surfboard',
'surfboard',
];
for (const target of targets) {
for (const [name, expected] of names) {
const output = produceExternal(target, {
type: 'http',
name,
server: 'example.com',
port: 443,
});
expect(output, target).to.equal(
`${expected}=http,example.com,443`,
);
}
}
});
it('emits udp-relay only for applicable Surge policies', function () {
// https://manual.nssurge.com/policies/udp.html
const proxies = [
@@ -2377,7 +2406,7 @@ describe('Proxy text producers', function () {
it('produces SurgeMac external lines', function () {
const output = produceExternal('SurgeMac', {
type: 'external',
name: 'External',
name: 'External #1',
exec: '/usr/bin/ssh',
'local-port': 1080,
args: ['-D', 'localhost:1080'],
@@ -2389,7 +2418,7 @@ describe('Proxy text producers', function () {
});
expect(output).to.equal(
'External=external,exec="/usr/bin/ssh",local-port=1080,args="-D",args="localhost:1080",addresses=1.1.1.1,addresses=2001:db8::1,udp-relay=true,tfo=true,test-url=https://test.example.com,block-quic=on',
'External 1=external,exec="/usr/bin/ssh",local-port=1080,args="-D",args="localhost:1080",addresses=1.1.1.1,addresses=2001:db8::1,udp-relay=true,tfo=true,test-url=https://test.example.com,block-quic=on',
);
});
@@ -2851,7 +2880,10 @@ describe('Proxy text producers', function () {
_config: override,
}));
const output = ProxyUtils.produce(proxies, 'SurgeMac', 'external', {});
const output = ProxyUtils.produce(proxies, 'SurgeMac', 'external', {
mergeName: 'Merged //Proxy',
});
expect(output).to.include('\nMerged Proxy=external,');
const args = [...output.matchAll(/args="([^"]+)"/g)].map((m) => m[1]);
const config = JSON.parse(Base64.decode(args[args.length - 1]));