mihomo/transport/tuic/v5/protocol.go

584 lines
12 KiB
Go
Raw Permalink Normal View History

2023-06-12 17:44:22 +08:00
package v5
2022-11-25 08:08:14 +08:00
import (
"encoding/binary"
"fmt"
"io"
"net"
"net/netip"
"strconv"
2023-11-03 21:01:45 +08:00
"github.com/metacubex/mihomo/common/utils"
C "github.com/metacubex/mihomo/constant"
"github.com/metacubex/mihomo/transport/socks5"
2023-06-12 17:44:22 +08:00
"github.com/metacubex/quic-go"
2022-11-25 08:08:14 +08:00
)
type BufferedReader interface {
io.Reader
io.ByteReader
}
type BufferedWriter interface {
io.Writer
io.ByteWriter
}
type CommandType byte
const (
AuthenticateType = CommandType(0x00)
ConnectType = CommandType(0x01)
PacketType = CommandType(0x02)
DissociateType = CommandType(0x03)
HeartbeatType = CommandType(0x04)
)
const VER byte = 0x05
2022-11-25 08:08:14 +08:00
func (c CommandType) String() string {
switch c {
case AuthenticateType:
return "Authenticate"
case ConnectType:
return "Connect"
case PacketType:
return "Packet"
case DissociateType:
return "Dissociate"
case HeartbeatType:
return "Heartbeat"
default:
return fmt.Sprintf("UnknowCommand: %#x", byte(c))
}
}
func (c CommandType) BytesLen() int {
return 1
}
type CommandHead struct {
VER byte
TYPE CommandType
}
func NewCommandHead(TYPE CommandType) CommandHead {
return CommandHead{
VER: VER,
2022-11-25 08:08:14 +08:00
TYPE: TYPE,
}
}
func ReadCommandHead(reader BufferedReader) (c CommandHead, err error) {
c.VER, err = reader.ReadByte()
if err != nil {
return
}
TYPE, err := reader.ReadByte()
if err != nil {
return
}
c.TYPE = CommandType(TYPE)
return
}
func (c CommandHead) WriteTo(writer BufferedWriter) (err error) {
err = writer.WriteByte(c.VER)
if err != nil {
return
}
err = writer.WriteByte(byte(c.TYPE))
if err != nil {
return
}
return
}
func (c CommandHead) BytesLen() int {
return 1 + c.TYPE.BytesLen()
}
type Authenticate struct {
CommandHead
2023-06-12 17:44:22 +08:00
UUID [16]byte
TOKEN [32]byte
2022-11-25 08:08:14 +08:00
}
2023-06-12 17:44:22 +08:00
func NewAuthenticate(UUID [16]byte, TOKEN [32]byte) Authenticate {
2022-11-25 08:08:14 +08:00
return Authenticate{
CommandHead: NewCommandHead(AuthenticateType),
2023-06-12 17:44:22 +08:00
UUID: UUID,
TOKEN: TOKEN,
2022-11-25 08:08:14 +08:00
}
}
2022-11-28 17:09:25 +08:00
func ReadAuthenticateWithHead(head CommandHead, reader BufferedReader) (c Authenticate, err error) {
c.CommandHead = head
if c.CommandHead.TYPE != AuthenticateType {
err = fmt.Errorf("error command type: %s", c.CommandHead.TYPE)
2022-11-29 00:42:26 +08:00
return
2022-11-28 17:09:25 +08:00
}
2023-06-12 17:44:22 +08:00
_, err = io.ReadFull(reader, c.UUID[:])
if err != nil {
return
}
_, err = io.ReadFull(reader, c.TOKEN[:])
2022-11-28 17:09:25 +08:00
if err != nil {
return
}
return
}
func ReadAuthenticate(reader BufferedReader) (c Authenticate, err error) {
head, err := ReadCommandHead(reader)
if err != nil {
return
}
return ReadAuthenticateWithHead(head, reader)
}
2023-06-12 17:44:22 +08:00
func GenToken(state quic.ConnectionState, uuid [16]byte, password string) (token [32]byte, err error) {
var tokenBytes []byte
tokenBytes, err = state.TLS.ExportKeyingMaterial(utils.StringFromImmutableBytes(uuid[:]), utils.ImmutableBytesFromString(password), 32)
if err != nil {
return
}
copy(token[:], tokenBytes)
return
2022-11-25 08:08:14 +08:00
}
func (c Authenticate) WriteTo(writer BufferedWriter) (err error) {
err = c.CommandHead.WriteTo(writer)
if err != nil {
return
}
2023-06-12 17:44:22 +08:00
_, err = writer.Write(c.UUID[:])
if err != nil {
return
}
_, err = writer.Write(c.TOKEN[:])
2022-11-25 08:08:14 +08:00
if err != nil {
return
}
return
}
func (c Authenticate) BytesLen() int {
2023-06-12 17:44:22 +08:00
return c.CommandHead.BytesLen() + 16 + 32
2022-11-25 08:08:14 +08:00
}
type Connect struct {
CommandHead
ADDR Address
}
func NewConnect(ADDR Address) Connect {
return Connect{
CommandHead: NewCommandHead(ConnectType),
ADDR: ADDR,
}
}
2022-11-28 17:09:25 +08:00
func ReadConnectWithHead(head CommandHead, reader BufferedReader) (c Connect, err error) {
c.CommandHead = head
if c.CommandHead.TYPE != ConnectType {
err = fmt.Errorf("error command type: %s", c.CommandHead.TYPE)
2022-11-29 00:42:26 +08:00
return
2022-11-28 17:09:25 +08:00
}
c.ADDR, err = ReadAddress(reader)
if err != nil {
return
}
return
}
func ReadConnect(reader BufferedReader) (c Connect, err error) {
head, err := ReadCommandHead(reader)
if err != nil {
return
}
return ReadConnectWithHead(head, reader)
}
2022-11-25 08:08:14 +08:00
func (c Connect) WriteTo(writer BufferedWriter) (err error) {
err = c.CommandHead.WriteTo(writer)
if err != nil {
return
}
err = c.ADDR.WriteTo(writer)
if err != nil {
return
}
return
}
func (c Connect) BytesLen() int {
return c.CommandHead.BytesLen() + c.ADDR.BytesLen()
}
type Packet struct {
CommandHead
2023-06-12 17:44:22 +08:00
ASSOC_ID uint16
PKT_ID uint16
FRAG_TOTAL uint8
FRAG_ID uint8
SIZE uint16
ADDR Address
DATA []byte
2022-11-25 08:08:14 +08:00
}
2023-06-12 17:44:22 +08:00
func NewPacket(ASSOC_ID uint16, PKT_ID uint16, FRGA_TOTAL uint8, FRAG_ID uint8, SIZE uint16, ADDR Address, DATA []byte) Packet {
2022-11-25 08:08:14 +08:00
return Packet{
CommandHead: NewCommandHead(PacketType),
ASSOC_ID: ASSOC_ID,
2023-06-12 17:44:22 +08:00
PKT_ID: PKT_ID,
FRAG_ID: FRAG_ID,
FRAG_TOTAL: FRGA_TOTAL,
SIZE: SIZE,
2022-11-25 08:08:14 +08:00
ADDR: ADDR,
DATA: DATA,
}
}
2022-11-26 23:53:59 +08:00
func ReadPacketWithHead(head CommandHead, reader BufferedReader) (c Packet, err error) {
c.CommandHead = head
2022-11-25 08:08:14 +08:00
if c.CommandHead.TYPE != PacketType {
err = fmt.Errorf("error command type: %s", c.CommandHead.TYPE)
2022-11-29 00:42:26 +08:00
return
2022-11-25 08:08:14 +08:00
}
err = binary.Read(reader, binary.BigEndian, &c.ASSOC_ID)
if err != nil {
return
}
2023-06-12 17:44:22 +08:00
err = binary.Read(reader, binary.BigEndian, &c.PKT_ID)
if err != nil {
return
}
err = binary.Read(reader, binary.BigEndian, &c.FRAG_TOTAL)
if err != nil {
return
}
err = binary.Read(reader, binary.BigEndian, &c.FRAG_ID)
if err != nil {
return
}
err = binary.Read(reader, binary.BigEndian, &c.SIZE)
2022-11-25 08:08:14 +08:00
if err != nil {
return
}
c.ADDR, err = ReadAddress(reader)
if err != nil {
return
}
2023-06-12 17:44:22 +08:00
c.DATA = make([]byte, c.SIZE)
2022-11-25 08:08:14 +08:00
_, err = io.ReadFull(reader, c.DATA)
if err != nil {
return
}
return
}
2022-11-26 23:53:59 +08:00
func ReadPacket(reader BufferedReader) (c Packet, err error) {
head, err := ReadCommandHead(reader)
if err != nil {
return
}
return ReadPacketWithHead(head, reader)
}
2022-11-25 08:08:14 +08:00
func (c Packet) WriteTo(writer BufferedWriter) (err error) {
err = c.CommandHead.WriteTo(writer)
if err != nil {
return
}
err = binary.Write(writer, binary.BigEndian, c.ASSOC_ID)
if err != nil {
return
}
2023-06-12 17:44:22 +08:00
err = binary.Write(writer, binary.BigEndian, c.PKT_ID)
if err != nil {
return
}
err = binary.Write(writer, binary.BigEndian, c.FRAG_TOTAL)
if err != nil {
return
}
err = binary.Write(writer, binary.BigEndian, c.FRAG_ID)
if err != nil {
return
}
err = binary.Write(writer, binary.BigEndian, c.SIZE)
2022-11-25 08:08:14 +08:00
if err != nil {
return
}
err = c.ADDR.WriteTo(writer)
if err != nil {
return
}
_, err = writer.Write(c.DATA)
if err != nil {
return
}
return
}
func (c Packet) BytesLen() int {
return c.CommandHead.BytesLen() + 4 + 2 + c.ADDR.BytesLen() + len(c.DATA)
}
2023-06-12 17:44:22 +08:00
var PacketOverHead = NewPacket(0, 0, 0, 0, 0, NewAddressAddrPort(netip.AddrPortFrom(netip.IPv6Unspecified(), 0)), nil).BytesLen()
2022-11-25 08:08:14 +08:00
type Dissociate struct {
CommandHead
2023-06-12 17:44:22 +08:00
ASSOC_ID uint16
2022-11-25 08:08:14 +08:00
}
2023-06-12 17:44:22 +08:00
func NewDissociate(ASSOC_ID uint16) Dissociate {
2022-11-25 08:08:14 +08:00
return Dissociate{
CommandHead: NewCommandHead(DissociateType),
ASSOC_ID: ASSOC_ID,
}
}
2022-11-28 17:09:25 +08:00
func ReadDissociateWithHead(head CommandHead, reader BufferedReader) (c Dissociate, err error) {
c.CommandHead = head
2022-11-29 00:42:26 +08:00
if c.CommandHead.TYPE != DissociateType {
2022-11-28 17:09:25 +08:00
err = fmt.Errorf("error command type: %s", c.CommandHead.TYPE)
2022-11-29 00:42:26 +08:00
return
2022-11-28 17:09:25 +08:00
}
err = binary.Read(reader, binary.BigEndian, &c.ASSOC_ID)
if err != nil {
return
}
return
}
func ReadDissociate(reader BufferedReader) (c Dissociate, err error) {
head, err := ReadCommandHead(reader)
if err != nil {
return
}
return ReadDissociateWithHead(head, reader)
}
2022-11-25 08:08:14 +08:00
func (c Dissociate) WriteTo(writer BufferedWriter) (err error) {
err = c.CommandHead.WriteTo(writer)
if err != nil {
return
}
err = binary.Write(writer, binary.BigEndian, c.ASSOC_ID)
if err != nil {
return
}
return
}
func (c Dissociate) BytesLen() int {
return c.CommandHead.BytesLen() + 4
}
type Heartbeat struct {
CommandHead
}
func NewHeartbeat() Heartbeat {
return Heartbeat{
CommandHead: NewCommandHead(HeartbeatType),
}
}
func ReadHeartbeatWithHead(head CommandHead, reader BufferedReader) (c Heartbeat, err error) {
2022-11-26 23:53:59 +08:00
c.CommandHead = head
2022-11-25 08:08:14 +08:00
if c.CommandHead.TYPE != HeartbeatType {
err = fmt.Errorf("error command type: %s", c.CommandHead.TYPE)
2022-11-29 00:42:26 +08:00
return
2022-11-25 08:08:14 +08:00
}
return
}
func ReadHeartbeat(reader BufferedReader) (c Heartbeat, err error) {
2022-11-26 23:53:59 +08:00
head, err := ReadCommandHead(reader)
if err != nil {
return
}
return ReadHeartbeatWithHead(head, reader)
}
2022-11-25 08:08:14 +08:00
// Addr types
const (
AtypDomainName byte = 0
AtypIPv4 byte = 1
AtypIPv6 byte = 2
2023-06-12 17:44:22 +08:00
AtypNone byte = 255 // Address type None is used in Packet commands that is not the first fragment of a UDP packet.
2022-11-25 08:08:14 +08:00
)
type Address struct {
TYPE byte
ADDR []byte
PORT uint16
}
func NewAddress(metadata *C.Metadata) Address {
var addrType byte
var addr []byte
switch metadata.AddrType() {
case socks5.AtypIPv4:
addrType = AtypIPv4
2023-01-16 09:54:52 +08:00
addr = metadata.DstIP.AsSlice()
2022-11-25 08:08:14 +08:00
case socks5.AtypIPv6:
addrType = AtypIPv6
2023-01-16 09:54:52 +08:00
addr = metadata.DstIP.AsSlice()
2022-11-25 08:08:14 +08:00
case socks5.AtypDomainName:
addrType = AtypDomainName
addr = make([]byte, len(metadata.Host)+1)
addr[0] = byte(len(metadata.Host))
copy(addr[1:], metadata.Host)
}
return Address{
TYPE: addrType,
ADDR: addr,
PORT: metadata.DstPort,
2022-11-25 08:08:14 +08:00
}
}
func NewAddressNetAddr(addr net.Addr) (Address, error) {
if addr, ok := addr.(interface{ AddrPort() netip.AddrPort }); ok {
if addrPort := addr.AddrPort(); addrPort.IsValid() { // sing's M.Socksaddr maybe return an invalid AddrPort if it's a DomainName
return NewAddressAddrPort(addrPort), nil
}
}
addrStr := addr.String()
if addrPort, err := netip.ParseAddrPort(addrStr); err == nil {
return NewAddressAddrPort(addrPort), nil
}
metadata := &C.Metadata{}
if err := metadata.SetRemoteAddress(addrStr); err != nil {
return Address{}, err
}
return NewAddress(metadata), nil
}
2022-11-25 08:08:14 +08:00
func NewAddressAddrPort(addrPort netip.AddrPort) Address {
var addrType byte
2023-03-12 19:03:03 +08:00
port := addrPort.Port()
addr := addrPort.Addr().Unmap()
if addr.Is4() {
2022-11-25 08:08:14 +08:00
addrType = AtypIPv4
} else {
addrType = AtypIPv6
}
return Address{
TYPE: addrType,
2023-03-12 19:03:03 +08:00
ADDR: addr.AsSlice(),
PORT: port,
2022-11-25 08:08:14 +08:00
}
}
func ReadAddress(reader BufferedReader) (c Address, err error) {
c.TYPE, err = reader.ReadByte()
if err != nil {
return
}
switch c.TYPE {
case AtypIPv4:
c.ADDR = make([]byte, net.IPv4len)
_, err = io.ReadFull(reader, c.ADDR)
if err != nil {
return
}
case AtypIPv6:
c.ADDR = make([]byte, net.IPv6len)
_, err = io.ReadFull(reader, c.ADDR)
if err != nil {
return
}
case AtypDomainName:
var addrLen byte
addrLen, err = reader.ReadByte()
if err != nil {
return
}
c.ADDR = make([]byte, addrLen+1)
c.ADDR[0] = addrLen
_, err = io.ReadFull(reader, c.ADDR[1:])
if err != nil {
return
}
}
2023-06-12 17:44:22 +08:00
if c.TYPE == AtypNone {
return
}
2022-11-25 08:08:14 +08:00
err = binary.Read(reader, binary.BigEndian, &c.PORT)
if err != nil {
return
}
return
}
func (c Address) WriteTo(writer BufferedWriter) (err error) {
err = writer.WriteByte(c.TYPE)
if err != nil {
return
}
2023-06-12 17:44:22 +08:00
if c.TYPE == AtypNone {
return
}
2022-11-25 08:08:14 +08:00
_, err = writer.Write(c.ADDR[:])
if err != nil {
return
}
err = binary.Write(writer, binary.BigEndian, c.PORT)
if err != nil {
return
}
return
}
2022-11-28 17:09:25 +08:00
func (c Address) String() string {
switch c.TYPE {
case AtypDomainName:
return net.JoinHostPort(string(c.ADDR[1:]), strconv.Itoa(int(c.PORT)))
default:
addr, _ := netip.AddrFromSlice(c.ADDR)
addrPort := netip.AddrPortFrom(addr, c.PORT)
return addrPort.String()
}
}
func (c Address) SocksAddr() socks5.Addr {
addr := make([]byte, 1+len(c.ADDR)+2)
switch c.TYPE {
case AtypIPv4:
addr[0] = socks5.AtypIPv4
case AtypIPv6:
addr[0] = socks5.AtypIPv6
case AtypDomainName:
addr[0] = socks5.AtypDomainName
}
copy(addr[1:], c.ADDR)
binary.BigEndian.PutUint16(addr[len(addr)-2:], c.PORT)
return addr
}
2022-11-25 08:08:14 +08:00
func (c Address) UDPAddr() *net.UDPAddr {
return &net.UDPAddr{
IP: c.ADDR,
Port: int(c.PORT),
Zone: "",
}
}
func (c Address) BytesLen() int {
return 1 + len(c.ADDR) + 2
}
const (
ProtocolError = quic.ApplicationErrorCode(0xfffffff0)
AuthenticationFailed = quic.ApplicationErrorCode(0xfffffff1)
AuthenticationTimeout = quic.ApplicationErrorCode(0xfffffff2)
BadCommand = quic.ApplicationErrorCode(0xfffffff3)
)