From 981e6d77517771da9510cc79c7e0cd8c7276d119 Mon Sep 17 00:00:00 2001 From: Roy Li Date: Mon, 26 Aug 2019 22:42:27 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E7=94=9F=E6=88=90=20v2rayn=20scheme?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/utils/index.spec.ts | 49 +++++++++++++++++++++++++++++++++++++++++ lib/utils/index.ts | 33 +++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) diff --git a/lib/utils/index.spec.ts b/lib/utils/index.spec.ts index b17ccde75..1532fb7c7 100644 --- a/lib/utils/index.spec.ts +++ b/lib/utils/index.spec.ts @@ -371,3 +371,52 @@ test('getV2rayNSubscription', async t => { uuid: '1386f85e-657b-4d6e-9d56-78badb75e1fd', }); }); + +test('getV2rayNNodes', t => { + const schemeList = utils.getV2rayNNodes([ + { + type: NodeTypeEnum.Vmess, + alterId: '64', + hostname: '1.1.1.1', + method: 'auto', + network: 'ws', + nodeName: '测试 1', + path: '/', + port: 8080, + tls: false, + host: 'example.com', + uuid: '1386f85e-657b-4d6e-9d56-78badb75e1fd', + }, + { + type: NodeTypeEnum.Vmess, + alterId: '64', + hostname: '1.1.1.1', + method: 'auto', + network: 'tcp', + nodeName: '测试 2', + path: '/', + port: 8080, + tls: false, + host: '', + uuid: '1386f85e-657b-4d6e-9d56-78badb75e1fd', + }, + { + type: NodeTypeEnum.Vmess, + alterId: '64', + hostname: '1.1.1.1', + method: 'auto', + network: 'ws', + nodeName: '测试 3', + path: '/', + port: 8080, + tls: false, + host: '', + uuid: '1386f85e-657b-4d6e-9d56-78badb75e1fd', + }, + ]) + .split('\n'); + + t.is(schemeList[0], 'vmess://eyJ2IjoiMiIsInBzIjoi5rWL6K+VIDEiLCJhZGQiOiIxLjEuMS4xIiwicG9ydCI6IjgwODAiLCJpZCI6IjEzODZmODVlLTY1N2ItNGQ2ZS05ZDU2LTc4YmFkYjc1ZTFmZCIsImFpZCI6IjY0IiwibmV0Ijoid3MiLCJ0eXBlIjoibm9uZSIsImhvc3QiOiJleGFtcGxlLmNvbSIsInBhdGgiOiIvIiwidGxzIjoiIn0='); + t.is(schemeList[1], 'vmess://eyJ2IjoiMiIsInBzIjoi5rWL6K+VIDIiLCJhZGQiOiIxLjEuMS4xIiwicG9ydCI6IjgwODAiLCJpZCI6IjEzODZmODVlLTY1N2ItNGQ2ZS05ZDU2LTc4YmFkYjc1ZTFmZCIsImFpZCI6IjY0IiwibmV0IjoidGNwIiwidHlwZSI6Im5vbmUiLCJob3N0IjoiIiwicGF0aCI6Ii8iLCJ0bHMiOiIifQ=='); + t.is(schemeList[2], 'vmess://eyJ2IjoiMiIsInBzIjoi5rWL6K+VIDMiLCJhZGQiOiIxLjEuMS4xIiwicG9ydCI6IjgwODAiLCJpZCI6IjEzODZmODVlLTY1N2ItNGQ2ZS05ZDU2LTc4YmFkYjc1ZTFmZCIsImFpZCI6IjY0IiwibmV0Ijoid3MiLCJ0eXBlIjoibm9uZSIsImhvc3QiOiIiLCJwYXRoIjoiLyIsInRscyI6IiJ9'); +}); diff --git a/lib/utils/index.ts b/lib/utils/index.ts index 95462e09e..53cd484c6 100644 --- a/lib/utils/index.ts +++ b/lib/utils/index.ts @@ -384,6 +384,39 @@ export const getShadowsocksrNodes = (list: ReadonlyArray return result.join('\n'); }; +export const getV2rayNNodes = (list: ReadonlyArray): string => { + const result: ReadonlyArray = list + .map(nodeConfig => { + if (nodeConfig.enable === false) { return null; } + + switch (nodeConfig.type) { + case NodeTypeEnum.Vmess: { + const json = { + v: '2', + ps: nodeConfig.nodeName, + add: nodeConfig.hostname, + port: `${nodeConfig.port}`, + id: nodeConfig.uuid, + aid: nodeConfig.alterId, + net: nodeConfig.network, + type: 'none', + host: nodeConfig.host, + path: nodeConfig.path, + tls: nodeConfig.tls ? 'tls' : '', + }; + + return 'vmess://' + toBase64(JSON.stringify(json)); + } + + default: + return null; + } + }) + .filter(item => !!item); + + return result.join('\n'); +}; + export const getShadowsocksNodesJSON = (list: ReadonlyArray): string => { const nodes: ReadonlyArray = list .map(nodeConfig => {