service_mtproto_test.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. func TestGetSubsMtprotoUsesHostEndpoint(t *testing.T) {
  60. initSubDB(t)
  61. db := database.GetDB()
  62. inbound := &model.Inbound{
  63. Listen: "127.0.0.1",
  64. Port: 4060,
  65. Protocol: model.MTProto,
  66. Enable: true,
  67. Tag: "mt-public-port",
  68. Settings: `{"clients":[{"email":"u@mt","enable":true,"subId":"sub-public-port","secret":"` + mtprotoTestSecret + `"}]}`,
  69. }
  70. if err := db.Create(inbound).Error; err != nil {
  71. t.Fatalf("create inbound: %v", err)
  72. }
  73. if err := db.Create(&model.Host{
  74. InboundId: inbound.Id,
  75. Remark: "public",
  76. Address: "proxy.example.com",
  77. Port: 443,
  78. Security: "same",
  79. }).Error; err != nil {
  80. t.Fatalf("create host: %v", err)
  81. }
  82. client := &model.ClientRecord{Email: "u@mt", SubID: "sub-public-port", Enable: true, Secret: mtprotoTestSecret}
  83. if err := db.Create(client).Error; err != nil {
  84. t.Fatalf("create client: %v", err)
  85. }
  86. if err := db.Create(&model.ClientInbound{ClientId: client.Id, InboundId: inbound.Id}).Error; err != nil {
  87. t.Fatalf("attach client: %v", err)
  88. }
  89. links, _, _, _, err := NewSubService("").GetSubs(client.SubID, "sub.example.com")
  90. if err != nil {
  91. t.Fatalf("GetSubs: %v", err)
  92. }
  93. if len(links) != 1 {
  94. t.Fatalf("links = %d, want 1: %v", len(links), links)
  95. }
  96. u, err := url.Parse(links[0])
  97. if err != nil {
  98. t.Fatalf("parse link: %v", err)
  99. }
  100. if got := u.Query().Get("server"); got != "proxy.example.com" {
  101. t.Fatalf("server = %q, want proxy.example.com", got)
  102. }
  103. if got := u.Query().Get("port"); got != "443" {
  104. t.Fatalf("port = %q, want public host port 443", got)
  105. }
  106. clientLinks := NewLinkProvider().LinksForClient("sub.example.com", inbound, client.Email)
  107. if len(clientLinks) != 1 {
  108. t.Fatalf("client links = %d, want 1: %v", len(clientLinks), clientLinks)
  109. }
  110. clientURL, err := url.Parse(clientLinks[0])
  111. if err != nil {
  112. t.Fatalf("parse client link: %v", err)
  113. }
  114. if got := clientURL.Query().Get("server"); got != "proxy.example.com" {
  115. t.Fatalf("client link server = %q, want proxy.example.com", got)
  116. }
  117. if got := clientURL.Query().Get("port"); got != "443" {
  118. t.Fatalf("client link port = %q, want 443", got)
  119. }
  120. }
  121. // Regression: an mtproto inbound must resolve for a subscription id the same way
  122. // every other client-bearing protocol does. It was previously dropped from the
  123. // getInboundsBySubId protocol allowlist, so multi-client MTProto subscriptions
  124. // (and the public sub page) emitted no tg://proxy link at all.
  125. func TestGetInboundsBySubIdIncludesMtproto(t *testing.T) {
  126. initSubDB(t)
  127. db := database.GetDB()
  128. in := &model.Inbound{
  129. Port: 8443,
  130. Protocol: model.MTProto,
  131. Enable: true,
  132. Tag: "mt-sub",
  133. Settings: `{"fakeTlsDomain":"www.cloudflare.com","clients":[{"email":"u@mt","enable":true,"subId":"submt","secret":"` + mtprotoTestSecret + `"}]}`,
  134. }
  135. if err := db.Create(in).Error; err != nil {
  136. t.Fatalf("create inbound: %v", err)
  137. }
  138. rec := &model.ClientRecord{Email: "u@mt", SubID: "submt", Enable: true, Secret: mtprotoTestSecret}
  139. if err := db.Create(rec).Error; err != nil {
  140. t.Fatalf("create client: %v", err)
  141. }
  142. if err := db.Create(&model.ClientInbound{ClientId: rec.Id, InboundId: in.Id}).Error; err != nil {
  143. t.Fatalf("create link: %v", err)
  144. }
  145. s := &SubService{}
  146. inbounds, err := s.getInboundsBySubId("submt")
  147. if err != nil {
  148. t.Fatalf("getInboundsBySubId: %v", err)
  149. }
  150. if len(inbounds) != 1 || inbounds[0].Id != in.Id {
  151. t.Fatalf("mtproto inbound not returned for subId: %+v", inbounds)
  152. }
  153. links, emails, _, _, err := s.GetSubs("submt", "sub.example.com")
  154. if err != nil {
  155. t.Fatalf("GetSubs: %v", err)
  156. }
  157. if len(links) != 1 || len(emails) != 1 || emails[0] != "u@mt" {
  158. t.Fatalf("subscription did not emit the mtproto client: links=%v emails=%v", links, emails)
  159. }
  160. if !strings.HasPrefix(links[0], "tg://proxy") || !strings.Contains(links[0], "secret="+mtprotoTestSecret) {
  161. t.Fatalf("subscription link is not a tg://proxy carrying the client secret: %q", links[0])
  162. }
  163. }