session.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. 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/signal"
  11. )
  12. // NewID generates a new ID. The generated ID is high likely to be unique, but not cryptographically secure.
  13. // The generated ID will never be 0.
  14. func NewID() c.ID {
  15. for {
  16. id := c.ID(rand.Uint32())
  17. if id != 0 {
  18. return id
  19. }
  20. }
  21. }
  22. // ExportIDToError transfers session.ID into an error object, for logging purpose.
  23. // This can be used with error.WriteToLog().
  24. func ExportIDToError(ctx context.Context) errors.ExportOption {
  25. id := c.IDFromContext(ctx)
  26. return func(h *errors.ExportOptionHolder) {
  27. h.SessionID = uint32(id)
  28. }
  29. }
  30. // Inbound is the metadata of an inbound connection.
  31. type Inbound struct {
  32. // Source address of the inbound connection.
  33. Source net.Destination
  34. // Local address of the inbound connection.
  35. Local net.Destination
  36. // Gateway address.
  37. Gateway net.Destination
  38. // Tag of the inbound proxy that handles the connection.
  39. Tag string
  40. // Name of the inbound proxy that handles the connection.
  41. Name string
  42. // User is the user that authenticates for the inbound. May be nil if the protocol allows anonymous traffic.
  43. User *protocol.MemoryUser
  44. // VlessRoute is the user-sent VLESS UUID's last byte.
  45. VlessRoute net.Port
  46. // Used by splice copy. Conn is actually internet.Connection. May be nil.
  47. Conn net.Conn
  48. // Used by splice copy. Timer of the inbound buf copier. May be nil.
  49. Timer *signal.ActivityTimer
  50. // CanSpliceCopy is a property for this connection
  51. // 1 = can, 2 = after processing protocol info should be able to, 3 = cannot
  52. CanSpliceCopy int
  53. }
  54. // Outbound is the metadata of an outbound connection.
  55. type Outbound struct {
  56. // Target address of the outbound connection.
  57. OriginalTarget net.Destination
  58. Target net.Destination
  59. RouteTarget net.Destination
  60. // Gateway address
  61. Gateway net.Address
  62. // Tag of the outbound proxy that handles the connection.
  63. Tag string
  64. // Name of the outbound proxy that handles the connection.
  65. Name string
  66. // Unused. Conn is actually internet.Connection. May be nil. It is currently nil for outbound with proxySettings
  67. Conn net.Conn
  68. // CanSpliceCopy is a property for this connection
  69. // 1 = can, 2 = after processing protocol info should be able to, 3 = cannot
  70. CanSpliceCopy int
  71. }
  72. // SniffingRequest controls the behavior of content sniffing. They are from inbound config. Read-only
  73. type SniffingRequest struct {
  74. ExcludeForDomain []string
  75. OverrideDestinationForProtocol []string
  76. Enabled bool
  77. MetadataOnly bool
  78. RouteOnly bool
  79. }
  80. // Content is the metadata of the connection content. Mainly used for routing.
  81. type Content struct {
  82. // Protocol of current content.
  83. Protocol string
  84. SniffingRequest SniffingRequest
  85. // HTTP traffic sniffed headers
  86. Attributes map[string]string
  87. // SkipDNSResolve is set from DNS module. the DOH remote server maybe a domain name, this prevents cycle resolving dead loop
  88. SkipDNSResolve bool
  89. }
  90. // Sockopt is the settings for socket connection.
  91. type Sockopt struct {
  92. // Mark of the socket connection.
  93. Mark int32
  94. }
  95. // SetAttribute attaches additional string attributes to content.
  96. func (c *Content) SetAttribute(name string, value string) {
  97. if c.Attributes == nil {
  98. c.Attributes = make(map[string]string)
  99. }
  100. c.Attributes[name] = value
  101. }
  102. // Attribute retrieves additional string attributes from content.
  103. func (c *Content) Attribute(name string) string {
  104. if c.Attributes == nil {
  105. return ""
  106. }
  107. return c.Attributes[name]
  108. }