Skip to content

Commit

Permalink
feat: nodeConfig no longer has kebab naming properties
Browse files Browse the repository at this point in the history
  • Loading branch information
geekdada committed Apr 3, 2023
1 parent 6ee4f93 commit 43ea656
Show file tree
Hide file tree
Showing 42 changed files with 232 additions and 278 deletions.
14 changes: 7 additions & 7 deletions docs/guide/custom-provider.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,9 +264,9 @@ module.exports = {
method: 'chacha20-ietf-poly1305',
password: 'password',
obfs: 'tls', // tls, http, ws, wss
'obfs-host': 'gateway-carry.icloud.com',
obfsHost: 'gateway-carry.icloud.com',
'obfs-uri': '/', // 当 obfs 为 ws 或 wss 时可配置
'udp-relay': true,
udpRelay: true,
tfo: false, // TCP Fast Open
tls13: false, // TLS 1.3,适用于 v2ray-plugin
mux: false, // 目前仅 Clash + Shadowsocks + v2ray-plugin 可用
Expand All @@ -292,7 +292,7 @@ module.exports = {
obfsparam: 'music.163.com',
protocol: 'auth_aes128_md5',
protoparam: '',
'udp-relay': true,
udpRelay: true,
tfo: false, // TCP Fast Open
}
```
Expand All @@ -314,7 +314,7 @@ module.exports = {
uuid: '1386f85e-657b-4d6e-9d56-78badb75e1fd',
tfo: false, // TCP Fast Open
tls13: false, // TLS 1.3, TLS 开启时有效
'udp-relay': true, // 开启 UDP 转发
udpRelay: true, // 开启 UDP 转发
wsHeaders: {
key: 'value',
},
Expand Down Expand Up @@ -374,7 +374,7 @@ module.exports = {
sni: 'example.com', // 可选
alpn: ['http/1.1'], // 可选
skipCertVerify: true, // 可选
'udp-relay': true, // 可选
udpRelay: true, // 可选
tls13: false, // TLS 1.3
network: 'ws', // 可不填
wsPath: '/', // 可选
Expand All @@ -394,7 +394,7 @@ module.exports = {
password: 'password', // 可选
tls: true, // 可选
skipCertVerify: true, // 可选
'udp-relay': false, // 可选, 仅 Clash 支持
udpRelay: false, // 可选, 仅 Clash 支持
sni: 'example.com', // 可选, 仅 Surge 支持
tfo: true, // 可选, 仅 Surge 支持
clientCert: 'item' // 可选, 仅 Surge 支持
Expand All @@ -417,7 +417,7 @@ module.exports = {
sni: 'sni.example.com', // 可选
skipCertVerify: true, // 可选
alpn: ['h3'], // 可选,Stash 不支持空值
'udp-relay': false, // 可选, 仅 Clash 支持更改,Surge 默认开启
udpRelay: false, // 可选, 仅 Clash 支持更改,Surge 默认开启
}
```

Expand Down
8 changes: 4 additions & 4 deletions examples/clash-remote-snippet/provider/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ module.exports = {
method: 'chacha20-ietf-poly1305',
password: 'surgio',
obfs: 'tls',
'obfs-host': 'world.taobao.com',
'udp-relay': true,
obfsHost: 'world.taobao.com',
udpRelay: true,
},
{
nodeName: '🇭🇰HK Netflix',
Expand All @@ -22,8 +22,8 @@ module.exports = {
method: 'chacha20-ietf-poly1305',
password: 'surgio',
obfs: 'tls',
'obfs-host': 'world.taobao.com',
'udp-relay': true,
obfsHost: 'world.taobao.com',
udpRelay: true,
},
],
};
8 changes: 4 additions & 4 deletions examples/quantumultx/provider/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ module.exports = {
method: 'chacha20-ietf-poly1305',
password: 'surgio',
obfs: 'tls',
'obfs-host': 'world.taobao.com',
'udp-relay': true,
obfsHost: 'world.taobao.com',
udpRelay: true,
},
{
nodeName: '🇭🇰HK Netflix',
Expand All @@ -22,8 +22,8 @@ module.exports = {
method: 'chacha20-ietf-poly1305',
password: 'surgio',
obfs: 'tls',
'obfs-host': 'world.taobao.com',
'udp-relay': true,
obfsHost: 'world.taobao.com',
udpRelay: true,
},
],
};
2 changes: 1 addition & 1 deletion src/generator/__tests__/__snapshots__/artifact.test.ts.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Snapshot report for `lib/generator/__tests__/artifact.test.ts`
# Snapshot report for `src/generator/__tests__/artifact.test.ts`

The actual snapshot is saved in `artifact.test.ts.snap`.

Expand Down
2 changes: 1 addition & 1 deletion src/generator/__tests__/__snapshots__/template.test.ts.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Snapshot report for `lib/generator/__tests__/template.test.ts`
# Snapshot report for `src/generator/__tests__/template.test.ts`

The actual snapshot is saved in `template.test.ts.snap`.

Expand Down
20 changes: 10 additions & 10 deletions src/provider/ClashProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,21 +192,21 @@ export const parseClashConfig = (
port: item.port,
method: item.cipher,
password: item.password,
'udp-relay': resolveUdpRelay(item.udp, udpRelay),
udpRelay: resolveUdpRelay(item.udp, udpRelay),

// obfs-local 新格式
...(item.plugin && item.plugin === 'obfs'
? {
obfs: item['plugin-opts'].mode,
'obfs-host': item['plugin-opts'].host || 'www.bing.com',
obfsHost: item['plugin-opts'].host || 'www.bing.com',
}
: null),

// obfs-local 旧格式
...(item.obfs
? {
obfs: item.obfs,
'obfs-host': item['obfs-host'] || 'www.bing.com',
obfsHost: item['obfs-host'] || 'www.bing.com',
}
: null),

Expand All @@ -216,8 +216,8 @@ export const parseClashConfig = (
item['plugin-opts'].mode === 'websocket'
? {
obfs: item['plugin-opts'].tls === true ? 'wss' : 'ws',
'obfs-host': item['plugin-opts'].host || item.server,
'obfs-uri': item['plugin-opts'].path || '/',
obfsHost: item['plugin-opts'].host || item.server,
obfsUri: item['plugin-opts'].path || '/',
wsHeaders,
...(item['plugin-opts'].tls === true
? {
Expand Down Expand Up @@ -266,7 +266,7 @@ export const parseClashConfig = (
uuid: item.uuid,
alterId: item.alterId ? `${item.alterId}` : '0',
method: item.cipher || 'auto',
'udp-relay': resolveUdpRelay(item.udp, udpRelay),
udpRelay: resolveUdpRelay(item.udp, udpRelay),
tls: item.tls ?? false,
network: item.network || 'tcp',
...(item.network === 'ws'
Expand Down Expand Up @@ -317,7 +317,7 @@ export const parseClashConfig = (
psk: item.psk,
obfs: _.get(item, 'obfs-opts.mode', 'http'),
...(typeof item?.['obfs-opts']?.host !== 'undefined'
? { 'obfs-host': item['obfs-opts'].host }
? { obfsHost: item['obfs-opts'].host }
: null),
...('version' in item ? { version: item.version } : null),
} as SnellNodeConfig;
Expand All @@ -335,7 +335,7 @@ export const parseClashConfig = (
protocol: item.protocol,
protoparam: item['protocol-param'] ?? item.protocolparam,
method: item.cipher,
'udp-relay': resolveUdpRelay(item.udp, udpRelay),
udpRelay: resolveUdpRelay(item.udp, udpRelay),
} as ShadowsocksrNodeConfig;

case 'trojan': {
Expand All @@ -354,7 +354,7 @@ export const parseClashConfig = (
: null),
...('alpn' in item ? { alpn: item.alpn } : null),
...('sni' in item ? { sni: item.sni } : null),
'udp-relay': resolveUdpRelay(item.udp, udpRelay),
udpRelay: resolveUdpRelay(item.udp, udpRelay),
tls13: tls13 ?? false,
...(network === 'ws'
? { network: 'ws', wsPath: _.get(wsOpts, 'path', '/'), wsHeaders }
Expand All @@ -369,7 +369,7 @@ export const parseClashConfig = (
hostname: item.server,
port: item.port,
token: item.token,
'udp-relay': resolveUdpRelay(item.udp, udpRelay),
udpRelay: resolveUdpRelay(item.udp, udpRelay),
...('skip-cert-verify' in item
? { skipCertVerify: item['skip-cert-verify'] === true }
: null),
Expand Down
31 changes: 20 additions & 11 deletions src/provider/CustomProvider.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Joi from 'joi';

import {
CustomProviderConfig,
NodeTypeEnum,
Expand All @@ -21,6 +22,9 @@ export default class CustomProvider extends Provider {
enable: Joi.boolean().strict(),
tfo: Joi.boolean().strict(),
mptcp: Joi.boolean().strict(),
udpRelay: Joi.boolean().strict(),
obfsHost: Joi.string(),
obfsUri: Joi.string(),
shadowTls: Joi.object({
password: Joi.string().required(),
sni: Joi.string(),
Expand Down Expand Up @@ -50,24 +54,29 @@ export default class CustomProvider extends Provider {
}

public async getNodeList(): Promise<ReadonlyArray<PossibleNodeConfigType>> {
const udpRelayCheckSchema = Joi.object({
'udp-relay': Joi.bool().strict(),
}).unknown();

return this.nodeList.map((item) => {
const { error: udpRelayCheckError } = udpRelayCheckSchema.validate(item);
const lowercaseKeys = ['wsHeaders'];
const propertyKeysMustBeLowercase = ['wsHeaders'];

if (this.underlyingProxy && !item.underlyingProxy) {
item.underlyingProxy = this.underlyingProxy;
}

// istanbul ignore next
if (udpRelayCheckError) {
throw udpRelayCheckError;
if (item['udp-relay']) {
throw new Error('udp-relay is abandoned, please use udpRelay instead');
}

if (this.underlyingProxy && !item.underlyingProxy) {
item.underlyingProxy = this.underlyingProxy;
// istanbul ignore next
if (item['obfs-host']) {
throw new Error('obfs-host is abandoned, please use obfsHost instead');
}

// istanbul ignore next
if (item['udp-relay']) {
throw new Error('obfs-uri is abandoned, please use obfsUri instead');
}

lowercaseKeys.forEach((key) => {
propertyKeysMustBeLowercase.forEach((key) => {
if (item[key]) {
item[key] = Object.keys(item[key]).reduce((acc, curr) => {
acc[curr.toLowerCase()] = item[key][curr];
Expand Down
4 changes: 2 additions & 2 deletions src/provider/ShadowsocksJsonSubscribeProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,15 @@ export const getShadowsocksJSONConfig = async (
};

if (typeof udpRelay === 'boolean') {
nodeConfig['udp-relay'] = udpRelay;
nodeConfig.udpRelay = udpRelay;
}
if (item.plugin === 'obfs-local') {
const obfs = item.plugin_opts.match(/obfs=(\w+)/);
const obfsHost = item.plugin_opts.match(/obfs-host=(.+)$/);

if (obfs) {
nodeConfig.obfs = obfs[1];
nodeConfig['obfs-host'] = obfsHost ? obfsHost[1] : 'www.bing.com';
nodeConfig.obfsHost = obfsHost ? obfsHost[1] : 'www.bing.com';
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/provider/ShadowsocksSubscribeProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export const getShadowsocksSubscription = async (
const nodeConfig = parseSSUri(item);

if (udpRelay !== void 0) {
(nodeConfig['udp-relay'] as boolean) = udpRelay;
(nodeConfig.udpRelay as boolean) = udpRelay;
}

return nodeConfig;
Expand Down
2 changes: 1 addition & 1 deletion src/provider/ShadowsocksrSubscribeProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export const getShadowsocksrSubscription = async (
const nodeConfig = parseSSRUri(str);

if (udpRelay !== void 0) {
(nodeConfig['udp-relay'] as boolean) = udpRelay;
(nodeConfig.udpRelay as boolean) = udpRelay;
}

return nodeConfig;
Expand Down
6 changes: 3 additions & 3 deletions src/provider/SsdProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,21 +170,21 @@ export const parseSsdConfig = (
port: server.port ?? port,
method: server.encryption ?? encryption,
password: server.password ?? password,
'udp-relay': udpRelay === true,
udpRelay: udpRelay === true,

// obfs-local
...(plugin && plugin === 'simple-obfs'
? {
obfs: pluginOpts.obfs as ShadowsocksNodeConfig['obfs'],
'obfs-host': (pluginOpts['obfs-host'] as string) || 'www.bing.com',
obfsHost: (pluginOpts['obfs-host'] as string) || 'www.bing.com',
}
: null),

// v2ray-plugin
...(plugin && plugin === 'v2ray-plugin'
? {
obfs: (pluginOpts.tls as boolean) ? 'wss' : 'ws',
'obfs-host': pluginOpts.host as string,
obfsHost: pluginOpts.host as string,
}
: null),
};
Expand Down
2 changes: 1 addition & 1 deletion src/provider/TrojanProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export const getTrojanSubscription = async ({

return {
...nodeConfig,
'udp-relay': udpRelay,
udpRelay,
tls13,
};
});
Expand Down
4 changes: 2 additions & 2 deletions src/provider/V2rayNSubscribeProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export const getV2rayNSubscription = async ({
if (item.startsWith('ss://')) {
return {
...parseSSUri(item),
'udp-relay': udpRelay,
udpRelay: udpRelay,
skipCertVerify: skipCertVerify,
tls13: tls13,
};
Expand Down Expand Up @@ -184,7 +184,7 @@ export const parseJSONConfig = (
tls: config.tls === 'tls',
host: config.host,
path: config.path || '/',
'udp-relay': udpRelay === true,
udpRelay: udpRelay === true,
...(config.tls === 'tls'
? {
skipCertVerify: skipCertVerify ?? false,
Expand Down
Loading

0 comments on commit 43ea656

Please sign in to comment.