Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions transport/internet/httpupgrade/hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ import (
type server struct {
config *Config
addConn internet.ConnHandler
innnerListener net.Listener
innerListener net.Listener
}

func (s *server) Close() error {
return s.innnerListener.Close()
return s.innerListener.Close()
}

func (s *server) Addr() net.Addr {
Expand All @@ -37,6 +37,9 @@ func (s *server) Handle(conn net.Conn) (stat.Connection, error) {
return nil, err
}

// Discard any remaining data in the buffer
connReader.Discard(connReader.Buffered())

if s.config != nil {
host := req.Host
if len(s.config.Host) > 0 && !internet.IsValidHTTPHost(host, s.config.Host) {
Expand Down Expand Up @@ -84,16 +87,19 @@ func (s *server) Handle(conn net.Conn) (stat.Connection, error) {

func (s *server) keepAccepting() {
for {
conn, err := s.innnerListener.Accept()
conn, err := s.innerListener.Accept()
if err != nil {
return
}
handledConn, err := s.Handle(conn)
if err != nil {
errors.LogInfoInner(context.Background(), err, "failed to handle request")
continue
}
s.addConn(handledConn)
// Handle the connection in a separate goroutine to avoid deadlock
go func(c net.Conn) {
handledConn, err := s.Handle(c)
if err != nil {
errors.LogInfoInner(context.Background(), err, "failed to handle request")
return
}
s.addConn(handledConn)
}(conn)
}
}

Expand Down Expand Up @@ -140,7 +146,7 @@ func ListenHTTPUpgrade(ctx context.Context, address net.Address, port net.Port,
serverInstance := &server{
config: transportConfiguration,
addConn: addConn,
innnerListener: listener,
innerListener: listener,
}
go serverInstance.keepAccepting()
return serverInstance, nil
Expand Down
17 changes: 13 additions & 4 deletions transport/internet/splithttp/hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,11 @@ type httpSession struct {

func (h *requestHandler) maybeReapSession(isFullyConnected *done.Instance, sessionId string) {
shouldReap := done.New()
go func() {
time.Sleep(30 * time.Second)
shouldReap.Close()
}()
time.AfterFunc(30*time.Second, func() {
if !isFullyConnected.Done() {
h.sessions.Delete(sessionId)
}
})

select {
case <-isFullyConnected.Wait():
Expand Down Expand Up @@ -278,6 +279,13 @@ func ListenSH(ctx context.Context, address net.Address, port net.Port, streamSet
return nil, errors.New("failed to listen unix domain socket(for SH) on ", address).Base(err)
}
errors.LogInfo(ctx, "listening unix domain socket(for SH) on ", address)

// Create a dummy HTTP server for UDS
l.server = http.Server{
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Do nothing, as this server is not actually listening
}),
}
} else if l.isH3 { // quic
Conn, err := internet.ListenSystemPacket(context.Background(), &net.UDPAddr{
IP: address.IP(),
Expand Down Expand Up @@ -326,6 +334,7 @@ func ListenSH(ctx context.Context, address net.Address, port net.Port, streamSet

// tcp/unix (h1/h2)
if listener != nil {
defer listener.Close() // Close the listener even if an error occurs
if config := v2tls.ConfigFromStreamSettings(streamSettings); config != nil {
if tlsConfig := config.GetTLSConfig(); tlsConfig != nil {
listener = tls.NewListener(listener, tlsConfig)
Expand Down