1
0

service_mtproto_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package sub
  2. import (
  3. "net/url"
  4. "strings"
  5. "testing"
  6. "github.com/mhsanaei/3x-ui/v3/internal/database"
  7. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  8. )
  9. const mtprotoTestSecret = "ee8196fe6ed8b637d001f91d6952cfcdf07777772e636c6f7564666c6172652e636f6d"
  10. func TestGenMtprotoLinkFields(t *testing.T) {
  11. inbound := &model.Inbound{
  12. Listen: "203.0.113.7",
  13. Port: 8443,
  14. Protocol: model.MTProto,
  15. Remark: "mt-sub",
  16. Settings: `{"fakeTlsDomain":"www.cloudflare.com","clients":[{"email":"user","enable":true,"secret":"` + mtprotoTestSecret + `"}]}`,
  17. }
  18. s := &SubService{}
  19. link := s.genMtprotoLink(inbound, "user")
  20. u, err := url.Parse(link)
  21. if err != nil {
  22. t.Fatalf("link does not parse: %v\n got: %s", err, link)
  23. }
  24. if u.Scheme != "tg" || u.Host != "proxy" {
  25. t.Fatalf("link = %q, want a tg://proxy deep link", link)
  26. }
  27. q := u.Query()
  28. if q.Get("server") != "203.0.113.7" {
  29. t.Fatalf("server = %q, want 203.0.113.7", q.Get("server"))
  30. }
  31. if q.Get("port") != "8443" {
  32. t.Fatalf("port = %q, want 8443", q.Get("port"))
  33. }
  34. if q.Get("secret") != mtprotoTestSecret {
  35. t.Fatalf("secret = %q, want the client's FakeTLS secret", q.Get("secret"))
  36. }
  37. if u.Fragment != "" {
  38. t.Fatalf("link carries a #%s fragment; tg://proxy links must have no remark fragment", u.Fragment)
  39. }
  40. }
  41. func TestGenMtprotoLinkWrongProtocol(t *testing.T) {
  42. s := &SubService{}
  43. vless := &model.Inbound{Protocol: model.VLESS, Settings: `{"clients":[{"email":"user"}]}`}
  44. if got := s.genMtprotoLink(vless, "user"); got != "" {
  45. t.Fatalf("wrong protocol should yield empty link, got %q", got)
  46. }
  47. }
  48. func TestGenMtprotoLinkNoSecret(t *testing.T) {
  49. s := &SubService{}
  50. inbound := &model.Inbound{
  51. Protocol: model.MTProto,
  52. Port: 8443,
  53. Settings: `{"fakeTlsDomain":"www.cloudflare.com","clients":[{"email":"user"}]}`,
  54. }
  55. if got := s.genMtprotoLink(inbound, "user"); got != "" {
  56. t.Fatalf("client without secret should yield empty link, got %q", got)
  57. }
  58. }
  59. // Regression: an mtproto inbound must resolve for a subscription id the same way
  60. // every other client-bearing protocol does. It was previously dropped from the
  61. // getInboundsBySubId protocol allowlist, so multi-client MTProto subscriptions
  62. // (and the public sub page) emitted no tg://proxy link at all.
  63. func TestGetInboundsBySubIdIncludesMtproto(t *testing.T) {
  64. initSubDB(t)
  65. db := database.GetDB()
  66. in := &model.Inbound{
  67. Port: 8443,
  68. Protocol: model.MTProto,
  69. Enable: true,
  70. Tag: "mt-sub",
  71. Settings: `{"fakeTlsDomain":"www.cloudflare.com","clients":[{"email":"u@mt","enable":true,"subId":"submt","secret":"` + mtprotoTestSecret + `"}]}`,
  72. }
  73. if err := db.Create(in).Error; err != nil {
  74. t.Fatalf("create inbound: %v", err)
  75. }
  76. rec := &model.ClientRecord{Email: "u@mt", SubID: "submt", Enable: true, Secret: mtprotoTestSecret}
  77. if err := db.Create(rec).Error; err != nil {
  78. t.Fatalf("create client: %v", err)
  79. }
  80. if err := db.Create(&model.ClientInbound{ClientId: rec.Id, InboundId: in.Id}).Error; err != nil {
  81. t.Fatalf("create link: %v", err)
  82. }
  83. s := &SubService{}
  84. inbounds, err := s.getInboundsBySubId("submt")
  85. if err != nil {
  86. t.Fatalf("getInboundsBySubId: %v", err)
  87. }
  88. if len(inbounds) != 1 || inbounds[0].Id != in.Id {
  89. t.Fatalf("mtproto inbound not returned for subId: %+v", inbounds)
  90. }
  91. links, emails, _, _, err := s.GetSubs("submt", "sub.example.com")
  92. if err != nil {
  93. t.Fatalf("GetSubs: %v", err)
  94. }
  95. if len(links) != 1 || len(emails) != 1 || emails[0] != "u@mt" {
  96. t.Fatalf("subscription did not emit the mtproto client: links=%v emails=%v", links, emails)
  97. }
  98. if !strings.HasPrefix(links[0], "tg://proxy") || !strings.Contains(links[0], "secret="+mtprotoTestSecret) {
  99. t.Fatalf("subscription link is not a tg://proxy carrying the client secret: %q", links[0])
  100. }
  101. }