shadowsocks_server_key_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package service
  2. import (
  3. "encoding/base64"
  4. "fmt"
  5. "strings"
  6. "testing"
  7. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  8. )
  9. func ss2022Settings(method, serverKey string) string {
  10. clientKey := base64.StdEncoding.EncodeToString(make([]byte, 16))
  11. if method != "2022-blake3-aes-128-gcm" {
  12. clientKey = base64.StdEncoding.EncodeToString(make([]byte, 32))
  13. }
  14. return fmt.Sprintf(`{"method":%q,"password":%q,"network":"tcp,udp","clients":[{"email":"ss-key","password":%q,"enable":true}]}`,
  15. method, serverKey, clientKey)
  16. }
  17. // xray-core refuses a wrong-size Shadowsocks-2022 server key while building the
  18. // inbound, which keeps the whole core from starting, not just this inbound.
  19. func TestShadowsocks2022ServerKeyRejected(t *testing.T) {
  20. key32 := base64.StdEncoding.EncodeToString(make([]byte, 32))
  21. key16 := base64.StdEncoding.EncodeToString(make([]byte, 16))
  22. t.Run("add", func(t *testing.T) {
  23. setupConflictDB(t)
  24. nodeID, fake := setupNodeRuntime(t)
  25. ib := &model.Inbound{
  26. UserId: 1, NodeID: &nodeID, Tag: "ss-add", Enable: true, Port: 41801, Protocol: model.Shadowsocks,
  27. Settings: ss2022Settings("2022-blake3-aes-128-gcm", key32),
  28. }
  29. _, _, err := (&InboundService{}).AddInbound(ib)
  30. if err == nil || !strings.Contains(err.Error(), "2022-blake3-aes-128-gcm needs a 16-byte key") {
  31. t.Fatalf("AddInbound with a 32-byte aes-128 key: err = %v", err)
  32. }
  33. if fake.addInbound.Load() != 0 {
  34. t.Fatal("rejected inbound dispatched to the runtime")
  35. }
  36. })
  37. t.Run("update", func(t *testing.T) {
  38. setupConflictDB(t)
  39. nodeID, fake := setupNodeRuntime(t)
  40. svc := &InboundService{}
  41. ib := &model.Inbound{
  42. UserId: 1, NodeID: &nodeID, Tag: "ss-update", Enable: true, Port: 41802, Protocol: model.Shadowsocks,
  43. Settings: ss2022Settings("2022-blake3-aes-128-gcm", key16),
  44. }
  45. if _, _, err := svc.AddInbound(ib); err != nil {
  46. t.Fatalf("AddInbound with a valid key: %v", err)
  47. }
  48. update := *ib
  49. update.Settings = ss2022Settings("2022-blake3-aes-256-gcm", key16)
  50. _, _, err := svc.UpdateInbound(&update)
  51. if err == nil || !strings.Contains(err.Error(), "2022-blake3-aes-256-gcm needs a 32-byte key") {
  52. t.Fatalf("UpdateInbound keeping a 16-byte key on aes-256: err = %v", err)
  53. }
  54. if fake.updateInbound.Load() != 0 {
  55. t.Fatal("rejected update dispatched to the runtime")
  56. }
  57. })
  58. // The core reads a non-base64 key as its raw bytes, so this one is valid.
  59. t.Run("raw key the core accepts", func(t *testing.T) {
  60. setupConflictDB(t)
  61. nodeID, _ := setupNodeRuntime(t)
  62. ib := &model.Inbound{
  63. UserId: 1, NodeID: &nodeID, Tag: "ss-raw", Enable: true, Port: 41803, Protocol: model.Shadowsocks,
  64. Settings: ss2022Settings("2022-blake3-aes-128-gcm", "0123456789abcd-!"),
  65. }
  66. if _, _, err := (&InboundService{}).AddInbound(ib); err != nil {
  67. t.Fatalf("AddInbound with a raw 16-byte key: %v", err)
  68. }
  69. })
  70. }
  71. // xray-core builds a Shadowsocks-2022 inbound with users only for the AES methods;
  72. // a 2022-chacha20 one with clients keeps the whole core from starting.
  73. func TestShadowsocks2022ChachaInboundRejected(t *testing.T) {
  74. const chacha = "2022-blake3-chacha20-poly1305"
  75. key32 := base64.StdEncoding.EncodeToString(make([]byte, 32))
  76. key16 := base64.StdEncoding.EncodeToString(make([]byte, 16))
  77. const wantErr = "shadowsocks inbound method 2022-blake3-chacha20-poly1305 does not support multiple users"
  78. t.Run("add", func(t *testing.T) {
  79. setupConflictDB(t)
  80. nodeID, fake := setupNodeRuntime(t)
  81. ib := &model.Inbound{
  82. UserId: 1, NodeID: &nodeID, Tag: "ss-chacha-add", Enable: true, Port: 41811, Protocol: model.Shadowsocks,
  83. Settings: ss2022Settings(chacha, key32),
  84. }
  85. _, _, err := (&InboundService{}).AddInbound(ib)
  86. if err == nil || !strings.Contains(err.Error(), wantErr) {
  87. t.Fatalf("AddInbound with %s: err = %v", chacha, err)
  88. }
  89. if fake.addInbound.Load() != 0 {
  90. t.Fatal("rejected inbound dispatched to the runtime")
  91. }
  92. })
  93. t.Run("update", func(t *testing.T) {
  94. setupConflictDB(t)
  95. nodeID, fake := setupNodeRuntime(t)
  96. svc := &InboundService{}
  97. ib := &model.Inbound{
  98. UserId: 1, NodeID: &nodeID, Tag: "ss-chacha-update", Enable: true, Port: 41812, Protocol: model.Shadowsocks,
  99. Settings: ss2022Settings("2022-blake3-aes-128-gcm", key16),
  100. }
  101. if _, _, err := svc.AddInbound(ib); err != nil {
  102. t.Fatalf("AddInbound: %v", err)
  103. }
  104. update := *ib
  105. update.Settings = ss2022Settings(chacha, key32)
  106. _, _, err := svc.UpdateInbound(&update)
  107. if err == nil || !strings.Contains(err.Error(), wantErr) {
  108. t.Fatalf("UpdateInbound to %s: err = %v", chacha, err)
  109. }
  110. if fake.updateInbound.Load() != 0 {
  111. t.Fatal("rejected update dispatched to the runtime")
  112. }
  113. })
  114. }