Skip to content

Commit

Permalink
Improve configuration detector (cone or symmetric)
Browse files Browse the repository at this point in the history
  • Loading branch information
RPRX authored Jan 10, 2021
1 parent 43eb5d1 commit ee15cc2
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 30 deletions.
1 change: 1 addition & 0 deletions app/proxyman/inbound/always.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ func NewAlwaysOnInboundHandler(ctx context.Context, tag string, receiverConfig *
uplinkCounter: uplinkCounter,
downlinkCounter: downlinkCounter,
stream: mss,
ctx: ctx,
}
h.workers = append(h.workers, worker)
}
Expand Down
1 change: 1 addition & 0 deletions app/proxyman/inbound/dynamic.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ func (h *DynamicInboundHandler) refresh() error {
uplinkCounter: uplinkCounter,
downlinkCounter: downlinkCounter,
stream: h.streamSettings,
ctx: h.ctx,
}
if err := worker.Start(); err != nil {
newError("failed to create UDP worker").Base(err).AtWarning().WriteToLog()
Expand Down
7 changes: 6 additions & 1 deletion app/proxyman/inbound/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,9 @@ type udpWorker struct {

checker *task.Periodic
activeConn map[connID]*udpConn

ctx context.Context
cone bool
}

func (w *udpWorker) getConnection(id connID) (*udpConn, bool) {
Expand Down Expand Up @@ -279,7 +282,7 @@ func (w *udpWorker) callback(b *buf.Buffer, source net.Destination, originalDest
src: source,
}
if originalDest.IsValid() {
if !buf.Cone {
if !w.cone {
id.dest = originalDest
}
b.UDP = &originalDest
Expand Down Expand Up @@ -360,6 +363,8 @@ func (w *udpWorker) Start() error {
return err
}

w.cone = w.ctx.Value("cone").(bool)

w.checker = &task.Periodic{
Interval: time.Minute,
Execute: w.clean,
Expand Down
2 changes: 0 additions & 2 deletions common/buf/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ const (

var pool = bytespool.GetPool(Size)

var Cone = true

// Buffer is a recyclable allocation of a byte array. Buffer.Release() recycles
// the buffer into an internal buffer pool, in order to recreate a buffer more
// quickly.
Expand Down
30 changes: 30 additions & 0 deletions core/xray.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@ package core
import (
"context"
"reflect"
"runtime/debug"
"strings"
"sync"

"github.com/golang/protobuf/proto"

"github.com/xtls/xray-core/app/proxyman"
"github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/serial"
"github.com/xtls/xray-core/features"
Expand Down Expand Up @@ -179,6 +184,31 @@ func NewWithContext(ctx context.Context, config *Config) (*Instance, error) {
}

func initInstanceWithConfig(config *Config, server *Instance) (bool, error) {
cone := true
v, t := false, false
for _, outbound := range config.Outbound {
s := strings.ToLower(outbound.ProxySettings.Type)
l := len(s)
if l >= 16 && s[11:16] == "vless" || l >= 16 && s[11:16] == "vmess" {
v = true
continue
}
if l >= 17 && s[11:17] == "trojan" || l >= 22 && s[11:22] == "shadowsocks" {
t = true
var m proxyman.SenderConfig
proto.Unmarshal(outbound.SenderSettings.Value, &m)
if m.MultiplexSettings != nil && m.MultiplexSettings.Enabled {
cone = false
break
}
}
}
if v && !t {
cone = false
}
server.ctx = context.WithValue(server.ctx, "cone", cone)
defer debug.FreeOSMemory()

if config.Transport != nil {
features.PrintDeprecatedFeatureWarning("global transport settings")
}
Expand Down
24 changes: 0 additions & 24 deletions main/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ import (
"strings"
"syscall"

"github.com/golang/protobuf/proto"

"github.com/xtls/xray-core/app/proxyman"
"github.com/xtls/xray-core/common/buf"
"github.com/xtls/xray-core/common/cmdarg"
"github.com/xtls/xray-core/common/platform"
"github.com/xtls/xray-core/core"
Expand Down Expand Up @@ -185,26 +181,6 @@ func startXray() (core.Server, error) {
return nil, newError("failed to load config files: [", configFiles.String(), "]").Base(err)
}

v, t := false, false
for _, outbound := range config.Outbound {
s := strings.ToLower(outbound.ProxySettings.Type)
l := len(s)
if l >= 16 && s[11:16] == "vless" || l >= 16 && s[11:16] == "vmess" {
v = true
continue
}
if l >= 17 && s[11:17] == "trojan" || l >= 22 && s[11:22] == "shadowsocks" {
var m proxyman.SenderConfig
proto.Unmarshal(outbound.SenderSettings.Value, &m)
if m.MultiplexSettings == nil || !m.MultiplexSettings.Enabled {
t = true
}
}
}
if v && !t {
buf.Cone = false
}

server, err := core.New(config)
if err != nil {
return nil, newError("failed to create server").Base(err)
Expand Down
4 changes: 3 additions & 1 deletion proxy/shadowsocks/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Server struct {
config *ServerConfig
user *protocol.MemoryUser
policyManager policy.Manager
cone bool
}

// NewServer create a new Shadowsocks server.
Expand All @@ -42,6 +43,7 @@ func NewServer(ctx context.Context, config *ServerConfig) (*Server, error) {
config: config,
user: mUser,
policyManager: v.GetFeature(policy.ManagerType()).(policy.Manager),
cone: ctx.Value("cone").(bool),
}

return s, nil
Expand Down Expand Up @@ -144,7 +146,7 @@ func (s *Server) handlerUDPPayload(ctx context.Context, conn internet.Connection

data.UDP = &destination

if !buf.Cone || dest == nil {
if !s.cone || dest == nil {
dest = &destination
}

Expand Down
4 changes: 3 additions & 1 deletion proxy/socks/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
type Server struct {
config *ServerConfig
policyManager policy.Manager
cone bool
}

// NewServer creates a new Server object.
Expand All @@ -34,6 +35,7 @@ func NewServer(ctx context.Context, config *ServerConfig) (*Server, error) {
s := &Server{
config: config,
policyManager: v.GetFeature(policy.ManagerType()).(policy.Manager),
cone: ctx.Value("cone").(bool),
}
return s, nil
}
Expand Down Expand Up @@ -261,7 +263,7 @@ func (s *Server) handleUDPPayload(ctx context.Context, conn internet.Connection,

payload.UDP = &destination

if !buf.Cone || dest == nil {
if !s.cone || dest == nil {
dest = &destination
}

Expand Down
4 changes: 3 additions & 1 deletion proxy/trojan/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type Server struct {
policyManager policy.Manager
validator *Validator
fallbacks map[string]map[string]*Fallback // or nil
cone bool
}

// NewServer creates a new trojan inbound handler.
Expand All @@ -67,6 +68,7 @@ func NewServer(ctx context.Context, config *ServerConfig) (*Server, error) {
server := &Server{
policyManager: v.GetFeature(policy.ManagerType()).(policy.Manager),
validator: validator,
cone: ctx.Value("cone").(bool),
}

if config.Fallbacks != nil {
Expand Down Expand Up @@ -293,7 +295,7 @@ func (s *Server) handleUDPPayload(ctx context.Context, clientReader *PacketReade
}
newError("tunnelling request to ", destination).WriteToLog(session.ExportIDToError(ctx))

if !buf.Cone || dest == nil {
if !s.cone || dest == nil {
dest = &destination
}

Expand Down

0 comments on commit ee15cc2

Please sign in to comment.