loopback.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package loopback
  2. import (
  3. "context"
  4. "github.com/xtls/xray-core/common"
  5. "github.com/xtls/xray-core/common/buf"
  6. "github.com/xtls/xray-core/common/errors"
  7. "github.com/xtls/xray-core/common/net"
  8. "github.com/xtls/xray-core/common/net/cnc"
  9. "github.com/xtls/xray-core/common/retry"
  10. "github.com/xtls/xray-core/common/session"
  11. "github.com/xtls/xray-core/common/task"
  12. "github.com/xtls/xray-core/core"
  13. "github.com/xtls/xray-core/features/routing"
  14. "github.com/xtls/xray-core/transport"
  15. "github.com/xtls/xray-core/transport/internet"
  16. )
  17. type Loopback struct {
  18. config *Config
  19. dispatcherInstance routing.Dispatcher
  20. }
  21. func (l *Loopback) Process(ctx context.Context, link *transport.Link, _ internet.Dialer) error {
  22. outbounds := session.OutboundsFromContext(ctx)
  23. ob := outbounds[len(outbounds)-1]
  24. if !ob.Target.IsValid() {
  25. return errors.New("target not specified.")
  26. }
  27. ob.Name = "loopback"
  28. destination := ob.Target
  29. errors.LogInfo(ctx, "opening connection to ", destination)
  30. input := link.Reader
  31. output := link.Writer
  32. var conn net.Conn
  33. err := retry.ExponentialBackoff(2, 100).On(func() error {
  34. dialDest := destination
  35. content := new(session.Content)
  36. content.SkipDNSResolve = true
  37. ctx = session.ContextWithContent(ctx, content)
  38. inbound := session.InboundFromContext(ctx)
  39. inbound.Tag = l.config.InboundTag
  40. ctx = session.ContextWithInbound(ctx, inbound)
  41. rawConn, err := l.dispatcherInstance.Dispatch(ctx, dialDest)
  42. if err != nil {
  43. return err
  44. }
  45. var readerOpt cnc.ConnectionOption
  46. if dialDest.Network == net.Network_TCP {
  47. readerOpt = cnc.ConnectionOutputMulti(rawConn.Reader)
  48. } else {
  49. readerOpt = cnc.ConnectionOutputMultiUDP(rawConn.Reader)
  50. }
  51. conn = cnc.NewConnection(cnc.ConnectionInputMulti(rawConn.Writer), readerOpt)
  52. return nil
  53. })
  54. if err != nil {
  55. return errors.New("failed to open connection to ", destination).Base(err)
  56. }
  57. defer conn.Close()
  58. requestDone := func() error {
  59. var writer buf.Writer
  60. if destination.Network == net.Network_TCP {
  61. writer = buf.NewWriter(conn)
  62. } else {
  63. writer = &buf.SequentialWriter{Writer: conn}
  64. }
  65. if err := buf.Copy(input, writer); err != nil {
  66. return errors.New("failed to process request").Base(err)
  67. }
  68. return nil
  69. }
  70. responseDone := func() error {
  71. var reader buf.Reader
  72. if destination.Network == net.Network_TCP {
  73. reader = buf.NewReader(conn)
  74. } else {
  75. reader = buf.NewPacketReader(conn)
  76. }
  77. if err := buf.Copy(reader, output); err != nil {
  78. return errors.New("failed to process response").Base(err)
  79. }
  80. return nil
  81. }
  82. if err := task.Run(ctx, requestDone, task.OnSuccess(responseDone, task.Close(output))); err != nil {
  83. return errors.New("connection ends").Base(err)
  84. }
  85. return nil
  86. }
  87. func (l *Loopback) init(config *Config, dispatcherInstance routing.Dispatcher) error {
  88. l.dispatcherInstance = dispatcherInstance
  89. l.config = config
  90. return nil
  91. }
  92. func init() {
  93. common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
  94. l := new(Loopback)
  95. err := core.RequireFeatures(ctx, func(dispatcherInstance routing.Dispatcher) error {
  96. return l.init(config.(*Config), dispatcherInstance)
  97. })
  98. return l, err
  99. }))
  100. }