From dfe1ac1f2b791d915e6ec78434c1d1227ad1d45a Mon Sep 17 00:00:00 2001 From: v2ray Date: Thu, 2 Jun 2016 02:20:53 +0200 Subject: [PATCH] Fix connection reuse --- proxy/vmess/inbound/inbound.go | 4 ++-- proxy/vmess/io/reader.go | 7 +------ proxy/vmess/outbound/outbound.go | 5 +++-- testing/scenarios/data/test_4_client.json | 6 ++++++ testing/scenarios/data/test_4_server.json | 8 +++++++- transport/config_json.go | 4 +++- transport/transport.go | 6 +++--- 7 files changed, 25 insertions(+), 15 deletions(-) diff --git a/proxy/vmess/inbound/inbound.go b/proxy/vmess/inbound/inbound.go index 6263ca8a..16e97c2d 100644 --- a/proxy/vmess/inbound/inbound.go +++ b/proxy/vmess/inbound/inbound.go @@ -170,7 +170,7 @@ func (this *VMessInboundHandler) HandleConnection(connection *hub.Connection) { requestReader = v2io.NewAdaptiveReader(bodyReader) } err := v2io.Pipe(requestReader, input) - if err != vmessio.ErrorStreamCompleted { + if err != io.EOF { connection.SetReusable(false) } @@ -202,7 +202,7 @@ func (this *VMessInboundHandler) HandleConnection(connection *hub.Connection) { writer.SetCached(false) err = v2io.Pipe(output, v2writer) - if err != vmessio.ErrorStreamCompleted { + if err != io.EOF { connection.SetReusable(false) } diff --git a/proxy/vmess/io/reader.go b/proxy/vmess/io/reader.go index d027bc3a..f984f87d 100644 --- a/proxy/vmess/io/reader.go +++ b/proxy/vmess/io/reader.go @@ -1,7 +1,6 @@ package io import ( - "errors" "hash" "hash/fnv" "io" @@ -11,10 +10,6 @@ import ( "github.com/v2ray/v2ray-core/transport" ) -var ( - ErrorStreamCompleted = errors.New("Stream completed.") -) - // @Private type Validator struct { actualAuth hash.Hash32 @@ -81,7 +76,7 @@ func (this *AuthChunkReader) Read() (*alloc.Buffer, error) { if this.chunkLength == 0 { buffer.Release() - return nil, ErrorStreamCompleted + return nil, io.EOF } if buffer.Len() < this.chunkLength { diff --git a/proxy/vmess/outbound/outbound.go b/proxy/vmess/outbound/outbound.go index 05721b0e..9a07b741 100644 --- a/proxy/vmess/outbound/outbound.go +++ b/proxy/vmess/outbound/outbound.go @@ -1,6 +1,7 @@ package outbound import ( + "io" "sync" "github.com/v2ray/v2ray-core/app" @@ -85,7 +86,7 @@ func (this *VMessOutboundHandler) handleRequest(session *raw.ClientSession, conn writer.SetCached(false) err := v2io.Pipe(input, streamWriter) - if err != vmessio.ErrorStreamCompleted { + if err != io.EOF { conn.SetReusable(false) } @@ -120,7 +121,7 @@ func (this *VMessOutboundHandler) handleResponse(session *raw.ClientSession, con } err = v2io.Pipe(bodyReader, output) - if err != vmessio.ErrorStreamCompleted { + if err != io.EOF { conn.SetReusable(false) } diff --git a/testing/scenarios/data/test_4_client.json b/testing/scenarios/data/test_4_client.json index 70598ab0..b56452b8 100644 --- a/testing/scenarios/data/test_4_client.json +++ b/testing/scenarios/data/test_4_client.json @@ -29,5 +29,11 @@ } ] } + }, + "transport": { + "streamType": "tcp", + "settings": { + "connectionReuse": true + } } } diff --git a/testing/scenarios/data/test_4_server.json b/testing/scenarios/data/test_4_server.json index e723277c..b7b7f737 100644 --- a/testing/scenarios/data/test_4_server.json +++ b/testing/scenarios/data/test_4_server.json @@ -38,5 +38,11 @@ "refresh": 5 } } - ] + ], + "transport": { + "streamType": "tcp", + "settings": { + "connectionReuse": true + } + } } diff --git a/transport/config_json.go b/transport/config_json.go index 84354d1f..6198efec 100644 --- a/transport/config_json.go +++ b/transport/config_json.go @@ -21,10 +21,12 @@ func (this *Config) UnmarshalJSON(data []byte) error { return err } + this.StreamType = StreamTypeTCP + streamType := strings.ToLower(typeConfig.StreamType) if streamType == "tcp" { jsonTCPConfig := new(JsonTCPConfig) - if err := json.Unmarshal(data, jsonTCPConfig); err != nil { + if err := json.Unmarshal(typeConfig.Settings, jsonTCPConfig); err != nil { return err } this.TCPConfig = &TCPConfig{ diff --git a/transport/transport.go b/transport/transport.go index e5bd2365..478ee4f9 100644 --- a/transport/transport.go +++ b/transport/transport.go @@ -3,7 +3,7 @@ package transport import "github.com/v2ray/v2ray-core/common/log" var ( - TCPStreamConfig = &TCPConfig{ + TCPStreamConfig = TCPConfig{ ConnectionReuse: false, } ) @@ -11,8 +11,8 @@ var ( func ApplyConfig(config *Config) error { if config.StreamType == StreamTypeTCP { if config.TCPConfig != nil { - TCPStreamConfig = config.TCPConfig - if config.TCPConfig.ConnectionReuse { + TCPStreamConfig.ConnectionReuse = config.TCPConfig.ConnectionReuse + if TCPStreamConfig.ConnectionReuse { log.Info("Transport: TCP connection reuse enabled.") } }