1
0

export_all_links_test.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. package sub
  2. import (
  3. "encoding/base64"
  4. "net/url"
  5. "strings"
  6. "testing"
  7. "github.com/mhsanaei/3x-ui/v3/internal/database"
  8. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  9. wgutil "github.com/mhsanaei/3x-ui/v3/internal/util/wireguard"
  10. )
  11. // inboundLinks (the "Export all inbound links" path) must render the remark
  12. // template's whole Client token group per client, name-only — the same engine
  13. // the client/QR pages use.
  14. func TestInboundLinks_RemarkTemplateClientTokens(t *testing.T) {
  15. seedSubDB(t)
  16. db := database.GetDB()
  17. settings := `{"clients":[{"id":"11111111-2222-4333-8444-000000000001","email":"john@e","subId":"subABC","comment":"vip","tgId":777,"enable":true}],"decryption":"none"}`
  18. ib := &model.Inbound{
  19. UserId: 1, Tag: "t", Enable: true, Listen: "203.0.113.5", Port: 4431,
  20. Protocol: model.VLESS, Remark: "Germany", Settings: settings,
  21. StreamSettings: `{"network":"ws","security":"tls","wsSettings":{"path":"/","host":""},"tlsSettings":{"serverName":"sni"}}`,
  22. }
  23. if err := db.Create(ib).Error; err != nil {
  24. t.Fatalf("seed inbound: %v", err)
  25. }
  26. client := &model.ClientRecord{
  27. Email: "john@e", SubID: "subABC", UUID: "11111111-2222-4333-8444-000000000001",
  28. Enable: true, Comment: "vip", TgID: 777,
  29. }
  30. if err := db.Create(client).Error; err != nil {
  31. t.Fatalf("seed client: %v", err)
  32. }
  33. if err := db.Create(&model.ClientInbound{ClientId: client.Id, InboundId: ib.Id}).Error; err != nil {
  34. t.Fatalf("seed client_inbound: %v", err)
  35. }
  36. svc := NewSubService("{{INBOUND}}-{{EMAIL}}-{{COMMENT}}-{{SUB_ID}}-{{TELEGRAM_ID}}-{{SHORT_ID}}|📊{{TRAFFIC_LEFT}}|⏳{{DAYS_LEFT}}D")
  37. svc.PrepareForRequest("req.example.com")
  38. links := svc.inboundLinks(ib)
  39. if len(links) != 1 {
  40. t.Fatalf("links = %d, want 1: %v", len(links), links)
  41. }
  42. frag := links[0]
  43. for _, want := range []string{"Germany-john", "vip", "subABC", "777", "11111111"} {
  44. if !strings.Contains(frag, want) {
  45. t.Fatalf("remark missing client token %q: %s", want, frag)
  46. }
  47. }
  48. if strings.Contains(frag, "GB") || strings.ContainsRune(frag, '⏳') {
  49. t.Fatalf("display mode must drop the traffic/expiry segments: %s", frag)
  50. }
  51. }
  52. // inboundLinks must use the clients-table UUID when the inbound settings JSON
  53. // still embeds a stale id (#6436).
  54. func TestInboundLinks_UsesClientsTableUUIDWhenSettingsStale(t *testing.T) {
  55. seedSubDB(t)
  56. db := database.GetDB()
  57. stale := "11111111-1111-1111-1111-111111111111"
  58. fresh := "22222222-2222-2222-2222-222222222222"
  59. settings := `{"clients":[{"id":"` + stale + `","email":"stale@e","subId":"subStale","enable":true}],"decryption":"none"}`
  60. ib := &model.Inbound{
  61. UserId: 1, Tag: "stale-uuid", Enable: true, Listen: "203.0.113.5", Port: 4432,
  62. Protocol: model.VLESS, Remark: "Stale", Settings: settings,
  63. StreamSettings: `{"network":"tcp","security":"none","tcpSettings":{"header":{"type":"none"}}}`,
  64. }
  65. if err := db.Create(ib).Error; err != nil {
  66. t.Fatalf("seed inbound: %v", err)
  67. }
  68. client := &model.ClientRecord{Email: "stale@e", SubID: "subStale", UUID: fresh, Enable: true}
  69. if err := db.Create(client).Error; err != nil {
  70. t.Fatalf("seed client: %v", err)
  71. }
  72. if err := db.Create(&model.ClientInbound{ClientId: client.Id, InboundId: ib.Id}).Error; err != nil {
  73. t.Fatalf("seed client_inbound: %v", err)
  74. }
  75. svc := NewSubService("{{EMAIL}}")
  76. svc.PrepareForRequest("req.example.com")
  77. links := svc.inboundLinks(ib)
  78. if len(links) != 1 {
  79. t.Fatalf("links = %d, want 1: %v", len(links), links)
  80. }
  81. if !strings.Contains(links[0], fresh) {
  82. t.Fatalf("link missing fresh UUID %q: %s", fresh, links[0])
  83. }
  84. if strings.Contains(links[0], stale) {
  85. t.Fatalf("link still carries stale settings UUID %q: %s", stale, links[0])
  86. }
  87. }
  88. // inboundLinks must still emit a VLESS link when clients exist only in the
  89. // inbound settings JSON (no clients / client_inbounds rows) — ListClientsForInbound
  90. // returns empty and clientsForLinkExport falls back to GetClients (#6458).
  91. func TestInboundLinks_SettingsOnlyVLESSProducesLink(t *testing.T) {
  92. seedSubDB(t)
  93. db := database.GetDB()
  94. uuid := "33333333-3333-3333-3333-333333333333"
  95. settings := `{"clients":[{"id":"` + uuid + `","email":"settings@e","subId":"subSettings","enable":true}],"decryption":"none"}`
  96. ib := &model.Inbound{
  97. UserId: 1, Tag: "settings-only", Enable: true, Listen: "203.0.113.5", Port: 4434,
  98. Protocol: model.VLESS, Remark: "SettingsOnly", Settings: settings,
  99. StreamSettings: `{"network":"tcp","security":"none","tcpSettings":{"header":{"type":"none"}}}`,
  100. }
  101. if err := db.Create(ib).Error; err != nil {
  102. t.Fatalf("seed inbound: %v", err)
  103. }
  104. svc := NewSubService("{{EMAIL}}")
  105. svc.PrepareForRequest("req.example.com")
  106. links := svc.inboundLinks(ib)
  107. if len(links) != 1 {
  108. t.Fatalf("links = %d, want 1: %v", len(links), links)
  109. }
  110. if !strings.Contains(links[0], "vless://") {
  111. t.Fatalf("link = %q, want vless:// prefix", links[0])
  112. }
  113. if !strings.Contains(links[0], uuid) {
  114. t.Fatalf("link missing settings UUID %q: %s", uuid, links[0])
  115. }
  116. }
  117. // inboundLinks must keep each WireGuard/AmneziaWG inbound's own tunnel address
  118. // and private key when the same email is attached to both — those fields live
  119. // only in the per-inbound settings JSON, not the shared clients.wg_* columns.
  120. func TestInboundLinks_PreservesPerInboundWireGuardIdentity(t *testing.T) {
  121. seedSubDB(t)
  122. db := database.GetDB()
  123. serverPriv, serverPub, err := wgutil.GenerateWireguardKeypair()
  124. if err != nil {
  125. t.Fatalf("server keypair: %v", err)
  126. }
  127. wgPriv, _, err := wgutil.GenerateWireguardKeypair()
  128. if err != nil {
  129. t.Fatalf("wg client keypair: %v", err)
  130. }
  131. awgPriv, _, err := wgutil.GenerateWireguardKeypair()
  132. if err != nil {
  133. t.Fatalf("awg client keypair: %v", err)
  134. }
  135. // Shared clients row deliberately holds the *other* tunnel's key/address
  136. // (last sync wins) — the failure mode ListClientsForInbound alone would export.
  137. mergedPriv, _, err := wgutil.GenerateWireguardKeypair()
  138. if err != nil {
  139. t.Fatalf("merged keypair: %v", err)
  140. }
  141. email := "dual@e"
  142. wgSettings := `{"secretKey":"` + serverPriv + `","clients":[{"email":"` + email + `","privateKey":"` + wgPriv + `","allowedIPs":["10.0.0.5/32"],"enable":true}]}`
  143. awgSettings := `{"server":{"privateKey":"` + serverPriv + `","publicKey":"` + serverPub + `","mtu":1420},` +
  144. `"clients":[{"email":"` + email + `","privateKey":"` + awgPriv + `","allowedIPs":["10.8.1.5/32"],"enable":true}]}`
  145. wgIb := &model.Inbound{
  146. UserId: 1, Tag: "wg-dual", Enable: true, Listen: "203.0.113.7", Port: 51820,
  147. Protocol: model.WireGuard, Remark: "WG", Settings: wgSettings,
  148. }
  149. awgIb := &model.Inbound{
  150. UserId: 1, Tag: "awg-dual", Enable: true, Listen: "203.0.113.8", Port: 443,
  151. Protocol: model.AmneziaWG, Remark: "AWG", Settings: awgSettings,
  152. }
  153. for _, ib := range []*model.Inbound{wgIb, awgIb} {
  154. if err := db.Create(ib).Error; err != nil {
  155. t.Fatalf("create inbound %s: %v", ib.Tag, err)
  156. }
  157. }
  158. rec := &model.ClientRecord{
  159. Email: email, SubID: "subDual", Enable: true,
  160. PrivateKey: mergedPriv, AllowedIPs: "10.9.9.9/32",
  161. }
  162. if err := db.Create(rec).Error; err != nil {
  163. t.Fatalf("create client: %v", err)
  164. }
  165. for _, ib := range []*model.Inbound{wgIb, awgIb} {
  166. if err := db.Create(&model.ClientInbound{ClientId: rec.Id, InboundId: ib.Id}).Error; err != nil {
  167. t.Fatalf("create client_inbound %s: %v", ib.Tag, err)
  168. }
  169. }
  170. svc := NewSubService("{{EMAIL}}")
  171. svc.PrepareForRequest("req.example.com")
  172. wgLinks := svc.inboundLinks(wgIb)
  173. if len(wgLinks) != 1 {
  174. t.Fatalf("wg links = %d, want 1: %v", len(wgLinks), wgLinks)
  175. }
  176. wu, err := url.Parse(wgLinks[0])
  177. if err != nil {
  178. t.Fatalf("wg link parse: %v (%s)", err, wgLinks[0])
  179. }
  180. if wu.User.Username() != wgPriv {
  181. t.Fatalf("wg private key = %q, want inbound settings key %q (not merged %q)", wu.User.Username(), wgPriv, mergedPriv)
  182. }
  183. if got := wu.Query().Get("address"); got != "10.0.0.5/32" {
  184. t.Fatalf("wg address = %q, want 10.0.0.5/32 (not merged 10.9.9.9/32)", got)
  185. }
  186. awgLinks := svc.inboundLinks(awgIb)
  187. if len(awgLinks) != 1 {
  188. t.Fatalf("awg links = %d, want 1: %v", len(awgLinks), awgLinks)
  189. }
  190. if !strings.HasPrefix(awgLinks[0], "vpn://") {
  191. t.Fatalf("awg link = %q, want vpn:// prefix", awgLinks[0])
  192. }
  193. raw, err := base64.RawURLEncoding.DecodeString(strings.TrimPrefix(awgLinks[0], "vpn://"))
  194. if err != nil {
  195. t.Fatalf("awg link decode: %v (%s)", err, awgLinks[0])
  196. }
  197. textCfg := string(raw)
  198. if !strings.Contains(textCfg, "PrivateKey = "+awgPriv) {
  199. t.Fatalf("awg config missing inbound private key %q:\n%s", awgPriv, textCfg)
  200. }
  201. if !strings.Contains(textCfg, "Address = 10.8.1.5/32") {
  202. t.Fatalf("awg config missing inbound address 10.8.1.5/32:\n%s", textCfg)
  203. }
  204. if strings.Contains(textCfg, mergedPriv) || strings.Contains(textCfg, "10.9.9.9/32") {
  205. t.Fatalf("awg config leaked merged clients-table tunnel identity:\n%s", textCfg)
  206. }
  207. }