| 
					
				 | 
			
			
				@@ -2,12 +2,8 @@ package scenarios 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import ( 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	"net" 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	"path/filepath" 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	"testing" 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	"io/ioutil" 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	"os" 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	"time" 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	"v2ray.com/core" 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -22,18 +18,11 @@ import ( 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	"v2ray.com/core/proxy/vmess/outbound" 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	"v2ray.com/core/testing/assert" 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	"v2ray.com/core/testing/servers/tcp" 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	tlsgen "v2ray.com/core/testing/tls" 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	"v2ray.com/core/transport/internet" 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	"v2ray.com/core/transport/internet/tls" 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 ) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-func mustReadFile(name string) []byte { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	content, err := ioutil.ReadFile(name) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	if err != nil { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		panic(err) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	return content 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 func TestSimpleTLSConnection(t *testing.T) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	assert := assert.On(t) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -64,12 +53,116 @@ func TestSimpleTLSConnection(t *testing.T) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 					SecurityType: serial.GetMessageType(&tls.Config{}), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 					SecuritySettings: []*serial.TypedMessage{ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 						serial.ToTypedMessage(&tls.Config{ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-							Certificate: []*tls.Certificate{ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+							Certificate: []*tls.Certificate{tlsgen.GenerateCertificateForTest()}, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+						}), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+					}, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+				}, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			}, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		}, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		Outbound: []*core.OutboundConnectionConfig{ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			{ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+				Settings: serial.ToTypedMessage(&freedom.Config{}), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			}, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		}, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	clientPort := pickPort() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	clientConfig := &core.Config{ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		Inbound: []*core.InboundConnectionConfig{ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			{ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+				PortRange: v2net.SinglePortRange(clientPort), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+				ListenOn:  v2net.NewIPOrDomain(v2net.LocalHostIP), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+				Settings: serial.ToTypedMessage(&dokodemo.Config{ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+					Address: v2net.NewIPOrDomain(dest.Address), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+					Port:    uint32(dest.Port), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+					NetworkList: &v2net.NetworkList{ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+						Network: []v2net.Network{v2net.Network_TCP}, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+					}, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+				}), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			}, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		}, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		Outbound: []*core.OutboundConnectionConfig{ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			{ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+				Settings: serial.ToTypedMessage(&outbound.Config{ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+					Receiver: []*protocol.ServerEndpoint{ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+						{ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+							Address: v2net.NewIPOrDomain(v2net.LocalHostIP), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+							Port:    uint32(serverPort), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+							User: []*protocol.User{ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 								{ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-									Certificate: mustReadFile(filepath.Join(os.Getenv("GOPATH"), "src", "v2ray.com", "core", "testing", "tls", "cert.pem")), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-									Key:         mustReadFile(filepath.Join(os.Getenv("GOPATH"), "src", "v2ray.com", "core", "testing", "tls", "key.pem")), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+									Account: serial.ToTypedMessage(&vmess.Account{ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+										Id: userID.String(), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+									}), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 								}, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 							}, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+						}, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+					}, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+				}), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+				StreamSettings: &internet.StreamConfig{ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+					SecurityType: serial.GetMessageType(&tls.Config{}), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+					SecuritySettings: []*serial.TypedMessage{ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+						serial.ToTypedMessage(&tls.Config{ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+							AllowInsecure: true, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+						}), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+					}, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+				}, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			}, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		}, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	assert.Error(InitializeServerConfig(serverConfig)).IsNil() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	assert.Error(InitializeServerConfig(clientConfig)).IsNil() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		IP:   []byte{127, 0, 0, 1}, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		Port: int(clientPort), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	}) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	assert.Error(err).IsNil() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	payload := "dokodemo request." 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	nBytes, err := conn.Write([]byte(payload)) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	assert.Error(err).IsNil() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	assert.Int(nBytes).Equals(len(payload)) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	response := readFrom(conn, time.Second*2, len(payload)) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	assert.Bytes(response).Equals(xor([]byte(payload))) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	assert.Error(conn.Close()).IsNil() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	CloseAllServers() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+func TestTLSOverKCP(t *testing.T) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	assert := assert.On(t) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	tcpServer := tcp.Server{ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		MsgProcessor: xor, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	dest, err := tcpServer.Start() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	assert.Error(err).IsNil() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	defer tcpServer.Close() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	userID := protocol.NewID(uuid.New()) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	serverPort := pickPort() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	serverConfig := &core.Config{ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		Inbound: []*core.InboundConnectionConfig{ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			{ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+				PortRange: v2net.SinglePortRange(serverPort), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+				ListenOn:  v2net.NewIPOrDomain(v2net.LocalHostIP), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+				Settings: serial.ToTypedMessage(&inbound.Config{ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+					User: []*protocol.User{ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+						{ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+							Account: serial.ToTypedMessage(&vmess.Account{ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+								Id: userID.String(), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+							}), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+						}, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+					}, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+				}), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+				StreamSettings: &internet.StreamConfig{ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+					Protocol:     internet.TransportProtocol_MKCP, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+					SecurityType: serial.GetMessageType(&tls.Config{}), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+					SecuritySettings: []*serial.TypedMessage{ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+						serial.ToTypedMessage(&tls.Config{ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+							Certificate: []*tls.Certificate{tlsgen.GenerateCertificateForTest()}, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 						}), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 					}, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 				}, 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -115,6 +208,7 @@ func TestSimpleTLSConnection(t *testing.T) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 					}, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 				}), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 				StreamSettings: &internet.StreamConfig{ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+					Protocol:     internet.TransportProtocol_MKCP, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 					SecurityType: serial.GetMessageType(&tls.Config{}), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 					SecuritySettings: []*serial.TypedMessage{ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 						serial.ToTypedMessage(&tls.Config{ 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -177,12 +271,7 @@ func TestTLSConnectionReuse(t *testing.T) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 					SecurityType: serial.GetMessageType(&tls.Config{}), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 					SecuritySettings: []*serial.TypedMessage{ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 						serial.ToTypedMessage(&tls.Config{ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-							Certificate: []*tls.Certificate{ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-								{ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-									Certificate: mustReadFile(filepath.Join(os.Getenv("GOPATH"), "src", "v2ray.com", "core", "testing", "tls", "cert.pem")), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-									Key:         mustReadFile(filepath.Join(os.Getenv("GOPATH"), "src", "v2ray.com", "core", "testing", "tls", "key.pem")), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-								}, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-							}, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+							Certificate: []*tls.Certificate{tlsgen.GenerateCertificateForTest()}, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 						}), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 					}, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 				}, 
			 |