xray_amneziawg_outbound.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package service
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/mhsanaei/3x-ui/v3/internal/amneziawg"
  6. "github.com/mhsanaei/3x-ui/v3/internal/amneziawgnet"
  7. json_util "github.com/mhsanaei/3x-ui/v3/internal/util/json_util"
  8. "github.com/mhsanaei/3x-ui/v3/internal/xray"
  9. )
  10. // transformAmneziaWGOutbounds swaps each template "amneziawg" outbound for
  11. // its socks bridge; unbridgeable entries fail generation rather than skip.
  12. func transformAmneziaWGOutbounds(cfg *xray.Config) error {
  13. if len(cfg.OutboundConfigs) == 0 {
  14. return nil
  15. }
  16. var outbounds []json.RawMessage
  17. if err := json.Unmarshal(cfg.OutboundConfigs, &outbounds); err != nil {
  18. return err
  19. }
  20. changed := false
  21. for i, raw := range outbounds {
  22. if !amneziawg.IsAmneziaWGOutbound(raw) {
  23. continue
  24. }
  25. var probe struct {
  26. Tag string `json:"tag"`
  27. }
  28. tagErr := json.Unmarshal(raw, &probe)
  29. replacement, ok := amneziawgnet.BuildSocksBridge(raw)
  30. if !ok {
  31. if tagErr != nil {
  32. return fmt.Errorf("amneziawg outbound %d: unreadable tag: %w", i, tagErr)
  33. }
  34. return fmt.Errorf("amneziawg outbound %d (%q): cannot bridge: tag must be a non-empty string", i, probe.Tag)
  35. }
  36. outbounds[i] = replacement
  37. changed = true
  38. }
  39. if !changed {
  40. return nil
  41. }
  42. bs, err := json.Marshal(outbounds)
  43. if err != nil {
  44. return err
  45. }
  46. cfg.OutboundConfigs = json_util.RawMessage(bs)
  47. return nil
  48. }