dokodemo.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package dokodemo
  2. //go:generate go run $GOPATH/src/v2ray.com/core/common/errors/errorgen/main.go -pkg dokodemo -path Proxy,Dokodemo
  3. import (
  4. "context"
  5. "v2ray.com/core/app"
  6. "v2ray.com/core/app/dispatcher"
  7. "v2ray.com/core/app/log"
  8. "v2ray.com/core/app/policy"
  9. "v2ray.com/core/common"
  10. "v2ray.com/core/common/buf"
  11. "v2ray.com/core/common/net"
  12. "v2ray.com/core/common/signal"
  13. "v2ray.com/core/proxy"
  14. "v2ray.com/core/transport/internet"
  15. "v2ray.com/core/transport/internet/udp"
  16. )
  17. type DokodemoDoor struct {
  18. config *Config
  19. address net.Address
  20. port net.Port
  21. policy policy.Policy
  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. space.On(app.SpaceInitializing, func(interface{}) error {
  37. pm := policy.FromSpace(space)
  38. if pm == nil {
  39. return newError("Policy not found in space.")
  40. }
  41. d.policy = pm.GetPolicy(config.UserLevel)
  42. if config.Timeout > 0 && config.UserLevel == 0 {
  43. d.policy.Timeout.ConnectionIdle.Value = config.Timeout
  44. }
  45. return nil
  46. })
  47. return d, nil
  48. }
  49. func (d *DokodemoDoor) Network() net.NetworkList {
  50. return *(d.config.NetworkList)
  51. }
  52. func (d *DokodemoDoor) Process(ctx context.Context, network net.Network, conn internet.Connection, dispatcher dispatcher.Interface) error {
  53. log.Trace(newError("processing connection from: ", conn.RemoteAddr()).AtDebug())
  54. dest := net.Destination{
  55. Network: network,
  56. Address: d.address,
  57. Port: d.port,
  58. }
  59. if d.config.FollowRedirect {
  60. if origDest, ok := proxy.OriginalTargetFromContext(ctx); ok {
  61. dest = origDest
  62. }
  63. }
  64. if !dest.IsValid() || dest.Address == nil {
  65. return newError("unable to get destination")
  66. }
  67. ctx, cancel := context.WithCancel(ctx)
  68. timer := signal.CancelAfterInactivity(ctx, cancel, d.policy.Timeout.ConnectionIdle.Duration())
  69. inboundRay, err := dispatcher.Dispatch(ctx, dest)
  70. if err != nil {
  71. return newError("failed to dispatch request").Base(err)
  72. }
  73. requestDone := signal.ExecuteAsync(func() error {
  74. defer inboundRay.InboundInput().Close()
  75. chunkReader := buf.NewReader(conn)
  76. if err := buf.Copy(chunkReader, inboundRay.InboundInput(), buf.UpdateActivity(timer)); err != nil {
  77. return newError("failed to transport request").Base(err)
  78. }
  79. timer.SetTimeout(d.policy.Timeout.DownlinkOnly.Duration())
  80. return nil
  81. })
  82. responseDone := signal.ExecuteAsync(func() error {
  83. var writer buf.Writer
  84. if network == net.Network_TCP {
  85. writer = buf.NewWriter(conn)
  86. } else {
  87. //if we are in TPROXY mode, use linux's udp forging functionality
  88. if !d.config.FollowRedirect {
  89. writer = buf.NewSequentialWriter(conn)
  90. } else {
  91. srca := net.UDPAddr{IP: dest.Address.IP(), Port: int(dest.Port.Value())}
  92. origsend, err := udp.TransmitSocket(&srca, conn.RemoteAddr())
  93. if err != nil {
  94. return err
  95. }
  96. writer = buf.NewSequentialWriter(origsend)
  97. }
  98. }
  99. if err := buf.Copy(inboundRay.InboundOutput(), writer, buf.UpdateActivity(timer)); err != nil {
  100. return newError("failed to transport response").Base(err)
  101. }
  102. timer.SetTimeout(d.policy.Timeout.UplinkOnly.Duration())
  103. return nil
  104. })
  105. if err := signal.ErrorOrFinish2(ctx, requestDone, responseDone); err != nil {
  106. inboundRay.InboundInput().CloseError()
  107. inboundRay.InboundOutput().CloseError()
  108. return newError("connection ends").Base(err)
  109. }
  110. return nil
  111. }
  112. func init() {
  113. common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
  114. return New(ctx, config.(*Config))
  115. }))
  116. }