mirror of
https://github.com/netbirdio/netbird.git
synced 2026-08-25 00:51:28 +02:00
People who only ever reach private services through the reverse proxy were invisible to activity accounting. Active users are counted from user.LastLogin or from the LastSeen of a peer they own, and neither column was written on the proxy paths — so a person signing in via SSO to a proxied service, or a peer serving one over the mesh, never showed up in the 24 hour numbers. Both writes now happen where the proxy already authenticates: - GenerateSessionToken stamps LastLogin after the session token is signed, the same column and the same way the dashboard and device login paths do. - ValidateTunnelPeer stamps the calling peer's LastSeen, the column its owner activates through. The policy lives in a new reverseproxy/activity manager rather than in the gRPC service, matching the module layout the other reverse proxy domains use. It skips what can never count — service users, embedded proxy peers and WASM clients — and throttles peer writes to once an hour, well inside the window accounting asks about and far above the proxy's five minute tunnel cache. The peer write is a single indexed UPDATE that touches only peer_status_last_seen. Connected and SessionStartedAt are left alone so the session-ownership fencing MarkPeerConnectedIfNewerSession relies on is never disturbed, and the timestamp comes from the database clock rather than the caller, for the same reason the other status writers take it from there. The caller's cutoff travels into the statement's WHERE, so concurrent requests for one peer collapse into a single write instead of each acting on its own stale read, and a peer that was never seen — NULL last seen, since Status is an embedded pointer — still records its first activity. Nothing outside the reverse proxy changes behaviour: the only addition elsewhere is the RefreshPeerLastSeen store method the manager calls.
67 lines
2.4 KiB
Go
67 lines
2.4 KiB
Go
package manager
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/netbirdio/netbird/management/internals/modules/reverseproxy/activity"
|
|
"github.com/netbirdio/netbird/management/server/peer"
|
|
"github.com/netbirdio/netbird/management/server/store"
|
|
"github.com/netbirdio/netbird/management/server/types"
|
|
)
|
|
|
|
// peerSeenInterval is how stale a peer's LastSeen must be before reaching a
|
|
// private service refreshes it. Positive tunnel validations are cached on the
|
|
// proxy for five minutes, so without a floor a busy peer would rewrite its row
|
|
// behind every request; an hour still sits well inside the window activity
|
|
// accounting asks about.
|
|
const peerSeenInterval = time.Hour
|
|
|
|
type managerImpl struct {
|
|
store store.Store
|
|
}
|
|
|
|
// NewManager returns the activity manager backed by the management store.
|
|
func NewManager(store store.Store) activity.Manager {
|
|
return &managerImpl{store: store}
|
|
}
|
|
|
|
// RecordUserLogin stamps the login the same way the dashboard and device login
|
|
// paths do, so a person who only ever reaches proxied services still has a
|
|
// login on record.
|
|
func (m *managerImpl) RecordUserLogin(ctx context.Context, accountID string, user *types.User) error {
|
|
if user == nil || user.IsServiceUser {
|
|
return nil
|
|
}
|
|
|
|
return m.store.SaveUserLastLogin(ctx, accountID, user.Id, time.Now().UTC())
|
|
}
|
|
|
|
// RecordPeerSeen stamps LastSeen, the column a peer activates its owner
|
|
// through. The peer the caller already holds answers the throttle without a
|
|
// query, so a peer seen inside the interval costs nothing to skip; the same
|
|
// cutoff goes to the store, which enforces it inside the UPDATE so concurrent
|
|
// requests for one peer cannot each write off their own stale read.
|
|
func (m *managerImpl) RecordPeerSeen(ctx context.Context, accountID string, peer *peer.Peer) error {
|
|
if peer == nil || !countsTowardActivity(peer) {
|
|
return nil
|
|
}
|
|
|
|
staleBefore := time.Now().UTC().Add(-peerSeenInterval)
|
|
if peer.Status != nil && peer.Status.LastSeen.After(staleBefore) {
|
|
return nil
|
|
}
|
|
|
|
_, err := m.store.RefreshPeerLastSeen(ctx, accountID, peer.ID, staleBefore)
|
|
|
|
return err
|
|
}
|
|
|
|
// countsTowardActivity reports whether the peer represents a device a person
|
|
// actually runs. Embedded proxy peers are infrastructure and browser (WASM)
|
|
// clients are ephemeral sessions, so activity accounting ignores both and a
|
|
// write for them could never count.
|
|
func countsTowardActivity(peer *peer.Peer) bool {
|
|
return !peer.ProxyMeta.Embedded && peer.Meta.KernelVersion != "wasm"
|
|
}
|