Compare commits

..
1 Commits
Author SHA1 Message Date
Fangliding dbee0f6561 Track alive hy conn 2026-10-08 04:00:54 +08:00
3 changed files with 55 additions and 47 deletions
+1 -3
View File
@@ -92,8 +92,6 @@ type Instance struct {
// Instance state // Instance state
func (server *Instance) IsRunning() bool { func (server *Instance) IsRunning() bool {
server.statusLock.Lock()
defer server.statusLock.Unlock()
return server.running return server.running
} }
@@ -322,7 +320,7 @@ func (s *Instance) RequireFeatures(callback interface{}, optional bool) error {
// AddFeature registers a feature into current Instance. // AddFeature registers a feature into current Instance.
func (s *Instance) AddFeature(feature features.Feature) error { func (s *Instance) AddFeature(feature features.Feature) error {
if s.IsRunning() { if s.running {
if err := feature.Start(); err != nil { if err := feature.Start(); err != nil {
errors.LogInfoInner(s.ctx, err, "failed to start feature") errors.LogInfoInner(s.ctx, err, "failed to start feature")
} }
+8
View File
@@ -5,6 +5,7 @@ import (
"encoding/binary" "encoding/binary"
"io" "io"
"sync" "sync"
"sync/atomic"
"time" "time"
"github.com/apernet/quic-go" "github.com/apernet/quic-go"
@@ -22,6 +23,8 @@ type interConn struct {
client bool client bool
user *protocol.MemoryUser user *protocol.MemoryUser
closeOnce sync.Once
aliveTCP *atomic.Int64
} }
func (c *interConn) User() *protocol.MemoryUser { func (c *interConn) User() *protocol.MemoryUser {
@@ -46,6 +49,11 @@ func (c *interConn) Write(b []byte) (int, error) {
func (c *interConn) Close() error { func (c *interConn) Close() error {
c.stream.CancelRead(0) c.stream.CancelRead(0)
if c.aliveTCP != nil {
c.closeOnce.Do(func() {
c.aliveTCP.Add(-1)
})
}
return c.stream.Close() return c.stream.Close()
} }
+39 -37
View File
@@ -9,6 +9,7 @@ import (
"runtime" "runtime"
"strconv" "strconv"
"sync" "sync"
"sync/atomic"
"time" "time"
"github.com/apernet/quic-go" "github.com/apernet/quic-go"
@@ -17,7 +18,6 @@ import (
"github.com/xtls/xray-core/common/errors" "github.com/xtls/xray-core/common/errors"
"github.com/xtls/xray-core/common/net" "github.com/xtls/xray-core/common/net"
"github.com/xtls/xray-core/common/net/cnc" "github.com/xtls/xray-core/common/net/cnc"
"github.com/xtls/xray-core/core"
"github.com/xtls/xray-core/transport/internet" "github.com/xtls/xray-core/transport/internet"
"github.com/xtls/xray-core/transport/internet/finalmask" "github.com/xtls/xray-core/transport/internet/finalmask"
"github.com/xtls/xray-core/transport/internet/hysteria/congestion" "github.com/xtls/xray-core/transport/internet/hysteria/congestion"
@@ -29,8 +29,6 @@ import (
type client struct { type client struct {
sync.Mutex sync.Mutex
instance *core.Instance
forced bool
dest net.Destination dest net.Destination
config *Config config *Config
tlsConfig *gotls.Config tlsConfig *gotls.Config
@@ -42,6 +40,8 @@ type client struct {
tr *quic.Transport tr *quic.Transport
pktConn net.PacketConn pktConn net.PacketConn
udpSM *udpSessionManager udpSM *udpSessionManager
aliveTCP *atomic.Int64
lastAlive time.Time
} }
func (c *client) status() status { func (c *client) status() status {
@@ -67,14 +67,11 @@ func (c *client) close() {
} }
func (c *client) dial(ctx context.Context) error { func (c *client) dial(ctx context.Context) error {
if c.forced { status := c.status()
return errors.New("client is closed") if status == StatusActive {
}
switch c.status() {
case StatusActive:
return nil return nil
case StatusInactive: }
if status == StatusInactive {
c.close() c.close()
} }
@@ -96,7 +93,7 @@ func (c *client) dial(ctx context.Context) error {
ChromeParrot: !quicParams.DisableChromeParrot, ChromeParrot: !quicParams.DisableChromeParrot,
EnableDatagrams: true, EnableDatagrams: true,
MaxDatagramFrameSize: MaxDatagramFrameSize, MaxDatagramFrameSize: MaxDatagramFrameSize,
OmitMaxDatagramFrameSize: true, OmitMaxDatagramFrameSize: time.Now().After(time.Date(2026, 9, 1, 0, 0, 0, 0, time.UTC)),
DisablePathManager: true, DisablePathManager: true,
} }
if quicParams.InitStreamReceiveWindow == 0 { if quicParams.InitStreamReceiveWindow == 0 {
@@ -241,12 +238,15 @@ func (c *client) tcp(ctx context.Context) (stat.Connection, error) {
return nil, err return nil, err
} }
c.lastAlive = time.Now()
c.aliveTCP.Add(1)
return &interConn{ return &interConn{
stream: stream, stream: stream,
local: c.conn.LocalAddr(), local: c.conn.LocalAddr(),
remote: c.conn.RemoteAddr(), remote: c.conn.RemoteAddr(),
client: true, client: true,
aliveTCP: c.aliveTCP,
}, nil }, nil
} }
@@ -262,15 +262,30 @@ func (c *client) udp(ctx context.Context) (stat.Connection, error) {
return c.udpSM.udp() return c.udpSM.udp()
} }
func (c *client) clean(force bool) { func (c *client) clean() {
c.Lock() c.Lock()
if force { defer c.Unlock()
c.forced = true switch c.status() {
} case StatusInactive:
if status := c.status(); force && status != StatusNull || status == StatusInactive {
c.close() c.close()
return
case StatusNull:
return
}
var udpSessions int
if c.udpSM != nil {
c.udpSM.RLock()
udpSessions = len(c.udpSM.m)
c.udpSM.RUnlock()
}
if udpSessions == 0 && c.aliveTCP.Load() == 0 {
if c.lastAlive.Add(net.ConnIdleTimeout).Before(time.Now()) {
c.close()
return
}
} else {
c.lastAlive = time.Now()
} }
c.Unlock()
} }
type dialerConf struct { type dialerConf struct {
@@ -286,23 +301,11 @@ type clientManager struct {
func (m *clientManager) clean() { func (m *clientManager) clean() {
ticker := time.NewTicker(idleCleanupInterval) ticker := time.NewTicker(idleCleanupInterval)
for range ticker.C { for range ticker.C {
var forced []dialerConf
m.RLock() m.RLock()
for k, c := range m.m { for _, c := range m.m {
force := c.instance != nil && !c.instance.IsRunning() c.clean()
c.clean(force)
if force {
forced = append(forced, k)
}
} }
m.RUnlock() m.RUnlock()
for i := range forced {
m.Lock()
delete(m.m, forced[i])
m.Unlock()
}
} }
} }
@@ -327,26 +330,25 @@ func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.Me
go manager.clean() go manager.clean()
}) })
dialerConfKey := dialerConf{dest, streamSettings}
manager.RLock() manager.RLock()
c := manager.m[dialerConfKey] c := manager.m[dialerConf{dest, streamSettings}]
manager.RUnlock() manager.RUnlock()
if c == nil { if c == nil {
manager.Lock() manager.Lock()
c = manager.m[dialerConfKey] c = manager.m[dialerConf{dest, streamSettings}]
if c == nil { if c == nil {
c = &client{ c = &client{
instance: core.FromContext(ctx),
dest: dest, dest: dest,
config: streamSettings.ProtocolSettings.(*Config), config: streamSettings.ProtocolSettings.(*Config),
tlsConfig: tlsConfig.GetTLSConfig(tls.WithDestination(dest)), tlsConfig: tlsConfig.GetTLSConfig(tls.WithDestination(dest)),
socketConfig: streamSettings.SocketSettings, socketConfig: streamSettings.SocketSettings,
finalMask: streamSettings.FinalMask, finalMask: streamSettings.FinalMask,
quicParams: streamSettings.QuicParams, quicParams: streamSettings.QuicParams,
aliveTCP: &atomic.Int64{},
lastAlive: time.Now(),
} }
manager.m[dialerConfKey] = c manager.m[dialerConf{dest, streamSettings}] = c
} }
manager.Unlock() manager.Unlock()
} }