dokodemo.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package dokodemo
  2. import (
  3. "context"
  4. "runtime"
  5. "time"
  6. "v2ray.com/core/app"
  7. "v2ray.com/core/app/dispatcher"
  8. "v2ray.com/core/common"
  9. "v2ray.com/core/common/buf"
  10. "v2ray.com/core/common/errors"
  11. "v2ray.com/core/common/log"
  12. "v2ray.com/core/common/net"
  13. "v2ray.com/core/common/signal"
  14. "v2ray.com/core/proxy"
  15. "v2ray.com/core/transport/internet"
  16. )
  17. type DokodemoDoor struct {
  18. config *Config
  19. address net.Address
  20. port net.Port
  21. packetDispatcher dispatcher.Interface
  22. }
  23. func New(ctx context.Context, config *Config) (*DokodemoDoor, error) {
  24. space := app.SpaceFromContext(ctx)
  25. if space == nil {
  26. return nil, errors.New("Dokodemo: No space in context.")
  27. }
  28. if config.NetworkList == nil || config.NetworkList.Size() == 0 {
  29. return nil, errors.New("DokodemoDoor: No network specified.")
  30. }
  31. d := &DokodemoDoor{
  32. config: config,
  33. address: config.GetPredefinedAddress(),
  34. port: net.Port(config.Port),
  35. }
  36. space.OnInitialize(func() error {
  37. d.packetDispatcher = dispatcher.FromSpace(space)
  38. if d.packetDispatcher == nil {
  39. return errors.New("Dokodemo: Dispatcher is not found in the space.")
  40. }
  41. return nil
  42. })
  43. return d, nil
  44. }
  45. func (d *DokodemoDoor) Network() net.NetworkList {
  46. return *(d.config.NetworkList)
  47. }
  48. func (d *DokodemoDoor) Process(ctx context.Context, network net.Network, conn internet.Connection) error {
  49. log.Debug("Dokodemo: processing connection from: ", conn.RemoteAddr())
  50. conn.SetReusable(false)
  51. dest := net.Destination{
  52. Network: network,
  53. Address: d.address,
  54. Port: d.port,
  55. }
  56. if d.config.FollowRedirect {
  57. if origDest := proxy.OriginalDestinationFromContext(ctx); origDest.IsValid() {
  58. dest = origDest
  59. }
  60. }
  61. if !dest.IsValid() || dest.Address == nil {
  62. log.Info("Dokodemo: Invalid destination. Discarding...")
  63. return errors.New("Dokodemo: Unable to get destination.")
  64. }
  65. ctx = proxy.ContextWithDestination(ctx, dest)
  66. ctx, cancel := context.WithCancel(ctx)
  67. timer := signal.CancelAfterInactivity(ctx, cancel, time.Minute*2)
  68. inboundRay := d.packetDispatcher.DispatchToOutbound(ctx)
  69. requestDone := signal.ExecuteAsync(func() error {
  70. defer inboundRay.InboundInput().Close()
  71. timedReader := net.NewTimeOutReader(d.config.Timeout, conn)
  72. chunkReader := buf.NewReader(timedReader)
  73. if err := buf.PipeUntilEOF(timer, chunkReader, inboundRay.InboundInput()); err != nil {
  74. log.Info("Dokodemo: Failed to transport request: ", err)
  75. return err
  76. }
  77. return nil
  78. })
  79. responseDone := signal.ExecuteAsync(func() error {
  80. v2writer := buf.NewWriter(conn)
  81. if err := buf.PipeUntilEOF(timer, inboundRay.InboundOutput(), v2writer); err != nil {
  82. log.Info("Dokodemo: Failed to transport response: ", err)
  83. return err
  84. }
  85. return nil
  86. })
  87. if err := signal.ErrorOrFinish2(ctx, requestDone, responseDone); err != nil {
  88. inboundRay.InboundInput().CloseError()
  89. inboundRay.InboundOutput().CloseError()
  90. log.Info("Dokodemo: Connection ends with ", err)
  91. return err
  92. }
  93. runtime.KeepAlive(timer)
  94. return nil
  95. }
  96. func init() {
  97. common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
  98. return New(ctx, config.(*Config))
  99. }))
  100. }