1
0

json_info_node_test.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. package sub
  2. import (
  3. "fmt"
  4. "strings"
  5. "testing"
  6. "time"
  7. "github.com/mhsanaei/3x-ui/v3/internal/database"
  8. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  9. "github.com/mhsanaei/3x-ui/v3/internal/web/service"
  10. "github.com/mhsanaei/3x-ui/v3/internal/xray"
  11. )
  12. func TestSubJson_InfoNode_Active(t *testing.T) {
  13. setupInfoNodeTestDB(t)
  14. db := database.GetDB()
  15. ib := &model.Inbound{
  16. Id: 1,
  17. UserId: 1,
  18. Remark: "Germany-VLESS",
  19. Enable: true,
  20. Port: 443,
  21. Protocol: model.VLESS,
  22. Settings: `{"clients":[{"id":"c1-uuid","email":"[email protected]","subId":"sub-json","enable":true,"totalGB":10737418240}]}`,
  23. StreamSettings: `{"network":"tcp","security":"none"}`,
  24. }
  25. if err := db.Create(ib).Error; err != nil {
  26. t.Fatal(err)
  27. }
  28. rec := &model.ClientRecord{
  29. Id: 1,
  30. Email: "[email protected]",
  31. SubID: "sub-json",
  32. UUID: "c1-uuid",
  33. Enable: true,
  34. TotalGB: 10737418240,
  35. }
  36. if err := db.Create(rec).Error; err != nil {
  37. t.Fatal(err)
  38. }
  39. if err := db.Create(&model.ClientInbound{InboundId: 1, ClientId: 1}).Error; err != nil {
  40. t.Fatal(err)
  41. }
  42. if err := db.Create(&xray.ClientTraffic{
  43. InboundId: 1,
  44. Email: "[email protected]",
  45. Up: 1073741824,
  46. Down: 1073741824,
  47. Total: 10737418240,
  48. Enable: true,
  49. }).Error; err != nil {
  50. t.Fatal(err)
  51. }
  52. sub := NewSubService("{{EMAIL}}|📊{{TRAFFIC_LEFT}}")
  53. sub.subInfoNodeEnable = true
  54. jsonSvc := NewSubJsonService("", "", "", sub)
  55. out, _, err := jsonSvc.GetJson("sub-json", "sub.example.com", false)
  56. if err != nil {
  57. t.Fatalf("GetJson: %v", err)
  58. }
  59. if !strings.Contains(out, "[email protected]|📊8.00GB") {
  60. t.Fatalf("expected dummy remark in JSON remarks, got:\n%s", out)
  61. }
  62. if !strings.Contains(out, `"protocol": "socks"`) && !strings.Contains(out, `"protocol":"socks"`) {
  63. t.Fatalf("expected socks outbound in JSON, got:\n%s", out)
  64. }
  65. }
  66. func TestSubJson_InfoNode_Expired(t *testing.T) {
  67. setupInfoNodeTestDB(t)
  68. db := database.GetDB()
  69. expiredTime := time.Now().Add(-24 * time.Hour).UnixMilli()
  70. ib := &model.Inbound{
  71. Id: 1,
  72. UserId: 1,
  73. Remark: "Germany-VLESS",
  74. Enable: true,
  75. Port: 443,
  76. Protocol: model.VLESS,
  77. Settings: fmt.Sprintf(`{"clients":[{"id":"c1-uuid","email":"[email protected]","subId":"sub-json-exp","enable":true,"expiryTime":%d}]}`, expiredTime),
  78. StreamSettings: `{"network":"tcp","security":"none"}`,
  79. }
  80. if err := db.Create(ib).Error; err != nil {
  81. t.Fatal(err)
  82. }
  83. rec := &model.ClientRecord{
  84. Id: 1,
  85. Email: "[email protected]",
  86. SubID: "sub-json-exp",
  87. UUID: "c1-uuid",
  88. Enable: true,
  89. ExpiryTime: expiredTime,
  90. }
  91. if err := db.Create(rec).Error; err != nil {
  92. t.Fatal(err)
  93. }
  94. if err := db.Create(&model.ClientInbound{InboundId: 1, ClientId: 1}).Error; err != nil {
  95. t.Fatal(err)
  96. }
  97. if err := db.Create(&xray.ClientTraffic{
  98. InboundId: 1,
  99. Email: "[email protected]",
  100. ExpiryTime: expiredTime,
  101. Enable: true,
  102. }).Error; err != nil {
  103. t.Fatal(err)
  104. }
  105. sub := NewSubService("{{INBOUND}}")
  106. sub.subInfoNodeEnable = true
  107. sub.subExpiredTemplate = service.DefaultSubExpiredTemplate
  108. jsonSvc := NewSubJsonService("", "", "", sub)
  109. out, _, err := jsonSvc.GetJson("sub-json-exp", "sub.example.com", false)
  110. if err != nil {
  111. t.Fatalf("GetJson: %v", err)
  112. }
  113. if !strings.Contains(out, "Expired") || !strings.Contains(out, "[email protected]") {
  114. t.Fatalf("expected expired remark in JSON remarks, got:\n%s", out)
  115. }
  116. if strings.Contains(out, "Germany-VLESS") {
  117. t.Fatalf("expired subscription must NOT contain working inbound in JSON, got:\n%s", out)
  118. }
  119. }
  120. func TestSubJson_InfoNode_Depleted(t *testing.T) {
  121. setupInfoNodeTestDB(t)
  122. db := database.GetDB()
  123. ib := &model.Inbound{
  124. Id: 1,
  125. UserId: 1,
  126. Remark: "Germany-VLESS",
  127. Enable: true,
  128. Port: 443,
  129. Protocol: model.VLESS,
  130. Settings: `{"clients":[{"id":"c1-uuid","email":"[email protected]","subId":"sub-json-dep","enable":true,"totalGB":5368709120}]}`,
  131. StreamSettings: `{"network":"tcp","security":"none"}`,
  132. }
  133. if err := db.Create(ib).Error; err != nil {
  134. t.Fatal(err)
  135. }
  136. rec := &model.ClientRecord{
  137. Id: 1,
  138. Email: "[email protected]",
  139. SubID: "sub-json-dep",
  140. UUID: "c1-uuid",
  141. Enable: true,
  142. TotalGB: 5368709120,
  143. }
  144. if err := db.Create(rec).Error; err != nil {
  145. t.Fatal(err)
  146. }
  147. if err := db.Create(&model.ClientInbound{InboundId: 1, ClientId: 1}).Error; err != nil {
  148. t.Fatal(err)
  149. }
  150. if err := db.Create(&xray.ClientTraffic{
  151. InboundId: 1,
  152. Email: "[email protected]",
  153. Up: 3221225472,
  154. Down: 2147483648,
  155. Total: 5368709120,
  156. Enable: true,
  157. }).Error; err != nil {
  158. t.Fatal(err)
  159. }
  160. sub := NewSubService("{{INBOUND}}")
  161. sub.subInfoNodeEnable = true
  162. sub.subTrafficDepletedTemplate = service.DefaultSubTrafficDepletedTemplate
  163. jsonSvc := NewSubJsonService("", "", "", sub)
  164. out, _, err := jsonSvc.GetJson("sub-json-dep", "sub.example.com", false)
  165. if err != nil {
  166. t.Fatalf("GetJson: %v", err)
  167. }
  168. if !strings.Contains(out, "Traffic Depleted") || !strings.Contains(out, "[email protected]") {
  169. t.Fatalf("expected depleted remark in JSON remarks, got:\n%s", out)
  170. }
  171. if strings.Contains(out, "Germany-VLESS") {
  172. t.Fatalf("depleted subscription must NOT contain working inbound in JSON, got:\n%s", out)
  173. }
  174. }
  175. func TestSubJson_InfoNode_StatusActive(t *testing.T) {
  176. setupInfoNodeTestDB(t)
  177. db := database.GetDB()
  178. ib := &model.Inbound{
  179. Id: 1,
  180. UserId: 1,
  181. Remark: "Germany-VLESS",
  182. Enable: true,
  183. Port: 443,
  184. Protocol: model.VLESS,
  185. Settings: `{"clients":[{"id":"c1-uuid","email":"[email protected]","subId":"sub-json-status","enable":true,"totalGB":10737418240}]}`,
  186. StreamSettings: `{"network":"tcp","security":"none"}`,
  187. }
  188. if err := db.Create(ib).Error; err != nil {
  189. t.Fatal(err)
  190. }
  191. rec := &model.ClientRecord{
  192. Id: 1,
  193. Email: "[email protected]",
  194. SubID: "sub-json-status",
  195. UUID: "c1-uuid",
  196. Enable: true,
  197. TotalGB: 10737418240,
  198. }
  199. if err := db.Create(rec).Error; err != nil {
  200. t.Fatal(err)
  201. }
  202. if err := db.Create(&model.ClientInbound{InboundId: 1, ClientId: 1}).Error; err != nil {
  203. t.Fatal(err)
  204. }
  205. if err := db.Create(&xray.ClientTraffic{
  206. InboundId: 1,
  207. Email: "[email protected]",
  208. Up: 100,
  209. Down: 100,
  210. Total: 10737418240,
  211. Enable: true,
  212. }).Error; err != nil {
  213. t.Fatal(err)
  214. }
  215. sub := NewSubService("{{EMAIL}}|{{STATUS_EMOJI}} {{STATUS}}")
  216. sub.subInfoNodeEnable = true
  217. jsonSvc := NewSubJsonService("", "", "", sub)
  218. out, _, err := jsonSvc.GetJson("sub-json-status", "sub.example.com", false)
  219. if err != nil {
  220. t.Fatalf("GetJson: %v", err)
  221. }
  222. if !strings.Contains(out, "[email protected]|✅ active") {
  223. t.Fatalf("expected '[email protected]|✅ active' in JSON remarks, got:\n%s", out)
  224. }
  225. }