session.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. "github.com/xtls/xray-core/common/errors"
  7. "github.com/xtls/xray-core/common/net"
  8. "github.com/xtls/xray-core/common/protocol"
  9. "github.com/xtls/xray-core/common/signal"
  10. )
  11. // ID of a session.
  12. type ID uint32
  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() ID {
  16. for {
  17. id := 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 := 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 authencates for the inbound. May be nil if the protocol allows anounymous 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. }
  48. // Outbound is the metadata of an outbound connection.
  49. type Outbound struct {
  50. // Target address of the outbound connection.
  51. Target net.Destination
  52. RouteTarget net.Destination
  53. // Gateway address
  54. Gateway net.Address
  55. }
  56. // SniffingRequest controls the behavior of content sniffing.
  57. type SniffingRequest struct {
  58. ExcludeForDomain []string
  59. OverrideDestinationForProtocol []string
  60. Enabled bool
  61. MetadataOnly bool
  62. RouteOnly bool
  63. }
  64. // Content is the metadata of the connection content.
  65. type Content struct {
  66. // Protocol of current content.
  67. Protocol string
  68. SniffingRequest SniffingRequest
  69. Attributes map[string]string
  70. SkipDNSResolve bool
  71. }
  72. // Sockopt is the settings for socket connection.
  73. type Sockopt struct {
  74. // Mark of the socket connection.
  75. Mark int32
  76. }
  77. // SetAttribute attaches additional string attributes to content.
  78. func (c *Content) SetAttribute(name string, value string) {
  79. if c.Attributes == nil {
  80. c.Attributes = make(map[string]string)
  81. }
  82. c.Attributes[name] = value
  83. }
  84. // Attribute retrieves additional string attributes from content.
  85. func (c *Content) Attribute(name string) string {
  86. if c.Attributes == nil {
  87. return ""
  88. }
  89. return c.Attributes[name]
  90. }