2020-11-25 11:01:53 +00:00
|
|
|
package websocket_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"runtime"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2020-12-04 01:36:16 +00:00
|
|
|
"github.com/xtls/xray-core/common"
|
|
|
|
"github.com/xtls/xray-core/common/net"
|
|
|
|
"github.com/xtls/xray-core/common/protocol/tls/cert"
|
2024-06-03 16:29:48 +00:00
|
|
|
"github.com/xtls/xray-core/testing/servers/tcp"
|
2020-12-04 01:36:16 +00:00
|
|
|
"github.com/xtls/xray-core/transport/internet"
|
2021-12-15 00:28:47 +00:00
|
|
|
"github.com/xtls/xray-core/transport/internet/stat"
|
2020-12-04 01:36:16 +00:00
|
|
|
"github.com/xtls/xray-core/transport/internet/tls"
|
|
|
|
. "github.com/xtls/xray-core/transport/internet/websocket"
|
2020-11-25 11:01:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func Test_listenWSAndDial(t *testing.T) {
|
2024-06-03 16:29:48 +00:00
|
|
|
listenPort := tcp.PickPort()
|
|
|
|
listen, err := ListenWS(context.Background(), net.LocalHostIP, listenPort, &internet.MemoryStreamConfig{
|
2020-11-25 11:01:53 +00:00
|
|
|
ProtocolName: "websocket",
|
|
|
|
ProtocolSettings: &Config{
|
|
|
|
Path: "ws",
|
|
|
|
},
|
2021-09-20 12:11:21 +00:00
|
|
|
}, func(conn stat.Connection) {
|
|
|
|
go func(c stat.Connection) {
|
2020-11-25 11:01:53 +00:00
|
|
|
defer c.Close()
|
|
|
|
|
|
|
|
var b [1024]byte
|
|
|
|
_, err := c.Read(b[:])
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
common.Must2(c.Write([]byte("Response")))
|
|
|
|
}(conn)
|
|
|
|
})
|
|
|
|
common.Must(err)
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
streamSettings := &internet.MemoryStreamConfig{
|
|
|
|
ProtocolName: "websocket",
|
|
|
|
ProtocolSettings: &Config{Path: "ws"},
|
|
|
|
}
|
2024-06-03 16:29:48 +00:00
|
|
|
conn, err := Dial(ctx, net.TCPDestination(net.DomainAddress("localhost"), listenPort), streamSettings)
|
2020-11-25 11:01:53 +00:00
|
|
|
|
|
|
|
common.Must(err)
|
|
|
|
_, err = conn.Write([]byte("Test connection 1"))
|
|
|
|
common.Must(err)
|
|
|
|
|
|
|
|
var b [1024]byte
|
|
|
|
n, err := conn.Read(b[:])
|
|
|
|
common.Must(err)
|
|
|
|
if string(b[:n]) != "Response" {
|
|
|
|
t.Error("response: ", string(b[:n]))
|
|
|
|
}
|
|
|
|
|
|
|
|
common.Must(conn.Close())
|
|
|
|
<-time.After(time.Second * 5)
|
2024-06-03 16:29:48 +00:00
|
|
|
conn, err = Dial(ctx, net.TCPDestination(net.DomainAddress("localhost"), listenPort), streamSettings)
|
2020-11-25 11:01:53 +00:00
|
|
|
common.Must(err)
|
|
|
|
_, err = conn.Write([]byte("Test connection 2"))
|
|
|
|
common.Must(err)
|
|
|
|
n, err = conn.Read(b[:])
|
|
|
|
common.Must(err)
|
|
|
|
if string(b[:n]) != "Response" {
|
|
|
|
t.Error("response: ", string(b[:n]))
|
|
|
|
}
|
|
|
|
common.Must(conn.Close())
|
|
|
|
|
|
|
|
common.Must(listen.Close())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDialWithRemoteAddr(t *testing.T) {
|
2024-06-03 16:29:48 +00:00
|
|
|
listenPort := tcp.PickPort()
|
|
|
|
listen, err := ListenWS(context.Background(), net.LocalHostIP, listenPort, &internet.MemoryStreamConfig{
|
2020-11-25 11:01:53 +00:00
|
|
|
ProtocolName: "websocket",
|
|
|
|
ProtocolSettings: &Config{
|
|
|
|
Path: "ws",
|
|
|
|
},
|
2021-09-20 12:11:21 +00:00
|
|
|
}, func(conn stat.Connection) {
|
|
|
|
go func(c stat.Connection) {
|
2020-11-25 11:01:53 +00:00
|
|
|
defer c.Close()
|
|
|
|
|
|
|
|
var b [1024]byte
|
|
|
|
_, err := c.Read(b[:])
|
|
|
|
// common.Must(err)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = c.Write([]byte("Response"))
|
|
|
|
common.Must(err)
|
|
|
|
}(conn)
|
|
|
|
})
|
|
|
|
common.Must(err)
|
|
|
|
|
2024-06-03 16:29:48 +00:00
|
|
|
conn, err := Dial(context.Background(), net.TCPDestination(net.DomainAddress("localhost"), listenPort), &internet.MemoryStreamConfig{
|
2020-11-25 11:01:53 +00:00
|
|
|
ProtocolName: "websocket",
|
2024-03-29 05:39:59 +00:00
|
|
|
ProtocolSettings: &Config{Path: "ws", Header: map[string]string{"X-Forwarded-For": "1.1.1.1"}},
|
2020-11-25 11:01:53 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
common.Must(err)
|
|
|
|
_, err = conn.Write([]byte("Test connection 1"))
|
|
|
|
common.Must(err)
|
|
|
|
|
|
|
|
var b [1024]byte
|
|
|
|
n, err := conn.Read(b[:])
|
|
|
|
common.Must(err)
|
|
|
|
if string(b[:n]) != "Response" {
|
|
|
|
t.Error("response: ", string(b[:n]))
|
|
|
|
}
|
|
|
|
|
|
|
|
common.Must(listen.Close())
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_listenWSAndDial_TLS(t *testing.T) {
|
2024-06-03 16:29:48 +00:00
|
|
|
listenPort := tcp.PickPort()
|
2020-11-25 11:01:53 +00:00
|
|
|
if runtime.GOARCH == "arm64" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
start := time.Now()
|
|
|
|
|
|
|
|
streamSettings := &internet.MemoryStreamConfig{
|
|
|
|
ProtocolName: "websocket",
|
|
|
|
ProtocolSettings: &Config{
|
|
|
|
Path: "wss",
|
|
|
|
},
|
|
|
|
SecurityType: "tls",
|
|
|
|
SecuritySettings: &tls.Config{
|
|
|
|
AllowInsecure: true,
|
|
|
|
Certificate: []*tls.Certificate{tls.ParseCertificate(cert.MustGenerate(nil, cert.CommonName("localhost")))},
|
|
|
|
},
|
|
|
|
}
|
2024-06-03 16:29:48 +00:00
|
|
|
listen, err := ListenWS(context.Background(), net.LocalHostIP, listenPort, streamSettings, func(conn stat.Connection) {
|
2020-11-25 11:01:53 +00:00
|
|
|
go func() {
|
|
|
|
_ = conn.Close()
|
|
|
|
}()
|
|
|
|
})
|
|
|
|
common.Must(err)
|
|
|
|
defer listen.Close()
|
|
|
|
|
2024-06-03 16:29:48 +00:00
|
|
|
conn, err := Dial(context.Background(), net.TCPDestination(net.DomainAddress("localhost"), listenPort), streamSettings)
|
2020-11-25 11:01:53 +00:00
|
|
|
common.Must(err)
|
|
|
|
_ = conn.Close()
|
|
|
|
|
|
|
|
end := time.Now()
|
|
|
|
if !end.Before(start.Add(time.Second * 5)) {
|
|
|
|
t.Error("end: ", end, " start: ", start)
|
|
|
|
}
|
|
|
|
}
|