dokodemo.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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/app/log"
  9. "v2ray.com/core/common"
  10. "v2ray.com/core/common/buf"
  11. "v2ray.com/core/common/errors"
  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. }
  22. func New(ctx context.Context, config *Config) (*DokodemoDoor, error) {
  23. space := app.SpaceFromContext(ctx)
  24. if space == nil {
  25. return nil, errors.New("Dokodemo: No space in context.")
  26. }
  27. if config.NetworkList == nil || config.NetworkList.Size() == 0 {
  28. return nil, errors.New("DokodemoDoor: No network specified.")
  29. }
  30. d := &DokodemoDoor{
  31. config: config,
  32. address: config.GetPredefinedAddress(),
  33. port: net.Port(config.Port),
  34. }
  35. return d, nil
  36. }
  37. func (d *DokodemoDoor) Network() net.NetworkList {
  38. return *(d.config.NetworkList)
  39. }
  40. func (d *DokodemoDoor) Process(ctx context.Context, network net.Network, conn internet.Connection, dispatcher dispatcher.Interface) error {
  41. log.Debug("Dokodemo: processing connection from: ", conn.RemoteAddr())
  42. conn.SetReusable(false)
  43. dest := net.Destination{
  44. Network: network,
  45. Address: d.address,
  46. Port: d.port,
  47. }
  48. if d.config.FollowRedirect {
  49. if origDest, ok := proxy.OriginalTargetFromContext(ctx); ok {
  50. dest = origDest
  51. }
  52. }
  53. if !dest.IsValid() || dest.Address == nil {
  54. log.Info("Dokodemo: Invalid destination. Discarding...")
  55. return errors.New("Dokodemo: Unable to get destination.")
  56. }
  57. ctx, cancel := context.WithCancel(ctx)
  58. defer cancel()
  59. timeout := time.Second * time.Duration(d.config.Timeout)
  60. if timeout == 0 {
  61. timeout = time.Minute * 2
  62. }
  63. timer := signal.CancelAfterInactivity(ctx, cancel, timeout)
  64. inboundRay, err := dispatcher.Dispatch(ctx, dest)
  65. if err != nil {
  66. return err
  67. }
  68. requestDone := signal.ExecuteAsync(func() error {
  69. defer inboundRay.InboundInput().Close()
  70. chunkReader := buf.NewReader(conn)
  71. if err := buf.PipeUntilEOF(timer, chunkReader, inboundRay.InboundInput()); err != nil {
  72. log.Info("Dokodemo: Failed to transport request: ", err)
  73. return err
  74. }
  75. return nil
  76. })
  77. responseDone := signal.ExecuteAsync(func() error {
  78. v2writer := buf.NewWriter(conn)
  79. if err := buf.PipeUntilEOF(timer, inboundRay.InboundOutput(), v2writer); err != nil {
  80. log.Info("Dokodemo: Failed to transport response: ", err)
  81. return err
  82. }
  83. return nil
  84. })
  85. if err := signal.ErrorOrFinish2(ctx, requestDone, responseDone); err != nil {
  86. inboundRay.InboundInput().CloseError()
  87. inboundRay.InboundOutput().CloseError()
  88. log.Info("Dokodemo: Connection ends with ", err)
  89. return err
  90. }
  91. runtime.KeepAlive(timer)
  92. return nil
  93. }
  94. func init() {
  95. common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
  96. return New(ctx, config.(*Config))
  97. }))
  98. }