Add mux server and XUDP client for VMess
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"io"
|
||||
"net"
|
||||
|
||||
"github.com/sagernet/sing-vmess"
|
||||
"github.com/sagernet/sing/common"
|
||||
"github.com/sagernet/sing/common/buf"
|
||||
M "github.com/sagernet/sing/common/metadata"
|
||||
@@ -25,20 +26,21 @@ func NewClient(userId string) (*Client, error) {
|
||||
}
|
||||
|
||||
func (c *Client) DialEarlyConn(conn net.Conn, destination M.Socksaddr) *Conn {
|
||||
return &Conn{Conn: conn, key: c.key, destination: destination}
|
||||
return &Conn{Conn: conn, key: c.key, command: vmess.CommandTCP, destination: destination}
|
||||
}
|
||||
|
||||
func (c *Client) DialPacketConn(conn net.Conn, destination M.Socksaddr) *PacketConn {
|
||||
return &PacketConn{Conn: conn, key: c.key, destination: destination}
|
||||
}
|
||||
|
||||
func (c *Client) DialXUDPPacketConn(conn net.Conn, destination M.Socksaddr) *XUDPConn {
|
||||
return &XUDPConn{Conn: conn, key: c.key, destination: destination}
|
||||
func (c *Client) DialXUDPPacketConn(conn net.Conn, destination M.Socksaddr) vmess.PacketConn {
|
||||
return vmess.NewXUDPConn(&Conn{Conn: conn, key: c.key, command: vmess.CommandMux, destination: destination}, destination)
|
||||
}
|
||||
|
||||
type Conn struct {
|
||||
net.Conn
|
||||
key []byte
|
||||
command byte
|
||||
destination M.Socksaddr
|
||||
requestWritten bool
|
||||
responseRead bool
|
||||
@@ -57,7 +59,7 @@ func (c *Conn) Read(b []byte) (n int, err error) {
|
||||
|
||||
func (c *Conn) Write(b []byte) (n int, err error) {
|
||||
if !c.requestWritten {
|
||||
err = WriteRequest(c.Conn, Request{c.key, CommandTCP, c.destination}, b)
|
||||
err = WriteRequest(c.Conn, Request{c.key, c.command, c.destination}, b)
|
||||
if err == nil {
|
||||
n = len(b)
|
||||
}
|
||||
@@ -100,7 +102,7 @@ func (c *PacketConn) Read(b []byte) (n int, err error) {
|
||||
|
||||
func (c *PacketConn) Write(b []byte) (n int, err error) {
|
||||
if !c.requestWritten {
|
||||
err = WritePacketRequest(c.Conn, Request{c.key, CommandUDP, c.destination}, b)
|
||||
err = WritePacketRequest(c.Conn, Request{c.key, vmess.CommandUDP, c.destination}, b)
|
||||
if err == nil {
|
||||
n = len(b)
|
||||
}
|
||||
@@ -119,7 +121,7 @@ func (c *PacketConn) WritePacket(buffer *buf.Buffer, destination M.Socksaddr) er
|
||||
dataLen := buffer.Len()
|
||||
binary.BigEndian.PutUint16(buffer.ExtendHeader(2), uint16(dataLen))
|
||||
if !c.requestWritten {
|
||||
err := WritePacketRequest(c.Conn, Request{c.key, CommandUDP, c.destination}, buffer.Bytes())
|
||||
err := WritePacketRequest(c.Conn, Request{c.key, vmess.CommandUDP, c.destination}, buffer.Bytes())
|
||||
c.requestWritten = true
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"encoding/binary"
|
||||
"io"
|
||||
|
||||
"github.com/sagernet/sing-vmess"
|
||||
"github.com/sagernet/sing/common"
|
||||
"github.com/sagernet/sing/common/buf"
|
||||
E "github.com/sagernet/sing/common/exceptions"
|
||||
@@ -11,20 +12,7 @@ import (
|
||||
"github.com/sagernet/sing/common/rw"
|
||||
)
|
||||
|
||||
const (
|
||||
Version = 0
|
||||
CommandTCP = 1
|
||||
CommandUDP = 2
|
||||
CommandMux = 3
|
||||
NetworkUDP = 2
|
||||
)
|
||||
|
||||
var AddressSerializer = M.NewSerializer(
|
||||
M.AddressFamilyByte(0x01, M.AddressFamilyIPv4),
|
||||
M.AddressFamilyByte(0x02, M.AddressFamilyFqdn),
|
||||
M.AddressFamilyByte(0x03, M.AddressFamilyIPv6),
|
||||
M.PortThenAddress(),
|
||||
)
|
||||
const Version = 0
|
||||
|
||||
type Request struct {
|
||||
UUID []byte
|
||||
@@ -38,7 +26,9 @@ func WriteRequest(writer io.Writer, request Request, payload []byte) error {
|
||||
requestLen += 16 // uuid
|
||||
requestLen += 1 // protobuf length
|
||||
requestLen += 1 // command
|
||||
requestLen += AddressSerializer.AddrPortLen(request.Destination)
|
||||
if request.Command != vmess.CommandMux {
|
||||
requestLen += vmess.AddressSerializer.AddrPortLen(request.Destination)
|
||||
}
|
||||
requestLen += len(payload)
|
||||
_buffer := buf.StackNewSize(requestLen)
|
||||
defer common.KeepAlive(_buffer)
|
||||
@@ -48,10 +38,14 @@ func WriteRequest(writer io.Writer, request Request, payload []byte) error {
|
||||
buffer.WriteByte(Version),
|
||||
common.Error(buffer.Write(request.UUID)),
|
||||
buffer.WriteByte(0),
|
||||
buffer.WriteByte(CommandTCP),
|
||||
AddressSerializer.WriteAddrPort(buffer, request.Destination),
|
||||
common.Error(buffer.Write(payload)),
|
||||
buffer.WriteByte(request.Command),
|
||||
)
|
||||
|
||||
if request.Command != vmess.CommandMux {
|
||||
common.Must(vmess.AddressSerializer.WriteAddrPort(buffer, request.Destination))
|
||||
}
|
||||
|
||||
common.Must1(buffer.Write(payload))
|
||||
return common.Error(writer.Write(buffer.Bytes()))
|
||||
}
|
||||
|
||||
@@ -61,7 +55,7 @@ func WritePacketRequest(writer io.Writer, request Request, payload []byte) error
|
||||
requestLen += 16 // uuid
|
||||
requestLen += 1 // protobuf length
|
||||
requestLen += 1 // command
|
||||
requestLen += AddressSerializer.AddrPortLen(request.Destination)
|
||||
requestLen += vmess.AddressSerializer.AddrPortLen(request.Destination)
|
||||
if len(payload) > 0 {
|
||||
requestLen += 2
|
||||
requestLen += len(payload)
|
||||
@@ -74,8 +68,8 @@ func WritePacketRequest(writer io.Writer, request Request, payload []byte) error
|
||||
buffer.WriteByte(Version),
|
||||
common.Error(buffer.Write(request.UUID)),
|
||||
buffer.WriteByte(0),
|
||||
buffer.WriteByte(CommandUDP),
|
||||
AddressSerializer.WriteAddrPort(buffer, request.Destination),
|
||||
buffer.WriteByte(vmess.CommandUDP),
|
||||
vmess.AddressSerializer.WriteAddrPort(buffer, request.Destination),
|
||||
binary.Write(buffer, binary.BigEndian, uint16(len(payload))),
|
||||
common.Error(buffer.Write(payload)),
|
||||
)
|
||||
|
||||
@@ -1,174 +0,0 @@
|
||||
package vless
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"io"
|
||||
"net"
|
||||
"os"
|
||||
|
||||
"github.com/sagernet/sing/common"
|
||||
"github.com/sagernet/sing/common/buf"
|
||||
E "github.com/sagernet/sing/common/exceptions"
|
||||
M "github.com/sagernet/sing/common/metadata"
|
||||
)
|
||||
|
||||
type XUDPConn struct {
|
||||
net.Conn
|
||||
key []byte
|
||||
destination M.Socksaddr
|
||||
requestWritten bool
|
||||
responseRead bool
|
||||
}
|
||||
|
||||
func (c *XUDPConn) ReadFrom(p []byte) (n int, addr net.Addr, err error) {
|
||||
return 0, nil, os.ErrInvalid
|
||||
}
|
||||
|
||||
func (c *XUDPConn) ReadPacket(buffer *buf.Buffer) (destination M.Socksaddr, err error) {
|
||||
start := buffer.Start()
|
||||
if !c.responseRead {
|
||||
err = ReadResponse(c.Conn)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
c.responseRead = true
|
||||
buffer.FullReset()
|
||||
}
|
||||
_, err = buffer.ReadFullFrom(c.Conn, 6)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
var length uint16
|
||||
err = binary.Read(buffer, binary.BigEndian, &length)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
header, err := buffer.ReadBytes(4)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
switch header[2] {
|
||||
case 1:
|
||||
// frame new
|
||||
return M.Socksaddr{}, E.New("unexpected frame new")
|
||||
case 2:
|
||||
// frame keep
|
||||
if length != 4 {
|
||||
_, err = buffer.ReadFullFrom(c.Conn, int(length)-2)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
buffer.Advance(1)
|
||||
destination, err = AddressSerializer.ReadAddrPort(buffer)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
} else {
|
||||
_, err = buffer.ReadFullFrom(c.Conn, 2)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
destination = c.destination
|
||||
}
|
||||
case 3:
|
||||
// frame end
|
||||
return M.Socksaddr{}, io.EOF
|
||||
case 4:
|
||||
// frame keep alive
|
||||
default:
|
||||
return M.Socksaddr{}, E.New("unexpected frame: ", buffer.Byte(2))
|
||||
}
|
||||
// option error
|
||||
if header[3]&2 == 2 {
|
||||
return M.Socksaddr{}, E.Cause(net.ErrClosed, "remote closed")
|
||||
}
|
||||
// option data
|
||||
if header[3]&1 != 1 {
|
||||
buffer.Resize(start, 0)
|
||||
return c.ReadPacket(buffer)
|
||||
} else {
|
||||
err = binary.Read(buffer, binary.BigEndian, &length)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
buffer.Resize(start, 0)
|
||||
_, err = buffer.ReadFullFrom(c.Conn, int(length))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (c *XUDPConn) WriteTo(p []byte, addr net.Addr) (n int, err error) {
|
||||
destination := M.SocksaddrFromNet(addr)
|
||||
headerLen := c.frontHeadroom(AddressSerializer.AddrPortLen(destination))
|
||||
buffer := buf.NewSize(headerLen + len(p))
|
||||
buffer.Advance(headerLen)
|
||||
common.Must1(buffer.Write(p))
|
||||
err = c.WritePacket(buffer, destination)
|
||||
if err == nil {
|
||||
n = len(p)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (c *XUDPConn) frontHeadroom(addrLen int) int {
|
||||
if !c.requestWritten {
|
||||
var headerLen int
|
||||
headerLen += 1 // version
|
||||
headerLen += 16 // uuid
|
||||
headerLen += 1 // protobuf length
|
||||
headerLen += 1 // command
|
||||
headerLen += 2 // frame len
|
||||
headerLen += 5 // frame header
|
||||
headerLen += addrLen
|
||||
headerLen += 2 // payload len
|
||||
return headerLen
|
||||
} else {
|
||||
return 7 + addrLen + 2
|
||||
}
|
||||
}
|
||||
|
||||
func (c *XUDPConn) WritePacket(buffer *buf.Buffer, destination M.Socksaddr) error {
|
||||
defer buffer.Release()
|
||||
dataLen := buffer.Len()
|
||||
addrLen := M.SocksaddrSerializer.AddrPortLen(destination)
|
||||
if !c.requestWritten {
|
||||
header := buf.With(buffer.ExtendHeader(c.frontHeadroom(addrLen)))
|
||||
common.Must(
|
||||
header.WriteByte(Version),
|
||||
common.Error(header.Write(c.key)),
|
||||
header.WriteByte(0),
|
||||
header.WriteByte(CommandMux),
|
||||
binary.Write(header, binary.BigEndian, uint16(5+addrLen)),
|
||||
header.WriteByte(0),
|
||||
header.WriteByte(0),
|
||||
header.WriteByte(1), // frame type new
|
||||
header.WriteByte(1), // option data
|
||||
header.WriteByte(NetworkUDP),
|
||||
AddressSerializer.WriteAddrPort(header, destination),
|
||||
binary.Write(header, binary.BigEndian, uint16(dataLen)),
|
||||
)
|
||||
c.requestWritten = true
|
||||
} else {
|
||||
header := buffer.ExtendHeader(c.frontHeadroom(addrLen))
|
||||
binary.BigEndian.PutUint16(header, uint16(5+addrLen))
|
||||
header[2] = 0
|
||||
header[3] = 0
|
||||
header[4] = 2 // frame keep
|
||||
header[5] = 1 // option data
|
||||
header[6] = NetworkUDP
|
||||
err := AddressSerializer.WriteAddrPort(buf.With(header[7:]), destination)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
binary.BigEndian.PutUint16(header[7+addrLen:], uint16(dataLen))
|
||||
}
|
||||
return common.Error(c.Conn.Write(buffer.Bytes()))
|
||||
}
|
||||
|
||||
func (c *XUDPConn) FrontHeadroom() int {
|
||||
return c.frontHeadroom(M.MaxSocksaddrLength)
|
||||
}
|
||||
|
||||
func (c *XUDPConn) Upstream() any {
|
||||
return c.Conn
|
||||
}
|
||||
Reference in New Issue
Block a user