Skip to content

Commit

Permalink
feat: 生成 v2rayn scheme
Browse files Browse the repository at this point in the history
  • Loading branch information
geekdada committed Aug 26, 2019
1 parent 05cc557 commit 981e6d7
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
49 changes: 49 additions & 0 deletions lib/utils/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
33 changes: 33 additions & 0 deletions lib/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,39 @@ export const getShadowsocksrNodes = (list: ReadonlyArray<ShadowsocksrNodeConfig>
return result.join('\n');
};

export const getV2rayNNodes = (list: ReadonlyArray<VmessNodeConfig>): string => {
const result: ReadonlyArray<string> = 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<ShadowsocksNodeConfig>): string => {
const nodes: ReadonlyArray<object> = list
.map(nodeConfig => {
Expand Down

0 comments on commit 981e6d7

Please sign in to comment.