github.com/eagleql/xray-core@v1.4.4/common/session/session.go (about) 1 // Package session provides functions for sessions of incoming requests. 2 package session // import "github.com/eagleql/xray-core/common/session" 3 4 import ( 5 "context" 6 "math/rand" 7 8 "github.com/eagleql/xray-core/common/errors" 9 "github.com/eagleql/xray-core/common/net" 10 "github.com/eagleql/xray-core/common/protocol" 11 "github.com/eagleql/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 // Getaway address. 42 Gateway net.Destination 43 // Tag of the inbound proxy that handles the connection. 44 Tag string 45 // User is the user that authencates for the inbound. May be nil if the protocol allows anounymous traffic. 46 User *protocol.MemoryUser 47 // Conn is actually internet.Connection. May be nil. 48 Conn net.Conn 49 // Timer of the inbound buf copier. May be nil. 50 Timer *signal.ActivityTimer 51 } 52 53 // Outbound is the metadata of an outbound connection. 54 type Outbound struct { 55 // Target address of the outbound connection. 56 Target net.Destination 57 // Gateway address 58 Gateway net.Address 59 } 60 61 // SniffingRequest controls the behavior of content sniffing. 62 type SniffingRequest struct { 63 ExcludeForDomain []string 64 OverrideDestinationForProtocol []string 65 Enabled bool 66 MetadataOnly bool 67 } 68 69 // Content is the metadata of the connection content. 70 type Content struct { 71 // Protocol of current content. 72 Protocol string 73 74 SniffingRequest SniffingRequest 75 76 Attributes map[string]string 77 78 SkipRoutePick bool 79 } 80 81 // Sockopt is the settings for socket connection. 82 type Sockopt struct { 83 // Mark of the socket connection. 84 Mark int32 85 } 86 87 // SetAttribute attachs additional string attributes to content. 88 func (c *Content) SetAttribute(name string, value string) { 89 if c.Attributes == nil { 90 c.Attributes = make(map[string]string) 91 } 92 c.Attributes[name] = value 93 } 94 95 // Attribute retrieves additional string attributes from content. 96 func (c *Content) Attribute(name string) string { 97 if c.Attributes == nil { 98 return "" 99 } 100 return c.Attributes[name] 101 }