mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-11-29 14:04:46 +03:00
3e1fc3c24f
An MTProto inbound must not relay a stranger's connection to a public host
picked by an SNI the panel never configured, and every byte it moves must
show up in the inbound's traffic.
mtg relays whatever it cannot serve as a proxy to the fronting target, byte
for byte, and the target answers by the SNI of that connection. With the
default FakeTLS domain on a CDN, anybody could reach any site of that CDN
through the inbound ("clean IP"). mtg dials the target itself, past the
Xray bridge and outside every secret, so neither the clients nor the
inbound showed that traffic.
- The generated config sets [domain-fronting] require-sni-match (mtg-multi
1.17.0) unless the fronting IP is a loopback, private or link-local
address, i.e. the operator's own web server. mtg then relays only a
ClientHello naming the FakeTLS domain and closes the rest. An older mtg
ignores the key.
- The /stats "fronting" counters are scraped per process and added to the
inbound's total, for routed inbounds too.
TestRenderConfig pinned the old rule (a bare config has no
[domain-fronting] section) and now expects the section without a host.
118 lines
4.1 KiB
Go
118 lines
4.1 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 := mgr.CollectTraffic()
|
|
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)
|
|
}
|
|
}
|