|
|
@@ -1,18 +1,20 @@
|
|
|
package ws_test
|
|
|
|
|
|
import (
|
|
|
+ "crypto/tls"
|
|
|
"testing"
|
|
|
"time"
|
|
|
|
|
|
v2net "v2ray.com/core/common/net"
|
|
|
"v2ray.com/core/testing/assert"
|
|
|
+ "v2ray.com/core/transport/internet"
|
|
|
. "v2ray.com/core/transport/internet/ws"
|
|
|
)
|
|
|
|
|
|
func Test_Connect_ws(t *testing.T) {
|
|
|
assert := assert.On(t)
|
|
|
- (&Config{Pto: "ws", Path: ""}).Apply()
|
|
|
- conn, err := Dial(v2net.AnyIP, v2net.TCPDestination(v2net.DomainAddress("echo.websocket.org"), 80))
|
|
|
+ (&Config{Path: ""}).Apply()
|
|
|
+ conn, err := Dial(v2net.AnyIP, v2net.TCPDestination(v2net.DomainAddress("echo.websocket.org"), 80), internet.DialerOptions{})
|
|
|
assert.Error(err).IsNil()
|
|
|
conn.Write([]byte("echo"))
|
|
|
s := make(chan int)
|
|
|
@@ -31,8 +33,12 @@ func Test_Connect_ws(t *testing.T) {
|
|
|
|
|
|
func Test_Connect_wss(t *testing.T) {
|
|
|
assert := assert.On(t)
|
|
|
- (&Config{Pto: "wss", Path: ""}).Apply()
|
|
|
- conn, err := Dial(v2net.AnyIP, v2net.TCPDestination(v2net.DomainAddress("echo.websocket.org"), 443))
|
|
|
+ (&Config{Path: ""}).Apply()
|
|
|
+ conn, err := Dial(v2net.AnyIP, v2net.TCPDestination(v2net.DomainAddress("echo.websocket.org"), 443), internet.DialerOptions{
|
|
|
+ Stream: &internet.StreamSettings{
|
|
|
+ Security: internet.StreamSecurityTypeTLS,
|
|
|
+ },
|
|
|
+ })
|
|
|
assert.Error(err).IsNil()
|
|
|
conn.Write([]byte("echo"))
|
|
|
s := make(chan int)
|
|
|
@@ -51,8 +57,12 @@ func Test_Connect_wss(t *testing.T) {
|
|
|
|
|
|
func Test_Connect_wss_1_nil(t *testing.T) {
|
|
|
assert := assert.On(t)
|
|
|
- (&Config{Pto: "wss", Path: ""}).Apply()
|
|
|
- conn, err := Dial(nil, v2net.TCPDestination(v2net.DomainAddress("echo.websocket.org"), 443))
|
|
|
+ (&Config{Path: ""}).Apply()
|
|
|
+ conn, err := Dial(nil, v2net.TCPDestination(v2net.DomainAddress("echo.websocket.org"), 443), internet.DialerOptions{
|
|
|
+ Stream: &internet.StreamSettings{
|
|
|
+ Security: internet.StreamSecurityTypeTLS,
|
|
|
+ },
|
|
|
+ })
|
|
|
assert.Error(err).IsNil()
|
|
|
conn.Write([]byte("echo"))
|
|
|
s := make(chan int)
|
|
|
@@ -71,8 +81,8 @@ func Test_Connect_wss_1_nil(t *testing.T) {
|
|
|
|
|
|
func Test_Connect_ws_guess(t *testing.T) {
|
|
|
assert := assert.On(t)
|
|
|
- (&Config{Pto: "", Path: ""}).Apply()
|
|
|
- conn, err := Dial(v2net.AnyIP, v2net.TCPDestination(v2net.DomainAddress("echo.websocket.org"), 80))
|
|
|
+ (&Config{Path: ""}).Apply()
|
|
|
+ conn, err := Dial(v2net.AnyIP, v2net.TCPDestination(v2net.DomainAddress("echo.websocket.org"), 80), internet.DialerOptions{})
|
|
|
assert.Error(err).IsNil()
|
|
|
conn.Write([]byte("echo"))
|
|
|
s := make(chan int)
|
|
|
@@ -91,8 +101,12 @@ func Test_Connect_ws_guess(t *testing.T) {
|
|
|
|
|
|
func Test_Connect_wss_guess(t *testing.T) {
|
|
|
assert := assert.On(t)
|
|
|
- (&Config{Pto: "", Path: ""}).Apply()
|
|
|
- conn, err := Dial(v2net.AnyIP, v2net.TCPDestination(v2net.DomainAddress("echo.websocket.org"), 443))
|
|
|
+ (&Config{Path: ""}).Apply()
|
|
|
+ conn, err := Dial(v2net.AnyIP, v2net.TCPDestination(v2net.DomainAddress("echo.websocket.org"), 443), internet.DialerOptions{
|
|
|
+ Stream: &internet.StreamSettings{
|
|
|
+ Security: internet.StreamSecurityTypeTLS,
|
|
|
+ },
|
|
|
+ })
|
|
|
assert.Error(err).IsNil()
|
|
|
conn.Write([]byte("echo"))
|
|
|
s := make(chan int)
|
|
|
@@ -111,24 +125,25 @@ func Test_Connect_wss_guess(t *testing.T) {
|
|
|
|
|
|
func Test_Connect_wss_guess_fail(t *testing.T) {
|
|
|
assert := assert.On(t)
|
|
|
- (&Config{Pto: "", Path: ""}).Apply()
|
|
|
- _, err := Dial(v2net.AnyIP, v2net.TCPDestination(v2net.DomainAddress("static.kkdev.org"), 443))
|
|
|
- assert.Error(err).IsNotNil()
|
|
|
-}
|
|
|
-
|
|
|
-func Test_Connect_wss_guess_fail_port(t *testing.T) {
|
|
|
- assert := assert.On(t)
|
|
|
- (&Config{Pto: "", Path: ""}).Apply()
|
|
|
- _, err := Dial(v2net.AnyIP, v2net.TCPDestination(v2net.DomainAddress("static.kkdev.org"), 179))
|
|
|
+ (&Config{Path: ""}).Apply()
|
|
|
+ _, err := Dial(v2net.AnyIP, v2net.TCPDestination(v2net.DomainAddress("static.kkdev.org"), 443), internet.DialerOptions{
|
|
|
+ Stream: &internet.StreamSettings{
|
|
|
+ Security: internet.StreamSecurityTypeTLS,
|
|
|
+ },
|
|
|
+ })
|
|
|
assert.Error(err).IsNotNil()
|
|
|
}
|
|
|
|
|
|
func Test_Connect_wss_guess_reuse(t *testing.T) {
|
|
|
assert := assert.On(t)
|
|
|
- (&Config{Pto: "", Path: "", ConnectionReuse: true}).Apply()
|
|
|
+ (&Config{Path: "", ConnectionReuse: true}).Apply()
|
|
|
i := 3
|
|
|
for i != 0 {
|
|
|
- conn, err := Dial(v2net.AnyIP, v2net.TCPDestination(v2net.DomainAddress("echo.websocket.org"), 443))
|
|
|
+ conn, err := Dial(v2net.AnyIP, v2net.TCPDestination(v2net.DomainAddress("echo.websocket.org"), 443), internet.DialerOptions{
|
|
|
+ Stream: &internet.StreamSettings{
|
|
|
+ Security: internet.StreamSecurityTypeTLS,
|
|
|
+ },
|
|
|
+ })
|
|
|
assert.Error(err).IsNil()
|
|
|
conn.Write([]byte("echo"))
|
|
|
s := make(chan int)
|
|
|
@@ -155,8 +170,8 @@ func Test_Connect_wss_guess_reuse(t *testing.T) {
|
|
|
|
|
|
func Test_listenWSAndDial(t *testing.T) {
|
|
|
assert := assert.On(t)
|
|
|
- (&Config{Pto: "ws", Path: "ws"}).Apply()
|
|
|
- listen, err := ListenWS(v2net.DomainAddress("localhost"), 13142)
|
|
|
+ (&Config{Path: "ws"}).Apply()
|
|
|
+ listen, err := ListenWS(v2net.DomainAddress("localhost"), 13142, internet.ListenOptions{})
|
|
|
assert.Error(err).IsNil()
|
|
|
go func() {
|
|
|
conn, err := listen.Accept()
|
|
|
@@ -170,15 +185,15 @@ func Test_listenWSAndDial(t *testing.T) {
|
|
|
conn.Close()
|
|
|
listen.Close()
|
|
|
}()
|
|
|
- conn, err := Dial(v2net.AnyIP, v2net.TCPDestination(v2net.DomainAddress("localhost"), 13142))
|
|
|
+ conn, err := Dial(v2net.AnyIP, v2net.TCPDestination(v2net.DomainAddress("localhost"), 13142), internet.DialerOptions{})
|
|
|
assert.Error(err).IsNil()
|
|
|
conn.Close()
|
|
|
<-time.After(time.Second * 5)
|
|
|
- conn, err = Dial(v2net.AnyIP, v2net.TCPDestination(v2net.DomainAddress("localhost"), 13142))
|
|
|
+ conn, err = Dial(v2net.AnyIP, v2net.TCPDestination(v2net.DomainAddress("localhost"), 13142), internet.DialerOptions{})
|
|
|
assert.Error(err).IsNil()
|
|
|
conn.Close()
|
|
|
<-time.After(time.Second * 15)
|
|
|
- conn, err = Dial(v2net.AnyIP, v2net.TCPDestination(v2net.DomainAddress("localhost"), 13142))
|
|
|
+ conn, err = Dial(v2net.AnyIP, v2net.TCPDestination(v2net.DomainAddress("localhost"), 13142), internet.DialerOptions{})
|
|
|
assert.Error(err).IsNil()
|
|
|
conn.Close()
|
|
|
}
|
|
|
@@ -189,8 +204,17 @@ func Test_listenWSAndDial_TLS(t *testing.T) {
|
|
|
<-time.After(time.Second * 5)
|
|
|
assert.Fail("Too slow")
|
|
|
}()
|
|
|
- (&Config{Pto: "wss", Path: "wss", ConnectionReuse: true, DeveloperInsecureSkipVerify: true, PrivKey: "./../../../testing/tls/key.pem", Cert: "./../../../testing/tls/cert.pem"}).Apply()
|
|
|
- listen, err := ListenWS(v2net.DomainAddress("localhost"), 13143)
|
|
|
+ (&Config{Path: "wss", ConnectionReuse: true}).Apply()
|
|
|
+
|
|
|
+ listen, err := ListenWS(v2net.DomainAddress("localhost"), 13143, internet.ListenOptions{
|
|
|
+ Stream: &internet.StreamSettings{
|
|
|
+ Security: internet.StreamSecurityTypeTLS,
|
|
|
+ TLSSettings: &internet.TLSSettings{
|
|
|
+ AllowInsecure: true,
|
|
|
+ Certs: LoadTestCert(assert),
|
|
|
+ },
|
|
|
+ },
|
|
|
+ })
|
|
|
assert.Error(err).IsNil()
|
|
|
go func() {
|
|
|
conn, err := listen.Accept()
|
|
|
@@ -198,7 +222,21 @@ func Test_listenWSAndDial_TLS(t *testing.T) {
|
|
|
conn.Close()
|
|
|
listen.Close()
|
|
|
}()
|
|
|
- conn, err := Dial(v2net.AnyIP, v2net.TCPDestination(v2net.DomainAddress("localhost"), 13143))
|
|
|
+ conn, err := Dial(v2net.AnyIP, v2net.TCPDestination(v2net.DomainAddress("localhost"), 13143), internet.DialerOptions{
|
|
|
+ Stream: &internet.StreamSettings{
|
|
|
+ Security: internet.StreamSecurityTypeTLS,
|
|
|
+ TLSSettings: &internet.TLSSettings{
|
|
|
+ AllowInsecure: true,
|
|
|
+ Certs: LoadTestCert(assert),
|
|
|
+ },
|
|
|
+ },
|
|
|
+ })
|
|
|
assert.Error(err).IsNil()
|
|
|
conn.Close()
|
|
|
}
|
|
|
+
|
|
|
+func LoadTestCert(assert *assert.Assert) []tls.Certificate {
|
|
|
+ cert, err := tls.LoadX509KeyPair("./../../../testing/tls/cert.pem", "./../../../testing/tls/key.pem")
|
|
|
+ assert.Error(err).IsNil()
|
|
|
+ return []tls.Certificate{cert}
|
|
|
+}
|