2016-01-28 06:33:58 -05:00
|
|
|
package shadowsocks
|
|
|
|
|
|
|
|
import (
|
2016-05-24 16:09:22 -04:00
|
|
|
"bytes"
|
2016-10-31 10:24:28 -04:00
|
|
|
"crypto/rand"
|
2016-01-28 06:33:58 -05:00
|
|
|
"io"
|
2016-08-20 14:55:45 -04:00
|
|
|
"v2ray.com/core/common/alloc"
|
2016-10-31 10:24:28 -04:00
|
|
|
"v2ray.com/core/common/crypto"
|
2016-12-04 03:10:47 -05:00
|
|
|
"v2ray.com/core/common/errors"
|
2016-10-31 10:24:28 -04:00
|
|
|
v2io "v2ray.com/core/common/io"
|
2016-08-20 14:55:45 -04:00
|
|
|
v2net "v2ray.com/core/common/net"
|
2016-10-31 10:24:28 -04:00
|
|
|
"v2ray.com/core/common/protocol"
|
2016-01-28 06:33:58 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2016-10-31 10:24:28 -04:00
|
|
|
Version = 1
|
|
|
|
RequestOptionOneTimeAuth = protocol.RequestOption(101)
|
|
|
|
|
2016-01-28 06:33:58 -05:00
|
|
|
AddrTypeIPv4 = 1
|
|
|
|
AddrTypeIPv6 = 4
|
|
|
|
AddrTypeDomain = 3
|
|
|
|
)
|
|
|
|
|
2016-10-31 10:24:28 -04:00
|
|
|
func ReadTCPSession(user *protocol.User, reader io.Reader) (*protocol.RequestHeader, v2io.Reader, error) {
|
|
|
|
rawAccount, err := user.GetTypedAccount()
|
|
|
|
if err != nil {
|
2016-12-04 03:10:47 -05:00
|
|
|
return nil, nil, errors.Base(err).Message("Shadowsocks|TCP: Failed to parse account.")
|
2016-05-01 11:18:02 -04:00
|
|
|
}
|
2016-10-31 10:24:28 -04:00
|
|
|
account := rawAccount.(*ShadowsocksAccount)
|
2016-05-01 11:18:02 -04:00
|
|
|
|
2016-12-05 11:05:47 -05:00
|
|
|
buffer := alloc.NewLocalBuffer(512).Clear()
|
2016-01-28 06:33:58 -05:00
|
|
|
defer buffer.Release()
|
|
|
|
|
2016-10-31 10:24:28 -04:00
|
|
|
ivLen := account.Cipher.IVSize()
|
2016-12-05 11:05:47 -05:00
|
|
|
_, err = buffer.FillFullFrom(reader, ivLen)
|
2016-01-28 06:33:58 -05:00
|
|
|
if err != nil {
|
2016-12-04 03:10:47 -05:00
|
|
|
return nil, nil, errors.Base(err).Message("Shadowsocks|TCP: Failed to read IV.")
|
2016-01-28 06:33:58 -05:00
|
|
|
}
|
|
|
|
|
2016-12-05 09:19:14 -05:00
|
|
|
iv := append([]byte(nil), buffer.Bytes()[:ivLen]...)
|
2016-10-31 10:24:28 -04:00
|
|
|
|
|
|
|
stream, err := account.Cipher.NewDecodingStream(account.Key, iv)
|
|
|
|
if err != nil {
|
2016-12-04 03:10:47 -05:00
|
|
|
return nil, nil, errors.Base(err).Message("Shadowsocks|TCP: Failed to initialize decoding stream.")
|
2016-10-31 10:24:28 -04:00
|
|
|
}
|
|
|
|
reader = crypto.NewCryptionReader(stream, reader)
|
|
|
|
|
|
|
|
authenticator := NewAuthenticator(HeaderKeyGenerator(account.Key, iv))
|
|
|
|
request := &protocol.RequestHeader{
|
|
|
|
Version: Version,
|
|
|
|
User: user,
|
|
|
|
Command: protocol.RequestCommandTCP,
|
|
|
|
}
|
|
|
|
|
2016-12-05 11:05:47 -05:00
|
|
|
buffer.Clear()
|
|
|
|
_, err = buffer.FillFullFrom(reader, 1)
|
2016-10-31 10:24:28 -04:00
|
|
|
if err != nil {
|
2016-12-04 03:10:47 -05:00
|
|
|
return nil, nil, errors.Base(err).Message("Shadowsocks|TCP: Failed to read address type.")
|
2016-10-31 10:24:28 -04:00
|
|
|
}
|
2016-01-28 06:33:58 -05:00
|
|
|
|
2016-12-05 09:19:14 -05:00
|
|
|
addrType := (buffer.Bytes()[0] & 0x0F)
|
|
|
|
if (buffer.Bytes()[0] & 0x10) == 0x10 {
|
2016-10-31 10:24:28 -04:00
|
|
|
request.Option |= RequestOptionOneTimeAuth
|
2016-01-29 09:09:51 -05:00
|
|
|
}
|
2016-10-31 10:24:28 -04:00
|
|
|
|
2016-11-25 10:46:59 -05:00
|
|
|
if request.Option.Has(RequestOptionOneTimeAuth) && account.OneTimeAuth == Account_Disabled {
|
|
|
|
return nil, nil, errors.New("Shadowsocks|TCP: Rejecting connection with OTA enabled, while server disables OTA.")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !request.Option.Has(RequestOptionOneTimeAuth) && account.OneTimeAuth == Account_Enabled {
|
|
|
|
return nil, nil, errors.New("Shadowsocks|TCP: Rejecting connection with OTA disabled, while server enables OTA.")
|
|
|
|
}
|
|
|
|
|
2016-01-28 06:33:58 -05:00
|
|
|
switch addrType {
|
|
|
|
case AddrTypeIPv4:
|
2016-12-05 11:05:47 -05:00
|
|
|
_, err := buffer.FillFullFrom(reader, 4)
|
2016-01-28 06:33:58 -05:00
|
|
|
if err != nil {
|
2016-12-04 03:10:47 -05:00
|
|
|
return nil, nil, errors.Base(err).Message("Shadowsocks|TCP: Failed to read IPv4 address.")
|
2016-01-28 06:33:58 -05:00
|
|
|
}
|
2016-12-05 11:05:47 -05:00
|
|
|
request.Address = v2net.IPAddress(buffer.BytesFrom(-4))
|
2016-01-28 06:33:58 -05:00
|
|
|
case AddrTypeIPv6:
|
2016-12-05 11:05:47 -05:00
|
|
|
_, err := buffer.FillFullFrom(reader, 16)
|
2016-01-28 06:33:58 -05:00
|
|
|
if err != nil {
|
2016-12-04 03:10:47 -05:00
|
|
|
return nil, nil, errors.Base(err).Message("Shadowsocks|TCP: Failed to read IPv6 address.")
|
2016-01-28 06:33:58 -05:00
|
|
|
}
|
2016-12-05 11:05:47 -05:00
|
|
|
request.Address = v2net.IPAddress(buffer.BytesFrom(-16))
|
2016-01-28 06:33:58 -05:00
|
|
|
case AddrTypeDomain:
|
2016-12-05 11:05:47 -05:00
|
|
|
_, err := buffer.FillFullFrom(reader, 1)
|
2016-01-28 06:33:58 -05:00
|
|
|
if err != nil {
|
2016-12-04 03:10:47 -05:00
|
|
|
return nil, nil, errors.Base(err).Message("Shadowsocks|TCP: Failed to read domain lenth.")
|
2016-01-28 06:33:58 -05:00
|
|
|
}
|
2016-12-05 11:05:47 -05:00
|
|
|
domainLength := int(buffer.BytesFrom(-1)[0])
|
|
|
|
_, err = buffer.FillFullFrom(reader, domainLength)
|
2016-01-28 06:33:58 -05:00
|
|
|
if err != nil {
|
2016-12-04 03:10:47 -05:00
|
|
|
return nil, nil, errors.Base(err).Message("Shadowsocks|TCP: Failed to read domain.")
|
2016-01-28 06:33:58 -05:00
|
|
|
}
|
2016-12-05 11:05:47 -05:00
|
|
|
request.Address = v2net.DomainAddress(string(buffer.BytesFrom(-domainLength)))
|
2016-01-28 06:33:58 -05:00
|
|
|
default:
|
2016-12-04 03:10:47 -05:00
|
|
|
return nil, nil, errors.New("Shadowsocks|TCP: Unknown address type: ", addrType)
|
2016-01-28 06:33:58 -05:00
|
|
|
}
|
|
|
|
|
2016-12-05 11:05:47 -05:00
|
|
|
_, err = buffer.FillFullFrom(reader, 2)
|
2016-01-28 06:33:58 -05:00
|
|
|
if err != nil {
|
2016-12-04 03:43:33 -05:00
|
|
|
return nil, nil, errors.Base(err).Message("Shadowsocks|TCP: Failed to read port.")
|
2016-01-28 06:33:58 -05:00
|
|
|
}
|
2016-12-05 11:05:47 -05:00
|
|
|
request.Port = v2net.PortFromBytes(buffer.BytesFrom(-2))
|
2016-01-29 10:43:45 -05:00
|
|
|
|
2016-10-31 10:24:28 -04:00
|
|
|
if request.Option.Has(RequestOptionOneTimeAuth) {
|
2016-12-05 11:05:47 -05:00
|
|
|
actualAuth := authenticator.Authenticate(nil, buffer.Bytes())
|
|
|
|
|
|
|
|
_, err := buffer.FillFullFrom(reader, AuthSize)
|
2016-01-29 10:43:45 -05:00
|
|
|
if err != nil {
|
2016-12-04 03:43:33 -05:00
|
|
|
return nil, nil, errors.Base(err).Message("Shadowsocks|TCP: Failed to read OTA.")
|
2016-01-29 15:55:42 -05:00
|
|
|
}
|
2016-10-31 10:24:28 -04:00
|
|
|
|
2016-12-05 11:05:47 -05:00
|
|
|
if !bytes.Equal(actualAuth, buffer.BytesFrom(-AuthSize)) {
|
2016-10-31 10:24:28 -04:00
|
|
|
return nil, nil, errors.New("Shadowsocks|TCP: Invalid OTA")
|
2016-01-29 10:43:45 -05:00
|
|
|
}
|
2016-10-31 10:24:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
var chunkReader v2io.Reader
|
|
|
|
if request.Option.Has(RequestOptionOneTimeAuth) {
|
|
|
|
chunkReader = NewChunkReader(reader, NewAuthenticator(ChunkKeyGenerator(iv)))
|
2016-01-29 15:55:42 -05:00
|
|
|
} else {
|
2016-10-31 10:24:28 -04:00
|
|
|
chunkReader = v2io.NewAdaptiveReader(reader)
|
|
|
|
}
|
|
|
|
|
|
|
|
return request, chunkReader, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func WriteTCPRequest(request *protocol.RequestHeader, writer io.Writer) (v2io.Writer, error) {
|
|
|
|
user := request.User
|
|
|
|
rawAccount, err := user.GetTypedAccount()
|
|
|
|
if err != nil {
|
2016-12-04 03:10:47 -05:00
|
|
|
return nil, errors.Base(err).Message("Shadowsocks|TCP: Failed to parse account.")
|
2016-10-31 10:24:28 -04:00
|
|
|
}
|
|
|
|
account := rawAccount.(*ShadowsocksAccount)
|
|
|
|
|
|
|
|
iv := make([]byte, account.Cipher.IVSize())
|
|
|
|
rand.Read(iv)
|
|
|
|
_, err = writer.Write(iv)
|
|
|
|
if err != nil {
|
2016-12-04 03:10:47 -05:00
|
|
|
return nil, errors.Base(err).Message("Shadowsocks|TCP: Failed to write IV.")
|
2016-10-31 10:24:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
stream, err := account.Cipher.NewEncodingStream(account.Key, iv)
|
|
|
|
if err != nil {
|
2016-12-04 03:10:47 -05:00
|
|
|
return nil, errors.Base(err).Message("Shadowsocks|TCP: Failed to create encoding stream.")
|
2016-10-31 10:24:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
writer = crypto.NewCryptionWriter(stream, writer)
|
|
|
|
|
|
|
|
header := alloc.NewLocalBuffer(512).Clear()
|
|
|
|
|
|
|
|
switch request.Address.Family() {
|
|
|
|
case v2net.AddressFamilyIPv4:
|
|
|
|
header.AppendBytes(AddrTypeIPv4)
|
|
|
|
header.Append([]byte(request.Address.IP()))
|
2016-11-06 15:04:56 -05:00
|
|
|
case v2net.AddressFamilyIPv6:
|
2016-10-31 10:24:28 -04:00
|
|
|
header.AppendBytes(AddrTypeIPv6)
|
|
|
|
header.Append([]byte(request.Address.IP()))
|
2016-11-06 15:04:56 -05:00
|
|
|
case v2net.AddressFamilyDomain:
|
2016-10-31 10:24:28 -04:00
|
|
|
header.AppendBytes(AddrTypeDomain, byte(len(request.Address.Domain())))
|
|
|
|
header.Append([]byte(request.Address.Domain()))
|
|
|
|
default:
|
2016-12-04 03:10:47 -05:00
|
|
|
return nil, errors.New("Shadowsocks|TCP: Unsupported address type: ", request.Address.Family())
|
2016-10-31 10:24:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
header.AppendUint16(uint16(request.Port))
|
|
|
|
|
|
|
|
if request.Option.Has(RequestOptionOneTimeAuth) {
|
2016-12-05 09:19:14 -05:00
|
|
|
header.Bytes()[0] |= 0x10
|
2016-10-31 10:24:28 -04:00
|
|
|
|
|
|
|
authenticator := NewAuthenticator(HeaderKeyGenerator(account.Key, iv))
|
|
|
|
header.Value = authenticator.Authenticate(header.Value, header.Value)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = writer.Write(header.Value)
|
|
|
|
if err != nil {
|
2016-12-04 03:10:47 -05:00
|
|
|
return nil, errors.Base(err).Message("Shadowsocks|TCP: Failed to write header.")
|
2016-10-31 10:24:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
var chunkWriter v2io.Writer
|
|
|
|
if request.Option.Has(RequestOptionOneTimeAuth) {
|
|
|
|
chunkWriter = NewChunkWriter(writer, NewAuthenticator(ChunkKeyGenerator(iv)))
|
|
|
|
} else {
|
|
|
|
chunkWriter = v2io.NewAdaptiveWriter(writer)
|
|
|
|
}
|
|
|
|
|
|
|
|
return chunkWriter, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func ReadTCPResponse(user *protocol.User, reader io.Reader) (v2io.Reader, error) {
|
|
|
|
rawAccount, err := user.GetTypedAccount()
|
|
|
|
if err != nil {
|
2016-12-04 03:43:33 -05:00
|
|
|
return nil, errors.Base(err).Message("Shadowsocks|TCP: Failed to parse account.")
|
2016-01-29 15:55:42 -05:00
|
|
|
}
|
2016-10-31 10:24:28 -04:00
|
|
|
account := rawAccount.(*ShadowsocksAccount)
|
2016-01-29 10:43:45 -05:00
|
|
|
|
2016-10-31 10:24:28 -04:00
|
|
|
iv := make([]byte, account.Cipher.IVSize())
|
|
|
|
_, err = io.ReadFull(reader, iv)
|
|
|
|
if err != nil {
|
2016-12-04 03:43:33 -05:00
|
|
|
return nil, errors.Base(err).Message("Shadowsocks|TCP: Failed to read IV.")
|
2016-10-31 10:24:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
stream, err := account.Cipher.NewDecodingStream(account.Key, iv)
|
|
|
|
if err != nil {
|
2016-12-04 03:43:33 -05:00
|
|
|
return nil, errors.Base(err).Message("Shadowsocks|TCP: Failed to initialize decoding stream.")
|
2016-10-31 10:24:28 -04:00
|
|
|
}
|
|
|
|
return v2io.NewAdaptiveReader(crypto.NewCryptionReader(stream, reader)), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func WriteTCPResponse(request *protocol.RequestHeader, writer io.Writer) (v2io.Writer, error) {
|
|
|
|
user := request.User
|
|
|
|
rawAccount, err := user.GetTypedAccount()
|
|
|
|
if err != nil {
|
2016-12-04 03:43:33 -05:00
|
|
|
return nil, errors.Base(err).Message("Shadowsocks|TCP: Failed to parse account.")
|
2016-10-31 10:24:28 -04:00
|
|
|
}
|
|
|
|
account := rawAccount.(*ShadowsocksAccount)
|
|
|
|
|
|
|
|
iv := make([]byte, account.Cipher.IVSize())
|
|
|
|
rand.Read(iv)
|
|
|
|
_, err = writer.Write(iv)
|
|
|
|
if err != nil {
|
2016-12-04 03:43:33 -05:00
|
|
|
return nil, errors.Base(err).Message("Shadowsocks|TCP: Failed to write IV.")
|
2016-10-31 10:24:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
stream, err := account.Cipher.NewEncodingStream(account.Key, iv)
|
|
|
|
if err != nil {
|
2016-12-04 03:43:33 -05:00
|
|
|
return nil, errors.Base(err).Message("Shadowsocks|TCP: Failed to create encoding stream.")
|
2016-10-31 10:24:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return v2io.NewAdaptiveWriter(crypto.NewCryptionWriter(stream, writer)), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func EncodeUDPPacket(request *protocol.RequestHeader, payload *alloc.Buffer) (*alloc.Buffer, error) {
|
|
|
|
user := request.User
|
|
|
|
rawAccount, err := user.GetTypedAccount()
|
|
|
|
if err != nil {
|
2016-12-04 03:43:33 -05:00
|
|
|
return nil, errors.Base(err).Message("Shadowsocks|UDP: Failed to parse account.")
|
2016-10-31 10:24:28 -04:00
|
|
|
}
|
|
|
|
account := rawAccount.(*ShadowsocksAccount)
|
|
|
|
|
2016-11-21 16:08:34 -05:00
|
|
|
buffer := alloc.NewSmallBuffer()
|
2016-10-31 10:24:28 -04:00
|
|
|
ivLen := account.Cipher.IVSize()
|
|
|
|
buffer.Slice(0, ivLen)
|
|
|
|
rand.Read(buffer.Value)
|
|
|
|
iv := buffer.Value
|
|
|
|
|
|
|
|
switch request.Address.Family() {
|
|
|
|
case v2net.AddressFamilyIPv4:
|
|
|
|
buffer.AppendBytes(AddrTypeIPv4)
|
|
|
|
buffer.Append([]byte(request.Address.IP()))
|
2016-11-06 08:32:04 -05:00
|
|
|
case v2net.AddressFamilyIPv6:
|
2016-10-31 10:24:28 -04:00
|
|
|
buffer.AppendBytes(AddrTypeIPv6)
|
|
|
|
buffer.Append([]byte(request.Address.IP()))
|
2016-11-06 08:32:04 -05:00
|
|
|
case v2net.AddressFamilyDomain:
|
2016-10-31 10:24:28 -04:00
|
|
|
buffer.AppendBytes(AddrTypeDomain, byte(len(request.Address.Domain())))
|
|
|
|
buffer.Append([]byte(request.Address.Domain()))
|
|
|
|
default:
|
2016-12-04 03:43:33 -05:00
|
|
|
return nil, errors.New("Shadowsocks|UDP: Unsupported address type: ", request.Address.Family())
|
2016-10-31 10:24:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
buffer.AppendUint16(uint16(request.Port))
|
|
|
|
buffer.Append(payload.Value)
|
|
|
|
|
|
|
|
if request.Option.Has(RequestOptionOneTimeAuth) {
|
|
|
|
authenticator := NewAuthenticator(HeaderKeyGenerator(account.Key, iv))
|
2016-11-06 08:32:04 -05:00
|
|
|
buffer.Value[ivLen] |= 0x10
|
2016-10-31 10:24:28 -04:00
|
|
|
|
|
|
|
buffer.Value = authenticator.Authenticate(buffer.Value, buffer.Value[ivLen:])
|
|
|
|
}
|
|
|
|
|
|
|
|
stream, err := account.Cipher.NewEncodingStream(account.Key, iv)
|
|
|
|
if err != nil {
|
2016-12-04 03:43:33 -05:00
|
|
|
return nil, errors.Base(err).Message("Shadowsocks|TCP: Failed to create encoding stream.")
|
2016-10-31 10:24:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
stream.XORKeyStream(buffer.Value[ivLen:], buffer.Value[ivLen:])
|
|
|
|
return buffer, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func DecodeUDPPacket(user *protocol.User, payload *alloc.Buffer) (*protocol.RequestHeader, *alloc.Buffer, error) {
|
|
|
|
rawAccount, err := user.GetTypedAccount()
|
|
|
|
if err != nil {
|
2016-12-04 03:43:33 -05:00
|
|
|
return nil, nil, errors.Base(err).Message("Shadowsocks|UDP: Failed to parse account.")
|
2016-10-31 10:24:28 -04:00
|
|
|
}
|
|
|
|
account := rawAccount.(*ShadowsocksAccount)
|
|
|
|
|
|
|
|
ivLen := account.Cipher.IVSize()
|
|
|
|
iv := payload.Value[:ivLen]
|
|
|
|
payload.SliceFrom(ivLen)
|
|
|
|
|
|
|
|
stream, err := account.Cipher.NewDecodingStream(account.Key, iv)
|
|
|
|
if err != nil {
|
2016-12-04 03:43:33 -05:00
|
|
|
return nil, nil, errors.Base(err).Message("Shadowsocks|UDP: Failed to initialize decoding stream.")
|
2016-10-31 10:24:28 -04:00
|
|
|
}
|
|
|
|
stream.XORKeyStream(payload.Value, payload.Value)
|
|
|
|
|
|
|
|
authenticator := NewAuthenticator(HeaderKeyGenerator(account.Key, iv))
|
|
|
|
request := &protocol.RequestHeader{
|
|
|
|
Version: Version,
|
|
|
|
User: user,
|
|
|
|
Command: protocol.RequestCommandUDP,
|
|
|
|
}
|
|
|
|
|
|
|
|
addrType := (payload.Value[0] & 0x0F)
|
|
|
|
if (payload.Value[0] & 0x10) == 0x10 {
|
|
|
|
request.Option |= RequestOptionOneTimeAuth
|
|
|
|
}
|
|
|
|
|
2016-11-25 10:46:59 -05:00
|
|
|
if request.Option.Has(RequestOptionOneTimeAuth) && account.OneTimeAuth == Account_Disabled {
|
|
|
|
return nil, nil, errors.New("Shadowsocks|UDP: Rejecting packet with OTA enabled, while server disables OTA.")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !request.Option.Has(RequestOptionOneTimeAuth) && account.OneTimeAuth == Account_Enabled {
|
|
|
|
return nil, nil, errors.New("Shadowsocks|UDP: Rejecting packet with OTA disabled, while server enables OTA.")
|
|
|
|
}
|
|
|
|
|
2016-10-31 10:24:28 -04:00
|
|
|
if request.Option.Has(RequestOptionOneTimeAuth) {
|
|
|
|
payloadLen := payload.Len() - AuthSize
|
|
|
|
authBytes := payload.Value[payloadLen:]
|
|
|
|
|
|
|
|
actualAuth := authenticator.Authenticate(nil, payload.Value[0:payloadLen])
|
2016-05-24 16:09:22 -04:00
|
|
|
if !bytes.Equal(actualAuth, authBytes) {
|
2016-10-31 10:24:28 -04:00
|
|
|
return nil, nil, errors.New("Shadowsocks|UDP: Invalid OTA.")
|
2016-01-29 10:43:45 -05:00
|
|
|
}
|
2016-10-31 10:24:28 -04:00
|
|
|
|
|
|
|
payload.Slice(0, payloadLen)
|
|
|
|
}
|
|
|
|
|
|
|
|
payload.SliceFrom(1)
|
|
|
|
|
|
|
|
switch addrType {
|
|
|
|
case AddrTypeIPv4:
|
|
|
|
request.Address = v2net.IPAddress(payload.Value[:4])
|
|
|
|
payload.SliceFrom(4)
|
|
|
|
case AddrTypeIPv6:
|
|
|
|
request.Address = v2net.IPAddress(payload.Value[:16])
|
|
|
|
payload.SliceFrom(16)
|
|
|
|
case AddrTypeDomain:
|
|
|
|
domainLength := int(payload.Value[0])
|
|
|
|
request.Address = v2net.DomainAddress(string(payload.Value[1 : 1+domainLength]))
|
|
|
|
payload.SliceFrom(1 + domainLength)
|
|
|
|
default:
|
2016-12-04 03:43:33 -05:00
|
|
|
return nil, nil, errors.New("Shadowsocks|UDP: Unknown address type: ", addrType)
|
2016-01-29 10:43:45 -05:00
|
|
|
}
|
|
|
|
|
2016-10-31 10:24:28 -04:00
|
|
|
request.Port = v2net.PortFromBytes(payload.Value[:2])
|
|
|
|
payload.SliceFrom(2)
|
|
|
|
|
|
|
|
return request, payload, nil
|
2016-01-28 06:33:58 -05:00
|
|
|
}
|
2016-10-31 11:35:18 -04:00
|
|
|
|
|
|
|
type UDPReader struct {
|
|
|
|
Reader io.Reader
|
|
|
|
User *protocol.User
|
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v *UDPReader) Read() (*alloc.Buffer, error) {
|
2016-11-21 16:08:34 -05:00
|
|
|
buffer := alloc.NewSmallBuffer()
|
2016-11-27 15:39:09 -05:00
|
|
|
nBytes, err := v.Reader.Read(buffer.Value)
|
2016-10-31 11:35:18 -04:00
|
|
|
if err != nil {
|
|
|
|
buffer.Release()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
buffer.Slice(0, nBytes)
|
2016-11-27 15:39:09 -05:00
|
|
|
_, payload, err := DecodeUDPPacket(v.User, buffer)
|
2016-10-31 11:35:18 -04:00
|
|
|
if err != nil {
|
|
|
|
buffer.Release()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return payload, nil
|
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v *UDPReader) Release() {
|
2016-10-31 11:35:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type UDPWriter struct {
|
|
|
|
Writer io.Writer
|
|
|
|
Request *protocol.RequestHeader
|
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v *UDPWriter) Write(buffer *alloc.Buffer) error {
|
|
|
|
payload, err := EncodeUDPPacket(v.Request, buffer)
|
2016-10-31 11:35:18 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-11-27 15:39:09 -05:00
|
|
|
_, err = v.Writer.Write(payload.Value)
|
2016-10-31 11:35:18 -04:00
|
|
|
payload.Release()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v *UDPWriter) Release() {
|
2016-10-31 11:35:18 -04:00
|
|
|
|
|
|
|
}
|