handler.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package tun
  2. import (
  3. "context"
  4. "github.com/xtls/xray-core/common"
  5. "github.com/xtls/xray-core/common/buf"
  6. c "github.com/xtls/xray-core/common/ctx"
  7. "github.com/xtls/xray-core/common/errors"
  8. "github.com/xtls/xray-core/common/net"
  9. "github.com/xtls/xray-core/common/protocol"
  10. "github.com/xtls/xray-core/common/session"
  11. "github.com/xtls/xray-core/core"
  12. "github.com/xtls/xray-core/features/policy"
  13. "github.com/xtls/xray-core/features/routing"
  14. "github.com/xtls/xray-core/transport"
  15. "github.com/xtls/xray-core/transport/internet/stat"
  16. )
  17. // Handler is managing object that tie together tun interface, ip stack and dispatch connections to the routing
  18. type Handler struct {
  19. ctx context.Context
  20. config *Config
  21. stack Stack
  22. policyManager policy.Manager
  23. dispatcher routing.Dispatcher
  24. }
  25. // ConnectionHandler interface with the only method that stack is going to push new connections to
  26. type ConnectionHandler interface {
  27. HandleConnection(conn net.Conn, destination net.Destination)
  28. }
  29. // Handler implements ConnectionHandler
  30. var _ ConnectionHandler = (*Handler)(nil)
  31. func (t *Handler) policy() policy.Session {
  32. p := t.policyManager.ForLevel(t.config.UserLevel)
  33. return p
  34. }
  35. // Init the Handler instance with necessary parameters
  36. func (t *Handler) Init(ctx context.Context, pm policy.Manager, dispatcher routing.Dispatcher) error {
  37. var err error
  38. t.ctx = core.ToBackgroundDetachedContext(ctx)
  39. t.policyManager = pm
  40. t.dispatcher = dispatcher
  41. tunName := t.config.Name
  42. tunOptions := TunOptions{
  43. Name: tunName,
  44. MTU: t.config.MTU,
  45. }
  46. tunInterface, err := NewTun(tunOptions)
  47. if err != nil {
  48. return err
  49. }
  50. errors.LogInfo(t.ctx, tunName, " created")
  51. tunStackOptions := StackOptions{
  52. Tun: tunInterface,
  53. IdleTimeout: pm.ForLevel(t.config.UserLevel).Timeouts.ConnectionIdle,
  54. }
  55. tunStack, err := NewStack(t.ctx, tunStackOptions, t)
  56. if err != nil {
  57. _ = tunInterface.Close()
  58. return err
  59. }
  60. err = tunStack.Start()
  61. if err != nil {
  62. _ = tunStack.Close()
  63. _ = tunInterface.Close()
  64. return err
  65. }
  66. err = tunInterface.Start()
  67. if err != nil {
  68. _ = tunStack.Close()
  69. _ = tunInterface.Close()
  70. return err
  71. }
  72. t.stack = tunStack
  73. errors.LogInfo(t.ctx, tunName, " up")
  74. return nil
  75. }
  76. // HandleConnection pass the connection coming from the ip stack to the routing dispatcher
  77. func (t *Handler) HandleConnection(conn net.Conn, destination net.Destination) {
  78. sid := session.NewID()
  79. ctx := c.ContextWithID(t.ctx, sid)
  80. errors.LogInfo(ctx, "processing connection from: ", conn.RemoteAddr())
  81. inbound := session.Inbound{}
  82. inbound.Name = "tun"
  83. inbound.CanSpliceCopy = 1
  84. inbound.Source = net.DestinationFromAddr(conn.RemoteAddr())
  85. inbound.User = &protocol.MemoryUser{
  86. Level: t.config.UserLevel,
  87. }
  88. ctx = session.ContextWithInbound(ctx, &inbound)
  89. ctx = session.SubContextFromMuxInbound(ctx)
  90. link := &transport.Link{
  91. Reader: &buf.TimeoutWrapperReader{Reader: buf.NewReader(conn)},
  92. Writer: buf.NewWriter(conn),
  93. }
  94. if err := t.dispatcher.DispatchLink(ctx, destination, link); err != nil {
  95. errors.LogError(ctx, errors.New("connection closed").Base(err))
  96. return
  97. }
  98. errors.LogInfo(ctx, "connection completed")
  99. }
  100. // Network implements proxy.Inbound
  101. // and exists only to comply to proxy interface, declaring it doesn't listen on any network,
  102. // making the process not open any port for this inbound (input will be network interface)
  103. func (t *Handler) Network() []net.Network {
  104. return []net.Network{}
  105. }
  106. // Process implements proxy.Inbound
  107. // and exists only to comply to proxy interface, which should never get any inputs due to no listening ports
  108. func (t *Handler) Process(ctx context.Context, network net.Network, conn stat.Connection, dispatcher routing.Dispatcher) error {
  109. return nil
  110. }
  111. func init() {
  112. common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
  113. t := &Handler{config: config.(*Config)}
  114. err := core.RequireFeatures(ctx, func(pm policy.Manager, dispatcher routing.Dispatcher) error {
  115. return t.Init(ctx, pm, dispatcher)
  116. })
  117. return t, err
  118. }))
  119. }