2021-02-11 11:33:08 +00:00
|
|
|
package xudp
|
2021-02-11 01:28:21 +00:00
|
|
|
|
|
|
|
import (
|
2023-04-06 10:21:35 +00:00
|
|
|
"context"
|
|
|
|
"crypto/rand"
|
|
|
|
"encoding/base64"
|
|
|
|
"fmt"
|
2021-02-11 01:28:21 +00:00
|
|
|
"io"
|
2023-04-06 10:21:35 +00:00
|
|
|
"os"
|
|
|
|
"strings"
|
2021-02-11 01:28:21 +00:00
|
|
|
|
|
|
|
"github.com/xtls/xray-core/common/buf"
|
|
|
|
"github.com/xtls/xray-core/common/net"
|
|
|
|
"github.com/xtls/xray-core/common/protocol"
|
2023-04-06 10:21:35 +00:00
|
|
|
"github.com/xtls/xray-core/common/session"
|
|
|
|
"lukechampine.com/blake3"
|
2021-02-11 01:28:21 +00:00
|
|
|
)
|
|
|
|
|
2023-04-06 10:21:35 +00:00
|
|
|
var AddrParser = protocol.NewAddressParser(
|
2021-02-11 01:28:21 +00:00
|
|
|
protocol.AddressFamilyByte(byte(protocol.AddressTypeIPv4), net.AddressFamilyIPv4),
|
|
|
|
protocol.AddressFamilyByte(byte(protocol.AddressTypeDomain), net.AddressFamilyDomain),
|
|
|
|
protocol.AddressFamilyByte(byte(protocol.AddressTypeIPv6), net.AddressFamilyIPv6),
|
|
|
|
protocol.PortThenAddress(),
|
|
|
|
)
|
|
|
|
|
2023-04-06 10:21:35 +00:00
|
|
|
var (
|
|
|
|
Show bool
|
2023-04-07 11:13:20 +00:00
|
|
|
BaseKey []byte
|
2023-04-06 10:21:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
EnvShow = "XRAY_XUDP_SHOW"
|
|
|
|
EnvBaseKey = "XRAY_XUDP_BASEKEY"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
if strings.ToLower(os.Getenv(EnvShow)) == "true" {
|
|
|
|
Show = true
|
|
|
|
}
|
2023-04-07 11:13:20 +00:00
|
|
|
if raw, found := os.LookupEnv(EnvBaseKey); found {
|
|
|
|
if BaseKey, _ = base64.RawURLEncoding.DecodeString(raw); len(BaseKey) == 32 {
|
2023-04-06 10:21:35 +00:00
|
|
|
return
|
|
|
|
}
|
2023-04-07 11:13:20 +00:00
|
|
|
panic(EnvBaseKey + ": invalid value: " + raw)
|
2023-04-06 10:21:35 +00:00
|
|
|
}
|
2023-04-07 11:13:20 +00:00
|
|
|
rand.Read(BaseKey)
|
2023-04-06 10:21:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetGlobalID(ctx context.Context) (globalID [8]byte) {
|
2023-04-12 15:20:26 +00:00
|
|
|
if cone := ctx.Value("cone"); cone == nil || !cone.(bool) { // cone is nil only in some unit tests
|
|
|
|
return
|
|
|
|
}
|
2023-04-06 10:21:35 +00:00
|
|
|
if inbound := session.InboundFromContext(ctx); inbound != nil && inbound.Source.Network == net.Network_UDP &&
|
|
|
|
(inbound.Name == "dokodemo-door" || inbound.Name == "socks" || inbound.Name == "shadowsocks") {
|
2023-04-07 11:13:20 +00:00
|
|
|
h := blake3.New(8, BaseKey)
|
2023-04-06 10:21:35 +00:00
|
|
|
h.Write([]byte(inbound.Source.String()))
|
|
|
|
copy(globalID[:], h.Sum(nil))
|
2023-04-07 11:13:20 +00:00
|
|
|
if Show {
|
|
|
|
fmt.Printf("XUDP inbound.Source.String(): %v\tglobalID: %v\n", inbound.Source.String(), globalID)
|
|
|
|
}
|
2023-04-06 10:21:35 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewPacketWriter(writer buf.Writer, dest net.Destination, globalID [8]byte) *PacketWriter {
|
2021-02-11 01:28:21 +00:00
|
|
|
return &PacketWriter{
|
2023-04-06 10:21:35 +00:00
|
|
|
Writer: writer,
|
|
|
|
Dest: dest,
|
|
|
|
GlobalID: globalID,
|
2021-02-11 01:28:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type PacketWriter struct {
|
2023-04-06 10:21:35 +00:00
|
|
|
Writer buf.Writer
|
|
|
|
Dest net.Destination
|
|
|
|
GlobalID [8]byte
|
2021-02-11 01:28:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *PacketWriter) WriteMultiBuffer(mb buf.MultiBuffer) error {
|
|
|
|
defer buf.ReleaseMulti(mb)
|
|
|
|
mb2Write := make(buf.MultiBuffer, 0, len(mb))
|
|
|
|
for _, b := range mb {
|
|
|
|
length := b.Len()
|
|
|
|
if length == 0 || length+666 > buf.Size {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
eb := buf.New()
|
2023-04-16 21:15:27 +00:00
|
|
|
eb.Write([]byte{0, 0, 0, 0}) // Meta data length; Mux Session ID
|
2021-02-11 01:28:21 +00:00
|
|
|
if w.Dest.Network == net.Network_UDP {
|
|
|
|
eb.WriteByte(1) // New
|
|
|
|
eb.WriteByte(1) // Opt
|
|
|
|
eb.WriteByte(2) // UDP
|
2023-04-06 10:21:35 +00:00
|
|
|
AddrParser.WriteAddressPort(eb, w.Dest.Address, w.Dest.Port)
|
|
|
|
if b.UDP != nil { // make sure it's user's proxy request
|
2023-04-16 21:15:27 +00:00
|
|
|
eb.Write(w.GlobalID[:]) // no need to check whether it's empty
|
2023-04-06 10:21:35 +00:00
|
|
|
}
|
2021-02-11 01:28:21 +00:00
|
|
|
w.Dest.Network = net.Network_Unknown
|
|
|
|
} else {
|
|
|
|
eb.WriteByte(2) // Keep
|
2023-04-16 21:15:27 +00:00
|
|
|
eb.WriteByte(1) // Opt
|
2021-02-11 01:28:21 +00:00
|
|
|
if b.UDP != nil {
|
2023-04-16 21:15:27 +00:00
|
|
|
eb.WriteByte(2) // UDP
|
2023-04-06 10:21:35 +00:00
|
|
|
AddrParser.WriteAddressPort(eb, b.UDP.Address, b.UDP.Port)
|
2021-02-11 01:28:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
l := eb.Len() - 2
|
|
|
|
eb.SetByte(0, byte(l>>8))
|
|
|
|
eb.SetByte(1, byte(l))
|
|
|
|
eb.WriteByte(byte(length >> 8))
|
|
|
|
eb.WriteByte(byte(length))
|
|
|
|
eb.Write(b.Bytes())
|
|
|
|
|
|
|
|
mb2Write = append(mb2Write, eb)
|
|
|
|
}
|
|
|
|
if mb2Write.IsEmpty() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return w.Writer.WriteMultiBuffer(mb2Write)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewPacketReader(reader io.Reader) *PacketReader {
|
|
|
|
return &PacketReader{
|
|
|
|
Reader: reader,
|
|
|
|
cache: make([]byte, 2),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type PacketReader struct {
|
|
|
|
Reader io.Reader
|
|
|
|
cache []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *PacketReader) ReadMultiBuffer() (buf.MultiBuffer, error) {
|
|
|
|
for {
|
|
|
|
if _, err := io.ReadFull(r.Reader, r.cache); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
l := int32(r.cache[0])<<8 | int32(r.cache[1])
|
|
|
|
if l < 4 {
|
|
|
|
return nil, io.EOF
|
|
|
|
}
|
|
|
|
b := buf.New()
|
|
|
|
if _, err := b.ReadFullFrom(r.Reader, l); err != nil {
|
|
|
|
b.Release()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
discard := false
|
|
|
|
switch b.Byte(2) {
|
|
|
|
case 2:
|
2023-04-16 21:15:27 +00:00
|
|
|
if l > 4 && b.Byte(4) == 2 { // MUST check the flag first
|
2021-02-11 01:28:21 +00:00
|
|
|
b.Advance(5)
|
2023-04-16 21:15:27 +00:00
|
|
|
// b.Clear() will be called automatically if all data had been read.
|
|
|
|
addr, port, err := AddrParser.ReadAddressPort(nil, b)
|
2021-02-11 01:28:21 +00:00
|
|
|
if err != nil {
|
|
|
|
b.Release()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
b.UDP = &net.Destination{
|
|
|
|
Network: net.Network_UDP,
|
|
|
|
Address: addr,
|
|
|
|
Port: port,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case 4:
|
|
|
|
discard = true
|
|
|
|
default:
|
|
|
|
b.Release()
|
|
|
|
return nil, io.EOF
|
|
|
|
}
|
2023-04-16 21:15:27 +00:00
|
|
|
b.Clear() // in case there is padding (empty bytes) attached
|
2021-02-11 01:28:21 +00:00
|
|
|
if b.Byte(3) == 1 {
|
|
|
|
if _, err := io.ReadFull(r.Reader, r.cache); err != nil {
|
|
|
|
b.Release()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
length := int32(r.cache[0])<<8 | int32(r.cache[1])
|
|
|
|
if length > 0 {
|
|
|
|
if _, err := b.ReadFullFrom(r.Reader, length); err != nil {
|
|
|
|
b.Release()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !discard {
|
|
|
|
return buf.MultiBuffer{b}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
b.Release()
|
|
|
|
}
|
|
|
|
}
|