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