dokodemo.go 3.5 KB

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