shadowsocks_server_key_test.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. }