external_only_sub_test.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package sub
  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. // A subscription whose only entries are external links — no enabled standard
  9. // inbound — must still render in the JSON and Clash formats, not just the raw
  10. // one. Regression guard for the premature len(inbounds)==0 early return that
  11. // short-circuited GetJson/GetClash before external links were ever fetched.
  12. func TestJsonAndClashServeExternalLinkOnlySub(t *testing.T) {
  13. initSubDB(t)
  14. db := database.GetDB()
  15. rec := &model.ClientRecord{Email: "ext@x", SubID: "ext-only", UUID: "ext-uuid", Enable: true}
  16. if err := db.Create(rec).Error; err != nil {
  17. t.Fatalf("seed client: %v", err)
  18. }
  19. link := "vless://[email protected]:443?type=tcp&security=reality&pbk=abc&sid=12&fp=chrome#orig"
  20. if err := db.Create(&model.ClientExternalLink{ClientId: rec.Id, Kind: model.ExternalLinkKindLink, Value: link, Remark: "DE-Provider", SortIndex: 1}).Error; err != nil {
  21. t.Fatalf("seed external link: %v", err)
  22. }
  23. base := NewSubService(false, "-io")
  24. jsonOut, _, err := NewSubJsonService("", "", "", base).GetJson("ext-only", "sub.example.com")
  25. if err != nil {
  26. t.Fatalf("GetJson err = %v", err)
  27. }
  28. if jsonOut == "" {
  29. t.Fatal("GetJson returned empty for an external-link-only sub")
  30. }
  31. if !strings.Contains(jsonOut, "DE-Provider") {
  32. t.Fatalf("GetJson missing external remark: %s", jsonOut)
  33. }
  34. clashOut, _, err := NewSubClashService(false, "", base).GetClash("ext-only", "sub.example.com")
  35. if err != nil {
  36. t.Fatalf("GetClash err = %v", err)
  37. }
  38. if clashOut == "" {
  39. t.Fatal("GetClash returned empty for an external-link-only sub")
  40. }
  41. if !strings.Contains(clashOut, "DE-Provider") {
  42. t.Fatalf("GetClash missing external proxy: %s", clashOut)
  43. }
  44. }