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