Skip to content

Commit

Permalink
feat: 优化了不同 Clash 核心的 Hysteria 密码兼容性问题
Browse files Browse the repository at this point in the history
  • Loading branch information
geekdada committed Nov 4, 2023
1 parent 88227d0 commit 6f95489
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 9 deletions.
24 changes: 23 additions & 1 deletion docs/guide/custom-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,29 @@ module.exports = {
- 类型: `boolean`
- 默认值: `false`

目前仅 Clash Meta 内核支持 Hysteria v2,如果你希望在 Clash 订阅中输出 Hysteria v2 节点请开启此项。
目前仅 Clash Meta 内核和 Stash 支持 Hysteria v2,如果你希望在 Clash 订阅中输出 Hysteria v2 节点请开启此项。

### clashConfig.clashCore

> <Badge text="v3.2.0" vertical="middle" />
- 类型: `string`
- 默认值: `clash`
- 可选值: `clash`, `clash.meta`, `stash`

Clash 核心版本。默认使用 Clash 核心,如果你希望输出针对 Clash Meta 内核或 Stash 的配置请修改此项。

下面是目前支持的变化:

| 核心 | 变化 |
| --- |----------------------------------------------------------------------------------|
| `clash` | 默认值,不输出任何变化 |
| `clash.meta` | 不输出任何变化 |
| `stash` | - Hysteria 协议的密码字段改为 `auth` |

:::warning 注意
`enableTuic`, `enableShadowTls`, `enableHysteria2` 这三个配置项和 `clashCore` 目前互不影响,但是将来会合并到 `clashCore` 中。
:::

## surfboardConfig

Expand Down
17 changes: 16 additions & 1 deletion docs/guide/custom-provider.md
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ module.exports = {

强制开启节点的 TLS 1.3

## 公共属性
## nodeConfig 公共属性

:::tip 提示
- 公共属性可以定义在任何一种 Provider 中;
Expand Down Expand Up @@ -685,6 +685,21 @@ Surgio 不会验证名称是否有效
是否为该节点开启 [ECN(Explicit Congestion Notification)](https://yach.me/2023/10/14/ccn-and-ecn/)。目前仅 Surge 支持这一特性。
### 客户端特殊配置
> <Badge text="v3.2.0" vertical="middle" />
- `nodeConfig.clashConfig`
- `nodeConfig.surgeConfig`
- `nodeConfig.quantumultXConfig`
- `nodeConfig.surfboardConfig`
你可以单独为某一个节点配置单独更改客户端的配置,Surgio 会将这些配置合并到最终的配置中。
比如 Hysteria 配置的密码字段默认是 `password` 但是 Stash 使用的是 `auth` 字段,你可以借助订阅请求的 UserAgent 动态地修改 `nodeConfig.clashConfig.clashCore` 这个字段,返回不同的订阅。
## Provider 公共属性
### provider.nodeFilter
- 类型: `Function`
Expand Down
2 changes: 2 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ export const normalizeConfig = (
clashConfig: {
enableShadowTls: false,
enableTuic: false,
enableHysteria2: false,
clashCore: 'clash',
},
quantumultXConfig: {
vmessAEAD: true,
Expand Down
20 changes: 16 additions & 4 deletions src/generator/artifact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,10 +370,22 @@ export class Artifact extends EventEmitter {
}

nodeConfig.provider = provider
nodeConfig.surgeConfig = config.surgeConfig
nodeConfig.clashConfig = config.clashConfig
nodeConfig.quantumultXConfig = config.quantumultXConfig
nodeConfig.surfboardConfig = config.surfboardConfig
nodeConfig.surgeConfig = Object.freeze({
...config.surgeConfig,
...nodeConfig.surgeConfig,
})
nodeConfig.clashConfig = Object.freeze({
...config.clashConfig,
...nodeConfig.clashConfig,
})
nodeConfig.quantumultXConfig = Object.freeze({
...config.quantumultXConfig,
...nodeConfig.quantumultXConfig,
})
nodeConfig.surfboardConfig = Object.freeze({
...config.surfboardConfig,
...nodeConfig.surfboardConfig,
})

if (provider.config.renameNode) {
const newName = provider.config.renameNode(nodeConfig.nodeName)
Expand Down
2 changes: 1 addition & 1 deletion src/provider/ClashProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ export const parseClashConfig = (
nodeName: item.name,
hostname: item.server,
port: item.port,
password: item.password,
password: item.auth || item.password,
...(item.down
? { downloadBandwidth: parseBitrate(item.down) }
: null),
Expand Down
22 changes: 22 additions & 0 deletions src/utils/__tests__/clash.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,19 @@ test('getClashNodes', async (t) => {
enableHysteria2: true,
},
},
{
type: NodeTypeEnum.Hysteria2,
nodeName: '测试 Hysteria2',
hostname: 'example.com',
port: 443,
password: 'password',
downloadBandwidth: 100,
uploadBandwidth: 100,
clashConfig: {
enableHysteria2: true,
clashCore: 'stash',
},
},
{
type: NodeTypeEnum.Hysteria2,
nodeName: '测试 Hysteria2',
Expand All @@ -776,6 +789,15 @@ test('getClashNodes', async (t) => {
type: 'hysteria2',
up: 100,
},
{
down: 100,
name: '测试 Hysteria2',
auth: 'password',
port: 443,
server: 'example.com',
type: 'hysteria2',
up: 100,
},
{
down: 100,
name: '测试 Hysteria2',
Expand Down
5 changes: 3 additions & 2 deletions src/utils/clash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ function nodeListMapper(nodeConfig: PossibleNodeConfigType) {
// Istanbul ignore next
if (!nodeConfig.clashConfig?.enableTuic) {
logger.warn(
`尚未开启 Clash 的 TUIC 支持,节点 ${nodeConfig.nodeName} 会被省略。如需开启,请在配置文件中设置 clashConfig.enableTuic 为 true。`,
`尚未开启 Clash 的 Tuic 支持,节点 ${nodeConfig.nodeName} 会被省略。如需开启,请在配置文件中设置 clashConfig.enableTuic 为 true。`,
)
return null
}
Expand Down Expand Up @@ -329,7 +329,8 @@ function nodeListMapper(nodeConfig: PossibleNodeConfigType) {
name: nodeConfig.nodeName,
server: nodeConfig.hostname,
port: nodeConfig.port,
password: nodeConfig.password,
[nodeConfig.clashConfig.clashCore === 'stash' ? 'auth' : 'password']:
nodeConfig.password,
up: nodeConfig.uploadBandwidth || 0,
down: nodeConfig.downloadBandwidth || 0,
...pickAndFormatKeys(
Expand Down
7 changes: 7 additions & 0 deletions src/validators/surgio-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ export const SurgioConfigValidator = z.object({
enableTuic: z.oboolean(),
enableShadowTls: z.oboolean(),
enableHysteria2: z.oboolean(),
clashCore: z
.union([
z.literal('clash'),
z.literal('clash.meta'),
z.literal('stash'),
])
.optional(),
})
.optional(),
gateway: z
Expand Down

0 comments on commit 6f95489

Please sign in to comment.