outbound.go 1.9 KB

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