Skip to content

Commit

Permalink
feat: udp-relay 的值改为布尔类型,兼容字符串类型
Browse files Browse the repository at this point in the history
  • Loading branch information
geekdada committed Nov 2, 2019
1 parent 2dda404 commit f3eaaed
Show file tree
Hide file tree
Showing 14 changed files with 67 additions and 50 deletions.
2 changes: 1 addition & 1 deletion docs/guide/custom-provider.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ module.exports = {
password: 'password',
obfs: 'tls', // tls 或 http
'obfs-host': 'gateway-carry.icloud.com',
'udp-relay': 'true',
'udp-relay': true,
}
```

Expand Down
2 changes: 1 addition & 1 deletion lib/class/ClashProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ async function requestConfigFromRemote(url: string, udpRelay?: boolean): Promise
port: item.port,
method: item.cipher,
password: item.password,
'udp-relay': resolveUdpRelay(item.udp, udpRelay) ? 'true' : 'false',
'udp-relay': resolveUdpRelay(item.udp, udpRelay),
...(item.plugin && item.plugin === 'obfs' ? {
obfs: item['plugin-opts'].mode,
'obfs-host': item['plugin-opts'].host || 'www.bing.com',
Expand Down
19 changes: 17 additions & 2 deletions lib/class/CustomProvider.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import Joi from '@hapi/joi';
import * as util from 'util';
import { DEP002 } from '../misc/deprecation';
import { CustomProviderConfig, NodeTypeEnum, PossibleNodeConfigType } from '../types';
import Provider from './Provider';

export default class CustomProvider extends Provider {
public readonly nodeList: ReadonlyArray<PossibleNodeConfigType>;
public readonly nodeList: ReadonlyArray<any>;

constructor(config: CustomProviderConfig) {
super(config);
Expand Down Expand Up @@ -33,6 +35,19 @@ export default class CustomProvider extends Provider {
}

public async getNodeList(): Promise<ReadonlyArray<PossibleNodeConfigType>> {
return this.nodeList;
return this.nodeList.map(item => {
if (item.type === NodeTypeEnum.Shadowsocks) {
// 兼容字符串 true 和 false 的写法,会弃用
if (typeof item['udp-relay'] === 'string') {
notifyDepUdpRelay();
item['udp-relay'] = item['udp-relay'] === 'true';
}
}
return item;
});
}
}

const notifyDepUdpRelay = util.deprecate(() => {
// do nothing
}, DEP002, 'DEP002');
2 changes: 1 addition & 1 deletion lib/command/generate.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Command from 'common-bin';
import path from 'path';

import { loadConfig } from '../utils';
import { loadConfig } from '../utils/config';
import generate from '../generate';
import { errorHandler } from '../utils/error-helper';

Expand Down
3 changes: 2 additions & 1 deletion lib/command/speed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import speedTest from 'speedtest-net';
import winston, { format, Logger } from 'winston';

import { NodeTypeEnum, PossibleNodeConfigType, ShadowsocksNodeConfig } from '../types';
import { getClashNodes, loadConfig, toYaml } from '../utils';
import { getClashNodes, toYaml } from '../utils';
import { loadConfig } from '../utils/config';
import getProvider from '../utils/get-provider';
import { errorHandler } from '../utils/error-helper';

Expand Down
2 changes: 1 addition & 1 deletion lib/command/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import dir from 'node-dir';
import ora, { Ora } from 'ora';
import path from 'path';

import { loadConfig } from '../utils';
import { loadConfig } from '../utils/config';
import { errorHandler } from '../utils/error-helper';

class GenerateCommand extends Command {
Expand Down
2 changes: 1 addition & 1 deletion lib/gateway/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Router from 'koa-router';
import util from 'util';
import { DEP001 } from '../misc/deprecation';

import { loadConfig } from '../utils';
import { loadConfig } from '../utils/config';
import { Server } from './server';
import { FcRequest, FcResponse } from './types';

Expand Down
1 change: 1 addition & 0 deletions lib/misc/deprecation.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export const DEP001 = 'nowHandler 已不推荐使用,请参考 http://bit.ly/2q5daCK 尽快更新您的代码';
export const DEP002 = 'udp-relay 的值已改为布尔类型,字符串类型依然可用但推荐更改';
2 changes: 1 addition & 1 deletion lib/misc/flag_cn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default {
'🇵🇭': ['菲律宾'],
'🇷🇴': ['罗马尼亚'],
'🇷🇺': ['RU', '普京', '俄罗斯', '伯力', '莫斯科', '圣彼得堡', '西伯利亚', '新西伯利亚'],
'🇸🇬': ['SG', '新加坡', '狮城','新'],
'🇸🇬': ['SG', '新加坡', '狮城'],
'🇹🇭': ['泰国', '曼谷'],
'🇹🇷': ['土耳其', '伊斯坦布尔'],
'🇺🇲': [
Expand Down
4 changes: 2 additions & 2 deletions lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export interface ClashProviderConfig extends ProviderConfig {
}

export interface CustomProviderConfig extends ProviderConfig {
readonly nodeList: ReadonlyArray<PossibleNodeConfigType>;
readonly nodeList: ReadonlyArray<any>;
}

export interface HttpsNodeConfig extends SimpleNodeConfig {
Expand All @@ -124,7 +124,7 @@ export interface ShadowsocksNodeConfig extends SimpleNodeConfig {
readonly port: number|string;
readonly method: string;
readonly password: string;
readonly 'udp-relay'?: 'true'|'false';
readonly 'udp-relay'?: boolean;
readonly obfs?: 'tls'|'http';
readonly 'obfs-host'?: string;
}
Expand Down
21 changes: 21 additions & 0 deletions lib/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,27 @@ import path from 'path';
import { CommandConfig } from '../types';
import { ensureConfigFolder } from './index';

export const loadConfig = (cwd: string, configPath: string, override?: Partial<CommandConfig>): CommandConfig => {
const absPath = path.resolve(cwd, configPath);

if (!fs.existsSync(absPath)) {
throw new Error(`配置文件 ${absPath} 不存在`);
}

const userConfig = _.cloneDeep(require(absPath));

validateConfig(userConfig);

if (override) {
return {
...normalizeConfig(cwd, userConfig),
...override,
};
}

return normalizeConfig(cwd, userConfig);
};

export const normalizeConfig = (cwd: string, userConfig: Partial<CommandConfig>): CommandConfig => {
const defaultConfig: Partial<CommandConfig> = {
artifacts: [],
Expand Down
27 changes: 3 additions & 24 deletions lib/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export const getShadowsocksJSONConfig = async (url: string, udpRelay: boolean):
};

if (typeof udpRelay === 'boolean') {
nodeConfig['udp-relay'] = udpRelay ? 'true' : 'false';
nodeConfig['udp-relay'] = udpRelay;
}
if (item.plugin === 'obfs-local') {
const obfs = item.plugin_opts.match(/obfs=(\w+)/);
Expand Down Expand Up @@ -162,7 +162,7 @@ export const getShadowsocksSubscription = async (url: string, udpRelay?: boolean
method: userInfo[0],
password: userInfo[1],
...(typeof udpRelay === 'boolean' ? {
'udp-relay': udpRelay ? 'true' : 'false',
'udp-relay': udpRelay,
} : null),
...(pluginInfo['obfs-local'] ? {
obfs: pluginInfo.obfs,
Expand Down Expand Up @@ -451,7 +451,7 @@ export const getClashNodes = (
password: nodeConfig.password,
port: nodeConfig.port,
server: nodeConfig.hostname,
udp: nodeConfig['udp-relay'] === 'true',
udp: nodeConfig['udp-relay'] || false,
...(nodeConfig.obfs ? {
plugin: 'obfs',
'plugin-opts': {
Expand Down Expand Up @@ -849,27 +849,6 @@ export const decodeStringList = <T = object>(stringList: ReadonlyArray<string>):
return result as T;
};

export const loadConfig = (cwd: string, configPath: string, override?: Partial<CommandConfig>): CommandConfig => {
const absPath = path.resolve(cwd, configPath);

if (!fs.existsSync(absPath)) {
throw new Error(`文件 ${absPath} 不存在`);
}

const userConfig = _.cloneDeep(require(absPath));

validateConfig(userConfig);

if (override) {
return {
...normalizeConfig(cwd, userConfig),
...override,
};
}

return normalizeConfig(cwd, userConfig);
};

export const normalizeClashProxyGroupConfig = (
nodeList: ReadonlyArray<PossibleNodeConfigType>,
customFilters: PlainObjectOf<NodeNameFilterType>,
Expand Down
10 changes: 5 additions & 5 deletions test/class/ClashProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ test('getClashSubscription', async t => {
port: 443,
method: 'chacha20-ietf-poly1305',
password: 'password',
'udp-relay': 'true'
'udp-relay': true
});
t.deepEqual(config[1], {
type: NodeTypeEnum.Shadowsocks,
Expand All @@ -22,7 +22,7 @@ test('getClashSubscription', async t => {
port: 443,
method: 'chacha20-ietf-poly1305',
password: 'password',
'udp-relay': 'false',
'udp-relay': false,
obfs: 'tls',
'obfs-host': 'www.bing.com'
});
Expand Down Expand Up @@ -53,7 +53,7 @@ test('getClashSubscription', async t => {
port: 443,
method: 'chacha20-ietf-poly1305',
password: 'password',
'udp-relay': 'false',
'udp-relay': false,
obfs: 'tls',
'obfs-host': 'example.com'
});
Expand All @@ -69,7 +69,7 @@ test('getClashSubscription udpRelay', async t => {
port: 443,
method: 'chacha20-ietf-poly1305',
password: 'password',
'udp-relay': 'true',
'udp-relay': true,
});
t.deepEqual(config[1], {
type: NodeTypeEnum.Shadowsocks,
Expand All @@ -78,7 +78,7 @@ test('getClashSubscription udpRelay', async t => {
port: 443,
method: 'chacha20-ietf-poly1305',
password: 'password',
'udp-relay': 'true',
'udp-relay': true,
obfs: 'tls',
'obfs-host': 'www.bing.com'
});
Expand Down
20 changes: 10 additions & 10 deletions test/utils/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ test('getSurgeNodes', async t => {
password: 'password',
obfs: 'tls',
'obfs-host': 'example.com',
'udp-relay': 'true',
'udp-relay': true,
}, {
nodeName: 'Test Node 2',
type: NodeTypeEnum.Shadowsocks,
Expand Down Expand Up @@ -155,7 +155,7 @@ test('getClashNodes', async t => {
password: 'password',
obfs: 'tls',
'obfs-host': 'example.com',
'udp-relay': 'true',
'udp-relay': true,
}, {
nodeName: 'Test Node 2',
type: NodeTypeEnum.Shadowsocks,
Expand Down Expand Up @@ -251,7 +251,7 @@ test('getShadowsocksNodes', async t => {
password: 'password',
obfs: 'tls',
'obfs-host': 'gateway.icloud.com',
'udp-relay': 'true',
'udp-relay': true,
},
];
const txt1 = utils.getShadowsocksNodes(nodeList, 'GroupName');
Expand Down Expand Up @@ -355,7 +355,7 @@ test('getShadowsocksJSONConfig', async t => {
port: 443,
method: 'chacha20-ietf-poly1305',
password: 'password',
'udp-relay': 'true',
'udp-relay': true,
obfs: 'tls',
'obfs-host': 'gateway-carry.icloud.com',
});
Expand All @@ -366,7 +366,7 @@ test('getShadowsocksJSONConfig', async t => {
port: 444,
method: 'chacha20-ietf-poly1305',
password: 'password',
'udp-relay': 'true',
'udp-relay': true,
});
t.deepEqual(config[2], {
nodeName: '🇺🇸US 3',
Expand All @@ -375,7 +375,7 @@ test('getShadowsocksJSONConfig', async t => {
port: 445,
method: 'chacha20-ietf-poly1305',
password: 'password',
'udp-relay': 'true',
'udp-relay': true,
obfs: 'tls',
'obfs-host': 'www.bing.com',
});
Expand All @@ -386,7 +386,7 @@ test('getShadowsocksJSONConfig', async t => {
port: 80,
method: 'chacha20-ietf-poly1305',
password: 'password',
'udp-relay': 'true',
'udp-relay': true,
obfs: 'http',
'obfs-host': 'www.bing.com',
});
Expand Down Expand Up @@ -625,7 +625,7 @@ test('getQuantumultNodes', t => {
port: 443,
method: 'chacha20-ietf-poly1305',
password: 'password',
'udp-relay': 'true',
'udp-relay': true,
obfs: 'tls',
'obfs-host': 'gateway-carry.icloud.com',
},
Expand Down Expand Up @@ -744,7 +744,7 @@ test('getShadowsocksSubscription with udp', async t => {
port: '443',
method: 'chacha20-ietf-poly1305',
password: 'password',
'udp-relay': 'true',
'udp-relay': true,
obfs: 'tls',
'obfs-host': 'gateway-carry.icloud.com',
});
Expand All @@ -755,7 +755,7 @@ test('getShadowsocksSubscription with udp', async t => {
port: '443',
method: 'chacha20-ietf-poly1305',
password: 'password',
'udp-relay': 'true',
'udp-relay': true,
});
});

Expand Down

0 comments on commit f3eaaed

Please sign in to comment.