Skip to content

Commit

Permalink
Add port hopping for hysteria2
Browse files Browse the repository at this point in the history
  • Loading branch information
nekohasekai committed Jan 8, 2025
1 parent 6f73096 commit 24a57b5
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 10 deletions.
11 changes: 8 additions & 3 deletions docs/configuration/inbound/hysteria2.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ icon: material/alert-decagram

!!! quote "Changes in sing-box 1.11.0"

:material-alert: [masquerade](#masquerade)
:material-alert: [masquerade](#masquerade)
:material-alert: [ignore_client_bandwidth](#ignore_client_bandwidth)

### Structure

Expand Down Expand Up @@ -75,9 +76,13 @@ Authentication password

#### ignore_client_bandwidth

Commands the client to use the BBR flow control algorithm instead of Hysteria CC.
*When `up_mbps` and `down_mbps` are not set*:

Conflict with `up_mbps` and `down_mbps`.
Commands clients to use the BBR CC instead of Hysteria CC.

*When `up_mbps` and `down_mbps` are set*:

Deny clients to use the BBR CC.

#### tls

Expand Down
9 changes: 7 additions & 2 deletions docs/configuration/inbound/hysteria2.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ icon: material/alert-decagram

!!! quote "sing-box 1.11.0 中的更改"

:material-alert: [masquerade](#masquerade)
:material-alert: [masquerade](#masquerade)
:material-alert: [ignore_client_bandwidth](#ignore_client_bandwidth)

### 结构

Expand Down Expand Up @@ -72,9 +73,13 @@ Hysteria 用户

#### ignore_client_bandwidth

*`up_mbps``down_mbps` 未设定时*:

命令客户端使用 BBR 拥塞控制算法而不是 Hysteria CC。

`up_mbps``down_mbps` 冲突。
*`up_mbps``down_mbps` 已设定时*:

禁止客户端使用 BBR 拥塞控制算法。

#### tls

Expand Down
35 changes: 35 additions & 0 deletions docs/configuration/outbound/hysteria2.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
---
icon: material/new-box
---

!!! quote "Changes in sing-box 1.11.0"

:material-plus: [server_ports](#server_ports)
:material-plus: [hop_interval](#hop_interval)

### Structure

```json
Expand All @@ -7,6 +16,10 @@

"server": "127.0.0.1",
"server_port": 1080,
"server_ports": [
"2080:3000"
],
"hop_interval": "",
"up_mbps": 100,
"down_mbps": 100,
"obfs": {
Expand All @@ -22,6 +35,10 @@
}
```

!!! note ""

You can ignore the JSON Array [] tag when the content is only one item

!!! warning "Difference from official Hysteria2"

The official Hysteria2 supports an authentication method called **userpass**,
Expand All @@ -44,6 +61,24 @@ The server address.

The server port.

Ignored if `server_ports` is set.

#### server_ports

!!! question "Since sing-box 1.11.0"

Server port range list.

Conflicts with `server_port`.

#### hop_interval

!!! question "Since sing-box 1.11.0"

Port hopping interval.

`30s` is used by default.

#### up_mbps, down_mbps

Max bandwidth, in Mbps.
Expand Down
35 changes: 35 additions & 0 deletions docs/configuration/outbound/hysteria2.zh.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
---
icon: material/new-box
---

!!! quote "sing-box 1.11.0 中的更改"

:material-plus: [server_ports](#server_ports)
:material-plus: [hop_interval](#hop_interval)

### 结构

```json
Expand All @@ -7,6 +16,10 @@

"server": "127.0.0.1",
"server_port": 1080,
"server_ports": [
"2080:3000"
],
"hop_interval": "",
"up_mbps": 100,
"down_mbps": 100,
"obfs": {
Expand All @@ -22,6 +35,10 @@
}
```

!!! note ""

当内容只有一项时,可以忽略 JSON 数组 [] 标签

!!! warning "与官方 Hysteria2 的区别"

官方程序支持一种名为 **userpass** 的验证方式,
Expand All @@ -42,6 +59,24 @@

服务器端口。

如果设置了 `server_ports`,则忽略此项。

#### server_ports

!!! question "自 sing-box 1.11.0 起"

服务器端口范围列表。

`server_port` 冲突。

#### hop_interval

!!! question "自 sing-box 1.11.0 起"

端口跳跃间隔。

默认使用 `30s`

#### up_mbps, down_mbps

最大带宽。
Expand Down
12 changes: 7 additions & 5 deletions option/hysteria2.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,13 @@ type Hysteria2MasqueradeString struct {
type Hysteria2OutboundOptions struct {
DialerOptions
ServerOptions
UpMbps int `json:"up_mbps,omitempty"`
DownMbps int `json:"down_mbps,omitempty"`
Obfs *Hysteria2Obfs `json:"obfs,omitempty"`
Password string `json:"password,omitempty"`
Network NetworkList `json:"network,omitempty"`
ServerPorts badoption.Listable[string] `json:"server_ports,omitempty"`
HopInterval badoption.Duration `json:"hop_interval,omitempty"`
UpMbps int `json:"up_mbps,omitempty"`
DownMbps int `json:"down_mbps,omitempty"`
Obfs *Hysteria2Obfs `json:"obfs,omitempty"`
Password string `json:"password,omitempty"`
Network NetworkList `json:"network,omitempty"`
OutboundTLSOptionsContainer
BrutalDebug bool `json:"brutal_debug,omitempty"`
}
3 changes: 3 additions & 0 deletions protocol/hysteria2/outbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"net"
"os"
"time"

"github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing-box/adapter/outbound"
Expand Down Expand Up @@ -70,6 +71,8 @@ func NewOutbound(ctx context.Context, router adapter.Router, logger log.ContextL
Logger: logger,
BrutalDebug: options.BrutalDebug,
ServerAddress: options.ServerOptions.Build(),
ServerPorts: options.ServerPorts,
HopInterval: time.Duration(options.HopInterval),
SendBPS: uint64(options.UpMbps * hysteria.MbpsToBps),
ReceiveBPS: uint64(options.DownMbps * hysteria.MbpsToBps),
SalamanderPassword: salamanderPassword,
Expand Down

0 comments on commit 24a57b5

Please sign in to comment.