Add ECH support for QUIC based protocols

This commit is contained in:
世界
2023-08-31 11:37:26 +08:00
parent 533fca9fa3
commit 4c050d7f4b
19 changed files with 385 additions and 93 deletions

View File

@@ -1,8 +1,9 @@
//go:build with_quic
package tuic
import (
"context"
"crypto/tls"
"io"
"net"
"runtime"
@@ -10,6 +11,8 @@ import (
"time"
"github.com/sagernet/quic-go"
"github.com/sagernet/sing-box/common/qtls"
"github.com/sagernet/sing-box/common/tls"
"github.com/sagernet/sing/common"
"github.com/sagernet/sing/common/baderror"
"github.com/sagernet/sing/common/buf"
@@ -25,7 +28,7 @@ type ClientOptions struct {
Context context.Context
Dialer N.Dialer
ServerAddress M.Socksaddr
TLSConfig *tls.Config
TLSConfig tls.Config
UUID uuid.UUID
Password string
CongestionControl string
@@ -38,7 +41,7 @@ type Client struct {
ctx context.Context
dialer N.Dialer
serverAddr M.Socksaddr
tlsConfig *tls.Config
tlsConfig tls.Config
quicConfig *quic.Config
uuid uuid.UUID
password string
@@ -108,9 +111,9 @@ func (c *Client) offerNew(ctx context.Context) (*clientQUICConnection, error) {
}
var quicConn quic.Connection
if c.zeroRTTHandshake {
quicConn, err = quic.DialEarly(ctx, bufio.NewUnbindPacketConn(udpConn), udpConn.RemoteAddr(), c.tlsConfig, c.quicConfig)
quicConn, err = qtls.DialEarly(ctx, bufio.NewUnbindPacketConn(udpConn), udpConn.RemoteAddr(), c.tlsConfig, c.quicConfig)
} else {
quicConn, err = quic.Dial(ctx, bufio.NewUnbindPacketConn(udpConn), udpConn.RemoteAddr(), c.tlsConfig, c.quicConfig)
quicConn, err = qtls.Dial(ctx, bufio.NewUnbindPacketConn(udpConn), udpConn.RemoteAddr(), c.tlsConfig, c.quicConfig)
}
if err != nil {
udpConn.Close()
@@ -141,13 +144,13 @@ func (c *Client) offerNew(ctx context.Context) (*clientQUICConnection, error) {
func (c *Client) clientHandshake(conn quic.Connection) error {
authStream, err := conn.OpenUniStream()
if err != nil {
return err
return E.Cause(err, "open handshake stream")
}
defer authStream.Close()
handshakeState := conn.ConnectionState().TLS
handshakeState := conn.ConnectionState()
tuicAuthToken, err := handshakeState.ExportKeyingMaterial(string(c.uuid[:]), []byte(c.password), 32)
if err != nil {
return err
return E.Cause(err, "export keying material")
}
authRequest := buf.NewSize(AuthenticateLen)
authRequest.WriteByte(Version)

View File

@@ -1,3 +1,5 @@
//go:build with_quic
package tuic
import (

View File

@@ -1,9 +1,10 @@
//go:build with_quic
package tuic
import (
"bytes"
"context"
"crypto/tls"
"encoding/binary"
"io"
"net"
@@ -13,6 +14,8 @@ import (
"time"
"github.com/sagernet/quic-go"
"github.com/sagernet/sing-box/common/qtls"
"github.com/sagernet/sing-box/common/tls"
"github.com/sagernet/sing/common"
"github.com/sagernet/sing/common/auth"
"github.com/sagernet/sing/common/baderror"
@@ -29,7 +32,7 @@ import (
type ServerOptions struct {
Context context.Context
Logger logger.Logger
TLSConfig *tls.Config
TLSConfig tls.ServerConfig
Users []User
CongestionControl string
AuthTimeout time.Duration
@@ -52,7 +55,7 @@ type ServerHandler interface {
type Server struct {
ctx context.Context
logger logger.Logger
tlsConfig *tls.Config
tlsConfig tls.ServerConfig
heartbeat time.Duration
quicConfig *quic.Config
userMap map[uuid.UUID]User
@@ -107,7 +110,7 @@ func NewServer(options ServerOptions) (*Server, error) {
func (s *Server) Start(conn net.PacketConn) error {
if !s.quicConfig.Allow0RTT {
listener, err := quic.Listen(conn, s.tlsConfig, s.quicConfig)
listener, err := qtls.Listen(conn, s.tlsConfig, s.quicConfig)
if err != nil {
return err
}
@@ -127,7 +130,7 @@ func (s *Server) Start(conn net.PacketConn) error {
}
}()
} else {
listener, err := quic.ListenEarly(conn, s.tlsConfig, s.quicConfig)
listener, err := qtls.ListenEarly(conn, s.tlsConfig, s.quicConfig)
if err != nil {
return err
}
@@ -247,7 +250,7 @@ func (s *serverSession) handleUniStream(stream quic.ReceiveStream) error {
if !loaded {
return E.New("authentication: unknown user ", userUUID)
}
handshakeState := s.quicConn.ConnectionState().TLS
handshakeState := s.quicConn.ConnectionState()
tuicToken, err := handshakeState.ExportKeyingMaterial(string(user.UUID[:]), []byte(user.Password), 32)
if err != nil {
return E.Cause(err, "authentication: export keying material")

View File

@@ -1,3 +1,5 @@
//go:build with_quic
package tuic
import (

View File

@@ -1,3 +1,5 @@
//go:build with_quic
package v2rayquic
import (
@@ -7,6 +9,7 @@ import (
"github.com/sagernet/quic-go"
"github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing-box/common/qtls"
"github.com/sagernet/sing-box/common/tls"
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/option"
@@ -23,7 +26,7 @@ type Client struct {
ctx context.Context
dialer N.Dialer
serverAddr M.Socksaddr
tlsConfig *tls.STDConfig
tlsConfig tls.Config
quicConfig *quic.Config
connAccess sync.Mutex
conn quic.Connection
@@ -34,18 +37,14 @@ func NewClient(ctx context.Context, dialer N.Dialer, serverAddr M.Socksaddr, opt
quicConfig := &quic.Config{
DisablePathMTUDiscovery: !C.IsLinux && !C.IsWindows,
}
stdConfig, err := tlsConfig.Config()
if err != nil {
return nil, err
}
if len(stdConfig.NextProtos) == 0 {
stdConfig.NextProtos = []string{"h2", "http/1.1"}
if len(tlsConfig.NextProtos()) == 0 {
tlsConfig.SetNextProtos([]string{"h2", "http/1.1"})
}
return &Client{
ctx: ctx,
dialer: dialer,
serverAddr: serverAddr,
tlsConfig: stdConfig,
tlsConfig: tlsConfig,
quicConfig: quicConfig,
}, nil
}
@@ -75,7 +74,7 @@ func (c *Client) offerNew() (quic.Connection, error) {
}
var packetConn net.PacketConn
packetConn = bufio.NewUnbindPacketConn(udpConn)
quicConn, err := quic.Dial(c.ctx, packetConn, udpConn.RemoteAddr(), c.tlsConfig, c.quicConfig)
quicConn, err := qtls.Dial(c.ctx, packetConn, udpConn.RemoteAddr(), c.tlsConfig, c.quicConfig)
if err != nil {
packetConn.Close()
return nil, err

View File

@@ -1,3 +1,5 @@
//go:build with_quic
package v2rayquic
import "github.com/sagernet/sing-box/transport/v2ray"

View File

@@ -1,3 +1,5 @@
//go:build with_quic
package v2rayquic
import (
@@ -7,6 +9,7 @@ import (
"github.com/sagernet/quic-go"
"github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing-box/common/qtls"
"github.com/sagernet/sing-box/common/tls"
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/option"
@@ -20,27 +23,23 @@ var _ adapter.V2RayServerTransport = (*Server)(nil)
type Server struct {
ctx context.Context
tlsConfig *tls.STDConfig
tlsConfig tls.ServerConfig
quicConfig *quic.Config
handler adapter.V2RayServerTransportHandler
udpListener net.PacketConn
quicListener *quic.Listener
quicListener qtls.QUICListener
}
func NewServer(ctx context.Context, options option.V2RayQUICOptions, tlsConfig tls.ServerConfig, handler adapter.V2RayServerTransportHandler) (adapter.V2RayServerTransport, error) {
quicConfig := &quic.Config{
DisablePathMTUDiscovery: !C.IsLinux && !C.IsWindows,
}
stdConfig, err := tlsConfig.Config()
if err != nil {
return nil, err
}
if len(stdConfig.NextProtos) == 0 {
stdConfig.NextProtos = []string{"h2", "http/1.1"}
if len(tlsConfig.NextProtos()) == 0 {
tlsConfig.SetNextProtos([]string{"h2", "http/1.1"})
}
server := &Server{
ctx: ctx,
tlsConfig: stdConfig,
tlsConfig: tlsConfig,
quicConfig: quicConfig,
handler: handler,
}
@@ -56,7 +55,7 @@ func (s *Server) Serve(listener net.Listener) error {
}
func (s *Server) ServePacket(listener net.PacketConn) error {
quicListener, err := quic.Listen(listener, s.tlsConfig, s.quicConfig)
quicListener, err := qtls.Listen(listener, s.tlsConfig, s.quicConfig)
if err != nil {
return err
}
@@ -92,5 +91,5 @@ func (s *Server) streamAcceptLoop(conn quic.Connection) error {
}
func (s *Server) Close() error {
return common.Close(s.udpListener, common.PtrOrNil(s.quicListener))
return common.Close(s.udpListener, s.quicListener)
}