Repository navigation
fix(stats): report bytes received from a WireGuard peer as uplink - #131
Open
Free-Guy-IR wants to merge 2 commits into
Open
Free-Guy-IR wants to merge 2 commits into
Free-Guy-IR wants to merge 2 commits into
Conversation
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the
|
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.
Fixes #126
Found by code review and confirmed with real wireguard-go counters; never seen in production. Checked against node
dev9ccd263.What
buildDeltaStats(pkg/stats/interface_counters.go:51-75at 9ccd263) emittedtxasuplinkandrxasdownlink. For WireGuard,Rxis filled from wgctrl'speer.ReceiveBytes(backend/wireguard/jobs.go:104-110), which is the client's upload. So per-user stats and the interface outbound stats reported the directions swapped compared with the xray backend, whereuplinkis client-to-server traffic.This PR emits
rxasuplinkandtxasdownlink.Impact
Display only. Totals do not change, so billing and limits are not affected: the panel (
devf92acaf51cec) sums per-user stats regardless of direction, and a node's limit usesdownlink + uplink. Only the uplink/downlink split shown for WireGuard nodes and users changes. The panel has no WireGuard-specific handling of directions, so there is no double swap.This corrects new samples only. History already recorded for WireGuard nodes keeps the swapped split; the PR does not rewrite past data.
MTProto (on
devsince #120) is not affected. It builds its own stats inbackend/mtproto/tracker.go:265-274, withuplink= telemt's octets from the client, and does not importpkg/stats. Its tests pass on the merged tree (below).Changes
Existing tests: nine tests (six in
pkg/stats/tracker_test.go, three inbackend/wireguard/stats_outbound_test.go) asserted the swapped mapping and fail with only the fix applied. This PR exchanges theiruplink/downlinklabels, and instats_outbound_test.goalso the direction named in the failure messages. The expected values are unchanged.New test,
pkg/stats/direction_test.go:TestBytesReceivedFromAWireGuardPeerAreReportedAsUplinkandTestInterfaceBytesReceivedAreReportedAsUplink. They use small fixed numbers (Rx1000,Tx7). The direction itself was checked with real counters (next section).Real counters
Real userspace wireguard-go (
golang.zx2c4.com/wireguardv0.0.0-20261024220952-b3bc704ef0ee, from the node'sgo.mod): two devices with gVisor netstack TUNs, UDP on 127.0.0.1, macOS arm64. The client uploaded 1 MiB over TCP through the tunnel. The server device's counters for the client peer were fed into the node'spkg/stats.On
dev9ccd263:With this PR (626952c):
On the tree of this PR merged onto
dev:uplink=1114788 downlink=7964. The counts include WireGuard and TCP overhead (rx_bytes1114692 for 1048576 bytes of payload) and differ slightly between runs.Real Xray 26.3.27 with the same 1 MiB upload reports inbound and outbound
uplink= 1048576.Not measured: Linux kernel WireGuard, and the netlink interface counters that feed
BuildInterfaceStatsin the backend. In the run aboveBuildInterfaceStatswas fed the peer counters, to show its mapping only.Verification
go test -race -count=1 -v, Go 1.27.3, macOS arm64.Before, on
dev9ccd263 with only this PR's test files:The nine relabelled tests fail there too (11 failures in total).
After, on 626952c:
On this PR merged onto
dev9ccd263 (git merge-tree --write-treeis clean):