dokodemo.go 3.6 KB

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