github.com/moqsien/xraycore@v1.8.5/common/session/session.go (about) 1 // Package session provides functions for sessions of incoming requests. 2 package session // import "github.com/moqsien/xraycore/common/session" 3 4 import ( 5 "context" 6 "math/rand" 7 8 "github.com/moqsien/xraycore/common/errors" 9 "github.com/moqsien/xraycore/common/net" 10 "github.com/moqsien/xraycore/common/protocol" 11 "github.com/moqsien/xraycore/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 OriginalTarget net.Destination 59 Target net.Destination 60 RouteTarget net.Destination 61 // Gateway address 62 Gateway net.Address 63 } 64 65 // SniffingRequest controls the behavior of content sniffing. 66 type SniffingRequest struct { 67 ExcludeForDomain []string 68 OverrideDestinationForProtocol []string 69 Enabled bool 70 MetadataOnly bool 71 RouteOnly bool 72 } 73 74 // Content is the metadata of the connection content. 75 type Content struct { 76 // Protocol of current content. 77 Protocol string 78 79 SniffingRequest SniffingRequest 80 81 Attributes map[string]string 82 83 SkipDNSResolve bool 84 } 85 86 // Sockopt is the settings for socket connection. 87 type Sockopt struct { 88 // Mark of the socket connection. 89 Mark int32 90 } 91 92 // SetAttribute attaches additional string attributes to content. 93 func (c *Content) SetAttribute(name string, value string) { 94 if c.Attributes == nil { 95 c.Attributes = make(map[string]string) 96 } 97 c.Attributes[name] = value 98 } 99 100 // Attribute retrieves additional string attributes from content. 101 func (c *Content) Attribute(name string) string { 102 if c.Attributes == nil { 103 return "" 104 } 105 return c.Attributes[name] 106 }