2016-01-29 14:09:51 +00:00
|
|
|
package shadowsocks_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2019-02-02 21:19:30 +00:00
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
|
2018-08-16 10:05:33 +00:00
|
|
|
"v2ray.com/core/common"
|
2016-12-09 10:35:27 +00:00
|
|
|
"v2ray.com/core/common/buf"
|
2017-08-29 10:56:57 +00:00
|
|
|
"v2ray.com/core/common/net"
|
2016-10-31 14:24:28 +00:00
|
|
|
"v2ray.com/core/common/protocol"
|
2016-08-20 18:55:45 +00:00
|
|
|
. "v2ray.com/core/proxy/shadowsocks"
|
2016-01-29 14:09:51 +00:00
|
|
|
)
|
|
|
|
|
2018-08-26 22:11:32 +00:00
|
|
|
func toAccount(a *Account) protocol.Account {
|
|
|
|
account, err := a.AsAccount()
|
|
|
|
common.Must(err)
|
|
|
|
return account
|
|
|
|
}
|
|
|
|
|
2016-10-31 14:24:28 +00:00
|
|
|
func TestUDPEncoding(t *testing.T) {
|
|
|
|
request := &protocol.RequestHeader{
|
|
|
|
Version: Version,
|
|
|
|
Command: protocol.RequestCommandUDP,
|
2017-08-29 10:56:57 +00:00
|
|
|
Address: net.LocalHostIP,
|
2016-10-31 14:24:28 +00:00
|
|
|
Port: 1234,
|
2018-08-26 22:11:32 +00:00
|
|
|
User: &protocol.MemoryUser{
|
2016-10-31 14:24:28 +00:00
|
|
|
Email: "love@v2ray.com",
|
2018-08-26 22:11:32 +00:00
|
|
|
Account: toAccount(&Account{
|
2016-10-31 14:24:28 +00:00
|
|
|
Password: "shadowsocks-password",
|
|
|
|
CipherType: CipherType_AES_128_CFB,
|
|
|
|
Ota: Account_Disabled,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-08-16 10:05:33 +00:00
|
|
|
data := buf.New()
|
2018-11-02 20:34:04 +00:00
|
|
|
common.Must2(data.WriteString("test string"))
|
2017-04-21 12:51:09 +00:00
|
|
|
encodedData, err := EncodeUDPPacket(request, data.Bytes())
|
2019-02-02 21:19:30 +00:00
|
|
|
common.Must(err)
|
2016-02-04 21:10:52 +00:00
|
|
|
|
2016-10-31 14:24:28 +00:00
|
|
|
decodedRequest, decodedData, err := DecodeUDPPacket(request.User, encodedData)
|
2019-02-02 21:19:30 +00:00
|
|
|
common.Must(err)
|
|
|
|
|
|
|
|
if r := cmp.Diff(decodedData.Bytes(), data.Bytes()); r != "" {
|
|
|
|
t.Error("data: ", r)
|
|
|
|
}
|
|
|
|
|
|
|
|
if r := cmp.Diff(decodedRequest, request); r != "" {
|
|
|
|
t.Error("request: ", r)
|
|
|
|
}
|
2016-01-29 14:09:51 +00:00
|
|
|
}
|
2016-01-29 20:55:42 +00:00
|
|
|
|
2016-10-31 14:24:28 +00:00
|
|
|
func TestTCPRequest(t *testing.T) {
|
2017-04-26 21:46:19 +00:00
|
|
|
cases := []struct {
|
|
|
|
request *protocol.RequestHeader
|
|
|
|
payload []byte
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
request: &protocol.RequestHeader{
|
|
|
|
Version: Version,
|
|
|
|
Command: protocol.RequestCommandTCP,
|
2017-08-29 10:56:57 +00:00
|
|
|
Address: net.LocalHostIP,
|
2017-04-26 21:46:19 +00:00
|
|
|
Option: RequestOptionOneTimeAuth,
|
|
|
|
Port: 1234,
|
2018-08-26 22:11:32 +00:00
|
|
|
User: &protocol.MemoryUser{
|
2017-04-26 21:46:19 +00:00
|
|
|
Email: "love@v2ray.com",
|
2018-08-26 22:11:32 +00:00
|
|
|
Account: toAccount(&Account{
|
2017-04-26 21:46:19 +00:00
|
|
|
Password: "tcp-password",
|
|
|
|
CipherType: CipherType_CHACHA20,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
payload: []byte("test string"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
request: &protocol.RequestHeader{
|
|
|
|
Version: Version,
|
|
|
|
Command: protocol.RequestCommandTCP,
|
2017-08-29 10:56:57 +00:00
|
|
|
Address: net.LocalHostIPv6,
|
2017-04-26 21:46:19 +00:00
|
|
|
Option: RequestOptionOneTimeAuth,
|
|
|
|
Port: 1234,
|
2018-08-26 22:11:32 +00:00
|
|
|
User: &protocol.MemoryUser{
|
2017-04-26 21:46:19 +00:00
|
|
|
Email: "love@v2ray.com",
|
2018-08-26 22:11:32 +00:00
|
|
|
Account: toAccount(&Account{
|
2017-04-26 21:46:19 +00:00
|
|
|
Password: "password",
|
|
|
|
CipherType: CipherType_AES_256_CFB,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
payload: []byte("test string"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
request: &protocol.RequestHeader{
|
|
|
|
Version: Version,
|
|
|
|
Command: protocol.RequestCommandTCP,
|
2017-08-29 10:56:57 +00:00
|
|
|
Address: net.DomainAddress("v2ray.com"),
|
2017-04-26 21:46:19 +00:00
|
|
|
Option: RequestOptionOneTimeAuth,
|
|
|
|
Port: 1234,
|
2018-08-26 22:11:32 +00:00
|
|
|
User: &protocol.MemoryUser{
|
2017-04-26 21:46:19 +00:00
|
|
|
Email: "love@v2ray.com",
|
2018-08-26 22:11:32 +00:00
|
|
|
Account: toAccount(&Account{
|
2017-04-26 21:46:19 +00:00
|
|
|
Password: "password",
|
|
|
|
CipherType: CipherType_CHACHA20_IETF,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
payload: []byte("test string"),
|
2016-10-31 14:24:28 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-04-26 21:46:19 +00:00
|
|
|
runTest := func(request *protocol.RequestHeader, payload []byte) {
|
|
|
|
data := buf.New()
|
2018-08-16 10:05:33 +00:00
|
|
|
common.Must2(data.Write(payload))
|
2016-10-31 14:24:28 +00:00
|
|
|
|
2017-04-26 21:46:19 +00:00
|
|
|
cache := buf.New()
|
|
|
|
defer cache.Release()
|
2016-01-29 20:55:42 +00:00
|
|
|
|
2017-04-26 21:46:19 +00:00
|
|
|
writer, err := WriteTCPRequest(request, cache)
|
2019-02-02 21:19:30 +00:00
|
|
|
common.Must(err)
|
2016-01-29 20:55:42 +00:00
|
|
|
|
2019-02-02 21:19:30 +00:00
|
|
|
common.Must(writer.WriteMultiBuffer(buf.MultiBuffer{data}))
|
2017-04-26 21:46:19 +00:00
|
|
|
|
|
|
|
decodedRequest, reader, err := ReadTCPSession(request.User, cache)
|
2019-02-02 21:19:30 +00:00
|
|
|
common.Must(err)
|
|
|
|
if r := cmp.Diff(decodedRequest, request); r != "" {
|
|
|
|
t.Error("request: ", r)
|
|
|
|
}
|
2017-04-26 21:46:19 +00:00
|
|
|
|
2017-11-09 21:33:15 +00:00
|
|
|
decodedData, err := reader.ReadMultiBuffer()
|
2019-02-02 21:19:30 +00:00
|
|
|
common.Must(err)
|
|
|
|
if r := cmp.Diff(decodedData[0].Bytes(), payload); r != "" {
|
|
|
|
t.Error("data: ", r)
|
|
|
|
}
|
2017-04-26 21:46:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range cases {
|
|
|
|
runTest(test.request, test.payload)
|
|
|
|
}
|
2016-01-29 20:55:42 +00:00
|
|
|
|
|
|
|
}
|
2016-11-06 13:32:04 +00:00
|
|
|
|
|
|
|
func TestUDPReaderWriter(t *testing.T) {
|
2018-08-26 22:11:32 +00:00
|
|
|
user := &protocol.MemoryUser{
|
|
|
|
Account: toAccount(&Account{
|
2016-11-06 13:32:04 +00:00
|
|
|
Password: "test-password",
|
2017-01-03 00:37:27 +00:00
|
|
|
CipherType: CipherType_CHACHA20_IETF,
|
2016-11-06 13:32:04 +00:00
|
|
|
}),
|
|
|
|
}
|
2016-12-09 11:08:25 +00:00
|
|
|
cache := buf.New()
|
2018-08-16 10:05:33 +00:00
|
|
|
defer cache.Release()
|
|
|
|
|
2018-07-31 11:43:27 +00:00
|
|
|
writer := &buf.SequentialWriter{Writer: &UDPWriter{
|
2016-11-06 13:32:04 +00:00
|
|
|
Writer: cache,
|
|
|
|
Request: &protocol.RequestHeader{
|
|
|
|
Version: Version,
|
2017-08-29 10:56:57 +00:00
|
|
|
Address: net.DomainAddress("v2ray.com"),
|
2016-11-06 13:32:04 +00:00
|
|
|
Port: 123,
|
|
|
|
User: user,
|
|
|
|
Option: RequestOptionOneTimeAuth,
|
|
|
|
},
|
2018-07-31 11:43:27 +00:00
|
|
|
}}
|
2016-11-06 13:32:04 +00:00
|
|
|
|
|
|
|
reader := &UDPReader{
|
|
|
|
Reader: cache,
|
|
|
|
User: user,
|
|
|
|
}
|
|
|
|
|
2018-08-16 10:05:33 +00:00
|
|
|
{
|
|
|
|
b := buf.New()
|
2018-11-02 20:34:04 +00:00
|
|
|
common.Must2(b.WriteString("test payload"))
|
2019-02-02 21:19:30 +00:00
|
|
|
common.Must(writer.WriteMultiBuffer(buf.MultiBuffer{b}))
|
2016-11-06 13:32:04 +00:00
|
|
|
|
2018-08-16 10:05:33 +00:00
|
|
|
payload, err := reader.ReadMultiBuffer()
|
2019-02-02 21:19:30 +00:00
|
|
|
common.Must(err)
|
|
|
|
if payload[0].String() != "test payload" {
|
|
|
|
t.Error("unexpected output: ", payload[0].String())
|
|
|
|
}
|
2018-08-16 10:05:33 +00:00
|
|
|
}
|
2016-11-06 13:32:04 +00:00
|
|
|
|
2018-08-16 10:05:33 +00:00
|
|
|
{
|
|
|
|
b := buf.New()
|
2018-11-02 20:34:04 +00:00
|
|
|
common.Must2(b.WriteString("test payload 2"))
|
2019-02-02 21:19:30 +00:00
|
|
|
common.Must(writer.WriteMultiBuffer(buf.MultiBuffer{b}))
|
2016-11-06 13:32:04 +00:00
|
|
|
|
2018-08-16 10:05:33 +00:00
|
|
|
payload, err := reader.ReadMultiBuffer()
|
2019-02-02 21:19:30 +00:00
|
|
|
common.Must(err)
|
|
|
|
if payload[0].String() != "test payload 2" {
|
|
|
|
t.Error("unexpected output: ", payload[0].String())
|
|
|
|
}
|
2018-08-16 10:05:33 +00:00
|
|
|
}
|
2016-11-06 13:32:04 +00:00
|
|
|
}
|