dokodemo.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package dokodemo
  2. //go:generate go run $GOPATH/src/v2ray.com/core/tools/generrorgen/main.go -pkg dokodemo -path Proxy,Dokodemo
  3. import (
  4. "context"
  5. "runtime"
  6. "time"
  7. "v2ray.com/core/app"
  8. "v2ray.com/core/app/dispatcher"
  9. "v2ray.com/core/app/log"
  10. "v2ray.com/core/common"
  11. "v2ray.com/core/common/buf"
  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, newError("no space in context")
  26. }
  27. if config.NetworkList == nil || config.NetworkList.Size() == 0 {
  28. return nil, newError("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(newError("processing connection from: ", conn.RemoteAddr()).AtDebug())
  42. dest := net.Destination{
  43. Network: network,
  44. Address: d.address,
  45. Port: d.port,
  46. }
  47. if d.config.FollowRedirect {
  48. if origDest, ok := proxy.OriginalTargetFromContext(ctx); ok {
  49. dest = origDest
  50. }
  51. }
  52. if !dest.IsValid() || dest.Address == nil {
  53. return newError("unable to get destination")
  54. }
  55. timeout := time.Second * time.Duration(d.config.Timeout)
  56. if timeout == 0 {
  57. timeout = time.Minute * 2
  58. }
  59. ctx, timer := signal.CancelAfterInactivity(ctx, timeout)
  60. inboundRay, err := dispatcher.Dispatch(ctx, dest)
  61. if err != nil {
  62. return err
  63. }
  64. requestDone := signal.ExecuteAsync(func() error {
  65. defer inboundRay.InboundInput().Close()
  66. chunkReader := buf.NewReader(conn)
  67. if err := buf.PipeUntilEOF(timer, chunkReader, inboundRay.InboundInput()); err != nil {
  68. return newError("failed to transport request").Base(err)
  69. }
  70. return nil
  71. })
  72. responseDone := signal.ExecuteAsync(func() error {
  73. v2writer := buf.NewWriter(conn)
  74. if err := buf.PipeUntilEOF(timer, inboundRay.InboundOutput(), v2writer); err != nil {
  75. return newError("failed to transport response").Base(err)
  76. }
  77. return nil
  78. })
  79. if err := signal.ErrorOrFinish2(ctx, requestDone, responseDone); err != nil {
  80. inboundRay.InboundInput().CloseError()
  81. inboundRay.InboundOutput().CloseError()
  82. return newError("connection ends").Base(err)
  83. }
  84. runtime.KeepAlive(timer)
  85. return nil
  86. }
  87. func init() {
  88. common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
  89. return New(ctx, config.(*Config))
  90. }))
  91. }