mirror of
https://gitclone.com/github.com/MetaCubeX/Clash.Meta
synced 2025-04-27 21:58:03 +08:00
chore: add MinIdleSession option to AnyTLS configuration
This commit is contained in:
parent
e2b75b35bb
commit
5830afcbde
@ -42,6 +42,7 @@ type AnyTLSOption struct {
|
|||||||
UDP bool `proxy:"udp,omitempty"`
|
UDP bool `proxy:"udp,omitempty"`
|
||||||
IdleSessionCheckInterval int `proxy:"idle-session-check-interval,omitempty"`
|
IdleSessionCheckInterval int `proxy:"idle-session-check-interval,omitempty"`
|
||||||
IdleSessionTimeout int `proxy:"idle-session-timeout,omitempty"`
|
IdleSessionTimeout int `proxy:"idle-session-timeout,omitempty"`
|
||||||
|
MinIdleSession int `proxy:"min-idle-session,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *AnyTLS) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.Conn, err error) {
|
func (t *AnyTLS) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.Conn, err error) {
|
||||||
@ -98,6 +99,7 @@ func NewAnyTLS(option AnyTLSOption) (*AnyTLS, error) {
|
|||||||
Dialer: singDialer,
|
Dialer: singDialer,
|
||||||
IdleSessionCheckInterval: time.Duration(option.IdleSessionCheckInterval) * time.Second,
|
IdleSessionCheckInterval: time.Duration(option.IdleSessionCheckInterval) * time.Second,
|
||||||
IdleSessionTimeout: time.Duration(option.IdleSessionTimeout) * time.Second,
|
IdleSessionTimeout: time.Duration(option.IdleSessionTimeout) * time.Second,
|
||||||
|
MinIdleSession: option.MinIdleSession,
|
||||||
}
|
}
|
||||||
tlsConfig := &vmess.TLSConfig{
|
tlsConfig := &vmess.TLSConfig{
|
||||||
Host: option.SNI,
|
Host: option.SNI,
|
||||||
|
@ -874,6 +874,7 @@ proxies: # socks5
|
|||||||
udp: true
|
udp: true
|
||||||
# idle-session-check-interval: 30 # seconds
|
# idle-session-check-interval: 30 # seconds
|
||||||
# idle-session-timeout: 30 # seconds
|
# idle-session-timeout: 30 # seconds
|
||||||
|
# min-idle-session: 0
|
||||||
# sni: "example.com"
|
# sni: "example.com"
|
||||||
# alpn:
|
# alpn:
|
||||||
# - h2
|
# - h2
|
||||||
|
@ -22,19 +22,19 @@ type ClientConfig struct {
|
|||||||
Password string
|
Password string
|
||||||
IdleSessionCheckInterval time.Duration
|
IdleSessionCheckInterval time.Duration
|
||||||
IdleSessionTimeout time.Duration
|
IdleSessionTimeout time.Duration
|
||||||
|
MinIdleSession int
|
||||||
Server M.Socksaddr
|
Server M.Socksaddr
|
||||||
Dialer N.Dialer
|
Dialer N.Dialer
|
||||||
TLSConfig *vmess.TLSConfig
|
TLSConfig *vmess.TLSConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
type Client struct {
|
type Client struct {
|
||||||
passwordSha256 []byte
|
passwordSha256 []byte
|
||||||
tlsConfig *vmess.TLSConfig
|
tlsConfig *vmess.TLSConfig
|
||||||
clientFingerprint string
|
dialer N.Dialer
|
||||||
dialer N.Dialer
|
server M.Socksaddr
|
||||||
server M.Socksaddr
|
sessionClient *session.Client
|
||||||
sessionClient *session.Client
|
padding atomic.TypedValue[*padding.PaddingFactory]
|
||||||
padding atomic.TypedValue[*padding.PaddingFactory]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewClient(ctx context.Context, config ClientConfig) *Client {
|
func NewClient(ctx context.Context, config ClientConfig) *Client {
|
||||||
@ -47,7 +47,7 @@ func NewClient(ctx context.Context, config ClientConfig) *Client {
|
|||||||
}
|
}
|
||||||
// Initialize the padding state of this client
|
// Initialize the padding state of this client
|
||||||
padding.UpdatePaddingScheme(padding.DefaultPaddingScheme, &c.padding)
|
padding.UpdatePaddingScheme(padding.DefaultPaddingScheme, &c.padding)
|
||||||
c.sessionClient = session.NewClient(ctx, c.CreateOutboundTLSConnection, &c.padding, config.IdleSessionCheckInterval, config.IdleSessionTimeout)
|
c.sessionClient = session.NewClient(ctx, c.CreateOutboundTLSConnection, &c.padding, config.IdleSessionCheckInterval, config.IdleSessionTimeout, config.MinIdleSession)
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -28,13 +28,15 @@ type Client struct {
|
|||||||
padding *atomic.TypedValue[*padding.PaddingFactory]
|
padding *atomic.TypedValue[*padding.PaddingFactory]
|
||||||
|
|
||||||
idleSessionTimeout time.Duration
|
idleSessionTimeout time.Duration
|
||||||
|
minIdleSession int
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewClient(ctx context.Context, dialOut func(ctx context.Context) (net.Conn, error), _padding *atomic.TypedValue[*padding.PaddingFactory], idleSessionCheckInterval, idleSessionTimeout time.Duration) *Client {
|
func NewClient(ctx context.Context, dialOut func(ctx context.Context) (net.Conn, error), _padding *atomic.TypedValue[*padding.PaddingFactory], idleSessionCheckInterval, idleSessionTimeout time.Duration, minIdleSession int) *Client {
|
||||||
c := &Client{
|
c := &Client{
|
||||||
dialOut: dialOut,
|
dialOut: dialOut,
|
||||||
padding: _padding,
|
padding: _padding,
|
||||||
idleSessionTimeout: idleSessionTimeout,
|
idleSessionTimeout: idleSessionTimeout,
|
||||||
|
minIdleSession: minIdleSession,
|
||||||
}
|
}
|
||||||
if idleSessionCheckInterval <= time.Second*5 {
|
if idleSessionCheckInterval <= time.Second*5 {
|
||||||
idleSessionCheckInterval = time.Second * 30
|
idleSessionCheckInterval = time.Second * 30
|
||||||
@ -138,17 +140,30 @@ func (c *Client) idleCleanup() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) idleCleanupExpTime(expTime time.Time) {
|
func (c *Client) idleCleanupExpTime(expTime time.Time) {
|
||||||
var sessionToRemove = make([]*Session, 0)
|
sessionToRemove := make([]*Session, 0, c.idleSession.Len())
|
||||||
|
|
||||||
c.idleSessionLock.Lock()
|
c.idleSessionLock.Lock()
|
||||||
it := c.idleSession.Iterate()
|
it := c.idleSession.Iterate()
|
||||||
|
|
||||||
|
activeCount := 0
|
||||||
for it.IsNotEnd() {
|
for it.IsNotEnd() {
|
||||||
session := it.Value()
|
session := it.Value()
|
||||||
if session.idleSince.Before(expTime) {
|
key := it.Key()
|
||||||
sessionToRemove = append(sessionToRemove, session)
|
|
||||||
c.idleSession.Remove(it.Key())
|
|
||||||
}
|
|
||||||
it.MoveToNext()
|
it.MoveToNext()
|
||||||
|
|
||||||
|
if !session.idleSince.Before(expTime) {
|
||||||
|
activeCount++
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if activeCount < c.minIdleSession {
|
||||||
|
session.idleSince = time.Now()
|
||||||
|
activeCount++
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
sessionToRemove = append(sessionToRemove, session)
|
||||||
|
c.idleSession.Remove(key)
|
||||||
}
|
}
|
||||||
c.idleSessionLock.Unlock()
|
c.idleSessionLock.Unlock()
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user