sing-box/transport/v2rayquic/client.go

112 lines
2.5 KiB
Go
Raw Normal View History

//go:build with_quic
2022-08-22 21:20:05 +08:00
package v2rayquic
import (
"context"
"net"
"sync"
"github.com/sagernet/quic-go"
"github.com/sagernet/quic-go/http3"
2022-08-22 21:20:05 +08:00
"github.com/sagernet/sing-box/adapter"
2022-09-09 18:45:10 +08:00
"github.com/sagernet/sing-box/common/tls"
2022-08-22 21:20:05 +08:00
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/option"
"github.com/sagernet/sing-quic"
2022-08-22 21:20:05 +08:00
"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
2022-08-22 21:20:05 +08:00
quicConfig *quic.Config
connAccess sync.Mutex
2022-09-23 13:14:31 +08:00
conn quic.Connection
rawConn net.Conn
2022-08-22 21:20:05 +08:00
}
2022-09-09 18:45:10 +08:00
func NewClient(ctx context.Context, dialer N.Dialer, serverAddr M.Socksaddr, options option.V2RayQUICOptions, tlsConfig tls.Config) (adapter.V2RayClientTransport, error) {
2022-08-22 21:20:05 +08:00
quicConfig := &quic.Config{
DisablePathMTUDiscovery: !C.IsLinux && !C.IsWindows,
}
if len(tlsConfig.NextProtos()) == 0 {
tlsConfig.SetNextProtos([]string{http3.NextProtoH3})
2022-08-22 21:20:05 +08:00
}
return &Client{
ctx: ctx,
dialer: dialer,
serverAddr: serverAddr,
tlsConfig: tlsConfig,
2022-08-22 21:20:05 +08:00
quicConfig: quicConfig,
2022-09-09 18:45:10 +08:00
}, nil
2022-08-22 21:20:05 +08:00
}
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
}
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 := qtls.Dial(c.ctx, packetConn, udpConn.RemoteAddr(), c.tlsConfig, c.quicConfig)
2022-08-22 21:20:05 +08:00
if err != nil {
packetConn.Close()
return nil, err
}
2022-09-23 13:14:31 +08:00
c.conn = quicConn
c.rawConn = udpConn
2022-08-22 21:20:05 +08:00
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 &StreamWrapper{Conn: conn, Stream: stream}, nil
2022-08-22 21:20:05 +08:00
}
2022-09-23 13:14:31 +08:00
func (c *Client) Close() error {
2024-07-07 15:45:50 +08:00
c.connAccess.Lock()
defer c.connAccess.Unlock()
if c.conn != nil {
c.conn.CloseWithError(0, "")
}
if c.rawConn != nil {
c.rawConn.Close()
}
c.conn = nil
c.rawConn = nil
return nil
2022-09-23 13:14:31 +08:00
}