inbound_detour.go 1.5 KB

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