|  | @@ -1,18 +1,20 @@
 | 
											
												
													
														|  |  package ws_test
 |  |  package ws_test
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |  import (
 |  |  import (
 | 
											
												
													
														|  | 
 |  | +	"crypto/tls"
 | 
											
												
													
														|  |  	"testing"
 |  |  	"testing"
 | 
											
												
													
														|  |  	"time"
 |  |  	"time"
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |  	v2net "v2ray.com/core/common/net"
 |  |  	v2net "v2ray.com/core/common/net"
 | 
											
												
													
														|  |  	"v2ray.com/core/testing/assert"
 |  |  	"v2ray.com/core/testing/assert"
 | 
											
												
													
														|  | 
 |  | +	"v2ray.com/core/transport/internet"
 | 
											
												
													
														|  |  	. "v2ray.com/core/transport/internet/ws"
 |  |  	. "v2ray.com/core/transport/internet/ws"
 | 
											
												
													
														|  |  )
 |  |  )
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |  func Test_Connect_ws(t *testing.T) {
 |  |  func Test_Connect_ws(t *testing.T) {
 | 
											
												
													
														|  |  	assert := assert.On(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()
 |  |  	assert.Error(err).IsNil()
 | 
											
												
													
														|  |  	conn.Write([]byte("echo"))
 |  |  	conn.Write([]byte("echo"))
 | 
											
												
													
														|  |  	s := make(chan int)
 |  |  	s := make(chan int)
 | 
											
										
											
												
													
														|  | @@ -31,8 +33,12 @@ func Test_Connect_ws(t *testing.T) {
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |  func Test_Connect_wss(t *testing.T) {
 |  |  func Test_Connect_wss(t *testing.T) {
 | 
											
												
													
														|  |  	assert := assert.On(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()
 |  |  	assert.Error(err).IsNil()
 | 
											
												
													
														|  |  	conn.Write([]byte("echo"))
 |  |  	conn.Write([]byte("echo"))
 | 
											
												
													
														|  |  	s := make(chan int)
 |  |  	s := make(chan int)
 | 
											
										
											
												
													
														|  | @@ -51,8 +57,12 @@ func Test_Connect_wss(t *testing.T) {
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |  func Test_Connect_wss_1_nil(t *testing.T) {
 |  |  func Test_Connect_wss_1_nil(t *testing.T) {
 | 
											
												
													
														|  |  	assert := assert.On(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()
 |  |  	assert.Error(err).IsNil()
 | 
											
												
													
														|  |  	conn.Write([]byte("echo"))
 |  |  	conn.Write([]byte("echo"))
 | 
											
												
													
														|  |  	s := make(chan int)
 |  |  	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) {
 |  |  func Test_Connect_ws_guess(t *testing.T) {
 | 
											
												
													
														|  |  	assert := assert.On(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()
 |  |  	assert.Error(err).IsNil()
 | 
											
												
													
														|  |  	conn.Write([]byte("echo"))
 |  |  	conn.Write([]byte("echo"))
 | 
											
												
													
														|  |  	s := make(chan int)
 |  |  	s := make(chan int)
 | 
											
										
											
												
													
														|  | @@ -91,8 +101,12 @@ func Test_Connect_ws_guess(t *testing.T) {
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |  func Test_Connect_wss_guess(t *testing.T) {
 |  |  func Test_Connect_wss_guess(t *testing.T) {
 | 
											
												
													
														|  |  	assert := assert.On(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()
 |  |  	assert.Error(err).IsNil()
 | 
											
												
													
														|  |  	conn.Write([]byte("echo"))
 |  |  	conn.Write([]byte("echo"))
 | 
											
												
													
														|  |  	s := make(chan int)
 |  |  	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) {
 |  |  func Test_Connect_wss_guess_fail(t *testing.T) {
 | 
											
												
													
														|  |  	assert := assert.On(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()
 |  |  	assert.Error(err).IsNotNil()
 | 
											
												
													
														|  |  }
 |  |  }
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |  func Test_Connect_wss_guess_reuse(t *testing.T) {
 |  |  func Test_Connect_wss_guess_reuse(t *testing.T) {
 | 
											
												
													
														|  |  	assert := assert.On(t)
 |  |  	assert := assert.On(t)
 | 
											
												
													
														|  | -	(&Config{Pto: "", Path: "", ConnectionReuse: true}).Apply()
 |  | 
 | 
											
												
													
														|  | 
 |  | +	(&Config{Path: "", ConnectionReuse: true}).Apply()
 | 
											
												
													
														|  |  	i := 3
 |  |  	i := 3
 | 
											
												
													
														|  |  	for i != 0 {
 |  |  	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()
 |  |  		assert.Error(err).IsNil()
 | 
											
												
													
														|  |  		conn.Write([]byte("echo"))
 |  |  		conn.Write([]byte("echo"))
 | 
											
												
													
														|  |  		s := make(chan int)
 |  |  		s := make(chan int)
 | 
											
										
											
												
													
														|  | @@ -155,8 +170,8 @@ func Test_Connect_wss_guess_reuse(t *testing.T) {
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |  func Test_listenWSAndDial(t *testing.T) {
 |  |  func Test_listenWSAndDial(t *testing.T) {
 | 
											
												
													
														|  |  	assert := assert.On(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()
 |  |  	assert.Error(err).IsNil()
 | 
											
												
													
														|  |  	go func() {
 |  |  	go func() {
 | 
											
												
													
														|  |  		conn, err := listen.Accept()
 |  |  		conn, err := listen.Accept()
 | 
											
										
											
												
													
														|  | @@ -170,15 +185,15 @@ func Test_listenWSAndDial(t *testing.T) {
 | 
											
												
													
														|  |  		conn.Close()
 |  |  		conn.Close()
 | 
											
												
													
														|  |  		listen.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()
 |  |  	assert.Error(err).IsNil()
 | 
											
												
													
														|  |  	conn.Close()
 |  |  	conn.Close()
 | 
											
												
													
														|  |  	<-time.After(time.Second * 5)
 |  |  	<-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()
 |  |  	assert.Error(err).IsNil()
 | 
											
												
													
														|  |  	conn.Close()
 |  |  	conn.Close()
 | 
											
												
													
														|  |  	<-time.After(time.Second * 15)
 |  |  	<-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()
 |  |  	assert.Error(err).IsNil()
 | 
											
												
													
														|  |  	conn.Close()
 |  |  	conn.Close()
 | 
											
												
													
														|  |  }
 |  |  }
 | 
											
										
											
												
													
														|  | @@ -189,8 +204,17 @@ func Test_listenWSAndDial_TLS(t *testing.T) {
 | 
											
												
													
														|  |  		<-time.After(time.Second * 5)
 |  |  		<-time.After(time.Second * 5)
 | 
											
												
													
														|  |  		assert.Fail("Too slow")
 |  |  		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()
 |  |  	assert.Error(err).IsNil()
 | 
											
												
													
														|  |  	go func() {
 |  |  	go func() {
 | 
											
												
													
														|  |  		conn, err := listen.Accept()
 |  |  		conn, err := listen.Accept()
 | 
											
										
											
												
													
														|  | @@ -198,7 +222,21 @@ func Test_listenWSAndDial_TLS(t *testing.T) {
 | 
											
												
													
														|  |  		conn.Close()
 |  |  		conn.Close()
 | 
											
												
													
														|  |  		listen.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()
 |  |  	assert.Error(err).IsNil()
 | 
											
												
													
														|  |  	conn.Close()
 |  |  	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}
 | 
											
												
													
														|  | 
 |  | +}
 |