github.com/xtls/xray-core@v1.8.12-0.20240518155711-3168d27b0bdb/common/session/session.go (about) 1 // Package session provides functions for sessions of incoming requests. 2 package session // import "github.com/xtls/xray-core/common/session" 3 4 import ( 5 "context" 6 "math/rand" 7 8 "github.com/xtls/xray-core/common/errors" 9 "github.com/xtls/xray-core/common/net" 10 "github.com/xtls/xray-core/common/protocol" 11 "github.com/xtls/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 authenticates for the inbound. May be nil if the protocol allows anonymous 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 // CanSpliceCopy is a property for this connection 54 // 1 = can, 2 = after processing protocol info should be able to, 3 = cannot 55 CanSpliceCopy int 56 } 57 58 // Outbound is the metadata of an outbound connection. 59 type Outbound struct { 60 // Target address of the outbound connection. 61 OriginalTarget net.Destination 62 Target net.Destination 63 RouteTarget net.Destination 64 // Gateway address 65 Gateway net.Address 66 // Tag of the outbound proxy that handles the connection. 67 Tag string 68 // Name of the outbound proxy that handles the connection. 69 Name string 70 // Conn is actually internet.Connection. May be nil. It is currently nil for outbound with proxySettings 71 Conn net.Conn 72 // CanSpliceCopy is a property for this connection 73 // 1 = can, 2 = after processing protocol info should be able to, 3 = cannot 74 CanSpliceCopy int 75 } 76 77 // SniffingRequest controls the behavior of content sniffing. 78 type SniffingRequest struct { 79 ExcludeForDomain []string 80 OverrideDestinationForProtocol []string 81 Enabled bool 82 MetadataOnly bool 83 RouteOnly bool 84 } 85 86 // Content is the metadata of the connection content. 87 type Content struct { 88 // Protocol of current content. 89 Protocol string 90 91 SniffingRequest SniffingRequest 92 93 Attributes map[string]string 94 95 SkipDNSResolve bool 96 } 97 98 // Sockopt is the settings for socket connection. 99 type Sockopt struct { 100 // Mark of the socket connection. 101 Mark int32 102 } 103 104 // SetAttribute attaches additional string attributes to content. 105 func (c *Content) SetAttribute(name string, value string) { 106 if c.Attributes == nil { 107 c.Attributes = make(map[string]string) 108 } 109 c.Attributes[name] = value 110 } 111 112 // Attribute retrieves additional string attributes from content. 113 func (c *Content) Attribute(name string) string { 114 if c.Attributes == nil { 115 return "" 116 } 117 return c.Attributes[name] 118 }