2016-12-22 23:30:46 +00:00
|
|
|
package websocket_test
|
2016-08-15 12:15:12 +00:00
|
|
|
|
|
|
|
import (
|
2017-01-26 19:46:44 +00:00
|
|
|
"context"
|
2018-08-09 14:51:47 +00:00
|
|
|
"runtime"
|
2017-02-24 00:05:16 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
2017-01-26 19:46:44 +00:00
|
|
|
|
2019-02-02 21:19:30 +00:00
|
|
|
"v2ray.com/core/common"
|
2017-08-29 12:32:54 +00:00
|
|
|
"v2ray.com/core/common/net"
|
2018-04-10 10:42:02 +00:00
|
|
|
"v2ray.com/core/common/protocol/tls/cert"
|
2016-09-30 14:53:40 +00:00
|
|
|
"v2ray.com/core/transport/internet"
|
2018-04-10 10:42:02 +00:00
|
|
|
"v2ray.com/core/transport/internet/tls"
|
2016-12-22 23:30:46 +00:00
|
|
|
. "v2ray.com/core/transport/internet/websocket"
|
2016-08-15 12:15:12 +00:00
|
|
|
)
|
|
|
|
|
2016-08-15 12:20:47 +00:00
|
|
|
func Test_listenWSAndDial(t *testing.T) {
|
2018-11-21 13:54:40 +00:00
|
|
|
listen, err := ListenWS(context.Background(), net.LocalHostIP, 13146, &internet.MemoryStreamConfig{
|
2018-09-07 12:50:25 +00:00
|
|
|
ProtocolName: "websocket",
|
|
|
|
ProtocolSettings: &Config{
|
|
|
|
Path: "ws",
|
|
|
|
},
|
2018-11-21 13:54:40 +00:00
|
|
|
}, func(conn internet.Connection) {
|
2017-05-08 22:01:15 +00:00
|
|
|
go func(c internet.Connection) {
|
|
|
|
defer c.Close()
|
2017-01-04 14:34:11 +00:00
|
|
|
|
2017-05-08 22:01:15 +00:00
|
|
|
var b [1024]byte
|
2019-02-10 14:02:28 +00:00
|
|
|
_, err := c.Read(b[:])
|
2017-05-08 22:01:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2017-01-04 14:34:11 +00:00
|
|
|
|
2019-02-10 14:02:28 +00:00
|
|
|
common.Must2(c.Write([]byte("Response")))
|
2017-05-08 22:01:15 +00:00
|
|
|
}(conn)
|
|
|
|
})
|
2019-02-02 21:19:30 +00:00
|
|
|
common.Must(err)
|
2017-01-26 19:46:44 +00:00
|
|
|
|
2018-11-21 13:54:40 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
streamSettings := &internet.MemoryStreamConfig{
|
2018-09-07 12:50:25 +00:00
|
|
|
ProtocolName: "websocket",
|
|
|
|
ProtocolSettings: &Config{Path: "ws"},
|
2018-11-21 13:54:40 +00:00
|
|
|
}
|
|
|
|
conn, err := Dial(ctx, net.TCPDestination(net.DomainAddress("localhost"), 13146), streamSettings)
|
2017-01-26 19:46:44 +00:00
|
|
|
|
2019-02-02 21:19:30 +00:00
|
|
|
common.Must(err)
|
2017-01-04 14:34:11 +00:00
|
|
|
_, err = conn.Write([]byte("Test connection 1"))
|
2019-02-02 21:19:30 +00:00
|
|
|
common.Must(err)
|
2017-01-04 14:34:11 +00:00
|
|
|
|
|
|
|
var b [1024]byte
|
|
|
|
n, err := conn.Read(b[:])
|
2019-02-02 21:19:30 +00:00
|
|
|
common.Must(err)
|
2019-02-10 14:02:28 +00:00
|
|
|
if string(b[:n]) != "Response" {
|
|
|
|
t.Error("response: ", string(b[:n]))
|
|
|
|
}
|
2017-01-04 14:34:11 +00:00
|
|
|
|
2019-02-10 14:02:28 +00:00
|
|
|
common.Must(conn.Close())
|
2016-08-15 13:29:15 +00:00
|
|
|
<-time.After(time.Second * 5)
|
2018-11-21 13:54:40 +00:00
|
|
|
conn, err = Dial(ctx, net.TCPDestination(net.DomainAddress("localhost"), 13146), streamSettings)
|
2019-02-02 21:19:30 +00:00
|
|
|
common.Must(err)
|
2017-01-04 14:34:11 +00:00
|
|
|
_, err = conn.Write([]byte("Test connection 2"))
|
2019-02-02 21:19:30 +00:00
|
|
|
common.Must(err)
|
2017-01-04 14:34:11 +00:00
|
|
|
n, err = conn.Read(b[:])
|
2019-02-02 21:19:30 +00:00
|
|
|
common.Must(err)
|
2019-02-10 14:02:28 +00:00
|
|
|
if string(b[:n]) != "Response" {
|
|
|
|
t.Error("response: ", string(b[:n]))
|
|
|
|
}
|
|
|
|
common.Must(conn.Close())
|
2017-12-18 19:34:00 +00:00
|
|
|
|
2019-02-10 14:02:28 +00:00
|
|
|
common.Must(listen.Close())
|
2017-12-18 19:34:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDialWithRemoteAddr(t *testing.T) {
|
2018-11-21 13:54:40 +00:00
|
|
|
listen, err := ListenWS(context.Background(), net.LocalHostIP, 13148, &internet.MemoryStreamConfig{
|
2018-09-07 12:50:25 +00:00
|
|
|
ProtocolName: "websocket",
|
|
|
|
ProtocolSettings: &Config{
|
|
|
|
Path: "ws",
|
|
|
|
},
|
2018-11-21 13:54:40 +00:00
|
|
|
}, func(conn internet.Connection) {
|
2017-12-18 19:34:00 +00:00
|
|
|
go func(c internet.Connection) {
|
|
|
|
defer c.Close()
|
|
|
|
|
|
|
|
var b [1024]byte
|
2019-02-10 14:02:28 +00:00
|
|
|
_, err := c.Read(b[:])
|
2019-02-02 21:19:30 +00:00
|
|
|
//common.Must(err)
|
2017-12-18 19:34:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = c.Write([]byte("Response"))
|
2019-02-02 21:19:30 +00:00
|
|
|
common.Must(err)
|
2017-12-18 19:34:00 +00:00
|
|
|
}(conn)
|
|
|
|
})
|
2019-02-02 21:19:30 +00:00
|
|
|
common.Must(err)
|
2017-12-18 19:34:00 +00:00
|
|
|
|
2018-11-21 13:54:40 +00:00
|
|
|
conn, err := Dial(context.Background(), net.TCPDestination(net.DomainAddress("localhost"), 13148), &internet.MemoryStreamConfig{
|
2018-09-07 12:50:25 +00:00
|
|
|
ProtocolName: "websocket",
|
|
|
|
ProtocolSettings: &Config{Path: "ws", Header: []*Header{{Key: "X-Forwarded-For", Value: "1.1.1.1"}}},
|
|
|
|
})
|
2017-12-18 19:34:00 +00:00
|
|
|
|
2019-02-02 21:19:30 +00:00
|
|
|
common.Must(err)
|
2017-12-18 19:34:00 +00:00
|
|
|
_, err = conn.Write([]byte("Test connection 1"))
|
2019-02-02 21:19:30 +00:00
|
|
|
common.Must(err)
|
2017-12-18 19:34:00 +00:00
|
|
|
|
|
|
|
var b [1024]byte
|
|
|
|
n, err := conn.Read(b[:])
|
2019-02-02 21:19:30 +00:00
|
|
|
common.Must(err)
|
2019-02-10 14:02:28 +00:00
|
|
|
if string(b[:n]) != "Response" {
|
|
|
|
t.Error("response: ", string(b[:n]))
|
|
|
|
}
|
2017-01-04 14:34:11 +00:00
|
|
|
|
2019-02-10 14:02:28 +00:00
|
|
|
common.Must(listen.Close())
|
2016-08-15 12:20:47 +00:00
|
|
|
}
|
2016-08-15 13:19:53 +00:00
|
|
|
|
|
|
|
func Test_listenWSAndDial_TLS(t *testing.T) {
|
2018-08-09 14:51:47 +00:00
|
|
|
if runtime.GOARCH == "arm64" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-10-24 14:15:35 +00:00
|
|
|
start := time.Now()
|
2016-09-30 14:53:40 +00:00
|
|
|
|
2018-11-21 13:54:40 +00:00
|
|
|
streamSettings := &internet.MemoryStreamConfig{
|
2018-09-07 12:50:25 +00:00
|
|
|
ProtocolName: "websocket",
|
|
|
|
ProtocolSettings: &Config{
|
|
|
|
Path: "wss",
|
|
|
|
},
|
|
|
|
SecurityType: "tls",
|
|
|
|
SecuritySettings: &tls.Config{
|
|
|
|
AllowInsecure: true,
|
|
|
|
Certificate: []*tls.Certificate{tls.ParseCertificate(cert.MustGenerate(nil, cert.CommonName("localhost")))},
|
|
|
|
},
|
2018-11-21 13:54:40 +00:00
|
|
|
}
|
|
|
|
listen, err := ListenWS(context.Background(), net.LocalHostIP, 13143, streamSettings, func(conn internet.Connection) {
|
2017-05-08 22:01:15 +00:00
|
|
|
go func() {
|
2017-10-22 14:04:39 +00:00
|
|
|
_ = conn.Close()
|
2017-05-08 22:01:15 +00:00
|
|
|
}()
|
|
|
|
})
|
2019-02-02 21:19:30 +00:00
|
|
|
common.Must(err)
|
2017-05-08 22:01:15 +00:00
|
|
|
defer listen.Close()
|
2017-02-24 00:05:16 +00:00
|
|
|
|
2018-11-21 13:54:40 +00:00
|
|
|
conn, err := Dial(context.Background(), net.TCPDestination(net.DomainAddress("localhost"), 13143), streamSettings)
|
2019-02-02 21:19:30 +00:00
|
|
|
common.Must(err)
|
2017-10-22 14:04:39 +00:00
|
|
|
_ = conn.Close()
|
2017-10-24 14:15:35 +00:00
|
|
|
|
|
|
|
end := time.Now()
|
2019-02-10 14:02:28 +00:00
|
|
|
if !end.Before(start.Add(time.Second * 5)) {
|
|
|
|
t.Error("end: ", end, " start: ", start)
|
|
|
|
}
|
2016-08-15 13:19:53 +00:00
|
|
|
}
|