1
0

vless_route_sub_test.go 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package sub
  2. import (
  3. "strings"
  4. "testing"
  5. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  6. )
  7. func TestHostToExternalProxyMap_VlessRoute(t *testing.T) {
  8. with := hostToExternalProxyMap(&model.Host{VlessRoute: "443"}, "d.example.com", 443)
  9. if with["vlessRoute"] != "443" {
  10. t.Fatalf(`ep["vlessRoute"] = %v, want "443"`, with["vlessRoute"])
  11. }
  12. without := hostToExternalProxyMap(&model.Host{}, "d.example.com", 443)
  13. if _, ok := without["vlessRoute"]; ok {
  14. t.Fatalf("empty VlessRoute must not add the key: %v", without["vlessRoute"])
  15. }
  16. }
  17. // seedSubInbound's client UUID is 11111111-2222-4333-8444-<port>, so route 443
  18. // -> 01bb, 53 -> 0035, and a route-less host keeps 4333.
  19. func TestSub_HostVlessRoute_RawMultiHost(t *testing.T) {
  20. seedSubDB(t)
  21. ib := seedSubInbound(t, "s1", "vr", 4500, 1, wsTLSStream)
  22. seedHost(t, &model.Host{InboundId: ib.Id, SortOrder: 1, Remark: "A", Address: "a.cdn.com", Port: 8443, Security: "tls", VlessRoute: "443"})
  23. seedHost(t, &model.Host{InboundId: ib.Id, SortOrder: 2, Remark: "B", Address: "b.cdn.com", Port: 8443, Security: "tls", VlessRoute: "53"})
  24. seedHost(t, &model.Host{InboundId: ib.Id, SortOrder: 3, Remark: "C", Address: "c.cdn.com", Port: 8443, Security: "tls"})
  25. links, _, _, _, err := NewSubService("").GetSubs("s1", "req.example.com")
  26. if err != nil {
  27. t.Fatalf("GetSubs: %v", err)
  28. }
  29. parts := strings.Split(strings.Join(links, "\n"), "\n")
  30. if len(parts) != 3 {
  31. t.Fatalf("want 3 host links, got %d: %v", len(parts), parts)
  32. }
  33. if !strings.Contains(parts[0], "vless://11111111-2222-01bb-8444-") {
  34. t.Fatalf("host A (route 443) must encode 01bb: %s", parts[0])
  35. }
  36. if !strings.Contains(parts[1], "vless://11111111-2222-0035-8444-") {
  37. t.Fatalf("host B (route 53) must encode 0035: %s", parts[1])
  38. }
  39. if !strings.Contains(parts[2], "vless://11111111-2222-4333-8444-") {
  40. t.Fatalf("host C (no route) must keep the original 3rd group: %s", parts[2])
  41. }
  42. }
  43. func TestSub_HostVlessRoute_JSON(t *testing.T) {
  44. seedSubDB(t)
  45. ib := seedSubInbound(t, "s1", "vrj", 4501, 1, wsTLSStream)
  46. seedHost(t, &model.Host{InboundId: ib.Id, SortOrder: 1, Remark: "J", Address: "j.cdn.com", Port: 8443, Security: "tls", VlessRoute: "443"})
  47. js := NewSubJsonService("", "", "", NewSubService(""))
  48. out, _, err := js.GetJson("s1", "req.example.com")
  49. if err != nil {
  50. t.Fatalf("GetJson: %v", err)
  51. }
  52. if !strings.Contains(out, "11111111-2222-01bb-8444-") {
  53. t.Fatalf("json outbound id should encode route 443 (01bb):\n%s", out)
  54. }
  55. if strings.Contains(out, "11111111-2222-4333-8444-") {
  56. t.Fatalf("original id 3rd group must be replaced in json:\n%s", out)
  57. }
  58. }
  59. func TestSub_HostVlessRoute_Clash(t *testing.T) {
  60. seedSubDB(t)
  61. ib := seedSubInbound(t, "s1", "vrc", 4502, 1, wsTLSStream)
  62. seedHost(t, &model.Host{InboundId: ib.Id, SortOrder: 1, Remark: "C", Address: "c.cdn.com", Port: 8443, Security: "tls", VlessRoute: "443"})
  63. clash := NewSubClashService(false, "", NewSubService(""))
  64. yaml, _, err := clash.GetClash("s1", "req.example.com")
  65. if err != nil {
  66. t.Fatalf("GetClash: %v", err)
  67. }
  68. if !strings.Contains(yaml, "11111111-2222-01bb-8444-") {
  69. t.Fatalf("clash proxy uuid should encode route 443 (01bb):\n%s", yaml)
  70. }
  71. if strings.Contains(yaml, "11111111-2222-4333-8444-") {
  72. t.Fatalf("original uuid 3rd group must be replaced in clash:\n%s", yaml)
  73. }
  74. }