dokodemo.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. "v2ray.com/core/transport/internet/udp"
  17. )
  18. type DokodemoDoor struct {
  19. config *Config
  20. address net.Address
  21. port net.Port
  22. }
  23. func New(ctx context.Context, config *Config) (*DokodemoDoor, error) {
  24. space := app.SpaceFromContext(ctx)
  25. if space == nil {
  26. return nil, newError("no space in context")
  27. }
  28. if config.NetworkList == nil || config.NetworkList.Size() == 0 {
  29. return nil, newError("no network specified")
  30. }
  31. d := &DokodemoDoor{
  32. config: config,
  33. address: config.GetPredefinedAddress(),
  34. port: net.Port(config.Port),
  35. }
  36. return d, nil
  37. }
  38. func (d *DokodemoDoor) Network() net.NetworkList {
  39. return *(d.config.NetworkList)
  40. }
  41. func (d *DokodemoDoor) Process(ctx context.Context, network net.Network, conn internet.Connection, dispatcher dispatcher.Interface) error {
  42. log.Trace(newError("processing connection from: ", conn.RemoteAddr()).AtDebug())
  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 newError("unable to get destination")
  55. }
  56. timeout := time.Second * time.Duration(d.config.Timeout)
  57. if timeout == 0 {
  58. timeout = time.Minute * 5
  59. }
  60. ctx, timer := signal.CancelAfterInactivity(ctx, timeout)
  61. inboundRay, err := dispatcher.Dispatch(ctx, dest)
  62. if err != nil {
  63. return newError("failed to dispatch request").Base(err)
  64. }
  65. requestDone := signal.ExecuteAsync(func() error {
  66. defer inboundRay.InboundInput().Close()
  67. chunkReader := buf.NewReader(conn)
  68. if err := buf.Copy(chunkReader, inboundRay.InboundInput(), buf.UpdateActivity(timer)); err != nil {
  69. return newError("failed to transport request").Base(err)
  70. }
  71. return nil
  72. })
  73. responseDone := signal.ExecuteAsync(func() error {
  74. var writer buf.Writer
  75. if network == net.Network_TCP {
  76. writer = buf.NewWriter(conn)
  77. } else {
  78. //if we are in TPROXY mode, use linux's udp forging functionality
  79. if !d.config.FollowRedirect {
  80. writer = buf.NewSequentialWriter(conn)
  81. } else {
  82. srca := net.UDPAddr{IP: dest.Address.IP(), Port: int(dest.Port.Value())}
  83. origsend, err := udp.TransmitSocket(&srca, conn.RemoteAddr())
  84. if err != nil {
  85. return err
  86. }
  87. writer = buf.NewSequentialWriter(origsend)
  88. }
  89. }
  90. if err := buf.Copy(inboundRay.InboundOutput(), writer, buf.UpdateActivity(timer)); err != nil {
  91. return newError("failed to transport response").Base(err)
  92. }
  93. timer.SetTimeout(time.Second * 2)
  94. return nil
  95. })
  96. if err := signal.ErrorOrFinish2(ctx, requestDone, responseDone); err != nil {
  97. inboundRay.InboundInput().CloseError()
  98. inboundRay.InboundOutput().CloseError()
  99. return newError("connection ends").Base(err)
  100. }
  101. runtime.KeepAlive(timer)
  102. return nil
  103. }
  104. func init() {
  105. common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
  106. return New(ctx, config.(*Config))
  107. }))
  108. }