github.com/imannamdari/v2ray-core/v5@v5.0.5/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/imannamdari/v2ray-core/v5/common/errors" 9 "github.com/imannamdari/v2ray-core/v5/common/net" 10 "github.com/imannamdari/v2ray-core/v5/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 // Domain resolver to use when dialing 55 Resolver func(ctx context.Context, domain string) net.Address 56 } 57 58 // SniffingRequest controls the behavior of content sniffing. 59 type SniffingRequest struct { 60 OverrideDestinationForProtocol []string 61 Enabled bool 62 MetadataOnly bool 63 } 64 65 // Content is the metadata of the connection content. 66 type Content struct { 67 // Protocol of current content. 68 Protocol string 69 70 SniffingRequest SniffingRequest 71 72 Attributes map[string]string 73 74 SkipDNSResolve bool 75 } 76 77 // Sockopt is the settings for socket connection. 78 type Sockopt struct { 79 // Mark of the socket connection. 80 Mark uint32 81 } 82 83 // SetAttribute attachs additional string attributes to content. 84 func (c *Content) SetAttribute(name string, value string) { 85 if c.Attributes == nil { 86 c.Attributes = make(map[string]string) 87 } 88 c.Attributes[name] = value 89 } 90 91 // Attribute retrieves additional string attributes from content. 92 func (c *Content) Attribute(name string) string { 93 if c.Attributes == nil { 94 return "" 95 } 96 return c.Attributes[name] 97 }