Skip to content

Commit

Permalink
feat: Add port hopping support to Clash and Surge config
Browse files Browse the repository at this point in the history
  • Loading branch information
geekdada committed Sep 8, 2024
1 parent 04390e7 commit 8d867cc
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/utils/__tests__/clash.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -823,12 +823,15 @@ test('getClashNodes', async (t) => {
clashConfig: {
enableTuic: true,
enableShadowTls: false,
clashCore: 'stash',
},
hostname: '1.1.1.1',
port: 443,
token: 'password',
skipCertVerify: true,
alpn: ['h3'],
portHopping: '5000-6000',
portHoppingInterval: 10,
},
]),
[
Expand All @@ -849,6 +852,8 @@ test('getClashNodes', async (t) => {
'skip-cert-verify': true,
udp: true,
alpn: ['h3'],
'hop-interval': 10,
ports: '5000-6000',
},
],
)
Expand Down Expand Up @@ -935,6 +940,8 @@ test('getClashNodes', async (t) => {
password: 'password',
downloadBandwidth: 100,
uploadBandwidth: 100,
portHopping: '5000-6000',
portHoppingInterval: 10,
clashConfig: {
enableHysteria2: true,
clashCore: 'stash',
Expand Down Expand Up @@ -974,6 +981,8 @@ test('getClashNodes', async (t) => {
server: 'example.com',
type: 'hysteria2',
up: 100,
ports: '5000-6000',
'hop-interval': 10,
},
{
down: 100,
Expand Down
4 changes: 3 additions & 1 deletion src/utils/__tests__/surge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,8 @@ test('getSurgeNodes - Tuic', (t) => {
port: 443,
token: 'token',
serverCertFingerprintSha256: 'sha256',
portHopping: '5000-6000',
portHoppingInterval: 10,
},
{
type: NodeTypeEnum.Tuic,
Expand Down Expand Up @@ -548,7 +550,7 @@ test('getSurgeNodes - Tuic', (t) => {
},
]),
[
'测试 Tuic = tuic, example.com, 443, token=token, server-cert-fingerprint-sha256=sha256',
'测试 Tuic = tuic, example.com, 443, token=token, server-cert-fingerprint-sha256=sha256, port-hopping=5000-6000, port-hopping-interval=10',
'测试 Tuic = tuic, example.com, 443, token=token, alpn=h3',
'测试 Tuic = tuic, example.com, 443, token=token, skip-cert-verify=true, sni=sni.example.com, alpn=h3',
'测试 Tuic = tuic-v5, example.com, 443, password=password, uuid=uuid, ecn=true, skip-cert-verify=true, sni=sni.example.com, alpn=h3',
Expand Down
35 changes: 35 additions & 0 deletions src/utils/clash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,17 @@ function nodeListMapper(nodeConfig: PossibleNodeConfigType) {
keyFormat: 'kebabCase',
},
),
...(clashConfig.clashCore === 'stash' && nodeConfig.portHopping
? {
ports: nodeConfig.portHopping,
}
: null),
...(clashConfig.clashCore === 'stash' &&
nodeConfig.portHoppingInterval
? {
'hop-interval': nodeConfig.portHoppingInterval,
}
: null),
...(nodeConfig.alpn ? { alpn: nodeConfig.alpn } : null),
} as const
}
Expand All @@ -426,6 +437,16 @@ function nodeListMapper(nodeConfig: PossibleNodeConfigType) {
keyFormat: 'kebabCase',
},
),
...(clashConfig.clashCore === 'stash' && nodeConfig.portHopping
? {
ports: nodeConfig.portHopping,
}
: null),
...(clashConfig.clashCore === 'stash' && nodeConfig.portHoppingInterval
? {
'hop-interval': nodeConfig.portHoppingInterval,
}
: null),
...(nodeConfig.alpn ? { alpn: nodeConfig.alpn } : null),
} as const

Expand Down Expand Up @@ -454,6 +475,20 @@ function nodeListMapper(nodeConfig: PossibleNodeConfigType) {
keyFormat: 'kebabCase',
},
),
...((clashConfig.clashCore === 'stash' ||
clashConfig.clashCore === 'clash.meta') &&
nodeConfig.portHopping
? {
ports: nodeConfig.portHopping,
}
: null),
...((clashConfig.clashCore === 'stash' ||
clashConfig.clashCore === 'clash.meta') &&
nodeConfig.portHoppingInterval
? {
'hop-interval': nodeConfig.portHoppingInterval,
}
: null),
...(nodeConfig.alpn ? { alpn: nodeConfig.alpn } : null),
} as const

Expand Down
2 changes: 2 additions & 0 deletions src/utils/surge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,8 @@ function appendCommonConfig(
'sni',
'serverCertFingerprintSha256',
'blockQuic',
'portHopping',
'portHoppingInterval',
],
{
keyFormat: 'kebabCase',
Expand Down
2 changes: 2 additions & 0 deletions src/validators/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ export const SimpleNodeConfigValidator = z.object({
.optional(),

// Misc
portHopping: z.string().optional(),
portHoppingInterval: z.number().optional(),
underlyingProxy: z.string().optional(),
testUrl: z.string().optional(),
testTimeout: z.number().optional(),
Expand Down

0 comments on commit 8d867cc

Please sign in to comment.