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
func (server *Instance) IsRunning() bool {
server.statusLock.Lock()
defer server.statusLock.Unlock()
return server.running
}
@@ -322,7 +320,7 @@ func (s *Instance) RequireFeatures(callback interface{}, optional bool) error {
// AddFeature registers a feature into current Instance.
func (s *Instance) AddFeature(feature features.Feature) error {
if s.IsRunning() {
if s.running {
if err := feature.Start(); err != nil {
errors.LogInfoInner(s.ctx, err, "failed to start feature")
}
+10 -2
View File
@@ -5,6 +5,7 @@ import (
"encoding/binary"
"io"
"sync"
"sync/atomic"
"time"
"github.com/apernet/quic-go"
@@ -20,8 +21,10 @@ type interConn struct {
local net.Addr
remote net.Addr
client bool
user *protocol.MemoryUser
client bool
user *protocol.MemoryUser
closeOnce sync.Once
aliveTCP *atomic.Int64
}
func (c *interConn) User() *protocol.MemoryUser {
@@ -46,6 +49,11 @@ func (c *interConn) Write(b []byte) (int, error) {
func (c *interConn) Close() error {
c.stream.CancelRead(0)
if c.aliveTCP != nil {
c.closeOnce.Do(func() {
c.aliveTCP.Add(-1)
})
}
return c.stream.Close()
}
+44 -42
View File
@@ -9,6 +9,7 @@ import (
"runtime"
"strconv"
"sync"
"sync/atomic"
"time"
"github.com/apernet/quic-go"
@@ -17,7 +18,6 @@ import (
"github.com/xtls/xray-core/common/errors"
"github.com/xtls/xray-core/common/net"
"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/finalmask"
"github.com/xtls/xray-core/transport/internet/hysteria/congestion"
@@ -29,8 +29,6 @@ import (
type client struct {
sync.Mutex
instance *core.Instance
forced bool
dest net.Destination
config *Config
tlsConfig *gotls.Config
@@ -38,10 +36,12 @@ type client struct {
finalMask *finalmask.FinalMask
quicParams *internet.QuicParams
conn *quic.Conn
tr *quic.Transport
pktConn net.PacketConn
udpSM *udpSessionManager
conn *quic.Conn
tr *quic.Transport
pktConn net.PacketConn
udpSM *udpSessionManager
aliveTCP *atomic.Int64
lastAlive time.Time
}
func (c *client) status() status {
@@ -67,14 +67,11 @@ func (c *client) close() {
}
func (c *client) dial(ctx context.Context) error {
if c.forced {
return errors.New("client is closed")
}
switch c.status() {
case StatusActive:
status := c.status()
if status == StatusActive {
return nil
case StatusInactive:
}
if status == StatusInactive {
c.close()
}
@@ -96,7 +93,7 @@ func (c *client) dial(ctx context.Context) error {
ChromeParrot: !quicParams.DisableChromeParrot,
EnableDatagrams: true,
MaxDatagramFrameSize: MaxDatagramFrameSize,
OmitMaxDatagramFrameSize: true,
OmitMaxDatagramFrameSize: time.Now().After(time.Date(2026, 9, 1, 0, 0, 0, 0, time.UTC)),
DisablePathManager: true,
}
if quicParams.InitStreamReceiveWindow == 0 {
@@ -241,12 +238,15 @@ func (c *client) tcp(ctx context.Context) (stat.Connection, error) {
return nil, err
}
c.lastAlive = time.Now()
c.aliveTCP.Add(1)
return &interConn{
stream: stream,
local: c.conn.LocalAddr(),
remote: c.conn.RemoteAddr(),
client: true,
client: true,
aliveTCP: c.aliveTCP,
}, nil
}
@@ -262,15 +262,30 @@ func (c *client) udp(ctx context.Context) (stat.Connection, error) {
return c.udpSM.udp()
}
func (c *client) clean(force bool) {
func (c *client) clean() {
c.Lock()
if force {
c.forced = true
}
if status := c.status(); force && status != StatusNull || status == StatusInactive {
defer c.Unlock()
switch c.status() {
case StatusInactive:
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 {
@@ -286,23 +301,11 @@ type clientManager struct {
func (m *clientManager) clean() {
ticker := time.NewTicker(idleCleanupInterval)
for range ticker.C {
var forced []dialerConf
m.RLock()
for k, c := range m.m {
force := c.instance != nil && !c.instance.IsRunning()
c.clean(force)
if force {
forced = append(forced, k)
}
for _, c := range m.m {
c.clean()
}
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()
})
dialerConfKey := dialerConf{dest, streamSettings}
manager.RLock()
c := manager.m[dialerConfKey]
c := manager.m[dialerConf{dest, streamSettings}]
manager.RUnlock()
if c == nil {
manager.Lock()
c = manager.m[dialerConfKey]
c = manager.m[dialerConf{dest, streamSettings}]
if c == nil {
c = &client{
instance: core.FromContext(ctx),
dest: dest,
config: streamSettings.ProtocolSettings.(*Config),
tlsConfig: tlsConfig.GetTLSConfig(tls.WithDestination(dest)),
socketConfig: streamSettings.SocketSettings,
finalMask: streamSettings.FinalMask,
quicParams: streamSettings.QuicParams,
aliveTCP: &atomic.Int64{},
lastAlive: time.Now(),
}
manager.m[dialerConfKey] = c
manager.m[dialerConf{dest, streamSettings}] = c
}
manager.Unlock()
}