mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-12-03 16:04:50 +03:00
fb14c8358a
Distinguish unavailable, pending, explicitly idle and legacy inbound online reports across REST, WebSocket and node snapshots. Require hosting-node emails and matching inbound activity; use a labeled node-wide fallback only for confirmed unsupported endpoints (HTTP 404/405). Track Xray freshness separately from sidecar observations and prevent accounting retries from publishing online state across core replacement. Preserve partial sidecar reports through mixed-version relays using the reserved nonempty NUL marker. Retain GUID/tag remapping and attached-client filtering. Validated on the reviewed head: all 12 CI jobs and Docs CI passed, including 2,332 frontend tests, mandatory PostgreSQL attribution regressions, race checks, Xray/node E2E and all four master/node version combinations. Exact client attribution across simultaneously active shared-client inbounds remains an estimate; older nodes may omit idle GUIDs during rollout. Fixes #7283.
121 lines
4.2 KiB
Go
121 lines
4.2 KiB
Go
package mtproto
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"reflect"
|
|
"strings"
|
|
"sync/atomic"
|
|
"testing"
|
|
)
|
|
|
|
// mtg relays an unauthenticated connection to the fronting target by the SNI the
|
|
// stranger sent, so a public target turns the inbound into an open relay (CDN "clean IP").
|
|
func TestRenderConfigRequiresSNIMatchForPublicFronting(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
frontingIP string
|
|
want bool
|
|
}{
|
|
{"FakeTLS domain resolved over DNS", "", true},
|
|
{"public address", "104.16.132.229", true},
|
|
{"hostname", "cdn.example.com", true},
|
|
{"loopback web server", "127.0.0.1", false},
|
|
{"IPv6 loopback web server", "::1", false},
|
|
{"web server on the private network", "10.0.0.5", false},
|
|
{"web server on a unique-local network", "fd00::5", false},
|
|
{"web server on the link", "169.254.10.1", false},
|
|
}
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
cfg := renderConfig(Instance{
|
|
Secrets: []SecretEntry{{Name: "alice", Secret: "ee00"}},
|
|
Listen: "0.0.0.0", Port: 2087,
|
|
FrontingIP: tc.frontingIP,
|
|
}, 5000, "")
|
|
got := strings.Contains(cfg, "\n[domain-fronting]\n") && strings.Contains(cfg, "\nrequire-sni-match = true\n")
|
|
if got != tc.want {
|
|
t.Fatalf("require-sni-match emitted = %v, want %v:\n%s", got, tc.want, cfg)
|
|
}
|
|
if strings.Index(cfg, "require-sni-match") > strings.Index(cfg, "[secrets]") {
|
|
t.Fatalf("require-sni-match must stay inside [domain-fronting], before [secrets]:\n%s", cfg)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// Fronted bytes belong to no secret, so they never showed in the per-user map:
|
|
// an inbound relaying gigabytes to its fronting domain reported no traffic.
|
|
func TestCollectTrafficReportsFrontedBytes(t *testing.T) {
|
|
pidFile := installFakeMtg(t)
|
|
mgr := &Manager{procs: map[int]*managed{}, swept: true}
|
|
if err := mgr.Ensure(mtgInst(1, SecretEntry{Name: "alice", Secret: "ee01"})); err != nil {
|
|
t.Fatalf("ensure: %v", err)
|
|
}
|
|
waitSpawnCount(t, pidFile, 1)
|
|
t.Cleanup(mgr.StopAll)
|
|
|
|
scrapes := []string{
|
|
`"fronting":{"connections":3,"bytes_in":100,"bytes_out":1000,"rejected":0}`,
|
|
`"fronting":{"connections":3,"bytes_in":150,"bytes_out":1600,"rejected":7}`,
|
|
`"fronting":{"connections":0,"bytes_in":150,"bytes_out":1600,"rejected":9}`,
|
|
}
|
|
var scrape atomic.Int32
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
|
body := scrapes[min(int(scrape.Add(1))-1, len(scrapes)-1)]
|
|
_, _ = io.WriteString(w, fmt.Sprintf(`{%s,"users":{"alice":{"connections":0,"bytes_in":10,"bytes_out":20}}}`, body))
|
|
}))
|
|
defer srv.Close()
|
|
mgr.procs[1].apiPort = serverPort(t, srv)
|
|
|
|
wants := [][]FrontedTraffic{
|
|
{{Tag: "inbound-1", Up: 100, Down: 1000}},
|
|
{{Tag: "inbound-1", Up: 50, Down: 600}},
|
|
nil,
|
|
}
|
|
for i, want := range wants {
|
|
clients, fronted, _, _ := mgr.CollectTraffic()
|
|
if len(clients) != 0 {
|
|
t.Fatalf("scrape %d: fronted bytes must not be attributed to a client: %+v", i+1, clients)
|
|
}
|
|
if !reflect.DeepEqual(fronted, want) {
|
|
t.Fatalf("scrape %d: fronted = %+v, want %+v", i+1, fronted, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestCollectTrafficWithoutFrontingCounters(t *testing.T) {
|
|
pidFile := installFakeMtg(t)
|
|
mgr := &Manager{procs: map[int]*managed{}, swept: true}
|
|
if err := mgr.Ensure(mtgInst(1, SecretEntry{Name: "alice", Secret: "ee01"})); err != nil {
|
|
t.Fatalf("ensure: %v", err)
|
|
}
|
|
waitSpawnCount(t, pidFile, 1)
|
|
t.Cleanup(mgr.StopAll)
|
|
|
|
var scrape atomic.Int32
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
|
n := int64(scrape.Add(1))
|
|
_, _ = io.WriteString(w, fmt.Sprintf(`{"users":{"alice":{"connections":1,"bytes_in":%d,"bytes_out":%d}}}`, 10*n, 20*n))
|
|
}))
|
|
defer srv.Close()
|
|
mgr.procs[1].apiPort = serverPort(t, srv)
|
|
|
|
mgr.CollectTraffic()
|
|
clients, fronted, online, tags := mgr.CollectTraffic()
|
|
if !reflect.DeepEqual(tags, []string{"inbound-1"}) {
|
|
t.Fatalf("successfully polled inbounds = %v, want [inbound-1]", tags)
|
|
}
|
|
if want := []Traffic{{Tag: "inbound-1", Email: "alice", Up: 10, Down: 20}}; !reflect.DeepEqual(clients, want) {
|
|
t.Fatalf("clients = %+v, want %+v", clients, want)
|
|
}
|
|
if len(fronted) != 0 {
|
|
t.Fatalf("an mtg without fronting counters must report no fronted traffic: %+v", fronted)
|
|
}
|
|
if !reflect.DeepEqual(online, []string{"alice"}) {
|
|
t.Fatalf("online = %+v, want [alice]", online)
|
|
}
|
|
}
|