inbound_hysteria_auth_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package service
  2. import (
  3. "strings"
  4. "testing"
  5. "github.com/mhsanaei/3x-ui/v3/internal/database"
  6. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  7. )
  8. // An inbound save keeps the stored clients, so switching to Hysteria is judged
  9. // on them: ones with no auth would leave an inbound nobody can connect to.
  10. func TestUpdateInbound_RejectsHysteriaClientWithoutAuth(t *testing.T) {
  11. setupConflictDB(t)
  12. seedInboundConflict(t, "in-45001-tcp", "0.0.0.0", 45001, model.VLESS,
  13. `{"network":"tcp"}`, `{"clients":[{"email":"hysteria@x","enable":true,"password":"not-hysteria-auth"}]}`)
  14. var existing model.Inbound
  15. if err := database.GetDB().Where("tag = ?", "in-45001-tcp").First(&existing).Error; err != nil {
  16. t.Fatalf("read seeded row: %v", err)
  17. }
  18. update := existing
  19. update.Protocol = model.Hysteria
  20. update.Settings = `{"clients":[]}`
  21. svc := &InboundService{}
  22. if _, _, err := svc.UpdateInbound(&update); err == nil || !strings.Contains(err.Error(), "empty client ID") {
  23. t.Fatalf("UpdateInbound error = %v, want empty client ID", err)
  24. }
  25. var reloaded model.Inbound
  26. if err := database.GetDB().First(&reloaded, existing.Id).Error; err != nil {
  27. t.Fatalf("reload: %v", err)
  28. }
  29. if reloaded.Protocol != existing.Protocol {
  30. t.Fatalf("persisted protocol = %q, want unchanged %q", reloaded.Protocol, existing.Protocol)
  31. }
  32. if reloaded.Settings != existing.Settings {
  33. t.Fatalf("rejected settings were persisted\ngot: %s\nwant: %s", reloaded.Settings, existing.Settings)
  34. }
  35. }
  36. func TestUpdateInbound_PreservesHysteriaClientAuth(t *testing.T) {
  37. setupConflictDB(t)
  38. seedInboundConflict(t, "in-45002-udp", "0.0.0.0", 45002, model.Hysteria,
  39. `{"network":"hysteria"}`, `{"clients":[]}`)
  40. var existing model.Inbound
  41. if err := database.GetDB().Where("tag = ?", "in-45002-udp").First(&existing).Error; err != nil {
  42. t.Fatalf("read seeded row: %v", err)
  43. }
  44. const wantAuth = "hysteria-auth"
  45. const password = "not-hysteria-auth"
  46. update := existing
  47. update.Settings = `{"clients":[{"email":"hysteria@x","enable":true,"password":"` + password + `","auth":"` + wantAuth + `"}]}`
  48. // Only a master's push still carries clients through an inbound save.
  49. svc := &InboundService{FromNodeSync: true}
  50. if _, _, err := svc.UpdateInbound(&update); err != nil {
  51. t.Fatalf("UpdateInbound: %v", err)
  52. }
  53. var reloaded model.Inbound
  54. if err := database.GetDB().First(&reloaded, existing.Id).Error; err != nil {
  55. t.Fatalf("reload: %v", err)
  56. }
  57. clients, err := ParseInboundSettingsClients(reloaded.Settings)
  58. if err != nil {
  59. t.Fatalf("parse persisted clients: %v", err)
  60. }
  61. if len(clients) != 1 {
  62. t.Fatalf("persisted clients = %d, want 1", len(clients))
  63. }
  64. if clients[0].Auth != wantAuth {
  65. t.Fatalf("persisted auth = %q, want %q", clients[0].Auth, wantAuth)
  66. }
  67. if clients[0].Password != password {
  68. t.Fatalf("persisted password = %q, want %q", clients[0].Password, password)
  69. }
  70. }
  71. func TestUpdateInbound_AllowsHysteriaWithoutClients(t *testing.T) {
  72. setupConflictDB(t)
  73. seedInboundConflict(t, "in-45003-udp", "0.0.0.0", 45003, model.Hysteria,
  74. `{"network":"hysteria"}`, `{"clients":[]}`)
  75. var existing model.Inbound
  76. if err := database.GetDB().Where("tag = ?", "in-45003-udp").First(&existing).Error; err != nil {
  77. t.Fatalf("read seeded row: %v", err)
  78. }
  79. update := existing
  80. update.Remark = "updated without clients"
  81. svc := &InboundService{}
  82. if _, _, err := svc.UpdateInbound(&update); err != nil {
  83. t.Fatalf("UpdateInbound: %v", err)
  84. }
  85. var reloaded model.Inbound
  86. if err := database.GetDB().First(&reloaded, existing.Id).Error; err != nil {
  87. t.Fatalf("reload: %v", err)
  88. }
  89. if reloaded.Remark != update.Remark {
  90. t.Fatalf("persisted remark = %q, want %q", reloaded.Remark, update.Remark)
  91. }
  92. }