Refactor TrustTunnel

This commit is contained in:
Shtorm
2026-06-04 10:03:20 +03:00
parent e363c2ff78
commit 9ff7a84afe
7 changed files with 105 additions and 209 deletions

View File

@@ -1,22 +1,15 @@
package trusttunnel
import (
"context"
"errors"
"net"
"time"
"github.com/sagernet/quic-go"
"github.com/sagernet/quic-go/congestion"
"github.com/sagernet/quic-go/http3"
"github.com/sagernet/sing-box/common/tls"
E "github.com/sagernet/sing/common/exceptions"
qtls "github.com/sagernet/sing-quic"
"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"
"github.com/sagernet/sing/common/ntp"
E "github.com/sagernet/sing/common/exceptions"
)
func NewCongestionControl(name string, cwnd int, bbrProfile string, timeFunc func() time.Time) (func(conn *quic.Conn) congestion.CongestionControl, error) {
@@ -82,62 +75,3 @@ func NewCongestionControl(name string, cwnd int, bbrProfile string, timeFunc fun
return nil, E.New("unknown congestion control: ", name)
}
}
type QUICService struct {
service *Service
h3Server *http3.Server
udpConn net.PacketConn
congestionControl string
cwnd int
bbrProfile string
}
func NewQUICService(service *Service, congestionControl string, cwnd int, bbrProfile string) *QUICService {
return &QUICService{
service: service,
congestionControl: congestionControl,
cwnd: cwnd,
bbrProfile: bbrProfile,
}
}
func (s *QUICService) Start(ctx context.Context, udpConn net.PacketConn, tlsConfig tls.ServerConfig) error {
s.udpConn = udpConn
congestionControlFactory, err := NewCongestionControl(s.congestionControl, s.cwnd, s.bbrProfile, ntp.TimeFuncFromContext(ctx))
if err != nil {
return err
}
s.h3Server = &http3.Server{
Handler: s.service,
ConnContext: func(ctx context.Context, conn *quic.Conn) context.Context {
conn.SetCongestionControl(congestionControlFactory(conn))
return ctx
},
}
if err := qtls.ConfigureHTTP3(tlsConfig); err != nil {
return err
}
quicListener, err := qtls.ListenEarly(udpConn, tlsConfig, &quic.Config{
MaxIdleTimeout: DefaultSessionTimeout * 2,
MaxIncomingStreams: 1 << 60,
Allow0RTT: true,
})
if err != nil {
return err
}
go func() {
_ = s.h3Server.ServeListener(quicListener)
}()
return nil
}
func (s *QUICService) Close() error {
var errs []error
if s.h3Server != nil {
errs = append(errs, s.h3Server.Close())
}
if s.udpConn != nil {
errs = append(errs, s.udpConn.Close())
}
return errors.Join(errs...)
}