v2fly/proxy/shadowsocks/protocol.go

273 lines
7.6 KiB
Go
Raw Normal View History

2016-01-28 11:33:58 +00:00
package shadowsocks
import (
"crypto/hmac"
2016-10-31 14:24:28 +00:00
"crypto/rand"
"crypto/sha256"
"hash/crc32"
2016-01-28 11:33:58 +00:00
"io"
2016-12-09 11:08:25 +00:00
2021-02-16 20:31:50 +00:00
"github.com/v2fly/v2ray-core/v4/common"
"github.com/v2fly/v2ray-core/v4/common/buf"
"github.com/v2fly/v2ray-core/v4/common/drain"
2021-02-16 20:31:50 +00:00
"github.com/v2fly/v2ray-core/v4/common/net"
"github.com/v2fly/v2ray-core/v4/common/protocol"
2016-01-28 11:33:58 +00:00
)
const (
2020-10-03 22:29:21 +00:00
Version = 1
2018-02-23 22:42:01 +00:00
)
2016-10-31 14:24:28 +00:00
2018-02-23 22:42:01 +00:00
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
}),
2016-01-28 11:33:58 +00:00
)
2017-12-14 14:02:36 +00:00
// ReadTCPSession reads a Shadowsocks TCP session from the given reader, returns its header and remaining parts.
2018-08-26 22:11:32 +00:00
func ReadTCPSession(user *protocol.MemoryUser, reader io.Reader) (*protocol.RequestHeader, buf.Reader, error) {
account := user.Account.(*MemoryAccount)
2016-05-01 15:18:02 +00:00
hashkdf := hmac.New(sha256.New, []byte("SSBSKDF"))
hashkdf.Write(account.Key)
behaviorSeed := crc32.ChecksumIEEE(hashkdf.Sum(nil))
drainer, err := drain.NewBehaviorSeedLimitedDrainer(int64(behaviorSeed), 16+38, 3266, 64)
if err != nil {
return nil, nil, newError("failed to initialize drainer").Base(err)
}
2018-03-09 10:26:00 +00:00
buffer := buf.New()
2016-01-28 11:33:58 +00:00
defer buffer.Release()
2016-10-31 14:24:28 +00:00
ivLen := account.Cipher.IVSize()
2017-11-29 21:19:04 +00:00
var iv []byte
if ivLen > 0 {
2018-11-02 14:01:33 +00:00
if _, err := buffer.ReadFullFrom(reader, ivLen); err != nil {
drainer.AcknowledgeReceive(int(buffer.Len()))
return nil, nil, drain.WithError(drainer, reader, newError("failed to read IV").Base(err))
2017-11-29 21:19:04 +00:00
}
2016-01-28 11:33:58 +00:00
2017-11-29 21:19:04 +00:00
iv = append([]byte(nil), buffer.BytesTo(ivLen)...)
}
2016-10-31 14:24:28 +00:00
2017-11-25 23:51:54 +00:00
r, err := account.Cipher.NewDecryptionReader(account.Key, iv, reader)
2016-10-31 14:24:28 +00:00
if err != nil {
drainer.AcknowledgeReceive(int(buffer.Len()))
return nil, nil, drain.WithError(drainer, reader, newError("failed to initialize decoding stream").Base(err).AtError())
2016-10-31 14:24:28 +00:00
}
2018-04-20 22:54:53 +00:00
br := &buf.BufferedReader{Reader: r}
2016-10-31 14:24:28 +00:00
request := &protocol.RequestHeader{
Version: Version,
User: user,
Command: protocol.RequestCommandTCP,
}
drainer.AcknowledgeReceive(int(buffer.Len()))
2018-02-23 22:42:01 +00:00
buffer.Clear()
addr, port, err := addrParser.ReadAddressPort(buffer, br)
if err != nil {
drainer.AcknowledgeReceive(int(buffer.Len()))
return nil, nil, drain.WithError(drainer, reader, newError("failed to read address").Base(err))
2016-10-31 14:24:28 +00:00
}
2016-01-28 11:33:58 +00:00
2018-02-23 22:42:01 +00:00
request.Address = addr
request.Port = port
2016-12-13 08:17:39 +00:00
if request.Address == nil {
drainer.AcknowledgeReceive(int(buffer.Len()))
return nil, nil, drain.WithError(drainer, reader, newError("invalid remote address."))
2016-12-13 08:17:39 +00:00
}
if ivError := account.CheckIV(iv); ivError != nil {
drainer.AcknowledgeReceive(int(buffer.Len()))
return nil, nil, drain.WithError(drainer, reader, newError("failed iv check").Base(ivError))
}
return request, br, nil
2016-10-31 14:24:28 +00:00
}
2017-12-14 14:02:36 +00:00
// WriteTCPRequest writes Shadowsocks request into the given writer, and returns a writer for body.
2016-12-09 12:17:34 +00:00
func WriteTCPRequest(request *protocol.RequestHeader, writer io.Writer) (buf.Writer, error) {
2016-10-31 14:24:28 +00:00
user := request.User
2018-08-26 22:11:32 +00:00
account := user.Account.(*MemoryAccount)
2016-10-31 14:24:28 +00:00
2017-11-29 21:19:04 +00:00
var iv []byte
if account.Cipher.IVSize() > 0 {
iv = make([]byte, account.Cipher.IVSize())
common.Must2(rand.Read(iv))
if ivError := account.CheckIV(iv); ivError != nil {
return nil, newError("failed to mark outgoing iv").Base(ivError)
}
2018-07-28 13:03:40 +00:00
if err := buf.WriteAllBytes(writer, iv); err != nil {
2017-11-29 21:19:04 +00:00
return nil, newError("failed to write IV")
}
2016-10-31 14:24:28 +00:00
}
2017-11-25 23:51:54 +00:00
w, err := account.Cipher.NewEncryptionWriter(account.Key, iv, writer)
2016-10-31 14:24:28 +00:00
if err != nil {
2017-04-26 22:09:01 +00:00
return nil, newError("failed to create encoding stream").Base(err).AtError()
2016-10-31 14:24:28 +00:00
}
2018-03-09 10:26:00 +00:00
header := buf.New()
2016-10-31 14:24:28 +00:00
2018-02-23 22:42:01 +00:00
if err := addrParser.WriteAddressPort(header, request.Address, request.Port); err != nil {
2017-12-03 23:55:34 +00:00
return nil, newError("failed to write address").Base(err)
2016-10-31 14:24:28 +00:00
}
2018-11-16 10:08:12 +00:00
if err := w.WriteMultiBuffer(buf.MultiBuffer{header}); err != nil {
2017-04-09 11:30:46 +00:00
return nil, newError("failed to write header").Base(err)
2016-10-31 14:24:28 +00:00
}
return w, nil
2016-10-31 14:24:28 +00:00
}
2018-08-26 22:11:32 +00:00
func ReadTCPResponse(user *protocol.MemoryUser, reader io.Reader) (buf.Reader, error) {
account := user.Account.(*MemoryAccount)
hashkdf := hmac.New(sha256.New, []byte("SSBSKDF"))
hashkdf.Write(account.Key)
behaviorSeed := crc32.ChecksumIEEE(hashkdf.Sum(nil))
drainer, err := drain.NewBehaviorSeedLimitedDrainer(int64(behaviorSeed), 16+38, 3266, 64)
if err != nil {
return nil, newError("failed to initialize drainer").Base(err)
}
2017-11-29 21:19:04 +00:00
var iv []byte
if account.Cipher.IVSize() > 0 {
iv = make([]byte, account.Cipher.IVSize())
if n, err := io.ReadFull(reader, iv); err != nil {
2017-11-29 21:19:04 +00:00
return nil, newError("failed to read IV").Base(err)
} else { // nolint: golint
drainer.AcknowledgeReceive(n)
2017-11-29 21:19:04 +00:00
}
2016-10-31 14:24:28 +00:00
}
if ivError := account.CheckIV(iv); ivError != nil {
return nil, drain.WithError(drainer, reader, newError("failed iv check").Base(ivError))
}
2017-11-25 23:51:54 +00:00
return account.Cipher.NewDecryptionReader(account.Key, iv, reader)
2016-10-31 14:24:28 +00:00
}
2016-12-09 12:17:34 +00:00
func WriteTCPResponse(request *protocol.RequestHeader, writer io.Writer) (buf.Writer, error) {
2016-10-31 14:24:28 +00:00
user := request.User
2018-08-26 22:11:32 +00:00
account := user.Account.(*MemoryAccount)
2016-10-31 14:24:28 +00:00
2017-11-29 21:19:04 +00:00
var iv []byte
if account.Cipher.IVSize() > 0 {
iv = make([]byte, account.Cipher.IVSize())
common.Must2(rand.Read(iv))
if ivError := account.CheckIV(iv); ivError != nil {
return nil, newError("failed to mark outgoing iv").Base(ivError)
}
2018-07-28 13:03:40 +00:00
if err := buf.WriteAllBytes(writer, iv); err != nil {
2017-11-29 21:19:04 +00:00
return nil, newError("failed to write IV.").Base(err)
}
2016-10-31 14:24:28 +00:00
}
2017-11-25 23:51:54 +00:00
return account.Cipher.NewEncryptionWriter(account.Key, iv, writer)
2016-10-31 14:24:28 +00:00
}
2017-04-21 12:51:09 +00:00
func EncodeUDPPacket(request *protocol.RequestHeader, payload []byte) (*buf.Buffer, error) {
2016-10-31 14:24:28 +00:00
user := request.User
2018-08-26 22:11:32 +00:00
account := user.Account.(*MemoryAccount)
2016-10-31 14:24:28 +00:00
2017-04-15 19:19:21 +00:00
buffer := buf.New()
2016-10-31 14:24:28 +00:00
ivLen := account.Cipher.IVSize()
2017-11-29 21:19:04 +00:00
if ivLen > 0 {
2018-11-02 14:01:33 +00:00
common.Must2(buffer.ReadFullFrom(rand.Reader, ivLen))
2017-11-29 21:19:04 +00:00
}
2016-10-31 14:24:28 +00:00
2018-02-23 22:42:01 +00:00
if err := addrParser.WriteAddressPort(buffer, request.Address, request.Port); err != nil {
2017-12-03 23:55:34 +00:00
return nil, newError("failed to write address").Base(err)
2016-10-31 14:24:28 +00:00
}
2018-04-19 20:56:55 +00:00
buffer.Write(payload)
2016-10-31 14:24:28 +00:00
2017-11-26 20:51:30 +00:00
if err := account.Cipher.EncodePacket(account.Key, buffer); err != nil {
return nil, newError("failed to encrypt UDP payload").Base(err)
2017-11-25 23:51:54 +00:00
}
2016-10-31 14:24:28 +00:00
return buffer, nil
}
2018-08-26 22:11:32 +00:00
func DecodeUDPPacket(user *protocol.MemoryUser, payload *buf.Buffer) (*protocol.RequestHeader, *buf.Buffer, error) {
account := user.Account.(*MemoryAccount)
2016-10-31 14:24:28 +00:00
2017-11-27 09:42:51 +00:00
var iv []byte
2017-11-29 21:19:04 +00:00
if !account.Cipher.IsAEAD() && account.Cipher.IVSize() > 0 {
2017-11-27 09:42:51 +00:00
// 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()))
2016-10-31 14:24:28 +00:00
}
2017-11-26 20:51:30 +00:00
if err := account.Cipher.DecodePacket(account.Key, payload); err != nil {
return nil, nil, newError("failed to decrypt UDP payload").Base(err)
2017-11-25 23:51:54 +00:00
}
2016-10-31 14:24:28 +00:00
request := &protocol.RequestHeader{
Version: Version,
User: user,
Command: protocol.RequestCommandUDP,
}
2018-02-23 22:42:01 +00:00
payload.SetByte(0, payload.Byte(0)&0x0F)
2018-02-23 22:42:01 +00:00
addr, port, err := addrParser.ReadAddressPort(nil, payload)
if err != nil {
return nil, nil, newError("failed to parse address").Base(err)
}
2018-02-23 22:42:01 +00:00
request.Address = addr
request.Port = port
2016-10-31 14:24:28 +00:00
return request, payload, nil
2016-01-28 11:33:58 +00:00
}
2016-10-31 15:35:18 +00:00
type UDPReader struct {
Reader io.Reader
2018-08-26 22:11:32 +00:00
User *protocol.MemoryUser
2016-10-31 15:35:18 +00:00
}
2017-11-09 21:33:15 +00:00
func (v *UDPReader) ReadMultiBuffer() (buf.MultiBuffer, error) {
2017-04-15 19:19:21 +00:00
buffer := buf.New()
2018-11-02 14:01:33 +00:00
_, err := buffer.ReadFrom(v.Reader)
2016-10-31 15:35:18 +00:00
if err != nil {
buffer.Release()
return nil, err
}
2016-11-27 20:39:09 +00:00
_, payload, err := DecodeUDPPacket(v.User, buffer)
2016-10-31 15:35:18 +00:00
if err != nil {
buffer.Release()
return nil, err
}
2018-11-16 10:08:12 +00:00
return buf.MultiBuffer{payload}, nil
2016-10-31 15:35:18 +00:00
}
type UDPWriter struct {
Writer io.Writer
Request *protocol.RequestHeader
}
2017-04-21 13:36:05 +00:00
// Write implements io.Writer.
2017-04-21 12:51:09 +00:00
func (w *UDPWriter) Write(payload []byte) (int, error) {
packet, err := EncodeUDPPacket(w.Request, payload)
2016-10-31 15:35:18 +00:00
if err != nil {
2017-04-21 12:51:09 +00:00
return 0, err
2016-10-31 15:35:18 +00:00
}
2017-04-21 12:51:09 +00:00
_, err = w.Writer.Write(packet.Bytes())
packet.Release()
return len(payload), err
2016-10-31 15:35:18 +00:00
}