github.com/Uhtred009/v2ray-core-1@v4.31.2+incompatible/common/session/session.go (about) 1 // Package session provides functions for sessions of incoming requests. 2 package session // import "v2ray.com/core/common/session" 3 4 import ( 5 "context" 6 "math/rand" 7 8 "v2ray.com/core/common/errors" 9 "v2ray.com/core/common/net" 10 "v2ray.com/core/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 // Getaway 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 } 61 62 // Content is the metadata of the connection content. 63 type Content struct { 64 // Protocol of current content. 65 Protocol string 66 67 SniffingRequest SniffingRequest 68 69 Attributes map[string]string 70 71 SkipRoutePick bool 72 } 73 74 // Sockopt is the settings for socket connection. 75 type Sockopt struct { 76 // Mark of the socket connection. 77 Mark int32 78 } 79 80 // SetAttribute attachs additional string attributes to content. 81 func (c *Content) SetAttribute(name string, value string) { 82 if c.Attributes == nil { 83 c.Attributes = make(map[string]string) 84 } 85 c.Attributes[name] = value 86 } 87 88 // Attribute retrieves additional string attributes from content. 89 func (c *Content) Attribute(name string) string { 90 if c.Attributes == nil { 91 return "" 92 } 93 return c.Attributes[name] 94 }