Ver Fonte

simplify connection reuse settings

v2ray há 9 anos atrás
pai
commit
ff0cb89efa

+ 1 - 2
shell/point/point.go

@@ -16,7 +16,6 @@ import (
 	"github.com/v2ray/v2ray-core/common/retry"
 	"github.com/v2ray/v2ray-core/proxy"
 	proxyrepo "github.com/v2ray/v2ray-core/proxy/repo"
-	"github.com/v2ray/v2ray-core/transport"
 )
 
 // Point shell of V2Ray.
@@ -40,7 +39,7 @@ func NewPoint(pConfig *Config) (*Point, error) {
 	vpoint.listen = pConfig.ListenOn
 
 	if pConfig.TransportConfig != nil {
-		transport.ApplyConfig(pConfig.TransportConfig)
+		pConfig.TransportConfig.Apply()
 	}
 
 	if pConfig.LogConfig != nil {

+ 1 - 4
testing/scenarios/data/test_4_client.json

@@ -31,9 +31,6 @@
     }
   },
   "transport": {
-    "streamType": "tcp",
-    "settings": {
-      "connectionReuse": true
-    }
+    "connectionReuse": true
   }
 }

+ 1 - 4
testing/scenarios/data/test_4_server.json

@@ -40,9 +40,6 @@
     }
   ],
   "transport": {
-    "streamType": "tcp",
-    "settings": {
-      "connectionReuse": true
-    }
+    "connectionReuse": true
   }
 }

+ 6 - 8
transport/config.go

@@ -2,15 +2,13 @@ package transport
 
 type StreamType int
 
-const (
-	StreamTypeTCP = StreamType(0)
-)
-
-type TCPConfig struct {
+type Config struct {
 	ConnectionReuse bool
 }
 
-type Config struct {
-	StreamType StreamType
-	TCPConfig  *TCPConfig
+func (this *Config) Apply() error {
+	if this.ConnectionReuse {
+		connectionReuse = true
+	}
+	return nil
 }

+ 5 - 26
transport/config_json.go

@@ -2,37 +2,16 @@
 
 package transport
 
-import (
-	"encoding/json"
-	"strings"
-)
+import "encoding/json"
 
 func (this *Config) UnmarshalJSON(data []byte) error {
-	type TypeConfig struct {
-		StreamType string          `json:"streamType"`
-		Settings   json.RawMessage `json:"settings"`
-	}
-	type JsonTCPConfig struct {
+	type JsonConfig struct {
 		ConnectionReuse bool `json:"connectionReuse"`
 	}
-
-	typeConfig := new(TypeConfig)
-	if err := json.Unmarshal(data, typeConfig); err != nil {
+	jsonConfig := new(JsonConfig)
+	if err := json.Unmarshal(data, jsonConfig); err != nil {
 		return err
 	}
-
-	this.StreamType = StreamTypeTCP
-
-	streamType := strings.ToLower(typeConfig.StreamType)
-	if streamType == "tcp" {
-		jsonTCPConfig := new(JsonTCPConfig)
-		if err := json.Unmarshal(typeConfig.Settings, jsonTCPConfig); err != nil {
-			return err
-		}
-		this.TCPConfig = &TCPConfig{
-			ConnectionReuse: jsonTCPConfig.ConnectionReuse,
-		}
-	}
-
+	this.ConnectionReuse = jsonConfig.ConnectionReuse
 	return nil
 }

+ 1 - 1
transport/hub/connection.go

@@ -39,7 +39,7 @@ func (this *Connection) Close() error {
 	if this == nil || this.conn == nil {
 		return ErrorClosedConnection
 	}
-	if transport.TCPStreamConfig.ConnectionReuse && this.Reusable() {
+	if transport.IsConnectionReusable() && this.Reusable() {
 		this.listener.Recycle(this.dest, this.conn)
 		return nil
 	}

+ 1 - 1
transport/hub/dialer.go

@@ -19,7 +19,7 @@ var (
 func Dial(dest v2net.Destination) (*Connection, error) {
 	destStr := dest.String()
 	var conn net.Conn
-	if transport.TCPStreamConfig.ConnectionReuse {
+	if transport.IsConnectionReusable() {
 		conn = globalCache.Get(destStr)
 	}
 	if conn == nil {

+ 3 - 16
transport/transport.go

@@ -1,22 +1,9 @@
 package transport
 
-import "github.com/v2ray/v2ray-core/common/log"
-
 var (
-	TCPStreamConfig = TCPConfig{
-		ConnectionReuse: false,
-	}
+	connectionReuse = false
 )
 
-func ApplyConfig(config *Config) error {
-	if config.StreamType == StreamTypeTCP {
-		if config.TCPConfig != nil {
-			TCPStreamConfig.ConnectionReuse = config.TCPConfig.ConnectionReuse
-			if TCPStreamConfig.ConnectionReuse {
-				log.Info("Transport: TCP connection reuse enabled.")
-			}
-		}
-	}
-
-	return nil
+func IsConnectionReusable() bool {
+	return connectionReuse
 }