dokodemo.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. timeout := time.Second * time.Duration(d.config.Timeout)
  58. if timeout == 0 {
  59. timeout = time.Minute * 2
  60. }
  61. ctx, timer := signal.CancelAfterInactivity(ctx, timeout)
  62. inboundRay, err := dispatcher.Dispatch(ctx, dest)
  63. if err != nil {
  64. return err
  65. }
  66. requestDone := signal.ExecuteAsync(func() error {
  67. defer inboundRay.InboundInput().Close()
  68. chunkReader := buf.NewReader(conn)
  69. if err := buf.PipeUntilEOF(timer, chunkReader, inboundRay.InboundInput()); err != nil {
  70. log.Info("Dokodemo: Failed to transport request: ", err)
  71. return err
  72. }
  73. return nil
  74. })
  75. responseDone := signal.ExecuteAsync(func() error {
  76. v2writer := buf.NewWriter(conn)
  77. if err := buf.PipeUntilEOF(timer, inboundRay.InboundOutput(), v2writer); err != nil {
  78. log.Info("Dokodemo: Failed to transport response: ", err)
  79. return err
  80. }
  81. return nil
  82. })
  83. if err := signal.ErrorOrFinish2(ctx, requestDone, responseDone); err != nil {
  84. inboundRay.InboundInput().CloseError()
  85. inboundRay.InboundOutput().CloseError()
  86. log.Info("Dokodemo: Connection ends with ", err)
  87. return err
  88. }
  89. runtime.KeepAlive(timer)
  90. return nil
  91. }
  92. func init() {
  93. common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
  94. return New(ctx, config.(*Config))
  95. }))
  96. }