Skip to content

Commit c3964f8

Browse files
committed
Add proxy support for ICMP echo request
1 parent 109a614 commit c3964f8

File tree

26 files changed

+617
-92
lines changed

26 files changed

+617
-92
lines changed

adapter/outbound.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ package adapter
33
import (
44
"context"
55
"net/netip"
6+
"time"
67

78
"github.com/sagernet/sing-box/log"
89
"github.com/sagernet/sing-box/option"
10+
"github.com/sagernet/sing-tun"
911
N "github.com/sagernet/sing/common/network"
1012
)
1113

@@ -20,10 +22,16 @@ type Outbound interface {
2022
}
2123

2224
type OutboundWithPreferredRoutes interface {
25+
Outbound
2326
PreferredDomain(domain string) bool
2427
PreferredAddress(address netip.Addr) bool
2528
}
2629

30+
type DirectRouteOutbound interface {
31+
Outbound
32+
NewDirectRouteConnection(metadata InboundContext, routeContext tun.DirectRouteContext, timeout time.Duration) (tun.DirectRouteDestination, error)
33+
}
34+
2735
type OutboundRegistry interface {
2836
option.OutboundOptionsRegistry
2937
CreateOutbound(ctx context.Context, router Router, logger log.ContextLogger, tag string, outboundType string, options any) (Outbound, error)

adapter/router.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ import (
66
"net"
77
"net/http"
88
"sync"
9+
"time"
910

1011
C "github.com/sagernet/sing-box/constant"
12+
"github.com/sagernet/sing-tun"
1113
M "github.com/sagernet/sing/common/metadata"
1214
N "github.com/sagernet/sing/common/network"
1315
"github.com/sagernet/sing/common/ntp"
@@ -19,7 +21,7 @@ import (
1921
type Router interface {
2022
Lifecycle
2123
ConnectionRouter
22-
PreMatch(metadata InboundContext) error
24+
PreMatch(metadata InboundContext, context tun.DirectRouteContext, timeout time.Duration) (tun.DirectRouteDestination, error)
2325
ConnectionRouterEx
2426
RuleSet(tag string) (RuleSet, bool)
2527
NeedWIFIState() bool

common/dialer/default.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,14 @@ func (d *DefaultDialer) ListenPacket(ctx context.Context, destination M.Socksadd
315315
}
316316
}
317317

318+
func (d *DefaultDialer) DialerForICMPDestination(destination netip.Addr) net.Dialer {
319+
if !destination.Is6() {
320+
return dialerFromTCPDialer(d.dialer6)
321+
} else {
322+
return dialerFromTCPDialer(d.dialer4)
323+
}
324+
}
325+
318326
func (d *DefaultDialer) ListenSerialInterfacePacket(ctx context.Context, destination M.Socksaddr, strategy *C.NetworkStrategy, interfaceType []C.InterfaceType, fallbackInterfaceType []C.InterfaceType, fallbackDelay time.Duration) (net.PacketConn, error) {
319327
if strategy == nil {
320328
strategy = d.networkStrategy

constant/rule.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,5 @@ const (
4040
const (
4141
RuleActionRejectMethodDefault = "default"
4242
RuleActionRejectMethodDrop = "drop"
43+
RuleActionRejectMethodReply = "reply"
4344
)

docs/configuration/route/rule.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ icon: material/new-box
77
:material-plus: [interface_address](#interface_address)
88
:material-plus: [network_interface_address](#network_interface_address)
99
:material-plus: [default_interface_address](#default_interface_address)
10-
:material-plus: [preferred_by](#preferred_by)
10+
:material-plus: [preferred_by](#preferred_by)
11+
:material-alert: [network](#network)
1112

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

@@ -226,7 +227,15 @@ Sniffed client type, see [Protocol Sniff](/configuration/route/sniff/) for detai
226227

227228
#### network
228229

229-
`tcp` or `udp`.
230+
!!! quote "Changes in sing-box 1.13.0"
231+
232+
Since sing-box 1.13.0, you can match ICMP echo (ping) requests via the new `icmp` network.
233+
234+
Such traffic originates from `TUN`, `WireGuard`, and `Tailscale` inbounds and can be routed to `Direct`, `WireGuard`, and `Tailscale` outbounds.
235+
236+
Match network type.
237+
238+
`tcp`, `udp` or `icmp`.
230239

231240
#### domain
232241

docs/configuration/route/rule.zh.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ icon: material/new-box
77
:material-plus: [interface_address](#interface_address)
88
:material-plus: [network_interface_address](#network_interface_address)
99
:material-plus: [default_interface_address](#default_interface_address)
10-
:material-plus: [preferred_by](#preferred_by)
10+
:material-plus: [preferred_by](#preferred_by)
11+
:material-alert: [network](#network)
1112

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

@@ -223,7 +224,15 @@ icon: material/new-box
223224

224225
#### network
225226

226-
`tcp``udp`
227+
!!! quote "sing-box 1.13.0 中的更改"
228+
229+
自 sing-box 1.13.0 起,您可以通过新的 `icmp` 网络匹配 ICMP 回显(ping)请求。
230+
231+
此类流量源自 `TUN`、`WireGuard` 和 `Tailscale` 入站,并可路由至 `Direct`、`WireGuard` 和 `Tailscale` 出站。
232+
233+
匹配网络类型。
234+
235+
`tcp``udp``icmp`
227236

228237
#### domain
229238

docs/configuration/route/rule_action.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
icon: material/new-box
33
---
44

5+
!!! quote "Changes in sing-box 1.13.0"
6+
7+
:material-alert: [reject](#reject)
8+
59
!!! quote "Changes in sing-box 1.12.0"
610

711
:material-plus: [tls_fragment](#tls_fragment)
@@ -42,6 +46,10 @@ See `route-options` fields below.
4246

4347
### reject
4448

49+
!!! quote "Changes in sing-box 1.13.0"
50+
51+
Since sing-box 1.13.0, you can reject (or directly reply to) ICMP echo (ping) requests using `reject` action.
52+
4553
```json
4654
{
4755
"action": "reject",
@@ -58,9 +66,17 @@ For non-tun connections and already established connections, will just be closed
5866

5967
#### method
6068

69+
For TCP and UDP connections:
70+
6171
- `default`: Reply with TCP RST for TCP connections, and ICMP port unreachable for UDP packets.
6272
- `drop`: Drop packets.
6373

74+
For ICMP echo requests:
75+
76+
- `default`: Reply with ICMP host unreachable.
77+
- `drop`: Drop packets.
78+
- `reply`: Reply with ICMP echo reply.
79+
6480
#### no_drop
6581

6682
If not enabled, `method` will be temporarily overwritten to `drop` after 50 triggers in 30s.

docs/configuration/route/rule_action.zh.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
icon: material/new-box
33
---
44

5+
!!! quote "sing-box 1.13.0 中的更改"
6+
7+
:material-alert: [reject](#reject)
8+
59
!!! quote "sing-box 1.12.0 中的更改"
610

711
:material-plus: [tls_fragment](#tls_fragment)
@@ -38,6 +42,10 @@ icon: material/new-box
3842

3943
### reject
4044

45+
!!! quote "sing-box 1.13.0 中的更改"
46+
47+
自 sing-box 1.13.0 起,您可以通过 `reject` 动作拒绝(或直接回复)ICMP 回显(ping)请求。
48+
4149
```json
4250
{
4351
"action": "reject",
@@ -54,9 +62,17 @@ icon: material/new-box
5462

5563
#### method
5664

65+
对于 TCP 和 UDP 连接:
66+
5767
- `default`: 对于 TCP 连接回复 RST,对于 UDP 包回复 ICMP 端口不可达。
5868
- `drop`: 丢弃数据包。
5969

70+
对于 ICMP 回显请求:
71+
72+
- `default`: 回复 ICMP 主机不可达。
73+
- `drop`: 丢弃数据包。
74+
- `reply`: 回复以 ICMP 回显应答。
75+
6076
#### no_drop
6177

6278
如果未启用,则 30 秒内触发 50 次后,`method` 将被暂时覆盖为 `drop`

go.mod

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,18 @@ require (
2525
github.com/sagernet/cors v1.2.1
2626
github.com/sagernet/fswatch v0.1.1
2727
github.com/sagernet/gomobile v0.1.8
28-
github.com/sagernet/gvisor v0.0.0-20250325023245-7a9c0f5725fb
28+
github.com/sagernet/gvisor v0.0.0-20250822052253-5558536cf237
2929
github.com/sagernet/quic-go v0.52.0-beta.1
30-
github.com/sagernet/sing v0.7.7
30+
github.com/sagernet/sing v0.7.8-0.20250906004629-421beb6473ea
3131
github.com/sagernet/sing-mux v0.3.3
3232
github.com/sagernet/sing-quic v0.5.1
3333
github.com/sagernet/sing-shadowsocks v0.2.8
3434
github.com/sagernet/sing-shadowsocks2 v0.2.1
3535
github.com/sagernet/sing-shadowtls v0.2.1-0.20250503051639-fcd445d33c11
36-
github.com/sagernet/sing-tun v0.7.0
36+
github.com/sagernet/sing-tun v0.8.0-beta.1
3737
github.com/sagernet/sing-vmess v0.2.7
3838
github.com/sagernet/smux v1.5.34-mod.2
39-
github.com/sagernet/tailscale v1.80.3-sing-box-1.12-mod.1
39+
github.com/sagernet/tailscale v1.80.3-sing-box-1.13-mod.1
4040
github.com/sagernet/wireguard-go v0.0.1-beta.7
4141
github.com/sagernet/ws v0.0.0-20231204124109-acfe8907c854
4242
github.com/spf13/cobra v1.9.1

go.sum

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -158,17 +158,17 @@ github.com/sagernet/fswatch v0.1.1 h1:YqID+93B7VRfqIH3PArW/XpJv5H4OLEVWDfProGoRQ
158158
github.com/sagernet/fswatch v0.1.1/go.mod h1:nz85laH0mkQqJfaOrqPpkwtU1znMFNVTpT/5oRsVz/o=
159159
github.com/sagernet/gomobile v0.1.8 h1:vXgoN0pjsMONAaYCTdsKBX2T1kxuS7sbT/mZ7PElGoo=
160160
github.com/sagernet/gomobile v0.1.8/go.mod h1:A8l3FlHi2D/+mfcd4HHvk5DGFPW/ShFb9jHP5VmSiDY=
161-
github.com/sagernet/gvisor v0.0.0-20250325023245-7a9c0f5725fb h1:pprQtDqNgqXkRsXn+0E8ikKOemzmum8bODjSfDene38=
162-
github.com/sagernet/gvisor v0.0.0-20250325023245-7a9c0f5725fb/go.mod h1:QkkPEJLw59/tfxgapHta14UL5qMUah5NXhO0Kw2Kan4=
161+
github.com/sagernet/gvisor v0.0.0-20250822052253-5558536cf237 h1:SUPFNB+vSP4RBPrSEgNII+HkfqC8hKMpYLodom4o4EU=
162+
github.com/sagernet/gvisor v0.0.0-20250822052253-5558536cf237/go.mod h1:QkkPEJLw59/tfxgapHta14UL5qMUah5NXhO0Kw2Kan4=
163163
github.com/sagernet/netlink v0.0.0-20240612041022-b9a21c07ac6a h1:ObwtHN2VpqE0ZNjr6sGeT00J8uU7JF4cNUdb44/Duis=
164164
github.com/sagernet/netlink v0.0.0-20240612041022-b9a21c07ac6a/go.mod h1:xLnfdiJbSp8rNqYEdIW/6eDO4mVoogml14Bh2hSiFpM=
165165
github.com/sagernet/nftables v0.3.0-beta.4 h1:kbULlAwAC3jvdGAC1P5Fa3GSxVwQJibNenDW2zaXr8I=
166166
github.com/sagernet/nftables v0.3.0-beta.4/go.mod h1:OQXAjvjNGGFxaTgVCSTRIhYB5/llyVDeapVoENYBDS8=
167167
github.com/sagernet/quic-go v0.52.0-beta.1 h1:hWkojLg64zjV+MJOvJU/kOeWndm3tiEfBLx5foisszs=
168168
github.com/sagernet/quic-go v0.52.0-beta.1/go.mod h1:OV+V5kEBb8kJS7k29MzDu6oj9GyMc7HA07sE1tedxz4=
169169
github.com/sagernet/sing v0.6.9/go.mod h1:ARkL0gM13/Iv5VCZmci/NuoOlePoIsW0m7BWfln/Hak=
170-
github.com/sagernet/sing v0.7.7 h1:o46FzVZS+wKbBMEkMEdEHoVZxyM9jvfRpKXc7pEgS/c=
171-
github.com/sagernet/sing v0.7.7/go.mod h1:ARkL0gM13/Iv5VCZmci/NuoOlePoIsW0m7BWfln/Hak=
170+
github.com/sagernet/sing v0.7.8-0.20250906004629-421beb6473ea h1:CDRl4q5Y2dM6MQE1MwukhrxbObfK/rj0QtK7vnJhST0=
171+
github.com/sagernet/sing v0.7.8-0.20250906004629-421beb6473ea/go.mod h1:ARkL0gM13/Iv5VCZmci/NuoOlePoIsW0m7BWfln/Hak=
172172
github.com/sagernet/sing-mux v0.3.3 h1:YFgt9plMWzH994BMZLmyKL37PdIVaIilwP0Jg+EcLfw=
173173
github.com/sagernet/sing-mux v0.3.3/go.mod h1:pht8iFY4c9Xltj7rhVd208npkNaeCxzyXCgulDPLUDA=
174174
github.com/sagernet/sing-quic v0.5.1 h1:o+mX/schfy6fbbU2rnb6ouUYOL+iUBjA4jOZqyIvDsU=
@@ -179,14 +179,14 @@ github.com/sagernet/sing-shadowsocks2 v0.2.1 h1:dWV9OXCeFPuYGHb6IRqlSptVnSzOelnq
179179
github.com/sagernet/sing-shadowsocks2 v0.2.1/go.mod h1:RnXS0lExcDAovvDeniJ4IKa2IuChrdipolPYWBv9hWQ=
180180
github.com/sagernet/sing-shadowtls v0.2.1-0.20250503051639-fcd445d33c11 h1:tK+75l64tm9WvEFrYRE1t0YxoFdWQqw/h7Uhzj0vJ+w=
181181
github.com/sagernet/sing-shadowtls v0.2.1-0.20250503051639-fcd445d33c11/go.mod h1:sWqKnGlMipCHaGsw1sTTlimyUpgzP4WP3pjhCsYt9oA=
182-
github.com/sagernet/sing-tun v0.7.0 h1:zda+KYbxVyeWKdE9k73Ax02jg7cmPZ7/4ZTVCloFBYo=
183-
github.com/sagernet/sing-tun v0.7.0/go.mod h1:pUEjh9YHQ2gJT6Lk0TYDklh3WJy7lz+848vleGM3JPM=
182+
github.com/sagernet/sing-tun v0.8.0-beta.1 h1:k8DOTDMBBc42sUW0C91MBMOsqE5jAWtB0kmJq9TTBvU=
183+
github.com/sagernet/sing-tun v0.8.0-beta.1/go.mod h1:LokZYuEV3crByjQc/XRohLgfNvybtXdx5qe/I4W6S7k=
184184
github.com/sagernet/sing-vmess v0.2.7 h1:2ee+9kO0xW5P4mfe6TYVWf9VtY8k1JhNysBqsiYj0sk=
185185
github.com/sagernet/sing-vmess v0.2.7/go.mod h1:5aYoOtYksAyS0NXDm0qKeTYW1yoE1bJVcv+XLcVoyJs=
186186
github.com/sagernet/smux v1.5.34-mod.2 h1:gkmBjIjlJ2zQKpLigOkFur5kBKdV6bNRoFu2WkltRQ4=
187187
github.com/sagernet/smux v1.5.34-mod.2/go.mod h1:0KW0+R+ycvA2INW4gbsd7BNyg+HEfLIAxa5N02/28Zc=
188-
github.com/sagernet/tailscale v1.80.3-sing-box-1.12-mod.1 h1:gMC0q+0VvZBotZMZ9G0R8ZMEIT/Q6KnXbw0/OgMjmdk=
189-
github.com/sagernet/tailscale v1.80.3-sing-box-1.12-mod.1/go.mod h1:EBxXsWu4OH2ELbQLq32WoBeIubG8KgDrg4/Oaxjs6lI=
188+
github.com/sagernet/tailscale v1.80.3-sing-box-1.13-mod.1 h1:cWM1iPwqIE1t06ft80wpvFB4xbhOpIFI+TFnTw2gnbs=
189+
github.com/sagernet/tailscale v1.80.3-sing-box-1.13-mod.1/go.mod h1:EBxXsWu4OH2ELbQLq32WoBeIubG8KgDrg4/Oaxjs6lI=
190190
github.com/sagernet/wireguard-go v0.0.1-beta.7 h1:ltgBwYHfr+9Wz1eG59NiWnHrYEkDKHG7otNZvu85DXI=
191191
github.com/sagernet/wireguard-go v0.0.1-beta.7/go.mod h1:jGXij2Gn2wbrWuYNUmmNhf1dwcZtvyAvQoe8Xd8MbUo=
192192
github.com/sagernet/ws v0.0.0-20231204124109-acfe8907c854 h1:6uUiZcDRnZSAegryaUGwPC/Fj13JSHwiTftrXhMmYOc=

0 commit comments

Comments
 (0)