2016-01-29 09:09:51 -05:00
|
|
|
package shadowsocks_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2016-12-09 05:35:27 -05:00
|
|
|
"v2ray.com/core/common/buf"
|
2017-08-29 06:56:57 -04:00
|
|
|
"v2ray.com/core/common/net"
|
2016-10-31 10:24:28 -04:00
|
|
|
"v2ray.com/core/common/protocol"
|
2016-12-06 05:03:42 -05:00
|
|
|
"v2ray.com/core/common/serial"
|
2016-08-20 14:55:45 -04:00
|
|
|
. "v2ray.com/core/proxy/shadowsocks"
|
2017-10-24 10:15:35 -04:00
|
|
|
. "v2ray.com/ext/assert"
|
2016-01-29 09:09:51 -05:00
|
|
|
)
|
|
|
|
|
2016-10-31 10:24:28 -04:00
|
|
|
func TestUDPEncoding(t *testing.T) {
|
2017-10-24 10:15:35 -04:00
|
|
|
assert := With(t)
|
2016-01-29 09:09:51 -05:00
|
|
|
|
2016-10-31 10:24:28 -04:00
|
|
|
request := &protocol.RequestHeader{
|
|
|
|
Version: Version,
|
|
|
|
Command: protocol.RequestCommandUDP,
|
2017-08-29 06:56:57 -04:00
|
|
|
Address: net.LocalHostIP,
|
2016-10-31 10:24:28 -04:00
|
|
|
Port: 1234,
|
|
|
|
User: &protocol.User{
|
|
|
|
Email: "love@v2ray.com",
|
2016-12-15 05:51:09 -05:00
|
|
|
Account: serial.ToTypedMessage(&Account{
|
2016-10-31 10:24:28 -04:00
|
|
|
Password: "shadowsocks-password",
|
|
|
|
CipherType: CipherType_AES_128_CFB,
|
|
|
|
Ota: Account_Disabled,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-03-11 18:29:17 -04:00
|
|
|
data := buf.NewSize(256)
|
2016-12-09 06:08:25 -05:00
|
|
|
data.AppendSupplier(serial.WriteString("test string"))
|
2017-04-21 08:51:09 -04:00
|
|
|
encodedData, err := EncodeUDPPacket(request, data.Bytes())
|
2017-10-24 10:15:35 -04:00
|
|
|
assert(err, IsNil)
|
2016-02-04 16:10:52 -05:00
|
|
|
|
2016-10-31 10:24:28 -04:00
|
|
|
decodedRequest, decodedData, err := DecodeUDPPacket(request.User, encodedData)
|
2017-10-24 10:15:35 -04:00
|
|
|
assert(err, IsNil)
|
|
|
|
assert(decodedData.Bytes(), Equals, data.Bytes())
|
|
|
|
assert(decodedRequest.Address, Equals, request.Address)
|
|
|
|
assert(decodedRequest.Port, Equals, request.Port)
|
2018-03-04 16:38:05 -05:00
|
|
|
assert(decodedRequest.Command, Equals, request.Command)
|
2016-01-29 09:09:51 -05:00
|
|
|
}
|
2016-01-29 15:55:42 -05:00
|
|
|
|
2016-10-31 10:24:28 -04:00
|
|
|
func TestTCPRequest(t *testing.T) {
|
2017-10-24 10:15:35 -04:00
|
|
|
assert := With(t)
|
2016-02-04 16:10:52 -05:00
|
|
|
|
2017-04-26 17:46:19 -04:00
|
|
|
cases := []struct {
|
|
|
|
request *protocol.RequestHeader
|
|
|
|
payload []byte
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
request: &protocol.RequestHeader{
|
|
|
|
Version: Version,
|
|
|
|
Command: protocol.RequestCommandTCP,
|
2017-08-29 06:56:57 -04:00
|
|
|
Address: net.LocalHostIP,
|
2017-04-26 17:46:19 -04:00
|
|
|
Option: RequestOptionOneTimeAuth,
|
|
|
|
Port: 1234,
|
|
|
|
User: &protocol.User{
|
|
|
|
Email: "love@v2ray.com",
|
|
|
|
Account: serial.ToTypedMessage(&Account{
|
|
|
|
Password: "tcp-password",
|
|
|
|
CipherType: CipherType_CHACHA20,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
payload: []byte("test string"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
request: &protocol.RequestHeader{
|
|
|
|
Version: Version,
|
|
|
|
Command: protocol.RequestCommandTCP,
|
2017-08-29 06:56:57 -04:00
|
|
|
Address: net.LocalHostIPv6,
|
2017-04-26 17:46:19 -04:00
|
|
|
Option: RequestOptionOneTimeAuth,
|
|
|
|
Port: 1234,
|
|
|
|
User: &protocol.User{
|
|
|
|
Email: "love@v2ray.com",
|
|
|
|
Account: serial.ToTypedMessage(&Account{
|
|
|
|
Password: "password",
|
|
|
|
CipherType: CipherType_AES_256_CFB,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
payload: []byte("test string"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
request: &protocol.RequestHeader{
|
|
|
|
Version: Version,
|
|
|
|
Command: protocol.RequestCommandTCP,
|
2017-08-29 06:56:57 -04:00
|
|
|
Address: net.DomainAddress("v2ray.com"),
|
2017-04-26 17:46:19 -04:00
|
|
|
Option: RequestOptionOneTimeAuth,
|
|
|
|
Port: 1234,
|
|
|
|
User: &protocol.User{
|
|
|
|
Email: "love@v2ray.com",
|
|
|
|
Account: serial.ToTypedMessage(&Account{
|
|
|
|
Password: "password",
|
|
|
|
CipherType: CipherType_CHACHA20_IETF,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
payload: []byte("test string"),
|
2016-10-31 10:24:28 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-04-26 17:46:19 -04:00
|
|
|
runTest := func(request *protocol.RequestHeader, payload []byte) {
|
|
|
|
data := buf.New()
|
|
|
|
defer data.Release()
|
2018-04-19 16:56:55 -04:00
|
|
|
data.Write(payload)
|
2016-10-31 10:24:28 -04:00
|
|
|
|
2017-04-26 17:46:19 -04:00
|
|
|
cache := buf.New()
|
|
|
|
defer cache.Release()
|
2016-01-29 15:55:42 -05:00
|
|
|
|
2017-04-26 17:46:19 -04:00
|
|
|
writer, err := WriteTCPRequest(request, cache)
|
2017-10-24 10:15:35 -04:00
|
|
|
assert(err, IsNil)
|
2016-01-29 15:55:42 -05:00
|
|
|
|
2017-11-09 16:33:15 -05:00
|
|
|
assert(writer.WriteMultiBuffer(buf.NewMultiBufferValue(data)), IsNil)
|
2017-04-26 17:46:19 -04:00
|
|
|
|
|
|
|
decodedRequest, reader, err := ReadTCPSession(request.User, cache)
|
2017-10-24 10:15:35 -04:00
|
|
|
assert(err, IsNil)
|
|
|
|
assert(decodedRequest.Address, Equals, request.Address)
|
|
|
|
assert(decodedRequest.Port, Equals, request.Port)
|
2018-03-04 16:38:05 -05:00
|
|
|
assert(decodedRequest.Command, Equals, request.Command)
|
2017-04-26 17:46:19 -04:00
|
|
|
|
2017-11-09 16:33:15 -05:00
|
|
|
decodedData, err := reader.ReadMultiBuffer()
|
2017-10-24 10:15:35 -04:00
|
|
|
assert(err, IsNil)
|
|
|
|
assert(decodedData[0].String(), Equals, string(payload))
|
2017-04-26 17:46:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range cases {
|
|
|
|
runTest(test.request, test.payload)
|
|
|
|
}
|
2016-01-29 15:55:42 -05:00
|
|
|
|
|
|
|
}
|
2016-11-06 08:32:04 -05:00
|
|
|
|
|
|
|
func TestUDPReaderWriter(t *testing.T) {
|
2017-10-24 10:15:35 -04:00
|
|
|
assert := With(t)
|
2016-11-06 08:32:04 -05:00
|
|
|
|
|
|
|
user := &protocol.User{
|
2016-12-15 05:51:09 -05:00
|
|
|
Account: serial.ToTypedMessage(&Account{
|
2016-11-06 08:32:04 -05:00
|
|
|
Password: "test-password",
|
2017-01-02 19:37:27 -05:00
|
|
|
CipherType: CipherType_CHACHA20_IETF,
|
2016-11-06 08:32:04 -05:00
|
|
|
}),
|
|
|
|
}
|
2016-12-09 06:08:25 -05:00
|
|
|
cache := buf.New()
|
2017-04-21 08:51:09 -04:00
|
|
|
writer := buf.NewSequentialWriter(&UDPWriter{
|
2016-11-06 08:32:04 -05:00
|
|
|
Writer: cache,
|
|
|
|
Request: &protocol.RequestHeader{
|
|
|
|
Version: Version,
|
2017-08-29 06:56:57 -04:00
|
|
|
Address: net.DomainAddress("v2ray.com"),
|
2016-11-06 08:32:04 -05:00
|
|
|
Port: 123,
|
|
|
|
User: user,
|
|
|
|
Option: RequestOptionOneTimeAuth,
|
|
|
|
},
|
2017-04-21 08:51:09 -04:00
|
|
|
})
|
2016-11-06 08:32:04 -05:00
|
|
|
|
|
|
|
reader := &UDPReader{
|
|
|
|
Reader: cache,
|
|
|
|
User: user,
|
|
|
|
}
|
|
|
|
|
2016-12-09 06:08:25 -05:00
|
|
|
b := buf.New()
|
|
|
|
b.AppendSupplier(serial.WriteString("test payload"))
|
2017-11-09 16:33:15 -05:00
|
|
|
err := writer.WriteMultiBuffer(buf.NewMultiBufferValue(b))
|
2017-10-24 10:15:35 -04:00
|
|
|
assert(err, IsNil)
|
2016-11-06 08:32:04 -05:00
|
|
|
|
2017-11-09 16:33:15 -05:00
|
|
|
payload, err := reader.ReadMultiBuffer()
|
2017-10-24 10:15:35 -04:00
|
|
|
assert(err, IsNil)
|
|
|
|
assert(payload[0].String(), Equals, "test payload")
|
2016-11-06 08:32:04 -05:00
|
|
|
|
2016-12-09 06:08:25 -05:00
|
|
|
b = buf.New()
|
|
|
|
b.AppendSupplier(serial.WriteString("test payload 2"))
|
2017-11-09 16:33:15 -05:00
|
|
|
err = writer.WriteMultiBuffer(buf.NewMultiBufferValue(b))
|
2017-10-24 10:15:35 -04:00
|
|
|
assert(err, IsNil)
|
2016-11-06 08:32:04 -05:00
|
|
|
|
2017-11-09 16:33:15 -05:00
|
|
|
payload, err = reader.ReadMultiBuffer()
|
2017-10-24 10:15:35 -04:00
|
|
|
assert(err, IsNil)
|
|
|
|
assert(payload[0].String(), Equals, "test payload 2")
|
2016-11-06 08:32:04 -05:00
|
|
|
}
|