session.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // Package session provides functions for sessions of incoming requests.
  2. package session // import "github.com/xtls/xray-core/common/session"
  3. import (
  4. "context"
  5. "math/rand"
  6. "sync"
  7. c "github.com/xtls/xray-core/common/ctx"
  8. "github.com/xtls/xray-core/common/errors"
  9. "github.com/xtls/xray-core/common/net"
  10. "github.com/xtls/xray-core/common/protocol"
  11. "github.com/xtls/xray-core/common/signal"
  12. )
  13. // NewID generates a new ID. The generated ID is high likely to be unique, but not cryptographically secure.
  14. // The generated ID will never be 0.
  15. func NewID() c.ID {
  16. for {
  17. id := c.ID(rand.Uint32())
  18. if id != 0 {
  19. return id
  20. }
  21. }
  22. }
  23. // ExportIDToError transfers session.ID into an error object, for logging purpose.
  24. // This can be used with error.WriteToLog().
  25. func ExportIDToError(ctx context.Context) errors.ExportOption {
  26. id := c.IDFromContext(ctx)
  27. return func(h *errors.ExportOptionHolder) {
  28. h.SessionID = uint32(id)
  29. }
  30. }
  31. // Inbound is the metadata of an inbound connection.
  32. type Inbound struct {
  33. // Source address of the inbound connection.
  34. Source net.Destination
  35. // Gateway address.
  36. Gateway net.Destination
  37. // Tag of the inbound proxy that handles the connection.
  38. Tag string
  39. // Name of the inbound proxy that handles the connection.
  40. Name string
  41. // User is the user that authenticates for the inbound. May be nil if the protocol allows anonymous traffic.
  42. User *protocol.MemoryUser
  43. // Conn is actually internet.Connection. May be nil.
  44. Conn net.Conn
  45. // Timer of the inbound buf copier. May be nil.
  46. Timer *signal.ActivityTimer
  47. // CanSpliceCopy is a property for this connection
  48. // 1 = can, 2 = after processing protocol info should be able to, 3 = cannot
  49. CanSpliceCopy int
  50. }
  51. // Outbound is the metadata of an outbound connection.
  52. type Outbound struct {
  53. // Target address of the outbound connection.
  54. OriginalTarget net.Destination
  55. Target net.Destination
  56. RouteTarget net.Destination
  57. // Gateway address
  58. Gateway net.Address
  59. // Tag of the outbound proxy that handles the connection.
  60. Tag string
  61. // Name of the outbound proxy that handles the connection.
  62. Name string
  63. // Conn is actually internet.Connection. May be nil. It is currently nil for outbound with proxySettings
  64. Conn net.Conn
  65. // CanSpliceCopy is a property for this connection
  66. // 1 = can, 2 = after processing protocol info should be able to, 3 = cannot
  67. CanSpliceCopy int
  68. }
  69. // SniffingRequest controls the behavior of content sniffing.
  70. type SniffingRequest struct {
  71. ExcludeForDomain []string
  72. OverrideDestinationForProtocol []string
  73. Enabled bool
  74. MetadataOnly bool
  75. RouteOnly bool
  76. }
  77. // Content is the metadata of the connection content.
  78. type Content struct {
  79. // Protocol of current content.
  80. Protocol string
  81. SniffingRequest SniffingRequest
  82. Attributes map[string]string
  83. SkipDNSResolve bool
  84. mu sync.Mutex
  85. isLocked bool
  86. }
  87. // Sockopt is the settings for socket connection.
  88. type Sockopt struct {
  89. // Mark of the socket connection.
  90. Mark int32
  91. }
  92. // Some how when using mux, there will be a same ctx between different requests
  93. // This will cause problem as it's designed for single request, like concurrent map writes
  94. // Add a Mutex as a temp solution
  95. // SetAttribute attaches additional string attributes to content.
  96. func (c *Content) SetAttribute(name string, value string) {
  97. if c.isLocked {
  98. errors.LogError(context.Background(), "Multiple goroutines are tring to access one routing content, tring to write ", name, ":", value)
  99. }
  100. c.mu.Lock()
  101. c.isLocked = true
  102. defer func() {
  103. c.isLocked = false
  104. c.mu.Unlock()
  105. }()
  106. if c.Attributes == nil {
  107. c.Attributes = make(map[string]string)
  108. }
  109. c.Attributes[name] = value
  110. }
  111. // Attribute retrieves additional string attributes from content.
  112. func (c *Content) Attribute(name string) string {
  113. c.mu.Lock()
  114. c.isLocked = true
  115. defer func() {
  116. c.isLocked = false
  117. c.mu.Unlock()
  118. }()
  119. if c.Attributes == nil {
  120. return ""
  121. }
  122. return c.Attributes[name]
  123. }
  124. func (c *Content) AttributeLen() int {
  125. c.mu.Lock()
  126. c.isLocked = true
  127. defer func() {
  128. c.isLocked = false
  129. c.mu.Unlock()
  130. }()
  131. return len(c.Attributes)
  132. }