From b013657350cdb2ebf016b302c0eada884d018950 Mon Sep 17 00:00:00 2001 From: ladit Date: Mon, 5 Aug 2024 21:41:42 +0800 Subject: [PATCH] fix: singbox wireguard ipv6 endpoint parsing; singbox test case --- src/utils/__tests__/singbox.test.ts | 52 ++++++++++++++++++++++++----- src/utils/singbox.ts | 26 +++++++-------- 2 files changed, 56 insertions(+), 22 deletions(-) diff --git a/src/utils/__tests__/singbox.test.ts b/src/utils/__tests__/singbox.test.ts index a5d7d785d..82145c1eb 100644 --- a/src/utils/__tests__/singbox.test.ts +++ b/src/utils/__tests__/singbox.test.ts @@ -129,7 +129,7 @@ const nodeList: ReadonlyArray = [ port: 443, method: 'auto', uuid: '1386f85e-657b-4d6e-9d56-78badb75e1fd', - alterId: 0, + alterId: 1, network: 'tcp', }, { @@ -575,6 +575,20 @@ const nodeList: ReadonlyArray = [ presharedKey: 'presharedKey2', reservedBits: [2, 2, 3], }, + { + publicKey: 'publicKey3', + endpoint: '162.159.195.115:7156', + allowedIps: '0.0.0.0/0, ::/0', + presharedKey: 'presharedKey3', + reservedBits: [3, 2, 3], + }, + { + publicKey: 'publicKey4', + endpoint: '[2606:4700:d0:0:8537:1837:8101:92fd]:942', + allowedIps: '0.0.0.0/0, ::/0', + presharedKey: 'presharedKey4', + reservedBits: [4, 2, 3], + }, ], }, ] @@ -675,7 +689,7 @@ const expectedNodes: Record[] = [ server_port: 443, security: 'auto', uuid: '1386f85e-657b-4d6e-9d56-78badb75e1fd', - alter_id: 0, + alter_id: 1, }, { type: 'vmess', @@ -784,7 +798,6 @@ const expectedNodes: Record[] = [ { type: 'vless', tag: 'vless.complex', - security: 'none', uuid: '1386f85e-657b-4d6e-9d56-78badb75e1fd', flow: 'xtls-rprx-vision', tls: { @@ -824,7 +837,6 @@ const expectedNodes: Record[] = [ tag: 'vless.http', server: 'example.com', server_port: 443, - security: 'none', uuid: '1386f85e-657b-4d6e-9d56-78badb75e1fd', flow: 'xtls-rprx-vision', tls: { @@ -844,7 +856,6 @@ const expectedNodes: Record[] = [ tag: 'vless.ws', server: 'example.com', server_port: 443, - security: 'none', uuid: '1386f85e-657b-4d6e-9d56-78badb75e1fd', flow: 'xtls-rprx-vision', tls: { @@ -859,7 +870,6 @@ const expectedNodes: Record[] = [ tag: 'vless.quic', server: 'example.com', server_port: 443, - security: 'none', uuid: '1386f85e-657b-4d6e-9d56-78badb75e1fd', flow: 'xtls-rprx-vision', tls: { @@ -874,7 +884,6 @@ const expectedNodes: Record[] = [ tag: 'vless.grpc', server: 'example.com', server_port: 443, - security: 'none', uuid: '1386f85e-657b-4d6e-9d56-78badb75e1fd', flow: 'xtls-rprx-vision', tls: { @@ -889,7 +898,6 @@ const expectedNodes: Record[] = [ tag: 'vless.httpupgrade', server: 'example.com', server_port: 443, - security: 'none', uuid: '1386f85e-657b-4d6e-9d56-78badb75e1fd', flow: 'xtls-rprx-vision', tls: { @@ -1029,6 +1037,22 @@ const expectedNodes: Record[] = [ allowed_ips: ['0.0.0.0/0', '::/0'], reserved: [2, 2, 3], }, + { + server: '162.159.195.115', + server_port: 7156, + public_key: 'publicKey3', + pre_shared_key: 'presharedKey3', + allowed_ips: ['0.0.0.0/0', '::/0'], + reserved: [3, 2, 3], + }, + { + server: '[2606:4700:d0:0:8537:1837:8101:92fd]', + server_port: 942, + public_key: 'publicKey4', + pre_shared_key: 'presharedKey4', + allowed_ips: ['0.0.0.0/0', '::/0'], + reserved: [4, 2, 3], + }, ], mtu: 1420, }, @@ -1049,3 +1073,15 @@ test('getSingboxNodeNames', async (t) => { ['ss'], ) }) + +test('getSingboxNodes', async (t) => { + t.deepEqual(singbox.getSingboxNodes(nodeList), expectedNodes) + + t.deepEqual( + singbox.getSingboxNodes( + nodeList, + (nodeConfig) => nodeConfig.nodeName === 'ss', + ), + [expectedNodes[0]], + ) +}) diff --git a/src/utils/singbox.ts b/src/utils/singbox.ts index c6bfb2254..d2fba30df 100644 --- a/src/utils/singbox.ts +++ b/src/utils/singbox.ts @@ -12,12 +12,7 @@ import { MultiplexValidator, TlsNodeConfigValidator } from '../validators' import { stringifySip003Options } from './ss' -import { - checkNotNullish, - getHostnameFromHost, - getPortFromHost, - pickAndFormatKeys, -} from './' +import { checkNotNullish, pickAndFormatKeys } from './' const logger = createLogger({ service: 'surgio:utils:singbox' }) @@ -292,14 +287,17 @@ function nodeListMapper(nodeConfig: PossibleNodeConfigType) { node.local_address.push(`${nodeConfig.selfIpV6}/128`) } node.private_key = nodeConfig.privateKey - node.peers = nodeConfig.peers.map((peer) => ({ - server: getHostnameFromHost(peer.endpoint), - server_port: getPortFromHost(peer.endpoint), - public_key: peer.publicKey, - pre_shared_key: peer.presharedKey, - allowed_ips: peer.allowedIps?.split(',').map((ip) => ip.trim()), - reserved: peer.reservedBits, - })) + node.peers = nodeConfig.peers.map((peer) => { + const endpoint = new URL(`http://${peer.endpoint}`) + return { + server: endpoint.hostname, + server_port: Number(endpoint.port), + public_key: peer.publicKey, + pre_shared_key: peer.presharedKey, + allowed_ips: peer.allowedIps?.split(',').map((ip) => ip.trim()), + reserved: peer.reservedBits, + } + }) node.mtu = nodeConfig.mtu break }