inbound.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package v5cfg
  2. import (
  3. "context"
  4. "path/filepath"
  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/proxy/dokodemo"
  10. "github.com/v2fly/v2ray-core/v5/transport/internet"
  11. )
  12. func (c InboundConfig) BuildV5(ctx context.Context) (proto.Message, error) {
  13. receiverSettings := &proxyman.ReceiverConfig{}
  14. if c.ListenOn == nil {
  15. // Listen on anyip, must set PortRange
  16. if c.PortRange == nil {
  17. return nil, newError("Listen on AnyIP but no Port(s) set in InboundDetour.")
  18. }
  19. receiverSettings.PortRange = c.PortRange.Build()
  20. } else {
  21. // Listen on specific IP or Unix Domain Socket
  22. receiverSettings.Listen = c.ListenOn.Build()
  23. listenDS := c.ListenOn.Family().IsDomain() && (filepath.IsAbs(c.ListenOn.Domain()) || c.ListenOn.Domain()[0] == '@')
  24. listenIP := c.ListenOn.Family().IsIP() || (c.ListenOn.Family().IsDomain() && c.ListenOn.Domain() == "localhost")
  25. switch {
  26. case listenIP:
  27. // Listen on specific IP, must set PortRange
  28. if c.PortRange == nil {
  29. return nil, newError("Listen on specific ip without port in InboundDetour.")
  30. }
  31. // Listen on IP:Port
  32. receiverSettings.PortRange = c.PortRange.Build()
  33. case listenDS:
  34. if c.PortRange != nil {
  35. // Listen on Unix Domain Socket, PortRange should be nil
  36. receiverSettings.PortRange = nil
  37. }
  38. default:
  39. return nil, newError("unable to listen on domain address: ", c.ListenOn.Domain())
  40. }
  41. }
  42. if c.StreamSetting != nil {
  43. ss, err := c.StreamSetting.BuildV5(ctx)
  44. if err != nil {
  45. return nil, err
  46. }
  47. receiverSettings.StreamSettings = ss.(*internet.StreamConfig)
  48. }
  49. if c.SniffingConfig != nil {
  50. s, err := c.SniffingConfig.Build()
  51. if err != nil {
  52. return nil, newError("failed to build sniffing config").Base(err)
  53. }
  54. receiverSettings.SniffingSettings = s
  55. }
  56. if c.Settings == nil {
  57. c.Settings = []byte("{}")
  58. }
  59. inboundConfigPack, err := loadHeterogeneousConfigFromRawJSON("inbound", c.Protocol, c.Settings)
  60. if err != nil {
  61. return nil, newError("unable to load inbound protocol config").Base(err)
  62. }
  63. if content, ok := inboundConfigPack.(*dokodemo.SimplifiedConfig); ok {
  64. receiverSettings.ReceiveOriginalDestination = content.FollowRedirect
  65. }
  66. if content, ok := inboundConfigPack.(*dokodemo.Config); ok {
  67. receiverSettings.ReceiveOriginalDestination = content.FollowRedirect
  68. }
  69. return &core.InboundHandlerConfig{
  70. Tag: c.Tag,
  71. ReceiverSettings: serial.ToTypedMessage(receiverSettings),
  72. ProxySettings: serial.ToTypedMessage(inboundConfigPack),
  73. }, nil
  74. }