inbound_hysteria_auth_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. func TestUpdateInbound_RejectsHysteriaClientWithoutAuth(t *testing.T) {
  9. setupConflictDB(t)
  10. seedInboundConflict(t, "in-45001-tcp", "0.0.0.0", 45001, model.VLESS,
  11. `{"network":"tcp"}`, `{"clients":[]}`)
  12. var existing model.Inbound
  13. if err := database.GetDB().Where("tag = ?", "in-45001-tcp").First(&existing).Error; err != nil {
  14. t.Fatalf("read seeded row: %v", err)
  15. }
  16. update := existing
  17. update.Protocol = model.Hysteria
  18. update.Settings = `{"clients":[{"email":"hysteria@x","enable":true,"password":"not-hysteria-auth"}]}`
  19. svc := &InboundService{}
  20. if _, _, err := svc.UpdateInbound(&update); err == nil || !strings.Contains(err.Error(), "empty client ID") {
  21. t.Fatalf("UpdateInbound error = %v, want empty client ID", err)
  22. }
  23. var reloaded model.Inbound
  24. if err := database.GetDB().First(&reloaded, existing.Id).Error; err != nil {
  25. t.Fatalf("reload: %v", err)
  26. }
  27. if reloaded.Protocol != existing.Protocol {
  28. t.Fatalf("persisted protocol = %q, want unchanged %q", reloaded.Protocol, existing.Protocol)
  29. }
  30. if reloaded.Settings != existing.Settings {
  31. t.Fatalf("rejected settings were persisted\ngot: %s\nwant: %s", reloaded.Settings, existing.Settings)
  32. }
  33. }
  34. func TestUpdateInbound_PreservesHysteriaClientAuth(t *testing.T) {
  35. setupConflictDB(t)
  36. seedInboundConflict(t, "in-45002-udp", "0.0.0.0", 45002, model.Hysteria,
  37. `{"network":"hysteria"}`, `{"clients":[]}`)
  38. var existing model.Inbound
  39. if err := database.GetDB().Where("tag = ?", "in-45002-udp").First(&existing).Error; err != nil {
  40. t.Fatalf("read seeded row: %v", err)
  41. }
  42. const wantAuth = "hysteria-auth"
  43. const password = "not-hysteria-auth"
  44. update := existing
  45. update.Settings = `{"clients":[{"email":"hysteria@x","enable":true,"password":"` + password + `","auth":"` + wantAuth + `"}]}`
  46. svc := &InboundService{}
  47. if _, _, err := svc.UpdateInbound(&update); err != nil {
  48. t.Fatalf("UpdateInbound: %v", err)
  49. }
  50. var reloaded model.Inbound
  51. if err := database.GetDB().First(&reloaded, existing.Id).Error; err != nil {
  52. t.Fatalf("reload: %v", err)
  53. }
  54. clients, err := ParseInboundSettingsClients(reloaded.Settings)
  55. if err != nil {
  56. t.Fatalf("parse persisted clients: %v", err)
  57. }
  58. if len(clients) != 1 {
  59. t.Fatalf("persisted clients = %d, want 1", len(clients))
  60. }
  61. if clients[0].Auth != wantAuth {
  62. t.Fatalf("persisted auth = %q, want %q", clients[0].Auth, wantAuth)
  63. }
  64. if clients[0].Password != password {
  65. t.Fatalf("persisted password = %q, want %q", clients[0].Password, password)
  66. }
  67. }
  68. func TestUpdateInbound_AllowsHysteriaWithoutClients(t *testing.T) {
  69. setupConflictDB(t)
  70. seedInboundConflict(t, "in-45003-udp", "0.0.0.0", 45003, model.Hysteria,
  71. `{"network":"hysteria"}`, `{"clients":[]}`)
  72. var existing model.Inbound
  73. if err := database.GetDB().Where("tag = ?", "in-45003-udp").First(&existing).Error; err != nil {
  74. t.Fatalf("read seeded row: %v", err)
  75. }
  76. update := existing
  77. update.Remark = "updated without clients"
  78. svc := &InboundService{}
  79. if _, _, err := svc.UpdateInbound(&update); err != nil {
  80. t.Fatalf("UpdateInbound: %v", err)
  81. }
  82. var reloaded model.Inbound
  83. if err := database.GetDB().First(&reloaded, existing.Id).Error; err != nil {
  84. t.Fatalf("reload: %v", err)
  85. }
  86. if reloaded.Remark != update.Remark {
  87. t.Fatalf("persisted remark = %q, want %q", reloaded.Remark, update.Remark)
  88. }
  89. }