salamander_test.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package maskcompat
  2. import (
  3. "context"
  4. "encoding/json"
  5. "testing"
  6. xnet "github.com/xtls/xray-core/common/net"
  7. "github.com/xtls/xray-core/infra/conf"
  8. "github.com/xtls/xray-core/transport/internet"
  9. )
  10. // coreListensSalamander opens a listener through the finalmask the way an inbound does; the core
  11. // checks the salamander password only here, never when it loads the config.
  12. func coreListensSalamander(t *testing.T, password string) bool {
  13. t.Helper()
  14. raw, _ := json.Marshal(map[string]any{"finalmask": map[string]any{"udp": []any{
  15. map[string]any{"type": "salamander", "settings": map[string]any{"password": password}},
  16. }}})
  17. var sc conf.StreamConfig
  18. if err := json.Unmarshal(raw, &sc); err != nil {
  19. t.Fatalf("decode stream: %v", err)
  20. }
  21. built, err := sc.Build()
  22. if err != nil {
  23. t.Fatalf("the core refuses to load the stream: %v", err)
  24. }
  25. mss, err := internet.ToMemoryStreamConfig(built)
  26. if err != nil {
  27. t.Fatalf("memory stream: %v", err)
  28. }
  29. conn, err := mss.FinalMask.ListenPacket(context.Background(), &xnet.UDPAddr{IP: []byte{127, 0, 0, 1}})
  30. if err != nil {
  31. return false
  32. }
  33. _ = conn.Close()
  34. return true
  35. }
  36. // The panel must refuse exactly the passwords the core refuses: it counts bytes, not characters.
  37. func TestCheckSalamanderPasswordsMatchesTheCore(t *testing.T) {
  38. for _, password := range []string{"", "abc", "€", "abcd", "a€", "long-enough"} {
  39. t.Run(password, func(t *testing.T) {
  40. finalmask := map[string]any{"udp": []any{
  41. map[string]any{"type": "salamander", "settings": map[string]any{"password": password}},
  42. }}
  43. panelAccepts := CheckSalamanderPasswords(finalmask) == nil
  44. if coreAccepts := coreListensSalamander(t, password); panelAccepts != coreAccepts {
  45. t.Fatalf("password %q: panel accepts %v, core listens %v", password, panelAccepts, coreAccepts)
  46. }
  47. })
  48. }
  49. }