Skip to content

Commit 9525cab

Browse files
committed
Fix missing UDP timeout
1 parent 499e257 commit 9525cab

File tree

5 files changed

+19
-3
lines changed

5 files changed

+19
-3
lines changed

hysteria/service.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ import (
99
"os"
1010
"runtime"
1111
"sync"
12+
"time"
1213

1314
"github.com/sagernet/quic-go"
1415
"github.com/sagernet/sing-quic"
1516
hyCC "github.com/sagernet/sing-quic/hysteria/congestion"
1617
"github.com/sagernet/sing/common"
1718
"github.com/sagernet/sing/common/auth"
1819
"github.com/sagernet/sing/common/baderror"
20+
"github.com/sagernet/sing/common/canceler"
1921
E "github.com/sagernet/sing/common/exceptions"
2022
"github.com/sagernet/sing/common/logger"
2123
M "github.com/sagernet/sing/common/metadata"
@@ -32,6 +34,7 @@ type ServiceOptions struct {
3234
XPlusPassword string
3335
TLSConfig aTLS.ServerConfig
3436
UDPDisabled bool
37+
UDPTimeout time.Duration
3538
Handler ServerHandler
3639

3740
// Legacy options
@@ -58,6 +61,7 @@ type Service[U comparable] struct {
5861
quicConfig *quic.Config
5962
userMap map[string]U
6063
udpDisabled bool
64+
udpTimeout time.Duration
6165
handler ServerHandler
6266
quicListener io.Closer
6367
}
@@ -109,6 +113,7 @@ func NewService[U comparable](options ServiceOptions) (*Service[U], error) {
109113
userMap: make(map[string]U),
110114
handler: options.Handler,
111115
udpDisabled: options.UDPDisabled,
116+
udpTimeout: options.UDPTimeout,
112117
}, nil
113118
}
114119

@@ -272,7 +277,8 @@ func (s *serverSession[U]) handleStream(stream quic.Stream) error {
272277
udpConn.closeWithError(E.Cause(err, "write server response"))
273278
return err
274279
}
275-
go s.handler.NewPacketConnection(udpConn.ctx, udpConn, M.Metadata{
280+
newCtx, newConn := canceler.NewPacketConn(udpConn.ctx, udpConn, s.udpTimeout)
281+
go s.handler.NewPacketConnection(newCtx, newConn, M.Metadata{
276282
Source: s.source,
277283
Destination: M.ParseSocksaddrHostPort(request.Host, request.Port),
278284
})

hysteria2/service.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ type ServiceOptions struct {
4141
SalamanderPassword string
4242
TLSConfig aTLS.ServerConfig
4343
UDPDisabled bool
44+
UDPTimeout time.Duration
4445
Handler ServerHandler
4546
MasqueradeHandler http.Handler
4647
}
@@ -62,6 +63,7 @@ type Service[U comparable] struct {
6263
quicConfig *quic.Config
6364
userMap map[string]U
6465
udpDisabled bool
66+
udpTimeout time.Duration
6567
handler ServerHandler
6668
masqueradeHandler http.Handler
6769
quicListener io.Closer
@@ -97,6 +99,7 @@ func NewService[U comparable](options ServiceOptions) (*Service[U], error) {
9799
quicConfig: quicConfig,
98100
userMap: make(map[string]U),
99101
udpDisabled: options.UDPDisabled,
102+
udpTimeout: options.UDPTimeout,
100103
handler: options.Handler,
101104
masqueradeHandler: options.MasqueradeHandler,
102105
}, nil

hysteria2/service_packet.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package hysteria2
33
import (
44
"github.com/sagernet/sing/common"
55
"github.com/sagernet/sing/common/auth"
6+
"github.com/sagernet/sing/common/canceler"
67
E "github.com/sagernet/sing/common/exceptions"
78
M "github.com/sagernet/sing/common/metadata"
89
)
@@ -47,7 +48,8 @@ func (s *serverSession[U]) handleUDPMessage(message *udpMessage) {
4748
s.udpAccess.Lock()
4849
s.udpConnMap[message.sessionID] = udpConn
4950
s.udpAccess.Unlock()
50-
go s.handler.NewPacketConnection(udpConn.ctx, udpConn, M.Metadata{
51+
newCtx, newConn := canceler.NewPacketConn(udpConn.ctx, udpConn, s.udpTimeout)
52+
go s.handler.NewPacketConnection(newCtx, newConn, M.Metadata{
5153
Source: s.source,
5254
Destination: M.ParseSocksaddr(message.destination),
5355
})

tuic/service.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ type ServiceOptions struct {
3535
AuthTimeout time.Duration
3636
ZeroRTTHandshake bool
3737
Heartbeat time.Duration
38+
UDPTimeout time.Duration
3839
Handler ServiceHandler
3940
}
4041

@@ -53,6 +54,7 @@ type Service[U comparable] struct {
5354
passwordMap map[U]string
5455
congestionControl string
5556
authTimeout time.Duration
57+
udpTimeout time.Duration
5658
handler ServiceHandler
5759

5860
quicListener io.Closer
@@ -88,6 +90,7 @@ func NewService[U comparable](options ServiceOptions) (*Service[U], error) {
8890
userMap: make(map[[16]byte]U),
8991
congestionControl: options.CongestionControl,
9092
authTimeout: options.AuthTimeout,
93+
udpTimeout: options.UDPTimeout,
9194
handler: options.Handler,
9295
}, nil
9396
}

tuic/service_packet.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package tuic
33
import (
44
"github.com/sagernet/sing/common"
55
"github.com/sagernet/sing/common/auth"
6+
"github.com/sagernet/sing/common/canceler"
67
E "github.com/sagernet/sing/common/exceptions"
78
M "github.com/sagernet/sing/common/metadata"
89
)
@@ -65,7 +66,8 @@ func (s *serverSession[U]) handleUDPMessage(message *udpMessage, udpStream bool)
6566
s.udpAccess.Lock()
6667
s.udpConnMap[message.sessionID] = udpConn
6768
s.udpAccess.Unlock()
68-
go s.handler.NewPacketConnection(udpConn.ctx, udpConn, M.Metadata{
69+
newCtx, newConn := canceler.NewPacketConn(udpConn.ctx, udpConn, s.udpTimeout)
70+
go s.handler.NewPacketConnection(newCtx, newConn, M.Metadata{
6971
Source: s.source,
7072
Destination: message.destination,
7173
})

0 commit comments

Comments
 (0)