inbound_detour.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package point
  2. import (
  3. "github.com/v2ray/v2ray-core/common/log"
  4. v2net "github.com/v2ray/v2ray-core/common/net"
  5. "github.com/v2ray/v2ray-core/common/retry"
  6. "github.com/v2ray/v2ray-core/proxy/common/connhandler"
  7. "github.com/v2ray/v2ray-core/shell/point/config"
  8. )
  9. type InboundConnectionHandlerWithPort struct {
  10. port v2net.Port
  11. handler connhandler.InboundConnectionHandler
  12. }
  13. type InboundDetourHandler struct {
  14. point *Point
  15. config config.InboundDetourConfig
  16. ich []*InboundConnectionHandlerWithPort
  17. }
  18. func (this *InboundDetourHandler) Initialize() error {
  19. ichFactory := connhandler.GetInboundConnectionHandlerFactory(this.config.Protocol())
  20. if ichFactory == nil {
  21. log.Error("Unknown inbound connection handler factory %s", this.config.Protocol())
  22. return config.BadConfiguration
  23. }
  24. ports := this.config.PortRange()
  25. this.ich = make([]*InboundConnectionHandlerWithPort, 0, ports.From()-ports.To()+1)
  26. for i := ports.From(); i <= ports.To(); i++ {
  27. ichConfig := this.config.Settings()
  28. ich, err := ichFactory.Create(this.point, ichConfig)
  29. if err != nil {
  30. log.Error("Failed to create inbound connection handler: %v", err)
  31. return err
  32. }
  33. this.ich = append(this.ich, &InboundConnectionHandlerWithPort{
  34. port: i,
  35. handler: ich,
  36. })
  37. }
  38. return nil
  39. }
  40. func (this *InboundDetourHandler) Start() error {
  41. for _, ich := range this.ich {
  42. return retry.Timed(100 /* times */, 100 /* ms */).On(func() error {
  43. err := ich.handler.Listen(ich.port.Value())
  44. if err != nil {
  45. return err
  46. }
  47. return nil
  48. })
  49. }
  50. return nil
  51. }