github.com/inazumav/sing-box@v0.0.0-20230926072359-ab51429a14f1/common/tls/ech_quic.go (about)

     1  //go:build with_quic && with_ech
     2  
     3  package tls
     4  
     5  import (
     6  	"context"
     7  	"net"
     8  	"net/http"
     9  
    10  	"github.com/inazumav/sing-box/common/qtls"
    11  	M "github.com/sagernet/sing/common/metadata"
    12  )
    13  
    14  var (
    15  	_ qtls.QUICConfig       = (*echClientConfig)(nil)
    16  	_ qtls.QUICServerConfig = (*echServerConfig)(nil)
    17  )
    18  
    19  func (c *echClientConfig) Dial(ctx context.Context, conn net.PacketConn, addr net.Addr, config *quic.Config) (quic.Connection, error) {
    20  	return quic.Dial(ctx, conn, addr, c.config, config)
    21  }
    22  
    23  func (c *echClientConfig) DialEarly(ctx context.Context, conn net.PacketConn, addr net.Addr, config *quic.Config) (quic.EarlyConnection, error) {
    24  	return quic.DialEarly(ctx, conn, addr, c.config, config)
    25  }
    26  
    27  func (c *echClientConfig) CreateTransport(conn net.PacketConn, quicConnPtr *quic.EarlyConnection, serverAddr M.Socksaddr, quicConfig *quic.Config, enableDatagrams bool) http.RoundTripper {
    28  	return &http3.RoundTripper{
    29  		TLSClientConfig: c.config,
    30  		QuicConfig:      quicConfig,
    31  		EnableDatagrams: enableDatagrams,
    32  		Dial: func(ctx context.Context, addr string, tlsCfg *tls.Config, cfg *quic.Config) (quic.EarlyConnection, error) {
    33  			quicConn, err := quic.DialEarly(ctx, conn, serverAddr.UDPAddr(), tlsCfg, cfg)
    34  			if err != nil {
    35  				return nil, err
    36  			}
    37  			*quicConnPtr = quicConn
    38  			return quicConn, nil
    39  		},
    40  	}
    41  }
    42  
    43  func (c *echServerConfig) Listen(conn net.PacketConn, config *quic.Config) (qtls.QUICListener, error) {
    44  	return quic.Listen(conn, c.config, config)
    45  }
    46  
    47  func (c *echServerConfig) ListenEarly(conn net.PacketConn, config *quic.Config) (qtls.QUICEarlyListener, error) {
    48  	return quic.ListenEarly(conn, c.config, config)
    49  }
    50  
    51  func (c *echServerConfig) ConfigureHTTP3() {
    52  	http3.ConfigureTLSConfig(c.config)
    53  }