2020-11-25 13:01:53 +02:00
|
|
|
package trojan_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/google/go-cmp/cmp"
|
2020-12-04 03:36:16 +02:00
|
|
|
"github.com/xtls/xray-core/common"
|
|
|
|
"github.com/xtls/xray-core/common/buf"
|
|
|
|
"github.com/xtls/xray-core/common/net"
|
|
|
|
"github.com/xtls/xray-core/common/protocol"
|
|
|
|
. "github.com/xtls/xray-core/proxy/trojan"
|
2020-11-25 13:01:53 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func toAccount(a *Account) protocol.Account {
|
|
|
|
account, err := a.AsAccount()
|
|
|
|
common.Must(err)
|
|
|
|
return account
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTCPRequest(t *testing.T) {
|
|
|
|
user := &protocol.MemoryUser{
|
|
|
|
Email: "love@example.com",
|
|
|
|
Account: toAccount(&Account{
|
|
|
|
Password: "password",
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
payload := []byte("test string")
|
|
|
|
data := buf.New()
|
|
|
|
common.Must2(data.Write(payload))
|
|
|
|
|
|
|
|
buffer := buf.New()
|
|
|
|
defer buffer.Release()
|
|
|
|
|
|
|
|
destination := net.Destination{Network: net.Network_TCP, Address: net.LocalHostIP, Port: 1234}
|
|
|
|
writer := &ConnWriter{Writer: buffer, Target: destination, Account: user.Account.(*MemoryAccount)}
|
|
|
|
common.Must(writer.WriteMultiBuffer(buf.MultiBuffer{data}))
|
|
|
|
|
|
|
|
reader := &ConnReader{Reader: buffer}
|
|
|
|
common.Must(reader.ParseHeader())
|
|
|
|
|
|
|
|
if r := cmp.Diff(reader.Target, destination); r != "" {
|
|
|
|
t.Error("destination: ", r)
|
|
|
|
}
|
|
|
|
|
|
|
|
decodedData, err := reader.ReadMultiBuffer()
|
|
|
|
common.Must(err)
|
|
|
|
if r := cmp.Diff(decodedData[0].Bytes(), payload); r != "" {
|
|
|
|
t.Error("data: ", r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUDPRequest(t *testing.T) {
|
|
|
|
user := &protocol.MemoryUser{
|
|
|
|
Email: "love@example.com",
|
|
|
|
Account: toAccount(&Account{
|
|
|
|
Password: "password",
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
payload := []byte("test string")
|
|
|
|
data := buf.New()
|
|
|
|
common.Must2(data.Write(payload))
|
|
|
|
|
|
|
|
buffer := buf.New()
|
|
|
|
defer buffer.Release()
|
|
|
|
|
|
|
|
destination := net.Destination{Network: net.Network_UDP, Address: net.LocalHostIP, Port: 1234}
|
|
|
|
writer := &PacketWriter{Writer: &ConnWriter{Writer: buffer, Target: destination, Account: user.Account.(*MemoryAccount)}, Target: destination}
|
|
|
|
common.Must(writer.WriteMultiBuffer(buf.MultiBuffer{data}))
|
|
|
|
|
|
|
|
connReader := &ConnReader{Reader: buffer}
|
|
|
|
common.Must(connReader.ParseHeader())
|
|
|
|
|
|
|
|
packetReader := &PacketReader{Reader: connReader}
|
2021-01-08 05:55:25 +02:00
|
|
|
mb, err := packetReader.ReadMultiBuffer()
|
2020-11-25 13:01:53 +02:00
|
|
|
common.Must(err)
|
|
|
|
|
2021-01-08 05:55:25 +02:00
|
|
|
if mb.IsEmpty() {
|
2020-11-25 13:01:53 +02:00
|
|
|
t.Error("no request data")
|
|
|
|
}
|
|
|
|
|
2021-01-08 05:55:25 +02:00
|
|
|
mb2, b := buf.SplitFirst(mb)
|
|
|
|
defer buf.ReleaseMulti(mb2)
|
|
|
|
|
|
|
|
dest := *b.UDP
|
|
|
|
if r := cmp.Diff(dest, destination); r != "" {
|
2020-11-25 13:01:53 +02:00
|
|
|
t.Error("destination: ", r)
|
|
|
|
}
|
|
|
|
|
2021-01-08 05:55:25 +02:00
|
|
|
if r := cmp.Diff(b.Bytes(), payload); r != "" {
|
2020-11-25 13:01:53 +02:00
|
|
|
t.Error("data: ", r)
|
|
|
|
}
|
|
|
|
}
|