dokodemo.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package dokodemo
  2. //go:generate errorgen
  3. import (
  4. "context"
  5. "time"
  6. "v2ray.com/core"
  7. "v2ray.com/core/common"
  8. "v2ray.com/core/common/buf"
  9. "v2ray.com/core/common/net"
  10. "v2ray.com/core/common/session"
  11. "v2ray.com/core/common/signal"
  12. "v2ray.com/core/common/task"
  13. "v2ray.com/core/features/policy"
  14. "v2ray.com/core/features/routing"
  15. "v2ray.com/core/transport/internet"
  16. "v2ray.com/core/transport/pipe"
  17. )
  18. func init() {
  19. common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
  20. d := new(DokodemoDoor)
  21. err := core.RequireFeatures(ctx, func(pm policy.Manager) error {
  22. return d.Init(config.(*Config), pm)
  23. })
  24. return d, err
  25. }))
  26. }
  27. type DokodemoDoor struct {
  28. policyManager policy.Manager
  29. config *Config
  30. address net.Address
  31. port net.Port
  32. }
  33. // Init initializes the DokodemoDoor instance with necessary parameters.
  34. func (d *DokodemoDoor) Init(config *Config, pm policy.Manager) error {
  35. if config.NetworkList == nil || config.NetworkList.Size() == 0 {
  36. return newError("no network specified")
  37. }
  38. d.config = config
  39. d.address = config.GetPredefinedAddress()
  40. d.port = net.Port(config.Port)
  41. d.policyManager = pm
  42. return nil
  43. }
  44. func (d *DokodemoDoor) Network() net.NetworkList {
  45. return *(d.config.NetworkList)
  46. }
  47. func (d *DokodemoDoor) policy() policy.Session {
  48. config := d.config
  49. p := d.policyManager.ForLevel(config.UserLevel)
  50. if config.Timeout > 0 && config.UserLevel == 0 {
  51. p.Timeouts.ConnectionIdle = time.Duration(config.Timeout) * time.Second
  52. }
  53. return p
  54. }
  55. type hasHandshakeAddress interface {
  56. HandshakeAddress() net.Address
  57. }
  58. func (d *DokodemoDoor) Process(ctx context.Context, network net.Network, conn internet.Connection, dispatcher routing.Dispatcher) error {
  59. newError("processing connection from: ", conn.RemoteAddr()).AtDebug().WriteToLog(session.ExportIDToError(ctx))
  60. dest := net.Destination{
  61. Network: network,
  62. Address: d.address,
  63. Port: d.port,
  64. }
  65. if d.config.FollowRedirect {
  66. if outbound := session.OutboundFromContext(ctx); outbound != nil && outbound.Target.IsValid() {
  67. dest = outbound.Target
  68. } else if handshake, ok := conn.(hasHandshakeAddress); ok {
  69. addr := handshake.HandshakeAddress()
  70. if addr != nil {
  71. dest.Address = addr
  72. }
  73. }
  74. }
  75. if !dest.IsValid() || dest.Address == nil {
  76. return newError("unable to get destination")
  77. }
  78. plcy := d.policy()
  79. ctx, cancel := context.WithCancel(ctx)
  80. timer := signal.CancelAfterInactivity(ctx, cancel, plcy.Timeouts.ConnectionIdle)
  81. ctx = policy.ContextWithBufferPolicy(ctx, plcy.Buffer)
  82. link, err := dispatcher.Dispatch(ctx, dest)
  83. if err != nil {
  84. return newError("failed to dispatch request").Base(err)
  85. }
  86. requestDone := func() error {
  87. defer timer.SetTimeout(plcy.Timeouts.DownlinkOnly)
  88. reader := buf.NewReader(conn)
  89. if err := buf.Copy(reader, link.Writer, buf.UpdateActivity(timer)); err != nil {
  90. return newError("failed to transport request").Base(err)
  91. }
  92. return nil
  93. }
  94. responseDone := func() error {
  95. defer timer.SetTimeout(plcy.Timeouts.UplinkOnly)
  96. var writer buf.Writer
  97. if network == net.Network_TCP {
  98. writer = buf.NewWriter(conn)
  99. } else {
  100. //if we are in TPROXY mode, use linux's udp forging functionality
  101. if !d.config.FollowRedirect {
  102. writer = &buf.SequentialWriter{Writer: conn}
  103. } else {
  104. tCtx := internet.ContextWithBindAddress(context.Background(), dest)
  105. tCtx = internet.ContextWithStreamSettings(tCtx, &internet.MemoryStreamConfig{
  106. ProtocolName: "udp",
  107. SocketSettings: &internet.SocketConfig{
  108. Tproxy: internet.SocketConfig_TProxy,
  109. },
  110. })
  111. tConn, err := internet.DialSystem(tCtx, net.DestinationFromAddr(conn.RemoteAddr()))
  112. if err != nil {
  113. return err
  114. }
  115. writer = &buf.SequentialWriter{Writer: tConn}
  116. }
  117. }
  118. if err := buf.Copy(link.Reader, writer, buf.UpdateActivity(timer)); err != nil {
  119. return newError("failed to transport response").Base(err)
  120. }
  121. return nil
  122. }
  123. if err := task.Run(task.WithContext(ctx),
  124. task.Parallel(
  125. task.Single(requestDone, task.OnSuccess(task.Close(link.Writer))),
  126. responseDone))(); err != nil {
  127. pipe.CloseError(link.Reader)
  128. pipe.CloseError(link.Writer)
  129. return newError("connection ends").Base(err)
  130. }
  131. return nil
  132. }