2020-11-25 13:01:53 +02:00
|
|
|
package shadowsocks
|
|
|
|
|
|
|
|
import (
|
2021-01-19 00:52:35 +02:00
|
|
|
"crypto/cipher"
|
2020-11-25 13:01:53 +02:00
|
|
|
"crypto/hmac"
|
|
|
|
"crypto/rand"
|
|
|
|
"crypto/sha256"
|
|
|
|
"hash/crc32"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
|
2020-12-04 03:36:16 +02:00
|
|
|
"github.com/xtls/xray-core/common"
|
|
|
|
"github.com/xtls/xray-core/common/buf"
|
2021-01-19 00:52:35 +02:00
|
|
|
"github.com/xtls/xray-core/common/crypto"
|
2020-12-04 03:36:16 +02:00
|
|
|
"github.com/xtls/xray-core/common/dice"
|
|
|
|
"github.com/xtls/xray-core/common/net"
|
|
|
|
"github.com/xtls/xray-core/common/protocol"
|
2020-11-25 13:01:53 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
Version = 1
|
|
|
|
)
|
|
|
|
|
|
|
|
var addrParser = protocol.NewAddressParser(
|
|
|
|
protocol.AddressFamilyByte(0x01, net.AddressFamilyIPv4),
|
|
|
|
protocol.AddressFamilyByte(0x04, net.AddressFamilyIPv6),
|
|
|
|
protocol.AddressFamilyByte(0x03, net.AddressFamilyDomain),
|
|
|
|
protocol.WithAddressTypeParser(func(b byte) byte {
|
|
|
|
return b & 0x0F
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
|
2021-01-19 00:52:35 +02:00
|
|
|
type FullReader struct {
|
|
|
|
reader io.Reader
|
|
|
|
buffer []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *FullReader) Read(p []byte) (n int, err error) {
|
|
|
|
if r.buffer != nil {
|
|
|
|
n := copy(p, r.buffer)
|
|
|
|
if n == len(r.buffer) {
|
|
|
|
r.buffer = nil
|
|
|
|
} else {
|
|
|
|
r.buffer = r.buffer[n:]
|
|
|
|
}
|
|
|
|
if n == len(p) {
|
|
|
|
return n, nil
|
|
|
|
} else {
|
|
|
|
m, err := r.reader.Read(p[n:])
|
|
|
|
return n + m, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return r.reader.Read(p)
|
|
|
|
}
|
|
|
|
|
2020-11-25 13:01:53 +02:00
|
|
|
// ReadTCPSession reads a Shadowsocks TCP session from the given reader, returns its header and remaining parts.
|
2021-02-12 17:17:31 +02:00
|
|
|
func ReadTCPSession(validator *Validator, reader io.Reader) (*protocol.RequestHeader, buf.Reader, error) {
|
2020-11-25 13:01:53 +02:00
|
|
|
|
|
|
|
hashkdf := hmac.New(sha256.New, []byte("SSBSKDF"))
|
|
|
|
|
|
|
|
behaviorSeed := crc32.ChecksumIEEE(hashkdf.Sum(nil))
|
|
|
|
|
|
|
|
behaviorRand := dice.NewDeterministicDice(int64(behaviorSeed))
|
|
|
|
BaseDrainSize := behaviorRand.Roll(3266)
|
|
|
|
RandDrainMax := behaviorRand.Roll(64) + 1
|
|
|
|
RandDrainRolled := dice.Roll(RandDrainMax)
|
|
|
|
DrainSize := BaseDrainSize + 16 + 38 + RandDrainRolled
|
|
|
|
readSizeRemain := DrainSize
|
|
|
|
|
2021-01-19 00:52:35 +02:00
|
|
|
var r2 buf.Reader
|
2021-02-12 17:17:31 +02:00
|
|
|
buffer := buf.New()
|
|
|
|
defer buffer.Release()
|
2020-11-25 13:01:53 +02:00
|
|
|
|
2021-02-12 17:17:31 +02:00
|
|
|
var user *protocol.MemoryUser
|
|
|
|
var ivLen int32
|
|
|
|
var err error
|
|
|
|
|
|
|
|
count := validator.Count()
|
|
|
|
if count == 0 {
|
|
|
|
readSizeRemain -= int(buffer.Len())
|
|
|
|
DrainConnN(reader, readSizeRemain)
|
|
|
|
return nil, nil, newError("invalid user")
|
|
|
|
} else if count > 1 {
|
|
|
|
var aead cipher.AEAD
|
2021-01-19 00:52:35 +02:00
|
|
|
|
|
|
|
if _, err := buffer.ReadFullFrom(reader, 50); err != nil {
|
2020-11-25 13:01:53 +02:00
|
|
|
readSizeRemain -= int(buffer.Len())
|
|
|
|
DrainConnN(reader, readSizeRemain)
|
2021-01-19 00:52:35 +02:00
|
|
|
return nil, nil, newError("failed to read 50 bytes").Base(err)
|
2020-11-25 13:01:53 +02:00
|
|
|
}
|
|
|
|
|
2021-01-19 00:52:35 +02:00
|
|
|
bs := buffer.Bytes()
|
2021-02-12 17:17:31 +02:00
|
|
|
user, aead, _, ivLen, err = validator.Get(bs, protocol.RequestCommandTCP)
|
2021-01-19 00:52:35 +02:00
|
|
|
|
2021-02-12 17:17:31 +02:00
|
|
|
if user != nil {
|
|
|
|
reader = &FullReader{reader, bs[ivLen:]}
|
|
|
|
auth := &crypto.AEADAuthenticator{
|
|
|
|
AEAD: aead,
|
|
|
|
NonceGenerator: crypto.GenerateInitialAEADNonce(),
|
2021-01-19 00:52:35 +02:00
|
|
|
}
|
2021-02-12 17:17:31 +02:00
|
|
|
r2 = crypto.NewAuthenticationReader(auth, &crypto.AEADChunkSizeParser{
|
|
|
|
Auth: auth,
|
|
|
|
}, reader, protocol.TransferTypeStream, nil)
|
|
|
|
} else {
|
2021-01-19 00:52:35 +02:00
|
|
|
readSizeRemain -= int(buffer.Len())
|
|
|
|
DrainConnN(reader, readSizeRemain)
|
|
|
|
return nil, nil, newError("failed to match an user").Base(err)
|
|
|
|
}
|
2021-02-12 17:17:31 +02:00
|
|
|
} else {
|
|
|
|
user, ivLen = validator.GetOnlyUser()
|
|
|
|
account := user.Account.(*MemoryAccount)
|
|
|
|
hashkdf.Write(account.Key)
|
2021-01-19 00:52:35 +02:00
|
|
|
var iv []byte
|
|
|
|
if ivLen > 0 {
|
|
|
|
if _, err := buffer.ReadFullFrom(reader, ivLen); err != nil {
|
|
|
|
readSizeRemain -= int(buffer.Len())
|
|
|
|
DrainConnN(reader, readSizeRemain)
|
|
|
|
return nil, nil, newError("failed to read IV").Base(err)
|
|
|
|
}
|
|
|
|
iv = append([]byte(nil), buffer.BytesTo(ivLen)...)
|
|
|
|
}
|
|
|
|
|
|
|
|
r, err := account.Cipher.NewDecryptionReader(account.Key, iv, reader)
|
|
|
|
if err != nil {
|
|
|
|
readSizeRemain -= int(buffer.Len())
|
|
|
|
DrainConnN(reader, readSizeRemain)
|
|
|
|
return nil, nil, newError("failed to initialize decoding stream").Base(err).AtError()
|
|
|
|
}
|
|
|
|
r2 = r
|
2020-11-25 13:01:53 +02:00
|
|
|
}
|
2021-01-19 00:52:35 +02:00
|
|
|
|
|
|
|
br := &buf.BufferedReader{Reader: r2}
|
2020-11-25 13:01:53 +02:00
|
|
|
|
|
|
|
request := &protocol.RequestHeader{
|
|
|
|
Version: Version,
|
|
|
|
User: user,
|
|
|
|
Command: protocol.RequestCommandTCP,
|
|
|
|
}
|
|
|
|
|
|
|
|
readSizeRemain -= int(buffer.Len())
|
|
|
|
buffer.Clear()
|
|
|
|
|
|
|
|
addr, port, err := addrParser.ReadAddressPort(buffer, br)
|
|
|
|
if err != nil {
|
|
|
|
readSizeRemain -= int(buffer.Len())
|
|
|
|
DrainConnN(reader, readSizeRemain)
|
|
|
|
return nil, nil, newError("failed to read address").Base(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
request.Address = addr
|
|
|
|
request.Port = port
|
|
|
|
|
|
|
|
if request.Address == nil {
|
|
|
|
readSizeRemain -= int(buffer.Len())
|
|
|
|
DrainConnN(reader, readSizeRemain)
|
|
|
|
return nil, nil, newError("invalid remote address.")
|
|
|
|
}
|
|
|
|
|
|
|
|
return request, br, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func DrainConnN(reader io.Reader, n int) error {
|
|
|
|
_, err := io.CopyN(ioutil.Discard, reader, int64(n))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteTCPRequest writes Shadowsocks request into the given writer, and returns a writer for body.
|
|
|
|
func WriteTCPRequest(request *protocol.RequestHeader, writer io.Writer) (buf.Writer, error) {
|
|
|
|
user := request.User
|
|
|
|
account := user.Account.(*MemoryAccount)
|
|
|
|
|
|
|
|
var iv []byte
|
|
|
|
if account.Cipher.IVSize() > 0 {
|
|
|
|
iv = make([]byte, account.Cipher.IVSize())
|
|
|
|
common.Must2(rand.Read(iv))
|
|
|
|
if err := buf.WriteAllBytes(writer, iv); err != nil {
|
|
|
|
return nil, newError("failed to write IV")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
w, err := account.Cipher.NewEncryptionWriter(account.Key, iv, writer)
|
|
|
|
if err != nil {
|
|
|
|
return nil, newError("failed to create encoding stream").Base(err).AtError()
|
|
|
|
}
|
|
|
|
|
|
|
|
header := buf.New()
|
|
|
|
|
|
|
|
if err := addrParser.WriteAddressPort(header, request.Address, request.Port); err != nil {
|
|
|
|
return nil, newError("failed to write address").Base(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := w.WriteMultiBuffer(buf.MultiBuffer{header}); err != nil {
|
|
|
|
return nil, newError("failed to write header").Base(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return w, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func ReadTCPResponse(user *protocol.MemoryUser, reader io.Reader) (buf.Reader, error) {
|
|
|
|
account := user.Account.(*MemoryAccount)
|
|
|
|
|
|
|
|
var iv []byte
|
|
|
|
if account.Cipher.IVSize() > 0 {
|
|
|
|
iv = make([]byte, account.Cipher.IVSize())
|
|
|
|
if _, err := io.ReadFull(reader, iv); err != nil {
|
|
|
|
return nil, newError("failed to read IV").Base(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return account.Cipher.NewDecryptionReader(account.Key, iv, reader)
|
|
|
|
}
|
|
|
|
|
|
|
|
func WriteTCPResponse(request *protocol.RequestHeader, writer io.Writer) (buf.Writer, error) {
|
|
|
|
user := request.User
|
|
|
|
account := user.Account.(*MemoryAccount)
|
|
|
|
|
|
|
|
var iv []byte
|
|
|
|
if account.Cipher.IVSize() > 0 {
|
|
|
|
iv = make([]byte, account.Cipher.IVSize())
|
|
|
|
common.Must2(rand.Read(iv))
|
|
|
|
if err := buf.WriteAllBytes(writer, iv); err != nil {
|
|
|
|
return nil, newError("failed to write IV.").Base(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return account.Cipher.NewEncryptionWriter(account.Key, iv, writer)
|
|
|
|
}
|
|
|
|
|
|
|
|
func EncodeUDPPacket(request *protocol.RequestHeader, payload []byte) (*buf.Buffer, error) {
|
|
|
|
user := request.User
|
|
|
|
account := user.Account.(*MemoryAccount)
|
|
|
|
|
|
|
|
buffer := buf.New()
|
|
|
|
ivLen := account.Cipher.IVSize()
|
|
|
|
if ivLen > 0 {
|
|
|
|
common.Must2(buffer.ReadFullFrom(rand.Reader, ivLen))
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := addrParser.WriteAddressPort(buffer, request.Address, request.Port); err != nil {
|
|
|
|
return nil, newError("failed to write address").Base(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer.Write(payload)
|
|
|
|
|
|
|
|
if err := account.Cipher.EncodePacket(account.Key, buffer); err != nil {
|
|
|
|
return nil, newError("failed to encrypt UDP payload").Base(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return buffer, nil
|
|
|
|
}
|
|
|
|
|
2021-02-12 17:17:31 +02:00
|
|
|
func DecodeUDPPacket(validator *Validator, payload *buf.Buffer) (*protocol.RequestHeader, *buf.Buffer, error) {
|
|
|
|
bs := payload.Bytes()
|
|
|
|
if len(bs) <= 32 {
|
|
|
|
return nil, nil, newError("len(bs) <= 32")
|
|
|
|
}
|
|
|
|
|
2021-01-19 00:52:35 +02:00
|
|
|
var user *protocol.MemoryUser
|
|
|
|
var err error
|
|
|
|
|
2021-02-12 17:17:31 +02:00
|
|
|
count := validator.Count()
|
|
|
|
if count == 0 {
|
|
|
|
return nil, nil, newError("invalid user")
|
|
|
|
} else if count > 1 {
|
2021-01-19 00:52:35 +02:00
|
|
|
var d []byte
|
2021-02-12 17:17:31 +02:00
|
|
|
user, _, d, _, err = validator.Get(bs, protocol.RequestCommandUDP)
|
|
|
|
|
|
|
|
if user != nil {
|
|
|
|
payload.Clear()
|
|
|
|
payload.Write(d)
|
|
|
|
} else {
|
|
|
|
return nil, nil, newError("failed to decrypt UDP payload").Base(err)
|
2021-01-19 00:52:35 +02:00
|
|
|
}
|
|
|
|
} else {
|
2021-02-12 17:17:31 +02:00
|
|
|
user, _ = validator.GetOnlyUser()
|
|
|
|
account := user.Account.(*MemoryAccount)
|
2021-01-19 00:52:35 +02:00
|
|
|
|
|
|
|
var iv []byte
|
|
|
|
if !account.Cipher.IsAEAD() && account.Cipher.IVSize() > 0 {
|
|
|
|
// Keep track of IV as it gets removed from payload in DecodePacket.
|
|
|
|
iv = make([]byte, account.Cipher.IVSize())
|
|
|
|
copy(iv, payload.BytesTo(account.Cipher.IVSize()))
|
|
|
|
}
|
2021-02-12 17:17:31 +02:00
|
|
|
if err = account.Cipher.DecodePacket(account.Key, payload); err != nil {
|
|
|
|
return nil, nil, newError("failed to decrypt UDP payload").Base(err)
|
|
|
|
}
|
2020-11-25 13:01:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
request := &protocol.RequestHeader{
|
|
|
|
Version: Version,
|
|
|
|
User: user,
|
|
|
|
Command: protocol.RequestCommandUDP,
|
|
|
|
}
|
|
|
|
|
|
|
|
payload.SetByte(0, payload.Byte(0)&0x0F)
|
|
|
|
|
|
|
|
addr, port, err := addrParser.ReadAddressPort(nil, payload)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, newError("failed to parse address").Base(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
request.Address = addr
|
|
|
|
request.Port = port
|
|
|
|
|
|
|
|
return request, payload, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type UDPReader struct {
|
|
|
|
Reader io.Reader
|
|
|
|
User *protocol.MemoryUser
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *UDPReader) ReadMultiBuffer() (buf.MultiBuffer, error) {
|
|
|
|
buffer := buf.New()
|
|
|
|
_, err := buffer.ReadFrom(v.Reader)
|
|
|
|
if err != nil {
|
|
|
|
buffer.Release()
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-02-12 17:17:31 +02:00
|
|
|
validator := new(Validator)
|
|
|
|
validator.Add(v.User)
|
|
|
|
|
|
|
|
u, payload, err := DecodeUDPPacket(validator, buffer)
|
2020-11-25 13:01:53 +02:00
|
|
|
if err != nil {
|
|
|
|
buffer.Release()
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-12-28 11:40:28 +02:00
|
|
|
dest := u.Destination()
|
|
|
|
payload.UDP = &dest
|
2020-11-25 13:01:53 +02:00
|
|
|
return buf.MultiBuffer{payload}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type UDPWriter struct {
|
|
|
|
Writer io.Writer
|
|
|
|
Request *protocol.RequestHeader
|
|
|
|
}
|
|
|
|
|
2020-12-23 15:06:21 +02:00
|
|
|
func (w *UDPWriter) WriteMultiBuffer(mb buf.MultiBuffer) error {
|
|
|
|
for {
|
|
|
|
mb2, b := buf.SplitFirst(mb)
|
|
|
|
mb = mb2
|
|
|
|
if b == nil {
|
|
|
|
break
|
|
|
|
}
|
2020-12-28 11:40:28 +02:00
|
|
|
request := w.Request
|
2020-12-23 15:06:21 +02:00
|
|
|
if b.UDP != nil {
|
2020-12-28 11:40:28 +02:00
|
|
|
request = &protocol.RequestHeader{
|
2020-12-23 15:06:21 +02:00
|
|
|
User: w.Request.User,
|
2020-12-28 11:40:28 +02:00
|
|
|
Address: b.UDP.Address,
|
|
|
|
Port: b.UDP.Port,
|
2020-12-23 15:06:21 +02:00
|
|
|
}
|
|
|
|
}
|
2020-12-28 11:40:28 +02:00
|
|
|
packet, err := EncodeUDPPacket(request, b.Bytes())
|
2020-12-23 15:06:21 +02:00
|
|
|
b.Release()
|
|
|
|
if err != nil {
|
|
|
|
buf.ReleaseMulti(mb)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = w.Writer.Write(packet.Bytes())
|
|
|
|
packet.Release()
|
|
|
|
if err != nil {
|
|
|
|
buf.ReleaseMulti(mb)
|
|
|
|
return err
|
|
|
|
}
|
2020-11-25 13:01:53 +02:00
|
|
|
}
|
2020-12-23 15:06:21 +02:00
|
|
|
return nil
|
2020-11-25 13:01:53 +02:00
|
|
|
}
|