fix: kill active connections when user is removed via gRPC#6102
Closed
nightcrawler42 wants to merge 1 commit into
Closed
fix: kill active connections when user is removed via gRPC#6102nightcrawler42 wants to merge 1 commit into
nightcrawler42 wants to merge 1 commit into
Conversation
When a user is removed through the gRPC API (AlterInbound → RemoveUser), existing connections survive because user validation only happens during the initial handshake. After authentication, the connection runs independently with a captured User pointer. This commit adds per-user connection tracking that forcibly terminates active connections on user removal: - New ConnTracker in proxy/conntrack.go tracks connections per user email using atomic sequence IDs and sync.Mutex - Track() stores both a context.CancelFunc and the underlying net.Conn (io.Closer) for each connection - KillAll() cancels contexts AND closes TCP connections, ensuring immediate termination even for long-lived streams (e.g. tunneling tools that maintain persistent connections) - All protocol handlers updated: VLESS, VMess, Trojan, Shadowsocks, Shadowsocks 2022 - Shadowsocks 2022: added bounds check for user index in NewConnection/NewPacketConnection to prevent panic when user is removed during active connection handoff - Shadowsocks 2022: added error propagation for UpdateUsersWithPasswords in RemoveUser (was silently discarded) Context cancellation alone is insufficient because buf.Copy (the core data relay loop) has no context awareness — it only stops on read/write errors. Closing the net.Conn forces an immediate TCP RST that breaks all in-flight relay loops.
Member
|
dup #5844 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When a user is removed through the gRPC API (
AlterInbound→RemoveUser), existing connections survive because user validation only happens during the initial handshake. After authentication, the connection runs independently with a capturedUserpointer — the validator is never consulted again.This is a real-world problem for panel operators (Marzban, 3x-ui, etc.) who expect disabled/removed users to be disconnected immediately. Currently, the only way to force-disconnect is to restart the entire xray process, which drops all connections.
Changes
New
ConnTracker(proxy/conntrack.go): per-user connection tracking using atomic sequence IDs andsync.Mutex. Stores bothcontext.CancelFuncandio.Closer(the underlyingnet.Conn) for each tracked connection.KillAll(email): cancels contexts AND closes TCP connections. Context cancellation alone is insufficient becausebuf.Copy(the core data relay loop incommon/buf/copy.go) has no context awareness — it only stops on read/write errors. Closing thenet.Connforces an immediate TCP RST.All 5 protocol handlers updated: VLESS, VMess, Trojan, Shadowsocks, Shadowsocks 2022
Track()called after user authentication inProcess()KillAll()called inRemoveUser()after successful removalShadowsocks 2022 fixes:
NewConnection/NewPacketConnection(prevents panic when user removed during active handoff)UpdateUsersWithPasswordsinRemoveUser(was silently discarded)Why not just cancel the context?
buf.Copyloops forever onreader.ReadMultiBuffer()with noctx.Done()check. Long-lived streams (e.g. Psiphon tunnels, persistent WebSocket connections) survive context cancellation indefinitely. Force-closing the TCP connection is the only reliable way to break all in-flight relay loops.Testing
CGO_ENABLED=0 go build ./...