github.com/sagernet/sing-box@v1.9.0-rc.20/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/sagernet/cloudflare-tls"
    11  	"github.com/sagernet/quic-go/ech"
    12  	"github.com/sagernet/quic-go/http3_ech"
    13  	"github.com/sagernet/sing-quic"
    14  	M "github.com/sagernet/sing/common/metadata"
    15  )
    16  
    17  var (
    18  	_ qtls.Config       = (*echClientConfig)(nil)
    19  	_ qtls.ServerConfig = (*echServerConfig)(nil)
    20  )
    21  
    22  func (c *echClientConfig) Dial(ctx context.Context, conn net.PacketConn, addr net.Addr, config *quic.Config) (quic.Connection, error) {
    23  	return quic.Dial(ctx, conn, addr, c.config, config)
    24  }
    25  
    26  func (c *echClientConfig) DialEarly(ctx context.Context, conn net.PacketConn, addr net.Addr, config *quic.Config) (quic.EarlyConnection, error) {
    27  	return quic.DialEarly(ctx, conn, addr, c.config, config)
    28  }
    29  
    30  func (c *echClientConfig) CreateTransport(conn net.PacketConn, quicConnPtr *quic.EarlyConnection, serverAddr M.Socksaddr, quicConfig *quic.Config) http.RoundTripper {
    31  	return &http3.RoundTripper{
    32  		TLSClientConfig: c.config,
    33  		QUICConfig:      quicConfig,
    34  		Dial: func(ctx context.Context, addr string, tlsCfg *tls.Config, cfg *quic.Config) (quic.EarlyConnection, error) {
    35  			quicConn, err := quic.DialEarly(ctx, conn, serverAddr.UDPAddr(), tlsCfg, cfg)
    36  			if err != nil {
    37  				return nil, err
    38  			}
    39  			*quicConnPtr = quicConn
    40  			return quicConn, nil
    41  		},
    42  	}
    43  }
    44  
    45  func (c *echServerConfig) Listen(conn net.PacketConn, config *quic.Config) (qtls.Listener, error) {
    46  	return quic.Listen(conn, c.config, config)
    47  }
    48  
    49  func (c *echServerConfig) ListenEarly(conn net.PacketConn, config *quic.Config) (qtls.EarlyListener, error) {
    50  	return quic.ListenEarly(conn, c.config, config)
    51  }
    52  
    53  func (c *echServerConfig) ConfigureHTTP3() {
    54  	http3.ConfigureTLSConfig(c.config)
    55  }