Files
sing-box-extended-mirror/protocol/naive/quic/inbound_init.go
T

133 lines
4.2 KiB
Go
Raw Normal View History

2024-11-02 00:39:02 +08:00
package quic
import (
2025-12-18 17:08:36 +08:00
"context"
2024-11-02 00:39:02 +08:00
"io"
"net/http"
2025-12-21 23:01:42 +08:00
"time"
2024-11-02 00:39:02 +08:00
"github.com/sagernet/quic-go"
2025-12-18 17:08:36 +08:00
"github.com/sagernet/quic-go/congestion"
2024-11-02 00:39:02 +08:00
"github.com/sagernet/quic-go/http3"
"github.com/sagernet/sing-box/common/listener"
"github.com/sagernet/sing-box/common/tls"
2025-12-21 23:01:42 +08:00
"github.com/sagernet/sing-box/log"
2025-12-18 17:08:36 +08:00
"github.com/sagernet/sing-box/option"
2024-11-02 00:39:02 +08:00
"github.com/sagernet/sing-box/protocol/naive"
"github.com/sagernet/sing-quic"
2025-12-18 17:08:36 +08:00
"github.com/sagernet/sing-quic/congestion_bbr1"
"github.com/sagernet/sing-quic/congestion_bbr2"
congestion_meta1 "github.com/sagernet/sing-quic/congestion_meta1"
congestion_meta2 "github.com/sagernet/sing-quic/congestion_meta2"
2026-06-19 20:57:18 +08:00
"github.com/sagernet/sing/common"
2024-11-02 00:39:02 +08:00
E "github.com/sagernet/sing/common/exceptions"
"github.com/sagernet/sing/common/logger"
2025-12-18 17:08:36 +08:00
"github.com/sagernet/sing/common/ntp"
2024-11-02 00:39:02 +08:00
)
func init() {
2025-12-18 17:08:36 +08:00
naive.ConfigureHTTP3ListenerFunc = func(ctx context.Context, logger logger.Logger, listener *listener.Listener, handler http.Handler, tlsConfig tls.ServerConfig, options option.NaiveInboundOptions) (io.Closer, error) {
2024-11-02 00:39:02 +08:00
err := qtls.ConfigureHTTP3(tlsConfig)
if err != nil {
return nil, err
}
2026-06-19 20:57:18 +08:00
if !common.Contains(tlsConfig.NextProtos(), http3.NextProtoH3) {
tlsConfig.SetNextProtos(append(append([]string{}, tlsConfig.NextProtos()...), http3.NextProtoH3))
}
2024-11-02 00:39:02 +08:00
udpConn, err := listener.ListenUDP()
if err != nil {
return nil, err
}
2025-12-18 17:08:36 +08:00
var congestionControl func(conn *quic.Conn) congestion.CongestionControl
2025-12-21 23:01:42 +08:00
timeFunc := ntp.TimeFuncFromContext(ctx)
if timeFunc == nil {
timeFunc = time.Now
}
2025-12-18 17:08:36 +08:00
switch options.QUICCongestionControl {
case "", "bbr":
congestionControl = func(conn *quic.Conn) congestion.CongestionControl {
return congestion_meta2.NewBbrSender(
2025-12-21 23:01:42 +08:00
congestion_meta2.DefaultClock{TimeFunc: timeFunc},
2025-12-18 17:08:36 +08:00
congestion.ByteCount(conn.Config().InitialPacketSize),
congestion.ByteCount(congestion_meta1.InitialCongestionWindow),
)
}
case "bbr_standard":
congestionControl = func(conn *quic.Conn) congestion.CongestionControl {
return congestion_bbr1.NewBbrSender(
2025-12-21 23:01:42 +08:00
congestion_bbr1.DefaultClock{TimeFunc: timeFunc},
2025-12-18 17:08:36 +08:00
congestion.ByteCount(conn.Config().InitialPacketSize),
congestion_bbr1.InitialCongestionWindowPackets,
congestion_bbr1.MaxCongestionWindowPackets,
)
}
case "bbr2":
congestionControl = func(conn *quic.Conn) congestion.CongestionControl {
return congestion_bbr2.NewBBR2Sender(
2025-12-21 23:01:42 +08:00
congestion_bbr2.DefaultClock{TimeFunc: timeFunc},
2025-12-18 17:08:36 +08:00
congestion.ByteCount(conn.Config().InitialPacketSize),
0,
false,
)
}
case "bbr2_variant":
congestionControl = func(conn *quic.Conn) congestion.CongestionControl {
return congestion_bbr2.NewBBR2Sender(
2025-12-21 23:01:42 +08:00
congestion_bbr2.DefaultClock{TimeFunc: timeFunc},
2025-12-18 17:08:36 +08:00
congestion.ByteCount(conn.Config().InitialPacketSize),
32*congestion.ByteCount(conn.Config().InitialPacketSize),
true,
)
}
case "cubic":
congestionControl = func(conn *quic.Conn) congestion.CongestionControl {
return congestion_meta1.NewCubicSender(
2025-12-21 23:01:42 +08:00
congestion_meta1.DefaultClock{TimeFunc: timeFunc},
2025-12-18 17:08:36 +08:00
congestion.ByteCount(conn.Config().InitialPacketSize),
false,
)
}
case "reno":
congestionControl = func(conn *quic.Conn) congestion.CongestionControl {
return congestion_meta1.NewCubicSender(
2025-12-21 23:01:42 +08:00
congestion_meta1.DefaultClock{TimeFunc: timeFunc},
2025-12-18 17:08:36 +08:00
congestion.ByteCount(conn.Config().InitialPacketSize),
true,
)
}
default:
return nil, E.New("unknown quic congestion control: ", options.QUICCongestionControl)
}
2024-11-02 00:39:02 +08:00
quicListener, err := qtls.ListenEarly(udpConn, tlsConfig, &quic.Config{
2025-12-21 23:01:42 +08:00
MaxIncomingStreams: 1 << 60,
Allow0RTT: true,
2024-11-02 00:39:02 +08:00
})
if err != nil {
udpConn.Close()
return nil, err
}
h3Server := &http3.Server{
Handler: handler,
2025-12-21 23:01:42 +08:00
ConnContext: func(ctx context.Context, conn *quic.Conn) context.Context {
conn.SetCongestionControl(congestionControl(conn))
return log.ContextWithNewID(ctx)
},
2024-11-02 00:39:02 +08:00
}
go func() {
sErr := h3Server.ServeListener(quicListener)
udpConn.Close()
if sErr != nil && !E.IsClosedOrCanceled(sErr) {
logger.Error("http3 server closed: ", sErr)
}
}()
return quicListener, nil
}
2026-03-30 22:27:12 +08:00
naive.WrapError = qtls.WrapError
2024-11-02 00:39:02 +08:00
}