session.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Package session provides functions for sessions of incoming requests.
  2. package session
  3. import (
  4. "context"
  5. "math/rand"
  6. "github.com/v2fly/v2ray-core/v4/common/errors"
  7. "github.com/v2fly/v2ray-core/v4/common/net"
  8. "github.com/v2fly/v2ray-core/v4/common/protocol"
  9. )
  10. // ID of a session.
  11. type ID uint32
  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() ID {
  15. for {
  16. id := 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 := 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. // User is the user that authencates for the inbound. May be nil if the protocol allows anounymous traffic.
  39. User *protocol.MemoryUser
  40. }
  41. // Outbound is the metadata of an outbound connection.
  42. type Outbound struct {
  43. // Target address of the outbound connection.
  44. Target net.Destination
  45. // Gateway address
  46. Gateway net.Address
  47. }
  48. // SniffingRequest controls the behavior of content sniffing.
  49. type SniffingRequest struct {
  50. OverrideDestinationForProtocol []string
  51. Enabled bool
  52. MetadataOnly bool
  53. }
  54. // Content is the metadata of the connection content.
  55. type Content struct {
  56. // Protocol of current content.
  57. Protocol string
  58. SniffingRequest SniffingRequest
  59. Attributes map[string]string
  60. SkipDNSResolve bool
  61. }
  62. // Sockopt is the settings for socket connection.
  63. type Sockopt struct {
  64. // Mark of the socket connection.
  65. Mark int32
  66. }
  67. // Handler is the handler setting for dispatching.
  68. type Handler struct {
  69. // Tag of outbound handler.
  70. Tag string
  71. }
  72. // SetAttribute attachs additional string attributes to content.
  73. func (c *Content) SetAttribute(name string, value string) {
  74. if c.Attributes == nil {
  75. c.Attributes = make(map[string]string)
  76. }
  77. c.Attributes[name] = value
  78. }
  79. // Attribute retrieves additional string attributes from content.
  80. func (c *Content) Attribute(name string) string {
  81. if c.Attributes == nil {
  82. return ""
  83. }
  84. return c.Attributes[name]
  85. }