dokodemo.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. "time"
  6. "v2ray.com/core"
  7. "v2ray.com/core/common"
  8. "v2ray.com/core/common/buf"
  9. "v2ray.com/core/common/functions"
  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. "v2ray.com/core/transport/pipe"
  16. )
  17. type DokodemoDoor struct {
  18. policyManager core.PolicyManager
  19. config *Config
  20. address net.Address
  21. port net.Port
  22. }
  23. func New(ctx context.Context, config *Config) (*DokodemoDoor, error) {
  24. if config.NetworkList == nil || config.NetworkList.Size() == 0 {
  25. return nil, newError("no network specified")
  26. }
  27. v := core.MustFromContext(ctx)
  28. d := &DokodemoDoor{
  29. config: config,
  30. address: config.GetPredefinedAddress(),
  31. port: net.Port(config.Port),
  32. policyManager: v.PolicyManager(),
  33. }
  34. return d, nil
  35. }
  36. func (d *DokodemoDoor) Network() net.NetworkList {
  37. return *(d.config.NetworkList)
  38. }
  39. func (d *DokodemoDoor) policy() core.Policy {
  40. config := d.config
  41. p := d.policyManager.ForLevel(config.UserLevel)
  42. if config.Timeout > 0 && config.UserLevel == 0 {
  43. p.Timeouts.ConnectionIdle = time.Duration(config.Timeout) * time.Second
  44. }
  45. return p
  46. }
  47. func (d *DokodemoDoor) Process(ctx context.Context, network net.Network, conn internet.Connection, dispatcher core.Dispatcher) error {
  48. newError("processing connection from: ", conn.RemoteAddr()).AtDebug().WithContext(ctx).WriteToLog()
  49. dest := net.Destination{
  50. Network: network,
  51. Address: d.address,
  52. Port: d.port,
  53. }
  54. if d.config.FollowRedirect {
  55. if origDest, ok := proxy.OriginalTargetFromContext(ctx); ok {
  56. dest = origDest
  57. }
  58. }
  59. if !dest.IsValid() || dest.Address == nil {
  60. return newError("unable to get destination")
  61. }
  62. plcy := d.policy()
  63. ctx, cancel := context.WithCancel(ctx)
  64. timer := signal.CancelAfterInactivity(ctx, cancel, plcy.Timeouts.ConnectionIdle)
  65. ctx = core.ContextWithBufferPolicy(ctx, plcy.Buffer)
  66. link, err := dispatcher.Dispatch(ctx, dest)
  67. if err != nil {
  68. return newError("failed to dispatch request").Base(err)
  69. }
  70. requestDone := func() error {
  71. defer timer.SetTimeout(plcy.Timeouts.DownlinkOnly)
  72. chunkReader := buf.NewReader(conn)
  73. if err := buf.Copy(chunkReader, link.Writer, buf.UpdateActivity(timer)); err != nil {
  74. return newError("failed to transport request").Base(err)
  75. }
  76. return nil
  77. }
  78. responseDone := func() error {
  79. defer timer.SetTimeout(plcy.Timeouts.UplinkOnly)
  80. var writer buf.Writer
  81. if network == net.Network_TCP {
  82. writer = buf.NewWriter(conn)
  83. } else {
  84. //if we are in TPROXY mode, use linux's udp forging functionality
  85. if !d.config.FollowRedirect {
  86. writer = buf.NewSequentialWriter(conn)
  87. } else {
  88. srca := net.UDPAddr{IP: dest.Address.IP(), Port: int(dest.Port.Value())}
  89. origsend, err := udp.TransmitSocket(&srca, conn.RemoteAddr())
  90. if err != nil {
  91. return err
  92. }
  93. writer = buf.NewSequentialWriter(origsend)
  94. }
  95. }
  96. if err := buf.Copy(link.Reader, writer, buf.UpdateActivity(timer)); err != nil {
  97. return newError("failed to transport response").Base(err)
  98. }
  99. return nil
  100. }
  101. if err := signal.ExecuteParallel(ctx, functions.OnSuccess(requestDone, functions.Close(link.Writer)), responseDone); err != nil {
  102. pipe.CloseError(link.Reader)
  103. pipe.CloseError(link.Writer)
  104. return newError("connection ends").Base(err)
  105. }
  106. return nil
  107. }
  108. func init() {
  109. common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
  110. return New(ctx, config.(*Config))
  111. }))
  112. }