masque_test.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package sub
  2. import (
  3. "encoding/json"
  4. "reflect"
  5. "testing"
  6. "github.com/mhsanaei/3x-ui/v3/internal/database"
  7. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  8. )
  9. // MASQUE has no share-link format, so the JSON subscription is the only way a client
  10. // gets one: a masque outbound authenticating with the client's email and password.
  11. func TestSubJsonServiceMasqueClientConfig(t *testing.T) {
  12. inbound := &model.Inbound{
  13. Listen: "203.0.113.9", Port: 8443, Protocol: model.MASQUE, Remark: "masque",
  14. Settings: `{"clients":[{"email":"[email protected]","password":"pw-ivy"}],"address":["10.14.0.1/24"]}`,
  15. StreamSettings: `{"network":"masque","security":"tls",
  16. "masqueSettings":{"path":"/.well-known/masque/ip/*/*/"},
  17. "tlsSettings":{"serverName":"masque.example.test","alpn":["h3"],
  18. "certificates":[{"certificateFile":"/etc/ssl/m.crt","keyFile":"/etc/ssl/m.key"}],
  19. "settings":{"fingerprint":"chrome"}}}`,
  20. }
  21. client := model.Client{Email: "[email protected]", Password: "pw-ivy"}
  22. configs := NewSubJsonService("", "", "", "", nil).getConfig(&SubService{address: "sub.example.com"}, inbound, client, "sub.example.com")
  23. if len(configs) != 1 {
  24. t.Fatalf("got %d configs, want one", len(configs))
  25. }
  26. var config struct {
  27. Outbounds []struct {
  28. Protocol string `json:"protocol"`
  29. Settings map[string]any `json:"settings"`
  30. StreamSettings map[string]any `json:"streamSettings"`
  31. } `json:"outbounds"`
  32. }
  33. if err := json.Unmarshal(configs[0], &config); err != nil {
  34. t.Fatalf("decode config: %v", err)
  35. }
  36. proxy := config.Outbounds[0]
  37. if proxy.Protocol != "masque" {
  38. t.Fatalf("first outbound protocol = %q, want masque", proxy.Protocol)
  39. }
  40. if proxy.Settings["address"] != "203.0.113.9" || proxy.Settings["port"] != float64(8443) {
  41. t.Fatalf("masque settings = %v, want the inbound address and port", proxy.Settings)
  42. }
  43. wantMasque := map[string]any{"path": "/.well-known/masque/ip/*/*/", "user": "[email protected]", "pass": "pw-ivy"}
  44. if got := proxy.StreamSettings["masqueSettings"]; !reflect.DeepEqual(got, wantMasque) {
  45. t.Fatalf("masqueSettings = %v, want %v", got, wantMasque)
  46. }
  47. tls, _ := proxy.StreamSettings["tlsSettings"].(map[string]any)
  48. if proxy.StreamSettings["network"] != "masque" || proxy.StreamSettings["security"] != "tls" ||
  49. tls["serverName"] != "masque.example.test" || tls["certificates"] != nil {
  50. t.Fatalf("stream = %v, want masque over client-side TLS to masque.example.test", proxy.StreamSettings)
  51. }
  52. }
  53. // The subscription SQL allowlist decides which inbounds a subId can see at all; left
  54. // off it, a MASQUE client's JSON subscription would come back empty.
  55. func TestGetInboundsBySubIdIncludesMasque(t *testing.T) {
  56. initSubDB(t)
  57. db := database.GetDB()
  58. in := &model.Inbound{Port: 8443, Protocol: model.MASQUE, Enable: true, Tag: "masque-sub", Settings: `{"clients":[],"address":["10.14.0.1/24"]}`}
  59. if err := db.Create(in).Error; err != nil {
  60. t.Fatalf("create inbound: %v", err)
  61. }
  62. rec := &model.ClientRecord{Email: "u@masque", SubID: "submasque", Enable: true}
  63. if err := db.Create(rec).Error; err != nil {
  64. t.Fatalf("create client: %v", err)
  65. }
  66. if err := db.Create(&model.ClientInbound{ClientId: rec.Id, InboundId: in.Id}).Error; err != nil {
  67. t.Fatalf("create link: %v", err)
  68. }
  69. inbounds, err := (&SubService{}).getInboundsBySubId("submasque")
  70. if err != nil {
  71. t.Fatalf("getInboundsBySubId: %v", err)
  72. }
  73. if len(inbounds) != 1 || inbounds[0].Id != in.Id {
  74. t.Fatalf("masque inbound not returned for subId: %+v", inbounds)
  75. }
  76. }
  77. // An inbound keeps its hop range in quicParams.udpHop (udphop refuses to run on a server),
  78. // but the client config must carry the udphop mask: the core ignores the old key since 26.9.9.
  79. func TestSubJsonServiceTurnsInboundHopIntoClientUDPHopMask(t *testing.T) {
  80. inbound := &model.Inbound{
  81. Listen: "203.0.113.10", Port: 443, Protocol: model.Hysteria, Remark: "hy",
  82. Settings: `{"version":2,"clients":[{"email":"[email protected]","auth":"a"}]}`,
  83. StreamSettings: `{"network":"hysteria","security":"tls",
  84. "hysteriaSettings":{"version":2},
  85. "tlsSettings":{"serverName":"hy.example.test","alpn":["h3"]},
  86. "finalmask":{"quicParams":{"udpHop":{"ports":"20000-30000","interval":"5-10"}}}}`,
  87. }
  88. client := model.Client{Email: "[email protected]", Auth: "a"}
  89. configs := NewSubJsonService("", "", "", "", nil).getConfig(&SubService{address: "sub.example.com"}, inbound, client, "sub.example.com")
  90. if len(configs) != 1 {
  91. t.Fatalf("got %d configs, want one", len(configs))
  92. }
  93. var config struct {
  94. Outbounds []struct {
  95. Protocol string `json:"protocol"`
  96. StreamSettings map[string]any `json:"streamSettings"`
  97. } `json:"outbounds"`
  98. }
  99. if err := json.Unmarshal(configs[0], &config); err != nil {
  100. t.Fatalf("decode config: %v", err)
  101. }
  102. got, _ := json.Marshal(config.Outbounds[0].StreamSettings["finalmask"])
  103. const want = `{"udp":[{"settings":{"interval":"5-10","mode":"intervalRemote","remotePorts":"20000-30000"},"type":"udphop"}]}`
  104. if string(got) != want {
  105. t.Fatalf("client finalmask\n got: %s\nwant: %s", got, want)
  106. }
  107. }
  108. // Every endpoint's config starts from the same parsed inbound finalmask; a host whose own
  109. // masks are merged in first must not use up the hop range the endpoints after it need.
  110. func TestSubJsonServiceKeepsUDPHopMaskOnEveryEndpoint(t *testing.T) {
  111. inbound := &model.Inbound{
  112. Listen: "203.0.113.10", Port: 443, Protocol: model.Hysteria, Remark: "hy",
  113. Settings: `{"version":2,"clients":[{"email":"[email protected]","auth":"a"}]}`,
  114. StreamSettings: `{"network":"hysteria","security":"tls",
  115. "hysteriaSettings":{"version":2},
  116. "tlsSettings":{"serverName":"hy.example.test","alpn":["h3"]},
  117. "finalmask":{"quicParams":{"udpHop":{"ports":"20000-30000","interval":"5-10"}}},
  118. "externalProxy":[
  119. {"forceTls":"same","dest":"a.example.test","port":443,"remark":"a",
  120. "finalMask":"{\"udp\":[{\"type\":\"salamander\",\"settings\":{\"password\":\"p\"}}]}"},
  121. {"forceTls":"same","dest":"b.example.test","port":443,"remark":"b"}]}`,
  122. }
  123. client := model.Client{Email: "[email protected]", Auth: "a"}
  124. configs := NewSubJsonService("", "", "", "", nil).getConfig(&SubService{address: "sub.example.com"}, inbound, client, "sub.example.com")
  125. if len(configs) != 2 {
  126. t.Fatalf("got %d configs, want one per endpoint", len(configs))
  127. }
  128. const hop = `{"settings":{"interval":"5-10","mode":"intervalRemote","remotePorts":"20000-30000"},"type":"udphop"}`
  129. wants := []string{
  130. `{"udp":[{"settings":{"password":"p"},"type":"salamander"},` + hop + `]}`,
  131. `{"udp":[` + hop + `]}`,
  132. }
  133. for i, want := range wants {
  134. var config struct {
  135. Outbounds []struct {
  136. StreamSettings map[string]any `json:"streamSettings"`
  137. } `json:"outbounds"`
  138. }
  139. if err := json.Unmarshal(configs[i], &config); err != nil {
  140. t.Fatalf("decode config %d: %v", i, err)
  141. }
  142. got, _ := json.Marshal(config.Outbounds[0].StreamSettings["finalmask"])
  143. if string(got) != want {
  144. t.Fatalf("endpoint %d finalmask\n got: %s\nwant: %s", i, got, want)
  145. }
  146. }
  147. }