Files
3x-ui/database/model/model_mtproto_test.go
Sanaei 1ca5924a44 feat(mtproto): add MTProto (FakeTLS) protocol via managed mtg sidecar (#5076)
* feat(mtproto): add MTProto (FakeTLS) protocol via managed mtg sidecar

Xray-core has no mtproto proxy, so mtproto inbounds run as standalone
mtg (9seconds/mtg) sidecar processes managed by the panel — one per
inbound — and are excluded from the generated Xray config entirely.

- model: MTProto protocol constant, validator, and FakeTLS secret
  helpers (GenerateFakeTLSSecret/HealMtprotoSecret)
- mtproto package: per-inbound mtg process manager with reconcile,
  graceful stop, and best-effort Prometheus traffic scraping
- runtime: delegate mtproto inbounds to the mtg manager instead of the
  Xray gRPC API; skip mtproto when building the Xray config
- web: boot reconcile + StopAll wiring, periodic reconcile/traffic job,
  port-conflict transport, secret healing on inbound add/update
- sub: tg:// proxy share-link generation
- frontend: protocol option, Zod schema, Protocol tab (FakeTLS domain +
  regenerable secret), info-modal link, and i18n
- provisioning: fetch mtg v2.2.8 in install.sh, DockerInit.sh, and the
  Linux + Windows release workflows

* fix

* fix

* fix: address Copilot review comments on mtproto PR

- web/web.go: create NewMtprotoJob once and reuse for cron + initial run
- mtproto/manager.go: StopAll cleans up per-inbound config files on shutdown
- mtproto/manager.go: CollectTraffic releases mutex before HTTP scrapes to
  avoid blocking Ensure/Reconcile/Remove during network I/O
- database/model/model.go: panic on crypto/rand failure in mtprotoRandomMiddle
  instead of silently producing a weak all-zero secret
- install.sh: fix chmod to handle renamed bin/mtg-linux-arm on armv5/v6/v7
2026-06-08 14:28:19 +02:00

72 lines
2.1 KiB
Go

package model
import (
"encoding/hex"
"encoding/json"
"strings"
"testing"
)
func TestGenerateFakeTLSSecret(t *testing.T) {
domain := "www.cloudflare.com"
s := GenerateFakeTLSSecret(domain)
if !strings.HasPrefix(s, "ee") {
t.Fatalf("secret must start with ee, got %q", s)
}
wantSuffix := hex.EncodeToString([]byte(domain))
if !strings.HasSuffix(s, wantSuffix) {
t.Fatalf("secret must end with hex(domain) %q, got %q", wantSuffix, s)
}
if len(s) != 2+32+len(wantSuffix) {
t.Fatalf("unexpected secret length %d", len(s))
}
if _, err := hex.DecodeString(s[2:34]); err != nil {
t.Fatalf("middle is not valid hex: %v", err)
}
}
func TestHealMtprotoSecret(t *testing.T) {
domain := "example.com"
suffix := hex.EncodeToString([]byte(domain))
in := `{"fakeTlsDomain":"example.com","secret":""}`
out, changed := HealMtprotoSecret(in)
if !changed {
t.Fatal("expected heal to populate an empty secret")
}
var parsed map[string]any
if err := json.Unmarshal([]byte(out), &parsed); err != nil {
t.Fatalf("healed settings not valid json: %v", err)
}
got, _ := parsed["secret"].(string)
if !strings.HasPrefix(got, "ee") || !strings.HasSuffix(got, suffix) {
t.Fatalf("healed secret malformed: %q", got)
}
if _, changed2 := HealMtprotoSecret(out); changed2 {
t.Fatal("expected no change for an already-valid secret")
}
mid := got[2:34]
newDomain := "telegram.org"
in3 := `{"fakeTlsDomain":"telegram.org","secret":"` + got + `"}`
out3, changed3 := HealMtprotoSecret(in3)
if !changed3 {
t.Fatal("expected heal to rewrite the domain suffix")
}
if err := json.Unmarshal([]byte(out3), &parsed); err != nil {
t.Fatalf("healed settings not valid json: %v", err)
}
got3, _ := parsed["secret"].(string)
if got3[2:34] != mid {
t.Fatalf("random middle should be preserved on domain change: %q vs %q", got3[2:34], mid)
}
if !strings.HasSuffix(got3, hex.EncodeToString([]byte(newDomain))) {
t.Fatalf("suffix not updated for new domain: %q", got3)
}
if _, changed4 := HealMtprotoSecret(`{"secret":"ee"}`); changed4 {
t.Fatal("expected no change when fakeTlsDomain is missing")
}
}