session.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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/matcher/domain"
  8. "github.com/xtls/xray-core/common/matcher/geoip"
  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. // ID of a session.
  14. type ID uint32
  15. // NewID generates a new ID. The generated ID is high likely to be unique, but not cryptographically secure.
  16. // The generated ID will never be 0.
  17. func NewID() ID {
  18. for {
  19. id := ID(rand.Uint32())
  20. if id != 0 {
  21. return id
  22. }
  23. }
  24. }
  25. // ExportIDToError transfers session.ID into an error object, for logging purpose.
  26. // This can be used with error.WriteToLog().
  27. func ExportIDToError(ctx context.Context) errors.ExportOption {
  28. id := IDFromContext(ctx)
  29. return func(h *errors.ExportOptionHolder) {
  30. h.SessionID = uint32(id)
  31. }
  32. }
  33. // Inbound is the metadata of an inbound connection.
  34. type Inbound struct {
  35. // Source address of the inbound connection.
  36. Source net.Destination
  37. // Getaway address.
  38. Gateway net.Destination
  39. // Tag of the inbound proxy that handles the connection.
  40. Tag 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. // Gateway address
  53. Gateway net.Address
  54. }
  55. // SniffingRequest controls the behavior of content sniffing.
  56. type SniffingRequest struct {
  57. ExcludedDomainMatcher *domain.DomainMatcher
  58. ExcludedIPMatcher *geoip.MultiGeoIPMatcher
  59. OverrideDestinationForProtocol []string
  60. Enabled bool
  61. MetadataOnly bool
  62. }
  63. // Content is the metadata of the connection content.
  64. type Content struct {
  65. // Protocol of current content.
  66. Protocol string
  67. SniffingRequest SniffingRequest
  68. Attributes map[string]string
  69. SkipDNSResolve bool
  70. }
  71. // Sockopt is the settings for socket connection.
  72. type Sockopt struct {
  73. // Mark of the socket connection.
  74. Mark int32
  75. }
  76. // SetAttribute attachs additional string attributes to content.
  77. func (c *Content) SetAttribute(name string, value string) {
  78. if c.Attributes == nil {
  79. c.Attributes = make(map[string]string)
  80. }
  81. c.Attributes[name] = value
  82. }
  83. // Attribute retrieves additional string attributes from content.
  84. func (c *Content) Attribute(name string) string {
  85. if c.Attributes == nil {
  86. return ""
  87. }
  88. return c.Attributes[name]
  89. }