Skip to content

Commit

Permalink
Add UDP timeout route option
Browse files Browse the repository at this point in the history
  • Loading branch information
nekohasekai committed Nov 24, 2024
1 parent 9c35bc1 commit 23123a3
Show file tree
Hide file tree
Showing 13 changed files with 143 additions and 223 deletions.
10 changes: 6 additions & 4 deletions adapter/inbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,12 @@ type InboundContext struct {
InboundOptions option.InboundOptions
UDPDisableDomainUnmapping bool
UDPConnect bool
NetworkStrategy C.NetworkStrategy
NetworkType []C.InterfaceType
FallbackNetworkType []C.InterfaceType
FallbackDelay time.Duration
UDPTimeout time.Duration

NetworkStrategy C.NetworkStrategy
NetworkType []C.InterfaceType
FallbackNetworkType []C.InterfaceType
FallbackDelay time.Duration

DNSServer string

Expand Down
157 changes: 0 additions & 157 deletions adapter/outbound/default.go

This file was deleted.

1 change: 1 addition & 0 deletions constant/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const (
ProtocolDTLS = "dtls"
ProtocolSSH = "ssh"
ProtocolRDP = "rdp"
ProtocolNTP = "ntp"
)

const (
Expand Down
17 changes: 15 additions & 2 deletions constant/timeout.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ const (
TCPTimeout = 15 * time.Second
ReadPayloadTimeout = 300 * time.Millisecond
DNSTimeout = 10 * time.Second
QUICTimeout = 30 * time.Second
STUNTimeout = 15 * time.Second
UDPTimeout = 5 * time.Minute
DefaultURLTestInterval = 3 * time.Minute
DefaultURLTestIdleTimeout = 30 * time.Minute
Expand All @@ -19,3 +17,18 @@ const (
FatalStopTimeout = 10 * time.Second
FakeIPMetadataSaveInterval = 10 * time.Second
)

var PortProtocols = map[uint16]string{
53: ProtocolDNS,
123: ProtocolNTP,
3478: ProtocolSTUN,
443: ProtocolQUIC,
}

var ProtocolTimeouts = map[string]time.Duration{
ProtocolDNS: 10 * time.Second,
ProtocolNTP: 10 * time.Second,
ProtocolSTUN: 10 * time.Second,
ProtocolQUIC: 30 * time.Second,
ProtocolDTLS: 30 * time.Second,
}
25 changes: 24 additions & 1 deletion docs/configuration/route/rule_action.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ See `route-options` fields below.
"network_strategy": "",
"fallback_delay": "",
"udp_disable_domain_unmapping": false,
"udp_connect": false
"udp_connect": false,
"udp_timeout": ""
}
```

Expand Down Expand Up @@ -86,6 +87,28 @@ do not support receiving UDP packets with domain addresses, such as Surge.

If enabled, attempts to connect UDP connection to the destination instead of listen.

#### udp_timeout

Timeout for UDP connections.

Setting a larger value than the UDP timeout in inbounds will have no effect.

Default value for protocol sniffed connections:

| Timeout | Protocol |
|---------|----------------------|
| `10s` | `dns`, `ntp`, `stun` |
| `30s` | `quic`, `dtls` |

If no protocol is sniffed, the following ports will be recognized as protocols by default:

| Port | Protocol |
|------|----------|
| 53 | `dns` |
| 123 | `ntp` |
| 443 | `quic` |
| 3478 | `stun` |

### reject

```json
Expand Down
25 changes: 24 additions & 1 deletion docs/configuration/route/rule_action.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ icon: material/new-box
"network_strategy": "",
"fallback_delay": "",
"udp_disable_domain_unmapping": false,
"udp_connect": false
"udp_connect": false,
"udp_timeout": ""
}
```

Expand Down Expand Up @@ -84,6 +85,28 @@ icon: material/new-box

如果启用,将尝试将 UDP 连接 connect 到目标而不是 listen。

#### udp_timeout

UDP 连接超时时间。

设置比入站 UDP 超时更大的值将无效。

已探测协议连接的默认值:

| 超时 | 协议 |
|-------|----------------------|
| `10s` | `dns`, `ntp`, `stun` |
| `30s` | `quic`, `dtls` |

如果没有探测到协议,以下端口将默认识别为协议:

| 端口 | 协议 |
|------|--------|
| 53 | `dns` |
| 123 | `ntp` |
| 443 | `quic` |
| 3478 | `stun` |

### reject

```json
Expand Down
5 changes: 3 additions & 2 deletions option/rule_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,9 @@ type RawRouteOptionsActionOptions struct {
NetworkStrategy NetworkStrategy `json:"network_strategy,omitempty"`
FallbackDelay uint32 `json:"fallback_delay,omitempty"`

UDPDisableDomainUnmapping bool `json:"udp_disable_domain_unmapping,omitempty"`
UDPConnect bool `json:"udp_connect,omitempty"`
UDPDisableDomainUnmapping bool `json:"udp_disable_domain_unmapping,omitempty"`
UDPConnect bool `json:"udp_connect,omitempty"`
UDPTimeout badoption.Duration `json:"udp_timeout,omitempty"`
}

type RouteOptionsActionOptions RawRouteOptionsActionOptions
Expand Down
2 changes: 1 addition & 1 deletion option/wireguard.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type WireGuardEndpointOptions struct {
PrivateKey string `json:"private_key"`
ListenPort uint16 `json:"listen_port,omitempty"`
Peers []WireGuardPeer `json:"peers,omitempty"`
UDPTimeout UDPTimeoutCompat `json:"udp_timeout,omitempty"`
UDPTimeout badoption.Duration `json:"udp_timeout,omitempty"`
Workers int `json:"workers,omitempty"`
DialerOptions
}
Expand Down
Loading

0 comments on commit 23123a3

Please sign in to comment.