session.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. // Gateway address.
  35. Gateway net.Destination
  36. // Tag of the inbound proxy that handles the connection.
  37. Tag string
  38. // Name of the inbound proxy that handles the connection.
  39. Name string
  40. // User is the user that authenticates for the inbound. May be nil if the protocol allows anonymous traffic.
  41. User *protocol.MemoryUser
  42. // Conn is actually internet.Connection. May be nil.
  43. Conn net.Conn
  44. // Timer of the inbound buf copier. May be nil.
  45. Timer *signal.ActivityTimer
  46. // CanSpliceCopy is a property for this connection
  47. // 1 = can, 2 = after processing protocol info should be able to, 3 = cannot
  48. CanSpliceCopy int
  49. }
  50. // Outbound is the metadata of an outbound connection.
  51. type Outbound struct {
  52. // Target address of the outbound connection.
  53. OriginalTarget net.Destination
  54. Target net.Destination
  55. RouteTarget net.Destination
  56. // Gateway address
  57. Gateway net.Address
  58. // Tag of the outbound proxy that handles the connection.
  59. Tag string
  60. // Name of the outbound proxy that handles the connection.
  61. Name string
  62. // Conn is actually internet.Connection. May be nil. It is currently nil for outbound with proxySettings
  63. Conn net.Conn
  64. // CanSpliceCopy is a property for this connection
  65. // 1 = can, 2 = after processing protocol info should be able to, 3 = cannot
  66. CanSpliceCopy int
  67. }
  68. // SniffingRequest controls the behavior of content sniffing.
  69. type SniffingRequest struct {
  70. ExcludeForDomain []string // read-only once set
  71. OverrideDestinationForProtocol []string // read-only once set
  72. Enabled bool
  73. MetadataOnly bool
  74. RouteOnly bool
  75. }
  76. // Content is the metadata of the connection content.
  77. type Content struct {
  78. // Protocol of current content.
  79. Protocol string
  80. SniffingRequest SniffingRequest
  81. Attributes map[string]string
  82. SkipDNSResolve bool
  83. }
  84. // Sockopt is the settings for socket connection.
  85. type Sockopt struct {
  86. // Mark of the socket connection.
  87. Mark int32
  88. }
  89. // SetAttribute attaches additional string attributes to content.
  90. func (c *Content) SetAttribute(name string, value string) {
  91. if c.Attributes == nil {
  92. c.Attributes = make(map[string]string)
  93. }
  94. c.Attributes[name] = value
  95. }
  96. // Attribute retrieves additional string attributes from content.
  97. func (c *Content) Attribute(name string) string {
  98. if c.Attributes == nil {
  99. return ""
  100. }
  101. return c.Attributes[name]
  102. }