1
0

shadowsocks_plugin_import_test.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package sub
  2. import (
  3. "encoding/json"
  4. "strings"
  5. "testing"
  6. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  7. "github.com/mhsanaei/3x-ui/v3/internal/util/link"
  8. )
  9. // The panel exports shadowsocks tcp/http obfuscation as the SIP002 plugin, so
  10. // importing that same link has to rebuild the header it stands for.
  11. func TestShadowsocksHTTPObfsSurvivesExportImport(t *testing.T) {
  12. in := &model.Inbound{
  13. Id: 940001, Listen: "203.0.113.1", Port: 8388, Protocol: model.Shadowsocks,
  14. Settings: `{"method":"aes-256-gcm","password":"serverpass","clients":[{"email":"user","password":"clientpass"}]}`,
  15. StreamSettings: `{"network":"tcp","security":"none","tcpSettings":{"header":{"type":"http","request":{"path":["/"],"headers":{"Host":["obfs.example.com"]}}}}}`,
  16. }
  17. exported := (&SubService{}).genShadowsocksLink(in, "user")
  18. if !strings.Contains(exported, "plugin=obfs-local%3Bobfs%3Dhttp") {
  19. t.Fatalf("export did not use the SIP002 plugin form: %q", exported)
  20. }
  21. parsed, err := link.ParseLink(exported)
  22. if err != nil {
  23. t.Fatalf("ParseLink(%q): %v", exported, err)
  24. }
  25. streamJSON, err := json.Marshal(parsed.Outbound["streamSettings"])
  26. if err != nil {
  27. t.Fatalf("marshal stream: %v", err)
  28. }
  29. var stream map[string]any
  30. if err := json.Unmarshal(streamJSON, &stream); err != nil {
  31. t.Fatalf("stream json: %v", err)
  32. }
  33. tcp, _ := stream["tcpSettings"].(map[string]any)
  34. header, _ := tcp["header"].(map[string]any)
  35. if header == nil || header["type"] != "http" {
  36. t.Fatalf("import dropped the tcp/http obfuscation: %s", streamJSON)
  37. }
  38. request, _ := header["request"].(map[string]any)
  39. headers, _ := request["headers"].(map[string]any)
  40. hosts, _ := headers["Host"].([]any)
  41. if len(hosts) == 0 || hosts[0] != "obfs.example.com" {
  42. t.Fatalf("import dropped the obfs host: %s", streamJSON)
  43. }
  44. }