Jelajahi Sumber

advanced end 2 end test

Darien Raymond 10 tahun lalu
induk
melakukan
1a25931944

+ 4 - 1
shell/point/inbound_detour.go

@@ -47,7 +47,7 @@ func (this *InboundDetourHandler) Initialize() error {
 // Starts the inbound connection handler.
 func (this *InboundDetourHandler) Start() error {
 	for _, ich := range this.ich {
-		return retry.Timed(100 /* times */, 100 /* ms */).On(func() error {
+		err := retry.Timed(100 /* times */, 100 /* ms */).On(func() error {
 			err := ich.handler.Listen(ich.port)
 			if err != nil {
 				log.Error("Failed to start inbound detour on port %d: %v", ich.port, err)
@@ -55,6 +55,9 @@ func (this *InboundDetourHandler) Start() error {
 			}
 			return nil
 		})
+		if err != nil {
+		  return err
+		}
 	}
 	return nil
 }

+ 25 - 0
testing/scenarios/data/test_1_client.json

@@ -0,0 +1,25 @@
+{
+  "port": 50000,
+  "inbound": {
+    "protocol": "socks",
+    "settings": {
+      "auth": "noauth",
+      "udp": false,
+      "ip": "127.0.0.1"
+    }
+  },
+  "outbound": {
+    "protocol": "vmess",
+    "settings": {
+      "vnext": [
+        {
+          "address": "127.0.0.1",
+          "port": 50001,
+          "users": [
+            {"id": "d17a1af7-efa5-42ca-b7e9-6a35282d737f"}
+          ]
+        }
+      ]
+    }
+  }
+}

+ 18 - 0
testing/scenarios/data/test_1_server.json

@@ -0,0 +1,18 @@
+{
+  "port": 50001,
+  "inbound": {
+    "protocol": "vmess",
+    "settings": {
+      "clients": [
+        {
+          "id": "d17a1af7-efa5-42ca-b7e9-6a35282d737f",
+          "level": 1
+        }
+      ]
+    }
+  },
+  "outbound": {
+    "protocol": "freedom",
+    "settings": {}
+  }
+}

+ 37 - 0
testing/scenarios/data/test_2_client.json

@@ -0,0 +1,37 @@
+{
+  "port": 50010,
+  "inbound": {
+    "protocol": "socks",
+    "settings": {
+      "auth": "noauth",
+      "udp": false,
+      "ip": "127.0.0.1"
+    }
+  },
+  "outbound": {
+    "protocol": "vmess",
+    "settings": {
+      "vnext": [
+        {
+          "address": "127.0.0.1",
+          "port": 50017,
+          "users": [
+            {"id": "d17a1af7-efa5-42ca-b7e9-6a35282d737f"}
+          ]
+        }
+      ]
+    }
+  },
+  "inboundDetour": [
+    {
+      "protocol": "dokodemo-door",
+      "port": "50011-50015",
+      "settings": {
+        "address": "127.0.0.1",
+        "port": 50016,
+        "network": "tcp",
+        "timeout": 0
+      }
+    }
+  ]
+}

+ 18 - 0
testing/scenarios/data/test_2_server.json

@@ -0,0 +1,18 @@
+{
+  "port": 50017,
+  "inbound": {
+    "protocol": "vmess",
+    "settings": {
+      "clients": [
+        {
+          "id": "d17a1af7-efa5-42ca-b7e9-6a35282d737f",
+          "level": 1
+        }
+      ]
+    }
+  },
+  "outbound": {
+    "protocol": "freedom",
+    "settings": {}
+  }
+}

+ 56 - 0
testing/scenarios/dokodemo_test.go

@@ -0,0 +1,56 @@
+package scenarios
+
+import (
+	"net"
+	"testing"
+
+	v2net "github.com/v2ray/v2ray-core/common/net"
+	v2testing "github.com/v2ray/v2ray-core/testing"
+	"github.com/v2ray/v2ray-core/testing/assert"
+	"github.com/v2ray/v2ray-core/testing/servers/tcp"
+)
+
+func TestDokodemoTCP(t *testing.T) {
+	v2testing.Current(t)
+
+	tcpServer := &tcp.Server{
+		Port: v2net.Port(50016),
+		MsgProcessor: func(data []byte) []byte {
+			buffer := make([]byte, 0, 2048)
+			buffer = append(buffer, []byte("Processed: ")...)
+			buffer = append(buffer, data...)
+			return buffer
+		},
+	}
+	_, err := tcpServer.Start()
+	assert.Error(err).IsNil()
+
+	err = InitializeServer(TestFile("test_2_client.json"))
+	assert.Error(err).IsNil()
+
+	err = InitializeServer(TestFile("test_2_server.json"))
+	assert.Error(err).IsNil()
+
+	dokodemoPortStart := v2net.Port(50011)
+	dokodemoPortEnd := v2net.Port(50015)
+
+	for port := dokodemoPortStart; port <= dokodemoPortEnd; port++ {
+		conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{
+			IP:   []byte{127, 0, 0, 1},
+			Port: int(port),
+		})
+
+		payload := "dokodemo request."
+		nBytes, err := conn.Write([]byte(payload))
+		assert.Error(err).IsNil()
+		assert.Int(nBytes).Equals(len(payload))
+
+		conn.CloseWrite()
+
+		response := make([]byte, 1024)
+		nBytes, err = conn.Read(response)
+		assert.Error(err).IsNil()
+		assert.StringLiteral("Processed: " + payload).Equals(string(response[:nBytes]))
+		conn.Close()
+	}
+}

+ 51 - 0
testing/scenarios/server_env.go

@@ -0,0 +1,51 @@
+package scenarios
+
+import (
+  "os"
+  "path/filepath"
+
+	_ "github.com/v2ray/v2ray-core/app/router/config/json"
+	_ "github.com/v2ray/v2ray-core/app/router/rules"
+	_ "github.com/v2ray/v2ray-core/app/router/rules/config/json"
+	"github.com/v2ray/v2ray-core/common/log"
+	"github.com/v2ray/v2ray-core/shell/point"
+	jsonconf "github.com/v2ray/v2ray-core/shell/point/config/json"
+
+	// The following are neccesary as they register handlers in their init functions.
+	_ "github.com/v2ray/v2ray-core/proxy/blackhole"
+	_ "github.com/v2ray/v2ray-core/proxy/blackhole/config/json"
+	_ "github.com/v2ray/v2ray-core/proxy/dokodemo"
+	_ "github.com/v2ray/v2ray-core/proxy/dokodemo/config/json"
+	_ "github.com/v2ray/v2ray-core/proxy/freedom"
+	_ "github.com/v2ray/v2ray-core/proxy/freedom/config/json"
+	_ "github.com/v2ray/v2ray-core/proxy/socks"
+	_ "github.com/v2ray/v2ray-core/proxy/socks/config/json"
+	_ "github.com/v2ray/v2ray-core/proxy/vmess"
+	_ "github.com/v2ray/v2ray-core/proxy/vmess/config/json"
+)
+
+func TestFile(filename string) string {
+  return filepath.Join(os.Getenv("GOPATH"), "src", "github.com", "v2ray", "v2ray-core", "testing", "scenarios", "data", filename)
+}
+
+func InitializeServer(configFile string) error {
+  config, err := jsonconf.LoadConfig(configFile)
+	if err != nil {
+		log.Error("Failed to read config file (%s): %v", configFile, err)
+		return err
+	}
+
+	vPoint, err := point.NewPoint(config)
+	if err != nil {
+		log.Error("Failed to create Point server: %v", err)
+		return err
+	}
+
+	err = vPoint.Start()
+	if err != nil {
+		log.Error("Error starting Point server: %v", err)
+		return err
+	}
+
+	return nil
+}

+ 0 - 124
testing/scenarios/socks5_helper.go

@@ -1,21 +1,7 @@
 package scenarios
 
 import (
-	"net"
-
-	routerconfig "github.com/v2ray/v2ray-core/app/router/config/testing"
-	_ "github.com/v2ray/v2ray-core/app/router/rules"
-	rulesconfig "github.com/v2ray/v2ray-core/app/router/rules/config/testing"
 	v2net "github.com/v2ray/v2ray-core/common/net"
-	v2nettesting "github.com/v2ray/v2ray-core/common/net/testing"
-	_ "github.com/v2ray/v2ray-core/proxy/freedom"
-	_ "github.com/v2ray/v2ray-core/proxy/socks"
-	socksjson "github.com/v2ray/v2ray-core/proxy/socks/config/json"
-	_ "github.com/v2ray/v2ray-core/proxy/vmess"
-	"github.com/v2ray/v2ray-core/proxy/vmess/config"
-	vmessjson "github.com/v2ray/v2ray-core/proxy/vmess/config/json"
-	"github.com/v2ray/v2ray-core/shell/point"
-	"github.com/v2ray/v2ray-core/shell/point/config/testing/mocks"
 )
 
 const (
@@ -60,113 +46,3 @@ func socks5UDPRequest(address v2net.Address, payload []byte) []byte {
 	request = append(request, payload...)
 	return request
 }
-
-func setUpV2Ray(routing func(v2net.Destination) bool) (v2net.Port, v2net.Port, error) {
-	id1, err := config.NewID("ad937d9d-6e23-4a5a-ba23-bce5092a7c51")
-	if err != nil {
-		return 0, 0, err
-	}
-	id2, err := config.NewID("93ccfc71-b136-4015-ac85-e037bd1ead9e")
-	if err != nil {
-		return 0, 0, err
-	}
-	users := []*vmessjson.ConfigUser{
-		&vmessjson.ConfigUser{Id: id1},
-		&vmessjson.ConfigUser{Id: id2},
-	}
-
-	portB := v2nettesting.PickPort()
-	configB := mocks.Config{
-		PortValue: portB,
-		InboundConfigValue: &mocks.ConnectionConfig{
-			ProtocolValue: "vmess",
-			SettingsValue: &vmessjson.Inbound{
-				AllowedClients: users,
-			},
-		},
-		OutboundConfigValue: &mocks.ConnectionConfig{
-			ProtocolValue: "freedom",
-			SettingsValue: nil,
-		},
-	}
-	pointB, err := point.NewPoint(&configB)
-	if err != nil {
-		return 0, 0, err
-	}
-	err = pointB.Start()
-	if err != nil {
-		return 0, 0, err
-	}
-
-	portA := v2nettesting.PickPort()
-	portA2 := v2nettesting.PickPort()
-	configA := mocks.Config{
-		PortValue: portA,
-		InboundConfigValue: &mocks.ConnectionConfig{
-			ProtocolValue: "socks",
-			SettingsValue: &socksjson.SocksConfig{
-				AuthMethod: "noauth",
-				UDP:        true,
-				HostIP:     socksjson.IPAddress(net.IPv4(127, 0, 0, 1)),
-			},
-		},
-		OutboundConfigValue: &mocks.ConnectionConfig{
-			ProtocolValue: "vmess",
-			SettingsValue: &vmessjson.Outbound{
-				[]*vmessjson.ConfigTarget{
-					&vmessjson.ConfigTarget{
-						Address: v2net.IPAddress([]byte{127, 0, 0, 1}, portB),
-						Users:   users,
-					},
-				},
-			},
-		},
-		InboundDetoursValue: []*mocks.InboundDetourConfig{
-			&mocks.InboundDetourConfig{
-				PortRangeValue: &mocks.PortRange{
-					FromValue: portA2,
-					ToValue:   portA2,
-				},
-				ConnectionConfig: &mocks.ConnectionConfig{
-					ProtocolValue: "socks",
-					SettingsValue: &socksjson.SocksConfig{
-						AuthMethod: "noauth",
-						UDP:        false,
-						HostIP:     socksjson.IPAddress(net.IPv4(127, 0, 0, 1)),
-					},
-				},
-			},
-		},
-		OutboundDetoursValue: []*mocks.OutboundDetourConfig{
-			&mocks.OutboundDetourConfig{
-				TagValue: "direct",
-				ConnectionConfig: &mocks.ConnectionConfig{
-					ProtocolValue: "freedom",
-					SettingsValue: nil,
-				},
-			},
-		},
-		RouterConfigValue: &routerconfig.RouterConfig{
-			StrategyValue: "rules",
-			SettingsValue: &rulesconfig.RouterRuleConfig{
-				RuleList: []*rulesconfig.TestRule{
-					&rulesconfig.TestRule{
-						TagValue: "direct",
-						Function: routing,
-					},
-				},
-			},
-		},
-	}
-
-	pointA, err := point.NewPoint(&configA)
-	if err != nil {
-		return 0, 0, err
-	}
-	err = pointA.Start()
-	if err != nil {
-		return 0, 0, err
-	}
-
-	return portA, portA2, nil
-}

+ 23 - 14
testing/scenarios/socks_end_test.go

@@ -12,12 +12,6 @@ import (
 	"github.com/v2ray/v2ray-core/testing/servers/udp"
 )
 
-var (
-	EmptyRouting = func(v2net.Destination) bool {
-		return false
-	}
-)
-
 func TestTCPConnection(t *testing.T) {
 	v2testing.Current(t)
 
@@ -34,13 +28,18 @@ func TestTCPConnection(t *testing.T) {
 	_, err := tcpServer.Start()
 	assert.Error(err).IsNil()
 
-	v2rayPort, _, err := setUpV2Ray(EmptyRouting)
+	err = InitializeServer(TestFile("test_1_client.json"))
 	assert.Error(err).IsNil()
 
+	err = InitializeServer(TestFile("test_1_server.json"))
+	assert.Error(err).IsNil()
+
+	socksPort := v2net.Port(50000)
+
 	for i := 0; i < 100; i++ {
 		conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{
 			IP:   []byte{127, 0, 0, 1},
-			Port: int(v2rayPort),
+			Port: int(socksPort),
 		})
 
 		authRequest := socks5AuthMethodRequest(byte(0))
@@ -100,12 +99,17 @@ func TestTCPBind(t *testing.T) {
 	_, err := tcpServer.Start()
 	assert.Error(err).IsNil()
 
-	v2rayPort, _, err := setUpV2Ray(EmptyRouting)
+	err = InitializeServer(TestFile("test_1_client.json"))
 	assert.Error(err).IsNil()
 
+	err = InitializeServer(TestFile("test_1_server.json"))
+	assert.Error(err).IsNil()
+
+	socksPort := v2net.Port(50000)
+
 	conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{
 		IP:   []byte{127, 0, 0, 1},
-		Port: int(v2rayPort),
+		Port: int(socksPort),
 	})
 
 	authRequest := socks5AuthMethodRequest(byte(0))
@@ -147,12 +151,17 @@ func TestUDPAssociate(t *testing.T) {
 	_, err := udpServer.Start()
 	assert.Error(err).IsNil()
 
-	v2rayPort, _, err := setUpV2Ray(EmptyRouting)
+	err = InitializeServer(TestFile("test_1_client.json"))
 	assert.Error(err).IsNil()
 
+	err = InitializeServer(TestFile("test_1_server.json"))
+	assert.Error(err).IsNil()
+
+	socksPort := v2net.Port(50000)
+
 	conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{
 		IP:   []byte{127, 0, 0, 1},
-		Port: int(v2rayPort),
+		Port: int(socksPort),
 	})
 
 	authRequest := socks5AuthMethodRequest(byte(0))
@@ -173,11 +182,11 @@ func TestUDPAssociate(t *testing.T) {
 	connectResponse := make([]byte, 1024)
 	nBytes, err = conn.Read(connectResponse)
 	assert.Error(err).IsNil()
-	assert.Bytes(connectResponse[:nBytes]).Equals([]byte{socks5Version, 0, 0, 1, 127, 0, 0, 1, byte(v2rayPort >> 8), byte(v2rayPort)})
+	assert.Bytes(connectResponse[:nBytes]).Equals([]byte{socks5Version, 0, 0, 1, 127, 0, 0, 1, byte(socksPort >> 8), byte(socksPort)})
 
 	udpConn, err := net.DialUDP("udp", nil, &net.UDPAddr{
 		IP:   []byte{127, 0, 0, 1},
-		Port: int(v2rayPort),
+		Port: int(socksPort),
 	})
 	assert.Error(err).IsNil()