dokodemo.go 4.1 KB

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