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