Add v2ray QUIC transport

This commit is contained in:
世界
2022-08-22 21:20:05 +08:00
parent 77c98fd042
commit d4b7e221f0
22 changed files with 336 additions and 25 deletions

View File

@@ -34,9 +34,18 @@ func (c *PacketConnWrapper) Upstream() any {
}
type StreamWrapper struct {
Conn quic.Connection
quic.Stream
}
func (s *StreamWrapper) LocalAddr() net.Addr {
return s.Conn.LocalAddr()
}
func (s *StreamWrapper) RemoteAddr() net.Addr {
return s.Conn.RemoteAddr()
}
func (s *StreamWrapper) Upstream() any {
return s.Stream
}

23
transport/v2ray/quic.go Normal file
View File

@@ -0,0 +1,23 @@
//go:build with_quic
package v2ray
import (
"context"
"crypto/tls"
"github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing-box/option"
"github.com/sagernet/sing-box/transport/v2rayquic"
E "github.com/sagernet/sing/common/exceptions"
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
)
func NewQUICServer(ctx context.Context, options option.V2RayQUICOptions, tlsConfig *tls.Config, handler N.TCPConnectionHandler, errorHandler E.Handler) (adapter.V2RayServerTransport, error) {
return v2rayquic.NewServer(ctx, options, tlsConfig, handler, errorHandler), nil
}
func NewQUICClient(ctx context.Context, dialer N.Dialer, serverAddr M.Socksaddr, options option.V2RayQUICOptions, tlsConfig *tls.Config) (adapter.V2RayClientTransport, error) {
return v2rayquic.NewClient(ctx, dialer, serverAddr, options, tlsConfig), nil
}

View File

@@ -0,0 +1,23 @@
//go:build !with_quic
package v2ray
import (
"context"
"crypto/tls"
"github.com/sagernet/sing-box/adapter"
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/option"
E "github.com/sagernet/sing/common/exceptions"
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
)
func NewQUICServer(ctx context.Context, options option.V2RayQUICOptions, tlsConfig *tls.Config, handler N.TCPConnectionHandler, errorHandler E.Handler) (adapter.V2RayServerTransport, error) {
return nil, C.ErrQUICNotIncluded
}
func NewQUICClient(ctx context.Context, dialer N.Dialer, serverAddr M.Socksaddr, options option.V2RayQUICOptions, tlsConfig *tls.Config) (adapter.V2RayClientTransport, error) {
return nil, C.ErrQUICNotIncluded
}

View File

@@ -22,6 +22,8 @@ func NewServerTransport(ctx context.Context, options option.V2RayTransportOption
return NewGRPCServer(ctx, options.GRPCOptions, tlsConfig, handler)
case C.V2RayTransportTypeWebsocket:
return v2raywebsocket.NewServer(ctx, options.WebsocketOptions, tlsConfig, handler, errorHandler), nil
case C.V2RayTransportTypeQUIC:
return NewQUICServer(ctx, options.QUICOptions, tlsConfig, handler, errorHandler)
default:
return nil, E.New("unknown transport type: " + options.Type)
}
@@ -36,6 +38,8 @@ func NewClientTransport(ctx context.Context, dialer N.Dialer, serverAddr M.Socks
return NewGRPCClient(ctx, dialer, serverAddr, options.GRPCOptions, tlsConfig)
case C.V2RayTransportTypeWebsocket:
return v2raywebsocket.NewClient(ctx, dialer, serverAddr, options.WebsocketOptions, tlsConfig), nil
case C.V2RayTransportTypeQUIC:
return NewQUICClient(ctx, dialer, serverAddr, options.QUICOptions, tlsConfig)
default:
return nil, E.New("unknown transport type: " + options.Type)
}

View File

@@ -4,6 +4,7 @@ import (
"context"
"crypto/tls"
"net"
"os"
"github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing-box/option"
@@ -44,10 +45,18 @@ func (s *Server) Tun(server GunService_TunServer) error {
func (s *Server) mustEmbedUnimplementedGunServiceServer() {
}
func (s *Server) Network() []string {
return []string{N.NetworkTCP}
}
func (s *Server) Serve(listener net.Listener) error {
return s.server.Serve(listener)
}
func (s *Server) ServePacket(listener net.PacketConn) error {
return os.ErrInvalid
}
func (s *Server) Close() error {
s.server.Stop()
return nil

View File

@@ -0,0 +1,92 @@
package v2rayquic
import (
"context"
"crypto/tls"
"net"
"sync"
"github.com/sagernet/quic-go"
"github.com/sagernet/sing-box/adapter"
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/option"
"github.com/sagernet/sing-box/transport/hysteria"
"github.com/sagernet/sing/common"
"github.com/sagernet/sing/common/bufio"
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
)
var _ adapter.V2RayClientTransport = (*Client)(nil)
type Client struct {
ctx context.Context
dialer N.Dialer
serverAddr M.Socksaddr
tlsConfig *tls.Config
quicConfig *quic.Config
conn quic.Connection
connAccess sync.Mutex
}
func NewClient(ctx context.Context, dialer N.Dialer, serverAddr M.Socksaddr, options option.V2RayQUICOptions, tlsConfig *tls.Config) adapter.V2RayClientTransport {
quicConfig := &quic.Config{
DisablePathMTUDiscovery: !C.IsLinux && !C.IsWindows,
}
if len(tlsConfig.NextProtos) == 0 {
tlsConfig.NextProtos = []string{"h2", "http/1.1"}
}
return &Client{
ctx: ctx,
dialer: dialer,
serverAddr: serverAddr,
tlsConfig: tlsConfig,
quicConfig: quicConfig,
}
}
func (c *Client) offer() (quic.Connection, error) {
conn := c.conn
if conn != nil && !common.Done(conn.Context()) {
return conn, nil
}
c.connAccess.Lock()
defer c.connAccess.Unlock()
conn = c.conn
if conn != nil && !common.Done(conn.Context()) {
return conn, nil
}
conn, err := c.offerNew()
if err != nil {
return nil, err
}
c.conn = conn
return conn, nil
}
func (c *Client) offerNew() (quic.Connection, error) {
udpConn, err := c.dialer.DialContext(c.ctx, "udp", c.serverAddr)
if err != nil {
return nil, err
}
var packetConn net.PacketConn
packetConn = bufio.NewUnbindPacketConn(udpConn)
quicConn, err := quic.Dial(packetConn, udpConn.RemoteAddr(), c.serverAddr.AddrString(), c.tlsConfig, c.quicConfig)
if err != nil {
packetConn.Close()
return nil, err
}
return quicConn, nil
}
func (c *Client) DialContext(ctx context.Context) (net.Conn, error) {
conn, err := c.offer()
if err != nil {
return nil, err
}
stream, err := conn.OpenStream()
if err != nil {
return nil, err
}
return &hysteria.StreamWrapper{Conn: conn, Stream: stream}, nil
}

View File

@@ -0,0 +1,95 @@
package v2rayquic
import (
"context"
"crypto/tls"
"net"
"os"
"github.com/sagernet/quic-go"
"github.com/sagernet/sing-box/adapter"
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/option"
"github.com/sagernet/sing-box/transport/hysteria"
"github.com/sagernet/sing/common"
E "github.com/sagernet/sing/common/exceptions"
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
)
var _ adapter.V2RayServerTransport = (*Server)(nil)
type Server struct {
ctx context.Context
tlsConfig *tls.Config
quicConfig *quic.Config
handler N.TCPConnectionHandler
errorHandler E.Handler
udpListener net.PacketConn
quicListener quic.Listener
}
func NewServer(ctx context.Context, options option.V2RayQUICOptions, tlsConfig *tls.Config, handler N.TCPConnectionHandler, errorHandler E.Handler) *Server {
quicConfig := &quic.Config{
DisablePathMTUDiscovery: !C.IsLinux && !C.IsWindows,
}
if len(tlsConfig.NextProtos) == 0 {
tlsConfig.NextProtos = []string{"h2", "http/1.1"}
}
server := &Server{
ctx: ctx,
tlsConfig: tlsConfig,
quicConfig: quicConfig,
handler: handler,
errorHandler: errorHandler,
}
return server
}
func (s *Server) Network() []string {
return []string{N.NetworkUDP}
}
func (s *Server) Serve(listener net.Listener) error {
return os.ErrInvalid
}
func (s *Server) ServePacket(listener net.PacketConn) error {
quicListener, err := quic.Listen(listener, s.tlsConfig, s.quicConfig)
if err != nil {
return err
}
s.udpListener = listener
s.quicListener = quicListener
go s.acceptLoop()
return nil
}
func (s *Server) acceptLoop() {
for {
conn, err := s.quicListener.Accept(s.ctx)
if err != nil {
return
}
go func() {
hErr := s.streamAcceptLoop(conn)
if hErr != nil {
s.errorHandler.NewError(conn.Context(), hErr)
}
}()
}
}
func (s *Server) streamAcceptLoop(conn quic.Connection) error {
for {
stream, err := conn.AcceptStream(s.ctx)
if err != nil {
return err
}
go s.handler.NewConnection(conn.Context(), &hysteria.StreamWrapper{Conn: conn, Stream: stream}, M.Metadata{})
}
}
func (s *Server) Close() error {
return common.Close(s.udpListener, s.quicListener)
}

View File

@@ -7,6 +7,7 @@ import (
"net"
"net/http"
"net/netip"
"os"
"strings"
"github.com/sagernet/sing-box/adapter"
@@ -125,6 +126,10 @@ func (s *Server) badRequest(request *http.Request, err error) {
s.errorHandler.NewError(request.Context(), E.Cause(err, "process connection from ", request.RemoteAddr))
}
func (s *Server) Network() []string {
return []string{N.NetworkTCP}
}
func (s *Server) Serve(listener net.Listener) error {
if s.httpServer.TLSConfig == nil {
return s.httpServer.Serve(listener)
@@ -133,6 +138,10 @@ func (s *Server) Serve(listener net.Listener) error {
}
}
func (s *Server) ServePacket(listener net.PacketConn) error {
return os.ErrInvalid
}
func (s *Server) Close() error {
return common.Close(s.httpServer)
}