outbound.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package v5cfg
  2. import (
  3. "context"
  4. "strings"
  5. "github.com/golang/protobuf/proto"
  6. core "github.com/v2fly/v2ray-core/v5"
  7. "github.com/v2fly/v2ray-core/v5/app/proxyman"
  8. "github.com/v2fly/v2ray-core/v5/common/serial"
  9. "github.com/v2fly/v2ray-core/v5/transport/internet"
  10. )
  11. func (c OutboundConfig) BuildV5(ctx context.Context) (proto.Message, error) {
  12. senderSettings := &proxyman.SenderConfig{}
  13. if c.SendThrough != nil {
  14. address := c.SendThrough
  15. if address.Family().IsDomain() {
  16. return nil, newError("unable to send through: " + address.String())
  17. }
  18. senderSettings.Via = address.Build()
  19. }
  20. if c.StreamSetting != nil {
  21. ss, err := c.StreamSetting.BuildV5(ctx)
  22. if err != nil {
  23. return nil, err
  24. }
  25. senderSettings.StreamSettings = ss.(*internet.StreamConfig)
  26. }
  27. if c.ProxySettings != nil {
  28. ps, err := c.ProxySettings.Build()
  29. if err != nil {
  30. return nil, newError("invalid outbound detour proxy settings.").Base(err)
  31. }
  32. senderSettings.ProxySettings = ps
  33. }
  34. if c.MuxSettings != nil {
  35. senderSettings.MultiplexSettings = c.MuxSettings.Build()
  36. }
  37. senderSettings.DomainStrategy = proxyman.SenderConfig_AS_IS
  38. switch strings.ToLower(c.DomainStrategy) {
  39. case "useip", "use_ip", "use-ip":
  40. senderSettings.DomainStrategy = proxyman.SenderConfig_USE_IP
  41. case "useip4", "useipv4", "use_ip4", "use_ipv4", "use_ip_v4", "use-ip4", "use-ipv4", "use-ip-v4":
  42. senderSettings.DomainStrategy = proxyman.SenderConfig_USE_IP4
  43. case "useip6", "useipv6", "use_ip6", "use_ipv6", "use_ip_v6", "use-ip6", "use-ipv6", "use-ip-v6":
  44. senderSettings.DomainStrategy = proxyman.SenderConfig_USE_IP6
  45. }
  46. if c.Settings == nil {
  47. c.Settings = []byte("{}")
  48. }
  49. outboundConfigPack, err := loadHeterogeneousConfigFromRawJSON("outbound", c.Protocol, c.Settings)
  50. if err != nil {
  51. return nil, newError("unable to load outbound protocol config").Base(err)
  52. }
  53. return &core.OutboundHandlerConfig{
  54. SenderSettings: serial.ToTypedMessage(senderSettings),
  55. Tag: c.Tag,
  56. ProxySettings: serial.ToTypedMessage(outboundConfigPack),
  57. }, nil
  58. }