mirror of https://github.com/v2ray/v2ray-core
simplify connection reuse settings
parent
9a8bc3fc3c
commit
ff0cb89efa
|
@ -16,7 +16,6 @@ import (
|
||||||
"github.com/v2ray/v2ray-core/common/retry"
|
"github.com/v2ray/v2ray-core/common/retry"
|
||||||
"github.com/v2ray/v2ray-core/proxy"
|
"github.com/v2ray/v2ray-core/proxy"
|
||||||
proxyrepo "github.com/v2ray/v2ray-core/proxy/repo"
|
proxyrepo "github.com/v2ray/v2ray-core/proxy/repo"
|
||||||
"github.com/v2ray/v2ray-core/transport"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Point shell of V2Ray.
|
// Point shell of V2Ray.
|
||||||
|
@ -40,7 +39,7 @@ func NewPoint(pConfig *Config) (*Point, error) {
|
||||||
vpoint.listen = pConfig.ListenOn
|
vpoint.listen = pConfig.ListenOn
|
||||||
|
|
||||||
if pConfig.TransportConfig != nil {
|
if pConfig.TransportConfig != nil {
|
||||||
transport.ApplyConfig(pConfig.TransportConfig)
|
pConfig.TransportConfig.Apply()
|
||||||
}
|
}
|
||||||
|
|
||||||
if pConfig.LogConfig != nil {
|
if pConfig.LogConfig != nil {
|
||||||
|
|
|
@ -31,9 +31,6 @@
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"transport": {
|
"transport": {
|
||||||
"streamType": "tcp",
|
"connectionReuse": true
|
||||||
"settings": {
|
|
||||||
"connectionReuse": true
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,9 +40,6 @@
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"transport": {
|
"transport": {
|
||||||
"streamType": "tcp",
|
"connectionReuse": true
|
||||||
"settings": {
|
|
||||||
"connectionReuse": true
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,15 +2,13 @@ package transport
|
||||||
|
|
||||||
type StreamType int
|
type StreamType int
|
||||||
|
|
||||||
const (
|
type Config struct {
|
||||||
StreamTypeTCP = StreamType(0)
|
|
||||||
)
|
|
||||||
|
|
||||||
type TCPConfig struct {
|
|
||||||
ConnectionReuse bool
|
ConnectionReuse bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type Config struct {
|
func (this *Config) Apply() error {
|
||||||
StreamType StreamType
|
if this.ConnectionReuse {
|
||||||
TCPConfig *TCPConfig
|
connectionReuse = true
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,37 +2,16 @@
|
||||||
|
|
||||||
package transport
|
package transport
|
||||||
|
|
||||||
import (
|
import "encoding/json"
|
||||||
"encoding/json"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
func (this *Config) UnmarshalJSON(data []byte) error {
|
func (this *Config) UnmarshalJSON(data []byte) error {
|
||||||
type TypeConfig struct {
|
type JsonConfig struct {
|
||||||
StreamType string `json:"streamType"`
|
|
||||||
Settings json.RawMessage `json:"settings"`
|
|
||||||
}
|
|
||||||
type JsonTCPConfig struct {
|
|
||||||
ConnectionReuse bool `json:"connectionReuse"`
|
ConnectionReuse bool `json:"connectionReuse"`
|
||||||
}
|
}
|
||||||
|
jsonConfig := new(JsonConfig)
|
||||||
typeConfig := new(TypeConfig)
|
if err := json.Unmarshal(data, jsonConfig); err != nil {
|
||||||
if err := json.Unmarshal(data, typeConfig); err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
this.ConnectionReuse = jsonConfig.ConnectionReuse
|
||||||
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,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,7 +39,7 @@ func (this *Connection) Close() error {
|
||||||
if this == nil || this.conn == nil {
|
if this == nil || this.conn == nil {
|
||||||
return ErrorClosedConnection
|
return ErrorClosedConnection
|
||||||
}
|
}
|
||||||
if transport.TCPStreamConfig.ConnectionReuse && this.Reusable() {
|
if transport.IsConnectionReusable() && this.Reusable() {
|
||||||
this.listener.Recycle(this.dest, this.conn)
|
this.listener.Recycle(this.dest, this.conn)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,7 +19,7 @@ var (
|
||||||
func Dial(dest v2net.Destination) (*Connection, error) {
|
func Dial(dest v2net.Destination) (*Connection, error) {
|
||||||
destStr := dest.String()
|
destStr := dest.String()
|
||||||
var conn net.Conn
|
var conn net.Conn
|
||||||
if transport.TCPStreamConfig.ConnectionReuse {
|
if transport.IsConnectionReusable() {
|
||||||
conn = globalCache.Get(destStr)
|
conn = globalCache.Get(destStr)
|
||||||
}
|
}
|
||||||
if conn == nil {
|
if conn == nil {
|
||||||
|
|
|
@ -1,22 +1,9 @@
|
||||||
package transport
|
package transport
|
||||||
|
|
||||||
import "github.com/v2ray/v2ray-core/common/log"
|
|
||||||
|
|
||||||
var (
|
var (
|
||||||
TCPStreamConfig = TCPConfig{
|
connectionReuse = false
|
||||||
ConnectionReuse: false,
|
|
||||||
}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func ApplyConfig(config *Config) error {
|
func IsConnectionReusable() bool {
|
||||||
if config.StreamType == StreamTypeTCP {
|
return connectionReuse
|
||||||
if config.TCPConfig != nil {
|
|
||||||
TCPStreamConfig.ConnectionReuse = config.TCPConfig.ConnectionReuse
|
|
||||||
if TCPStreamConfig.ConnectionReuse {
|
|
||||||
log.Info("Transport: TCP connection reuse enabled.")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue