usersession

package
v1.9.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 22, 2026 License: GPL-3.0 Imports: 8 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package usersession tracks the client sessions of an inbound, so that the sessions of a user who was just removed can actually be cut.

Protocols that authenticate once per session (the QUIC ones, anytls, and anything carried over sing-mux) keep serving an already authenticated client after its user is gone from the inbound: swapping the auth map only affects new sessions, and closing the routed connections is not enough because the client simply opens another stream on the session it already has. Reaching the session itself is what this registry is for.

Two kinds of session

anytls and the sing-mux carrier arrive as a net.Conn that lives exactly as long as the session does. Those are tracked by source address and closed outright, and that is the whole story for anytls, vless, vmess and trojan: the key is a single TCP connection's address, it cannot move, and Untrack runs when the session ends.

The QUIC protocols give this layer no handle on the session -- and, the part that decides the design, no usable name for one either:

  • sing-quic keeps its session list unexported, and the ctx it hands the handler per stream is the Service's own, shared by every session;
  • all three services set quic-go's DisablePathManager, which rewrites a connection's remote address the moment a decryptable packet arrives from a new one, with no path validation -- so the source address moves under one NAT rebind;
  • that address is an ephemeral UDP port, recycled to somebody else once the session ends, and nothing tells this layer that it has been;
  • and there is no moment at which a QUIC session can be declared gone. quic-go keeps one alive with a 10s PING whether or not a single byte is routed, so an idle session and a dead one look identical from here.

So for QUIC this package does not track sessions at all. It records something weaker and completely reliable: **which users have been seen on this inbound**. That set is bounded by the client count rather than the session count, which is what lets it carry no timeout -- and a set with no timeout is the only kind that cannot be wrong about a session it cannot see.

Earlier revisions tried to do better and could not. Muting the source address is defeated by the second and third points; ageing a per-session entry out after ten idle minutes is defeated by the fourth, and silently let a removed user keep an idle session. Both looked like they worked because a test client that reconnects every second never exercises either.

What a removal does

CloseUsers cuts every session it holds a transport for, and reports the rest as Unclosable. The QUIC inbounds turn that into ErrRestartRequired and the caller rebuilds the inbound, which destroys every QUIC session on it, the removed user's included. Everyone on that inbound reconnects once -- at once, because core/quicgrace sends each of them a CONNECTION_CLOSE as the inbound closes; without it they each sat out their idle timeout first.

Being wrong here is one-sided on purpose: a user who connected and then left for good is still in the seen set, so removing them costs one restart that bought nothing. The other direction -- deciding a session is gone when it is not -- is what issue #175 is, so the cost is paid on that side.

This lives at the inbound layer rather than in ConnTracker on purpose. The two stay separate: IP limits keep their gate in ConnTracker, because that policy has to outlive a core restart, while everything here is per-inbound and goes away with the Box.

Index

Constants

This section is empty.

Variables

View Source
var ErrRestartRequired = E.New("removed user is still connected by a session that cannot be cut")

ErrRestartRequired reports that a user who was just removed is still connected by a session this layer cannot close, so the only way to disconnect them is to tear the inbound down and build it again.

The caller already has that path -- it is the same fallback taken by protocols with no in-place user update at all -- so returning this error is all an inbound has to do. It is not a failure: the user table was swapped successfully, and the caller tells the two apart so that this one is not logged as one.

Functions

func KeepSet

func KeepSet(names []string) map[string]struct{}

KeepSet turns the user-name list an inbound just installed into the set CloseUsers takes. The names an in-place update is given are exactly the clients still enabled on that inbound, so nothing else has to be looked up.

Types

type Mode

type Mode int

Mode is what a wrapped router does with the connections it sees. Which one an inbound wants follows from how its protocol reuses an authenticated connection.

const (
	// BindOnly records which user is connected from where, and nothing else.
	//
	// The QUIC inbounds take this because they have no session handle to give:
	// what the registry does for them is answer, at removal time, whether the
	// user being removed is connected at all -- which is what decides between
	// a free in-place update and rebuilding the inbound.
	//
	// anytls takes it too, for the opposite reason: it does have a closer, but
	// it holds it in NewConnection, which the router never sees, so it
	// registers the session there instead.
	BindOnly Mode = iota
	// TrackMuxCarrier ignores everything but a sing-mux carrier connection.
	// These protocols authenticate per connection, so a removed user is already
	// locked out of new ones and ConnTracker closes the routed ones -- except
	// on a multiplex session, where the carrier authenticated once and every
	// stream after that rides it. The carrier is a net.Conn we can close, so it
	// is tracked and cut.
	TrackMuxCarrier
)

type Registry

type Registry struct {
	// contains filtered or unexported fields
}

Registry is what an inbound knows about who is connected to it. One instance per inbound; it goes away with the inbound.

Neither map is consulted to decide whether a connection may pass -- nothing in this package refuses anything. They decide whose transport to close, and whether a removed user was connected at all.

func NewRegistry

func NewRegistry() *Registry

func (*Registry) Bind

func (r *Registry) Bind(user string, source string)

Bind records that user is connected. If the connection belongs to a session something already tracked (anytls registers its transport in NewConnection before any stream is routed), the user is attached to that session so it can be closed by name. Otherwise there is no session to attach to and the user goes into the seen set -- which is the QUIC path.

func (*Registry) BindAndTrack

func (r *Registry) BindAndTrack(user string, source string, closer io.Closer)

BindAndTrack records the user and the session transport in one go, for a carrier that arrives with both already known.

The two must land under a single lock. A CloseUsers that ran in between -- seeing the user but not yet the closer -- would put it in the seen set and report it Unclosable, costing a restart that the closer it was about to be given would have made unnecessary.

func (*Registry) CloseUsers

func (r *Registry) CloseUsers(keep map[string]struct{}) Result

CloseUsers cuts the sessions of every user not in keep, and reports the users it could not reach. keep is the user table the inbound has just installed, so anything bound to a name outside it belongs to a user who is gone.

func (*Registry) KickUserSessions

func (r *Registry) KickUserSessions(user string) Result

KickUserSessions disconnects a user who is still enabled -- an operator action, not a revocation. Only a session with a closer can be cut; a user who is merely known to be connected is reported instead, and it is the caller's business whether disconnecting one user is worth restarting the inbound everyone else is on.

For a QUIC user "known to be connected" is the seen set, which never forgets. That is the right signal for a removal and too weak for a kick, so Core.DisconnectUser passes the report on only for a QUIC inbound the user has a routed connection on (#234).

func (*Registry) Track

func (r *Registry) Track(source string, closer io.Closer)

Track stores the session transport, for a protocol whose session has a closer of its own but does not know the user yet. The Bind that follows attaches it.

func (*Registry) Untrack

func (r *Registry) Untrack(source string)

Untrack forgets a session that has ended. This is what keeps sources bounded, and it is why a tracked session never needs to be aged out: its inbound defers this call for exactly as long as the session lives.

type Result

type Result struct {
	// Cut counts the sessions closed outright.
	Cut int
	// Unclosable counts the users left connected because this layer has no
	// handle on their session. For a QUIC inbound that is the signal to rebuild
	// the inbound instead; see ErrRestartRequired.
	Unclosable int
}

Result is what a removal or a kick managed to do.

func (Result) RestartRequired

func (r Result) RestartRequired() error

RestartRequired is what an inbound whose sessions it cannot close returns from UpdateUsers: nil when the removal was carried out in full, and ErrRestartRequired when a removed user is still connected by a session that only rebuilding the inbound will end.

Only the QUIC inbounds call this. anytls and the mux protocols hold the transport of every session they register -- see the notes on their own CloseUsers calls.

type Router

type Router struct {
	adapter.Router
	// contains filtered or unexported fields
}

Router wraps an inbound whose router field is the full adapter.Router, which hysteria and hysteria2 hold. Everything it does not override is forwarded by the embedded interface.

func WrapRouter

func WrapRouter(router adapter.Router, mode Mode) *Router

WrapRouter returns router with the session hooks in front of it.

func (*Router) Registry

func (r *Router) Registry() *Registry

func (*Router) RouteConnection

func (r *Router) RouteConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error

func (*Router) RouteConnectionEx

func (r *Router) RouteConnectionEx(ctx context.Context, conn net.Conn, metadata adapter.InboundContext, onClose N.CloseHandlerFunc)

func (*Router) RoutePacketConnection

func (r *Router) RoutePacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error

func (*Router) RoutePacketConnectionEx

func (r *Router) RoutePacketConnectionEx(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext, onClose N.CloseHandlerFunc)

type RouterEx

type RouterEx struct {
	adapter.ConnectionRouterEx
	// contains filtered or unexported fields
}

RouterEx wraps an inbound whose router field is an adapter.ConnectionRouterEx.

func WrapRouterEx

func WrapRouterEx(router adapter.ConnectionRouterEx, mode Mode) *RouterEx

WrapRouterEx returns router with the session hooks in front of it.

func (*RouterEx) Registry

func (r *RouterEx) Registry() *Registry

Registry is how the owning inbound reaches the registry it just installed.

func (*RouterEx) RouteConnection

func (r *RouterEx) RouteConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error

The deprecated pair is overridden too, so that a transport still routing through it cannot slip a session past the hooks unrecorded.

func (*RouterEx) RouteConnectionEx

func (r *RouterEx) RouteConnectionEx(ctx context.Context, conn net.Conn, metadata adapter.InboundContext, onClose N.CloseHandlerFunc)

func (*RouterEx) RoutePacketConnection

func (r *RouterEx) RoutePacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error

func (*RouterEx) RoutePacketConnectionEx

func (r *RouterEx) RoutePacketConnectionEx(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext, onClose N.CloseHandlerFunc)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL