Skip to content

Commit

Permalink
feat: 支持 Clash 的 'fallback-auto', 'load-balance' 策略
Browse files Browse the repository at this point in the history
Close #34
  • Loading branch information
geekdada committed Nov 13, 2019
1 parent 89b0b92 commit 18f106f
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 10 deletions.
1 change: 1 addition & 0 deletions lib/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ export async function generate(
artifact.proxyGroupModifier,
{
proxyTestUrl: config.proxyTestUrl,
proxyTestInterval: config.proxyTestInterval,
},
),
},
Expand Down
3 changes: 2 additions & 1 deletion lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export interface CommandConfig {
readonly auth?: boolean;
},
readonly proxyTestUrl?: string;
readonly proxyTestInterval?: number;
}

export interface RemoteSnippetConfig {
Expand Down Expand Up @@ -188,7 +189,7 @@ export type PossibleNodeConfigType = HttpsNodeConfig|ShadowsocksNodeConfig|Shado

export type ProxyGroupModifier = (nodeList: ReadonlyArray<PossibleNodeConfigType>, filters: PlainObjectOf<NodeNameFilterType>) => ReadonlyArray<{
readonly name: string;
readonly type: 'select' | 'url-test';
readonly type: 'select'|'url-test'|'fallback-auto'|'load-balance';
readonly proxies?: ReadonlyArray<string>;
readonly filter?: NodeNameFilterType;
}>;
4 changes: 3 additions & 1 deletion lib/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import _ from 'lodash';
import path from 'path';

import { CommandConfig } from '../types';
import { PROXY_TEST_URL } from './constant';
import { PROXY_TEST_INTERVAL, PROXY_TEST_URL } from './constant';
import { ensureConfigFolder } from './index';

export const loadConfig = (cwd: string, configPath: string, override?: Partial<CommandConfig>): CommandConfig => {
Expand Down Expand Up @@ -40,6 +40,7 @@ export const normalizeConfig = (cwd: string, userConfig: Partial<CommandConfig>)
v2ray: 'external',
},
proxyTestUrl: PROXY_TEST_URL,
proxyTestInterval: PROXY_TEST_INTERVAL,
};
const config: CommandConfig = _.defaultsDeep(userConfig, defaultConfig);

Expand Down Expand Up @@ -101,6 +102,7 @@ export const validateConfig = (userConfig: Partial<CommandConfig>): void => {
/https?/,
],
}),
proxyTestInterval: Joi.number(),
})
.unknown();

Expand Down
4 changes: 3 additions & 1 deletion lib/utils/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ export const NETWORK_TIMEOUT = process.env.SURGIO_NETWORK_TIMEOUT ? Number(proce

export const OBFS_UA = 'Mozilla/5.0 (iPhone; CPU iPhone OS 12_3_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148';

export const PROXY_TEST_URL = process.env.SURGIO_PROXY_TEST_URL ?? 'http://www.qualcomm.cn/generate_204';
export const PROXY_TEST_URL = 'http://www.qualcomm.cn/generate_204';

export const PROXY_TEST_INTERVAL = 1200;
9 changes: 6 additions & 3 deletions lib/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -971,12 +971,13 @@ export const getNodeNames = (

export const getClashNodeNames = (
ruleName: string,
ruleType: 'select' | 'url-test',
ruleType: 'select'|'url-test'|'fallback-auto'|'load-balance',
nodeNameList: ReadonlyArray<SimpleNodeConfig>,
options: {
readonly filter?: NodeNameFilterType,
readonly existingProxies?: ReadonlyArray<string>,
readonly proxyTestUrl?: string,
readonly proxyTestInterval?: number,
} = {},
): {
readonly type: string;
Expand All @@ -1002,9 +1003,9 @@ export const getClashNodeNames = (
type: ruleType,
name: ruleName,
proxies,
...(ruleType === 'url-test' ? {
...(['url-test', 'fallback-auto', 'load-balance'].includes(ruleType) ? {
url: options.proxyTestUrl,
interval: 1200,
interval: options.proxyTestInterval,
} : null),
};
};
Expand Down Expand Up @@ -1036,6 +1037,7 @@ export const normalizeClashProxyGroupConfig = (
proxyGroupModifier: ProxyGroupModifier,
options: {
readonly proxyTestUrl?: string,
readonly proxyTestInterval?: number,
} = {},
): ReadonlyArray<any> => {
const proxyGroup = proxyGroupModifier(nodeList, customFilters);
Expand All @@ -1046,6 +1048,7 @@ export const normalizeClashProxyGroupConfig = (
filter: item.filter,
existingProxies: item.proxies,
proxyTestUrl: options.proxyTestUrl,
proxyTestInterval: options.proxyTestInterval,
});
} else if (item.proxies) {
return item;
Expand Down
35 changes: 31 additions & 4 deletions test/utils/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
SnellNodeConfig,
} from '../../lib/types';
import * as utils from '../../lib/utils';
import { PROXY_TEST_URL } from '../../lib/utils/constant';
import { PROXY_TEST_INTERVAL, PROXY_TEST_URL } from '../../lib/utils/constant';
import * as filter from '../../lib/utils/filter';

test('getSurgeNodes', async t => {
Expand Down Expand Up @@ -304,6 +304,18 @@ test('normalizeClashProxyGroupConfig', t => {
proxies: ['DIRECT'],
type: 'url-test',
},
{
name: 'load-balance',
filter: filters.hkFilter,
proxies: ['🚀 Proxy', 'US'],
type: 'load-balance',
},
{
name: 'fallback-auto',
filter: filters.hkFilter,
proxies: ['🚀 Proxy', 'US'],
type: 'fallback-auto',
},
];
}
const result = [
Expand All @@ -317,14 +329,14 @@ test('normalizeClashProxyGroupConfig', t => {
type: 'url-test',
proxies: [],
url: PROXY_TEST_URL,
interval: 1200,
interval: PROXY_TEST_INTERVAL,
},
{
name: 'HK',
type: 'url-test',
proxies: ['🇭🇰HK(Example)'],
url: PROXY_TEST_URL,
interval: 1200,
interval: PROXY_TEST_INTERVAL,
},
{
name: '🍎 Apple',
Expand All @@ -336,7 +348,21 @@ test('normalizeClashProxyGroupConfig', t => {
proxies: ['DIRECT', '🇭🇰HK(Example)'],
type: 'url-test',
url: PROXY_TEST_URL,
interval: 1200,
interval: PROXY_TEST_INTERVAL,
},
{
name: 'load-balance',
type: 'load-balance',
proxies: ['🚀 Proxy', 'US', '🇭🇰HK(Example)'],
url: PROXY_TEST_URL,
interval: PROXY_TEST_INTERVAL,
},
{
name: 'fallback-auto',
type: 'fallback-auto',
proxies: ['🚀 Proxy', 'US', '🇭🇰HK(Example)'],
url: PROXY_TEST_URL,
interval: PROXY_TEST_INTERVAL,
},
];

Expand All @@ -359,6 +385,7 @@ test('normalizeClashProxyGroupConfig', t => {
proxyGroupModifier as any,
{
proxyTestUrl: PROXY_TEST_URL,
proxyTestInterval: PROXY_TEST_INTERVAL,
}
),
result);
Expand Down

0 comments on commit 18f106f

Please sign in to comment.