github.com/v2fly/v2ray-core/v4@v4.45.2/common/session/session.go (about) 1 // Package session provides functions for sessions of incoming requests. 2 package session 3 4 import ( 5 "context" 6 "math/rand" 7 8 "github.com/v2fly/v2ray-core/v4/common/errors" 9 "github.com/v2fly/v2ray-core/v4/common/net" 10 "github.com/v2fly/v2ray-core/v4/common/protocol" 11 ) 12 13 // ID of a session. 14 type ID uint32 15 16 // NewID generates a new ID. The generated ID is high likely to be unique, but not cryptographically secure. 17 // The generated ID will never be 0. 18 func NewID() ID { 19 for { 20 id := ID(rand.Uint32()) 21 if id != 0 { 22 return id 23 } 24 } 25 } 26 27 // ExportIDToError transfers session.ID into an error object, for logging purpose. 28 // This can be used with error.WriteToLog(). 29 func ExportIDToError(ctx context.Context) errors.ExportOption { 30 id := IDFromContext(ctx) 31 return func(h *errors.ExportOptionHolder) { 32 h.SessionID = uint32(id) 33 } 34 } 35 36 // Inbound is the metadata of an inbound connection. 37 type Inbound struct { 38 // Source address of the inbound connection. 39 Source net.Destination 40 // Gateway address 41 Gateway net.Destination 42 // Tag of the inbound proxy that handles the connection. 43 Tag string 44 // User is the user that authencates for the inbound. May be nil if the protocol allows anounymous traffic. 45 User *protocol.MemoryUser 46 } 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 56 // SniffingRequest controls the behavior of content sniffing. 57 type SniffingRequest struct { 58 OverrideDestinationForProtocol []string 59 Enabled bool 60 MetadataOnly bool 61 } 62 63 // Content is the metadata of the connection content. 64 type Content struct { 65 // Protocol of current content. 66 Protocol string 67 68 SniffingRequest SniffingRequest 69 70 Attributes map[string]string 71 72 SkipDNSResolve bool 73 } 74 75 // Sockopt is the settings for socket connection. 76 type Sockopt struct { 77 // Mark of the socket connection. 78 Mark uint32 79 } 80 81 // SetAttribute attachs additional string attributes to content. 82 func (c *Content) SetAttribute(name string, value string) { 83 if c.Attributes == nil { 84 c.Attributes = make(map[string]string) 85 } 86 c.Attributes[name] = value 87 } 88 89 // Attribute retrieves additional string attributes from content. 90 func (c *Content) Attribute(name string) string { 91 if c.Attributes == nil { 92 return "" 93 } 94 return c.Attributes[name] 95 }