outbound.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. if c.Settings == nil {
  37. c.Settings = []byte("{}")
  38. }
  39. outboundConfigPack, err := loadHeterogeneousConfigFromRawJSON("outbound", c.Protocol, c.Settings)
  40. if err != nil {
  41. return nil, newError("unable to load outbound protocol config").Base(err)
  42. }
  43. return &core.OutboundHandlerConfig{
  44. SenderSettings: serial.ToTypedMessage(senderSettings),
  45. Tag: c.Tag,
  46. ProxySettings: serial.ToTypedMessage(outboundConfigPack),
  47. }, nil
  48. }