dokodemo.go 2.8 KB

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