Skip to content

Commit

Permalink
fix: should now be able to get trojan config from clash subscriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
geekdada committed Mar 23, 2020
1 parent 2584046 commit 62688ec
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 12 deletions.
25 changes: 15 additions & 10 deletions lib/provider/ClashProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,11 @@ export const getClashSubscription = async (
}

if (
typeof clashConfig !== 'object' ||
!('Proxy' in clashConfig) ||
!('proxies' in clashConfig)
!_.isPlainObject(clashConfig) ||
(
!('Proxy' in clashConfig) &&
!('proxies' in clashConfig)
)
) {
throw new Error(`${url} 订阅内容有误,请检查后重试`);
}
Expand All @@ -134,7 +136,15 @@ export const getClashSubscription = async (
throw new Error(`${url} 订阅内容有误,请检查后重试`);
}

const nodeList = proxyList.map<SupportConfigTypes>(item => {
return {
nodeList: parseClashConfig(proxyList, udpRelay),
subscriptionUserinfo: response.subscriptionUserinfo,
};
};

export const parseClashConfig = (proxyList: ReadonlyArray<any>, udpRelay?: boolean): ReadonlyArray<SupportConfigTypes> => {
return proxyList
.map<SupportConfigTypes>(item => {
switch (item.type) {
case 'ss': {
// istanbul ignore next
Expand Down Expand Up @@ -239,7 +249,7 @@ export const getClashSubscription = async (
port: item.port,
psk: item.psk,
obfs: _.get(item, 'obfs-opts.mode', 'http'),
...('host' in item?.['obfs-opts'] ? { 'obfs-host': item['obfs-opts'].host } : null),
...(typeof item?.['obfs-opts']?.host !== 'undefined' ? { 'obfs-host': item['obfs-opts'].host } : null),
...('version' in item ? { version: item.version } : null),
} as SnellNodeConfig;

Expand Down Expand Up @@ -277,11 +287,6 @@ export const getClashSubscription = async (
}
})
.filter(item => !!item);

return {
nodeList,
subscriptionUserinfo: response.subscriptionUserinfo,
};
};

function resolveUdpRelay(val?: boolean, defaultVal = false): boolean {
Expand Down
107 changes: 105 additions & 2 deletions lib/provider/__tests__/ClashProvider.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import test from 'ava';
import ClashProvider, { getClashSubscription } from '../ClashProvider';
import nock from 'nock';

import ClashProvider, { getClashSubscription, parseClashConfig } from '../ClashProvider';
import { NodeTypeEnum, SupportProviderEnum } from '../../types';

test('ClashProvider', async t => {
Expand All @@ -13,6 +15,21 @@ test('ClashProvider', async t => {
});
});

test('ClashProvider new format', async t => {
const scope = nock('http://local')
.get('/success-1')
.reply(200, `
proxies: []
`);

const provider = new ClashProvider('test', {
type: SupportProviderEnum.Clash,
url: 'http://local/success-1',
});

t.deepEqual(await provider.getNodeList(), []);
});

test('ClashProvider.getSubscriptionUserInfo', async t => {
const provider = new ClashProvider('test', {
type: SupportProviderEnum.Clash,
Expand Down Expand Up @@ -199,7 +216,93 @@ test('getClashSubscription udpRelay', async t => {
});

test('getClashSubscription - invalid yaml', async t => {
const scope = nock('http://local')
.get('/fail-1')
.reply(200, '')
.get('/fail-2')
.reply(200, `
foo: bar
`);

await t.throwsAsync(async () => {
await getClashSubscription('http://example.com/test-v2rayn-sub.txt');
}, {instanceOf: Error, message: 'http://example.com/test-v2rayn-sub.txt 不是一个合法的 YAML 文件'});
}, {instanceOf: Error, message: 'http://example.com/test-v2rayn-sub.txt 订阅内容有误,请检查后重试'});

await t.throwsAsync(async () => {
await getClashSubscription('http://local/fail-1');
}, {instanceOf: Error, message: 'http://local/fail-1 订阅内容有误,请检查后重试'});

await t.throwsAsync(async () => {
await getClashSubscription('http://local/fail-2');
}, {instanceOf: Error, message: 'http://local/fail-2 订阅内容有误,请检查后重试'});
});

test('snell Configurations', t => {
t.deepEqual(
parseClashConfig([{
type: 'snell',
name: 'snell',
server: 'server',
port: 44046,
psk: 'yourpsk',
'obfs-opts': {
mode: 'tls',
host: 'example.com'
},
version: '2',
}]),
[{
type: NodeTypeEnum.Snell,
nodeName: 'snell',
hostname: 'server',
port: 44046,
psk: 'yourpsk',
obfs: 'tls',
'obfs-host': 'example.com',
version: '2',
}]
);
});

test('trojan configurations', t => {
t.deepEqual(
parseClashConfig([{
type: 'trojan',
name: 'trojan',
server: 'example.com',
port: 443,
password: 'password1',
}]),
[{
type: NodeTypeEnum.Trojan,
nodeName: 'trojan',
hostname: 'example.com',
port: 443,
password: 'password1',
}]
);
t.deepEqual(
parseClashConfig([{
type: 'trojan',
name: 'trojan',
server: 'example.com',
port: 443,
password: 'password1',
skipCertVerify: true,
alpn: ['http/1.1'],
sni: 'sni.example.com',
udp: true,
}]),
[{
type: NodeTypeEnum.Trojan,
nodeName: 'trojan',
hostname: 'example.com',
port: 443,
password: 'password1',
skipCertVerify: true,
alpn: ['http/1.1'],
sni: 'sni.example.com',
'udp-relay': true,
}]
);
});

0 comments on commit 62688ec

Please sign in to comment.