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