2023-02-25 08:24:08 +00:00
|
|
|
package vless
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"io"
|
|
|
|
"net"
|
2023-08-03 07:03:29 +00:00
|
|
|
"sync"
|
2023-02-25 08:24:08 +00:00
|
|
|
|
|
|
|
"github.com/sagernet/sing-vmess"
|
|
|
|
"github.com/sagernet/sing/common"
|
|
|
|
"github.com/sagernet/sing/common/buf"
|
2023-04-18 06:59:44 +00:00
|
|
|
"github.com/sagernet/sing/common/bufio"
|
2023-02-25 08:24:08 +00:00
|
|
|
E "github.com/sagernet/sing/common/exceptions"
|
2023-02-27 07:07:15 +00:00
|
|
|
"github.com/sagernet/sing/common/logger"
|
2023-02-25 08:24:08 +00:00
|
|
|
M "github.com/sagernet/sing/common/metadata"
|
2023-02-26 15:08:20 +00:00
|
|
|
N "github.com/sagernet/sing/common/network"
|
2023-02-25 08:24:08 +00:00
|
|
|
|
2023-04-09 07:37:06 +00:00
|
|
|
"github.com/gofrs/uuid/v5"
|
2023-02-25 08:24:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Client struct {
|
2023-02-27 07:07:15 +00:00
|
|
|
key [16]byte
|
|
|
|
flow string
|
|
|
|
logger logger.Logger
|
2023-02-25 08:24:08 +00:00
|
|
|
}
|
|
|
|
|
2023-02-27 07:07:15 +00:00
|
|
|
func NewClient(userId string, flow string, logger logger.Logger) (*Client, error) {
|
2023-02-25 08:24:08 +00:00
|
|
|
user := uuid.FromStringOrNil(userId)
|
|
|
|
if user == uuid.Nil {
|
|
|
|
user = uuid.NewV5(user, userId)
|
|
|
|
}
|
|
|
|
switch flow {
|
|
|
|
case "", "xtls-rprx-vision":
|
|
|
|
default:
|
|
|
|
return nil, E.New("unsupported flow: " + flow)
|
|
|
|
}
|
2023-02-27 07:07:15 +00:00
|
|
|
return &Client{user, flow, logger}, nil
|
2023-02-25 08:24:08 +00:00
|
|
|
}
|
|
|
|
|
2023-04-18 06:59:44 +00:00
|
|
|
func (c *Client) prepareConn(conn net.Conn, tlsConn net.Conn) (net.Conn, error) {
|
2023-02-25 08:24:08 +00:00
|
|
|
if c.flow == FlowVision {
|
2023-04-18 06:59:44 +00:00
|
|
|
protocolConn, err := NewVisionConn(conn, tlsConn, c.key, c.logger)
|
2023-02-25 08:24:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, E.Cause(err, "initialize vision")
|
|
|
|
}
|
2023-04-18 06:59:44 +00:00
|
|
|
conn = protocolConn
|
2023-02-25 08:24:08 +00:00
|
|
|
}
|
|
|
|
return conn, nil
|
|
|
|
}
|
|
|
|
|
2023-04-18 06:59:44 +00:00
|
|
|
func (c *Client) DialConn(conn net.Conn, destination M.Socksaddr) (net.Conn, error) {
|
|
|
|
remoteConn := NewConn(conn, c.key, vmess.CommandTCP, destination, c.flow)
|
|
|
|
protocolConn, err := c.prepareConn(remoteConn, conn)
|
2023-02-25 08:24:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-04-18 06:59:44 +00:00
|
|
|
return protocolConn, common.Error(remoteConn.Write(nil))
|
2023-02-25 08:24:08 +00:00
|
|
|
}
|
|
|
|
|
2023-04-18 06:59:44 +00:00
|
|
|
func (c *Client) DialEarlyConn(conn net.Conn, destination M.Socksaddr) (net.Conn, error) {
|
|
|
|
return c.prepareConn(NewConn(conn, c.key, vmess.CommandTCP, destination, c.flow), conn)
|
2023-02-25 08:24:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) DialPacketConn(conn net.Conn, destination M.Socksaddr) (*PacketConn, error) {
|
|
|
|
serverConn := &PacketConn{Conn: conn, key: c.key, destination: destination, flow: c.flow}
|
|
|
|
return serverConn, common.Error(serverConn.Write(nil))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) DialEarlyPacketConn(conn net.Conn, destination M.Socksaddr) (*PacketConn, error) {
|
|
|
|
return &PacketConn{Conn: conn, key: c.key, destination: destination, flow: c.flow}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) DialXUDPPacketConn(conn net.Conn, destination M.Socksaddr) (vmess.PacketConn, error) {
|
2023-04-18 06:59:44 +00:00
|
|
|
remoteConn := NewConn(conn, c.key, vmess.CommandTCP, destination, c.flow)
|
|
|
|
protocolConn, err := c.prepareConn(remoteConn, conn)
|
2023-02-25 08:24:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-04-18 06:59:44 +00:00
|
|
|
return vmess.NewXUDPConn(protocolConn, destination), common.Error(remoteConn.Write(nil))
|
2023-02-25 08:24:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) DialEarlyXUDPPacketConn(conn net.Conn, destination M.Socksaddr) (vmess.PacketConn, error) {
|
2023-04-18 06:59:44 +00:00
|
|
|
remoteConn := NewConn(conn, c.key, vmess.CommandMux, destination, c.flow)
|
|
|
|
protocolConn, err := c.prepareConn(remoteConn, conn)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return vmess.NewXUDPConn(protocolConn, destination), common.Error(remoteConn.Write(nil))
|
2023-02-25 08:24:08 +00:00
|
|
|
}
|
|
|
|
|
2023-04-18 06:59:44 +00:00
|
|
|
var (
|
|
|
|
_ N.EarlyConn = (*Conn)(nil)
|
|
|
|
_ N.VectorisedWriter = (*Conn)(nil)
|
|
|
|
)
|
2023-02-26 15:08:20 +00:00
|
|
|
|
2023-02-25 08:24:08 +00:00
|
|
|
type Conn struct {
|
2023-04-18 06:59:44 +00:00
|
|
|
N.ExtendedConn
|
|
|
|
writer N.VectorisedWriter
|
|
|
|
request Request
|
2023-02-25 08:24:08 +00:00
|
|
|
requestWritten bool
|
|
|
|
responseRead bool
|
|
|
|
}
|
|
|
|
|
2023-04-18 06:59:44 +00:00
|
|
|
func NewConn(conn net.Conn, uuid [16]byte, command byte, destination M.Socksaddr, flow string) *Conn {
|
|
|
|
return &Conn{
|
|
|
|
ExtendedConn: bufio.NewExtendedConn(conn),
|
|
|
|
writer: bufio.NewVectorisedWriter(conn),
|
|
|
|
request: Request{
|
|
|
|
UUID: uuid,
|
|
|
|
Command: command,
|
|
|
|
Destination: destination,
|
|
|
|
Flow: flow,
|
|
|
|
},
|
|
|
|
}
|
2023-02-26 15:08:20 +00:00
|
|
|
}
|
|
|
|
|
2023-02-25 08:24:08 +00:00
|
|
|
func (c *Conn) Read(b []byte) (n int, err error) {
|
|
|
|
if !c.responseRead {
|
2023-04-18 06:59:44 +00:00
|
|
|
err = ReadResponse(c.ExtendedConn)
|
2023-02-25 08:24:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.responseRead = true
|
|
|
|
}
|
2023-04-18 06:59:44 +00:00
|
|
|
return c.ExtendedConn.Read(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Conn) ReadBuffer(buffer *buf.Buffer) error {
|
|
|
|
if !c.responseRead {
|
|
|
|
err := ReadResponse(c.ExtendedConn)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.responseRead = true
|
|
|
|
}
|
|
|
|
return c.ExtendedConn.ReadBuffer(buffer)
|
2023-02-25 08:24:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Conn) Write(b []byte) (n int, err error) {
|
|
|
|
if !c.requestWritten {
|
2023-04-18 06:59:44 +00:00
|
|
|
err = WriteRequest(c.ExtendedConn, c.request, b)
|
2023-02-25 08:24:08 +00:00
|
|
|
if err == nil {
|
|
|
|
n = len(b)
|
|
|
|
}
|
|
|
|
c.requestWritten = true
|
2023-04-18 06:59:44 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
return c.ExtendedConn.Write(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Conn) WriteBuffer(buffer *buf.Buffer) error {
|
|
|
|
if !c.requestWritten {
|
|
|
|
EncodeRequest(c.request, buf.With(buffer.ExtendHeader(RequestLen(c.request))))
|
|
|
|
c.requestWritten = true
|
2023-02-25 08:24:08 +00:00
|
|
|
}
|
2023-04-18 06:59:44 +00:00
|
|
|
return c.ExtendedConn.WriteBuffer(buffer)
|
2023-02-25 08:24:08 +00:00
|
|
|
}
|
|
|
|
|
2023-04-18 06:59:44 +00:00
|
|
|
func (c *Conn) WriteVectorised(buffers []*buf.Buffer) error {
|
|
|
|
if !c.requestWritten {
|
|
|
|
buffer := buf.NewSize(RequestLen(c.request))
|
|
|
|
EncodeRequest(c.request, buffer)
|
|
|
|
c.requestWritten = true
|
|
|
|
return c.writer.WriteVectorised(append([]*buf.Buffer{buffer}, buffers...))
|
|
|
|
}
|
|
|
|
return c.writer.WriteVectorised(buffers)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Conn) ReaderReplaceable() bool {
|
|
|
|
return c.responseRead
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Conn) WriterReplaceable() bool {
|
|
|
|
return c.requestWritten
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Conn) NeedHandshake() bool {
|
|
|
|
return !c.requestWritten
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Conn) FrontHeadroom() int {
|
|
|
|
if c.requestWritten {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return RequestLen(c.request)
|
2023-04-19 13:48:54 +00:00
|
|
|
}
|
|
|
|
|
2023-02-25 08:24:08 +00:00
|
|
|
func (c *Conn) Upstream() any {
|
2023-04-18 06:59:44 +00:00
|
|
|
return c.ExtendedConn
|
2023-02-25 08:24:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type PacketConn struct {
|
|
|
|
net.Conn
|
2023-08-03 07:03:29 +00:00
|
|
|
access sync.Mutex
|
2023-02-25 08:24:08 +00:00
|
|
|
key [16]byte
|
|
|
|
destination M.Socksaddr
|
|
|
|
flow string
|
|
|
|
requestWritten bool
|
|
|
|
responseRead bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *PacketConn) Read(b []byte) (n int, err error) {
|
|
|
|
if !c.responseRead {
|
|
|
|
err = ReadResponse(c.Conn)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.responseRead = true
|
|
|
|
}
|
|
|
|
var length uint16
|
|
|
|
err = binary.Read(c.Conn, binary.BigEndian, &length)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if cap(b) < int(length) {
|
|
|
|
return 0, io.ErrShortBuffer
|
|
|
|
}
|
|
|
|
return io.ReadFull(c.Conn, b[:length])
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *PacketConn) Write(b []byte) (n int, err error) {
|
|
|
|
if !c.requestWritten {
|
2023-08-03 07:03:29 +00:00
|
|
|
c.access.Lock()
|
|
|
|
if c.requestWritten {
|
|
|
|
c.access.Unlock()
|
|
|
|
} else {
|
|
|
|
err = WritePacketRequest(c.Conn, Request{c.key, vmess.CommandUDP, c.destination, c.flow}, nil)
|
|
|
|
if err == nil {
|
|
|
|
n = len(b)
|
|
|
|
}
|
|
|
|
c.requestWritten = true
|
|
|
|
c.access.Unlock()
|
2023-02-25 08:24:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
err = binary.Write(c.Conn, binary.BigEndian, uint16(len(b)))
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return c.Conn.Write(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *PacketConn) WritePacket(buffer *buf.Buffer, destination M.Socksaddr) error {
|
|
|
|
defer buffer.Release()
|
|
|
|
dataLen := buffer.Len()
|
|
|
|
binary.BigEndian.PutUint16(buffer.ExtendHeader(2), uint16(dataLen))
|
|
|
|
if !c.requestWritten {
|
2023-08-03 07:03:29 +00:00
|
|
|
c.access.Lock()
|
|
|
|
if c.requestWritten {
|
|
|
|
c.access.Unlock()
|
|
|
|
} else {
|
|
|
|
err := WritePacketRequest(c.Conn, Request{c.key, vmess.CommandUDP, c.destination, c.flow}, buffer.Bytes())
|
|
|
|
c.requestWritten = true
|
|
|
|
c.access.Unlock()
|
|
|
|
return err
|
|
|
|
}
|
2023-02-25 08:24:08 +00:00
|
|
|
}
|
|
|
|
return common.Error(c.Conn.Write(buffer.Bytes()))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *PacketConn) ReadFrom(p []byte) (n int, addr net.Addr, err error) {
|
|
|
|
n, err = c.Read(p)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2023-04-12 12:44:17 +00:00
|
|
|
if c.destination.IsFqdn() {
|
|
|
|
addr = c.destination
|
|
|
|
} else {
|
|
|
|
addr = c.destination.UDPAddr()
|
|
|
|
}
|
2023-02-25 08:24:08 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *PacketConn) WriteTo(p []byte, addr net.Addr) (n int, err error) {
|
|
|
|
return c.Write(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *PacketConn) FrontHeadroom() int {
|
|
|
|
return 2
|
|
|
|
}
|
|
|
|
|
2023-04-19 13:48:54 +00:00
|
|
|
func (c *PacketConn) NeedAdditionalReadDeadline() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2023-02-25 08:24:08 +00:00
|
|
|
func (c *PacketConn) Upstream() any {
|
|
|
|
return c.Conn
|
|
|
|
}
|