2024-05-27 19:24:41 +08:00
|
|
|
package sniff
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/binary"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/sagernet/sing-box/adapter"
|
|
|
|
C "github.com/sagernet/sing-box/constant"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
trackerConnectFlag = 0
|
|
|
|
trackerProtocolID = 0x41727101980
|
|
|
|
trackerConnectMinSize = 16
|
|
|
|
)
|
|
|
|
|
|
|
|
// BitTorrent detects if the stream is a BitTorrent connection.
|
|
|
|
// For the BitTorrent protocol specification, see https://www.bittorrent.org/beps/bep_0003.html
|
2024-07-07 15:45:50 +08:00
|
|
|
func BitTorrent(_ context.Context, metadata *adapter.InboundContext, reader io.Reader) error {
|
2024-05-27 19:24:41 +08:00
|
|
|
var first byte
|
|
|
|
err := binary.Read(reader, binary.BigEndian, &first)
|
|
|
|
if err != nil {
|
2024-07-07 15:45:50 +08:00
|
|
|
return err
|
2024-05-27 19:24:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if first != 19 {
|
2024-07-07 15:45:50 +08:00
|
|
|
return os.ErrInvalid
|
2024-05-27 19:24:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var protocol [19]byte
|
|
|
|
_, err = reader.Read(protocol[:])
|
|
|
|
if err != nil {
|
2024-07-07 15:45:50 +08:00
|
|
|
return err
|
2024-05-27 19:24:41 +08:00
|
|
|
}
|
|
|
|
if string(protocol[:]) != "BitTorrent protocol" {
|
2024-07-07 15:45:50 +08:00
|
|
|
return os.ErrInvalid
|
2024-05-27 19:24:41 +08:00
|
|
|
}
|
|
|
|
|
2024-07-07 15:45:50 +08:00
|
|
|
metadata.Protocol = C.ProtocolBitTorrent
|
|
|
|
return nil
|
2024-05-27 19:24:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// UTP detects if the packet is a uTP connection packet.
|
|
|
|
// For the uTP protocol specification, see
|
|
|
|
// 1. https://www.bittorrent.org/beps/bep_0029.html
|
|
|
|
// 2. https://github.com/bittorrent/libutp/blob/2b364cbb0650bdab64a5de2abb4518f9f228ec44/utp_internal.cpp#L112
|
2024-07-07 15:45:50 +08:00
|
|
|
func UTP(_ context.Context, metadata *adapter.InboundContext, packet []byte) error {
|
2024-05-27 19:24:41 +08:00
|
|
|
// A valid uTP packet must be at least 20 bytes long.
|
|
|
|
if len(packet) < 20 {
|
2024-07-07 15:45:50 +08:00
|
|
|
return os.ErrInvalid
|
2024-05-27 19:24:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
version := packet[0] & 0x0F
|
|
|
|
ty := packet[0] >> 4
|
|
|
|
if version != 1 || ty > 4 {
|
2024-07-07 15:45:50 +08:00
|
|
|
return os.ErrInvalid
|
2024-05-27 19:24:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validate the extensions
|
|
|
|
extension := packet[1]
|
|
|
|
reader := bytes.NewReader(packet[20:])
|
|
|
|
for extension != 0 {
|
|
|
|
err := binary.Read(reader, binary.BigEndian, &extension)
|
|
|
|
if err != nil {
|
2024-07-07 15:45:50 +08:00
|
|
|
return err
|
2024-05-27 19:24:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var length byte
|
|
|
|
err = binary.Read(reader, binary.BigEndian, &length)
|
|
|
|
if err != nil {
|
2024-07-07 15:45:50 +08:00
|
|
|
return err
|
2024-05-27 19:24:41 +08:00
|
|
|
}
|
|
|
|
_, err = reader.Seek(int64(length), io.SeekCurrent)
|
|
|
|
if err != nil {
|
2024-07-07 15:45:50 +08:00
|
|
|
return err
|
2024-05-27 19:24:41 +08:00
|
|
|
}
|
|
|
|
}
|
2024-07-07 15:45:50 +08:00
|
|
|
metadata.Protocol = C.ProtocolBitTorrent
|
|
|
|
return nil
|
2024-05-27 19:24:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// UDPTracker detects if the packet is a UDP Tracker Protocol packet.
|
|
|
|
// For the UDP Tracker Protocol specification, see https://www.bittorrent.org/beps/bep_0015.html
|
2024-07-07 15:45:50 +08:00
|
|
|
func UDPTracker(_ context.Context, metadata *adapter.InboundContext, packet []byte) error {
|
2024-05-27 19:24:41 +08:00
|
|
|
if len(packet) < trackerConnectMinSize {
|
2024-07-07 15:45:50 +08:00
|
|
|
return os.ErrInvalid
|
2024-05-27 19:24:41 +08:00
|
|
|
}
|
|
|
|
if binary.BigEndian.Uint64(packet[:8]) != trackerProtocolID {
|
2024-07-07 15:45:50 +08:00
|
|
|
return os.ErrInvalid
|
2024-05-27 19:24:41 +08:00
|
|
|
}
|
|
|
|
if binary.BigEndian.Uint32(packet[8:12]) != trackerConnectFlag {
|
2024-07-07 15:45:50 +08:00
|
|
|
return os.ErrInvalid
|
2024-05-27 19:24:41 +08:00
|
|
|
}
|
2024-07-07 15:45:50 +08:00
|
|
|
metadata.Protocol = C.ProtocolBitTorrent
|
|
|
|
return nil
|
2024-05-27 19:24:41 +08:00
|
|
|
}
|