Skip to content

Commit

Permalink
Fix missing UDP timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
nekohasekai committed Dec 20, 2023
1 parent 499e257 commit 9525cab
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 3 deletions.
8 changes: 7 additions & 1 deletion hysteria/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ import (
"os"
"runtime"
"sync"
"time"

"github.com/sagernet/quic-go"
"github.com/sagernet/sing-quic"
hyCC "github.com/sagernet/sing-quic/hysteria/congestion"
"github.com/sagernet/sing/common"
"github.com/sagernet/sing/common/auth"
"github.com/sagernet/sing/common/baderror"
"github.com/sagernet/sing/common/canceler"
E "github.com/sagernet/sing/common/exceptions"
"github.com/sagernet/sing/common/logger"
M "github.com/sagernet/sing/common/metadata"
Expand All @@ -32,6 +34,7 @@ type ServiceOptions struct {
XPlusPassword string
TLSConfig aTLS.ServerConfig
UDPDisabled bool
UDPTimeout time.Duration
Handler ServerHandler

// Legacy options
Expand All @@ -58,6 +61,7 @@ type Service[U comparable] struct {
quicConfig *quic.Config
userMap map[string]U
udpDisabled bool
udpTimeout time.Duration
handler ServerHandler
quicListener io.Closer
}
Expand Down Expand Up @@ -109,6 +113,7 @@ func NewService[U comparable](options ServiceOptions) (*Service[U], error) {
userMap: make(map[string]U),
handler: options.Handler,
udpDisabled: options.UDPDisabled,
udpTimeout: options.UDPTimeout,
}, nil
}

Expand Down Expand Up @@ -272,7 +277,8 @@ func (s *serverSession[U]) handleStream(stream quic.Stream) error {
udpConn.closeWithError(E.Cause(err, "write server response"))
return err
}
go s.handler.NewPacketConnection(udpConn.ctx, udpConn, M.Metadata{
newCtx, newConn := canceler.NewPacketConn(udpConn.ctx, udpConn, s.udpTimeout)
go s.handler.NewPacketConnection(newCtx, newConn, M.Metadata{
Source: s.source,
Destination: M.ParseSocksaddrHostPort(request.Host, request.Port),
})
Expand Down
3 changes: 3 additions & 0 deletions hysteria2/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type ServiceOptions struct {
SalamanderPassword string
TLSConfig aTLS.ServerConfig
UDPDisabled bool
UDPTimeout time.Duration
Handler ServerHandler
MasqueradeHandler http.Handler
}
Expand All @@ -62,6 +63,7 @@ type Service[U comparable] struct {
quicConfig *quic.Config
userMap map[string]U
udpDisabled bool
udpTimeout time.Duration
handler ServerHandler
masqueradeHandler http.Handler
quicListener io.Closer
Expand Down Expand Up @@ -97,6 +99,7 @@ func NewService[U comparable](options ServiceOptions) (*Service[U], error) {
quicConfig: quicConfig,
userMap: make(map[string]U),
udpDisabled: options.UDPDisabled,
udpTimeout: options.UDPTimeout,
handler: options.Handler,
masqueradeHandler: options.MasqueradeHandler,
}, nil
Expand Down
4 changes: 3 additions & 1 deletion hysteria2/service_packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package hysteria2
import (
"github.com/sagernet/sing/common"
"github.com/sagernet/sing/common/auth"
"github.com/sagernet/sing/common/canceler"
E "github.com/sagernet/sing/common/exceptions"
M "github.com/sagernet/sing/common/metadata"
)
Expand Down Expand Up @@ -47,7 +48,8 @@ func (s *serverSession[U]) handleUDPMessage(message *udpMessage) {
s.udpAccess.Lock()
s.udpConnMap[message.sessionID] = udpConn
s.udpAccess.Unlock()
go s.handler.NewPacketConnection(udpConn.ctx, udpConn, M.Metadata{
newCtx, newConn := canceler.NewPacketConn(udpConn.ctx, udpConn, s.udpTimeout)
go s.handler.NewPacketConnection(newCtx, newConn, M.Metadata{
Source: s.source,
Destination: M.ParseSocksaddr(message.destination),
})
Expand Down
3 changes: 3 additions & 0 deletions tuic/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type ServiceOptions struct {
AuthTimeout time.Duration
ZeroRTTHandshake bool
Heartbeat time.Duration
UDPTimeout time.Duration
Handler ServiceHandler
}

Expand All @@ -53,6 +54,7 @@ type Service[U comparable] struct {
passwordMap map[U]string
congestionControl string
authTimeout time.Duration
udpTimeout time.Duration
handler ServiceHandler

quicListener io.Closer
Expand Down Expand Up @@ -88,6 +90,7 @@ func NewService[U comparable](options ServiceOptions) (*Service[U], error) {
userMap: make(map[[16]byte]U),
congestionControl: options.CongestionControl,
authTimeout: options.AuthTimeout,
udpTimeout: options.UDPTimeout,
handler: options.Handler,
}, nil
}
Expand Down
4 changes: 3 additions & 1 deletion tuic/service_packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tuic
import (
"github.com/sagernet/sing/common"
"github.com/sagernet/sing/common/auth"
"github.com/sagernet/sing/common/canceler"
E "github.com/sagernet/sing/common/exceptions"
M "github.com/sagernet/sing/common/metadata"
)
Expand Down Expand Up @@ -65,7 +66,8 @@ func (s *serverSession[U]) handleUDPMessage(message *udpMessage, udpStream bool)
s.udpAccess.Lock()
s.udpConnMap[message.sessionID] = udpConn
s.udpAccess.Unlock()
go s.handler.NewPacketConnection(udpConn.ctx, udpConn, M.Metadata{
newCtx, newConn := canceler.NewPacketConn(udpConn.ctx, udpConn, s.udpTimeout)
go s.handler.NewPacketConnection(newCtx, newConn, M.Metadata{
Source: s.source,
Destination: message.destination,
})
Expand Down

0 comments on commit 9525cab

Please sign in to comment.