dokodemo.go 3.7 KB

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