From 84aff5dc86d620295008212a52364405c9d0dc9c Mon Sep 17 00:00:00 2001 From: Roy Li Date: Tue, 5 Mar 2024 14:19:31 +0800 Subject: [PATCH] refactor: refactor WebSocket options handling in surge and surfboard utils --- src/utils/__tests__/surge.test.ts | 32 ++++++++++++------------------- src/utils/surfboard.ts | 4 +--- src/utils/surge.ts | 22 ++++++++++----------- 3 files changed, 23 insertions(+), 35 deletions(-) diff --git a/src/utils/__tests__/surge.test.ts b/src/utils/__tests__/surge.test.ts index 219fc27d1..ccedf8166 100644 --- a/src/utils/__tests__/surge.test.ts +++ b/src/utils/__tests__/surge.test.ts @@ -64,13 +64,12 @@ test('getSurgeNodes', async (t) => { method: 'auto', network: 'ws', nodeName: '测试 3', - path: '/', port: 8080, tls: false, - host: '', uuid: '1386f85e-657b-4d6e-9d56-78badb75e1fd', - binPath: '/usr/local/bin/v2ray', - localPort: 61101, + wsOpts: { + path: '/', + }, }, { type: NodeTypeEnum.Vmess, @@ -79,13 +78,12 @@ test('getSurgeNodes', async (t) => { method: 'auto', network: 'ws', nodeName: '测试 4', - path: '/', port: 8080, tls: true, - host: '', uuid: '1386f85e-657b-4d6e-9d56-78badb75e1fd', - binPath: '/usr/local/bin/v2ray', - localPort: 61101, + wsOpts: { + path: '/', + }, }, { type: NodeTypeEnum.Vmess, @@ -94,13 +92,9 @@ test('getSurgeNodes', async (t) => { method: 'aes-128-gcm', network: 'tcp', nodeName: '测试 5', - path: '/', port: 8080, tls: false, - host: '', uuid: '1386f85e-657b-4d6e-9d56-78badb75e1fd', - binPath: '/usr/local/bin/v2ray', - localPort: 61101, }, { nodeName: 'Test Node 4', @@ -148,17 +142,16 @@ test('getSurgeNodes', async (t) => { method: 'auto', network: 'ws', nodeName: '测试 6', - path: '/', port: 8080, tls: true, tls13: true, skipCertVerify: true, - host: '', uuid: '1386f85e-657b-4d6e-9d56-78badb75e1fd', - binPath: '/usr/local/bin/v2ray', - localPort: 61101, tfo: true, mptcp: true, + wsOpts: { + path: '/', + }, }, { type: NodeTypeEnum.Vmess, @@ -167,18 +160,17 @@ test('getSurgeNodes', async (t) => { method: 'auto', network: 'ws', nodeName: '测试 7', - path: '/', port: 8080, tls: true, tls13: true, skipCertVerify: true, - host: '', uuid: '1386f85e-657b-4d6e-9d56-78badb75e1fd', - binPath: '/usr/local/bin/v2ray', - localPort: 61101, tfo: true, mptcp: true, underlyingProxy: 'another-proxy', + wsOpts: { + path: '/', + }, }, ] const txt1 = surge.getSurgeNodes(nodeList).split('\n') diff --git a/src/utils/surfboard.ts b/src/utils/surfboard.ts index d800988da..b33a032b3 100644 --- a/src/utils/surfboard.ts +++ b/src/utils/surfboard.ts @@ -141,9 +141,7 @@ function nodeListMapper( if (nodeConfig.network === 'ws') { result.push('ws=true') - if (nodeConfig.wsOpts) { - result.push(`ws-path=${nodeConfig.wsOpts.path}`) - } + result.push(`ws-path=${nodeConfig.wsOpts?.path || '/'}`) result.push( 'ws-headers=' + diff --git a/src/utils/surge.ts b/src/utils/surge.ts index d525ada88..9a02de8e5 100644 --- a/src/utils/surge.ts +++ b/src/utils/surge.ts @@ -318,18 +318,16 @@ function nodeListMapper( if (nodeConfig.network === 'ws') { result.push('ws=true') - if (nodeConfig.wsOpts) { - result.push(`ws-path=${nodeConfig.wsOpts.path}`) - result.push( - 'ws-headers=' + - JSON.stringify( - getSurgeExtendHeaders({ - 'user-agent': OBFS_UA, - ...nodeConfig.wsOpts.headers, - }), - ), - ) - } + result.push(`ws-path=${nodeConfig.wsOpts?.path || '/'}`) + result.push( + 'ws-headers=' + + JSON.stringify( + getSurgeExtendHeaders({ + 'user-agent': OBFS_UA, + ...nodeConfig.wsOpts?.headers, + }), + ), + ) } if (nodeConfig.tls) {