| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- // Package session provides functions for sessions of incoming requests.
- package session // import "v2ray.com/core/common/session"
- import (
- "context"
- "math/rand"
- "v2ray.com/core/common/errors"
- "v2ray.com/core/common/net"
- "v2ray.com/core/common/protocol"
- )
- // ID of a session.
- type ID uint32
- // NewID generates a new ID. The generated ID is high likely to be unique, but not cryptographically secure.
- // The generated ID will never be 0.
- func NewID() ID {
- for {
- id := ID(rand.Uint32())
- if id != 0 {
- return id
- }
- }
- }
- // ExportIDToError transfers session.ID into an error object, for logging purpose.
- // This can be used with error.WriteToLog().
- func ExportIDToError(ctx context.Context) errors.ExportOption {
- id := IDFromContext(ctx)
- return func(h *errors.ExportOptionHolder) {
- h.SessionID = uint32(id)
- }
- }
- // Inbound is the metadata of an inbound connection.
- type Inbound struct {
- // Source address of the inbound connection.
- Source net.Destination
- // Getaway address
- Gateway net.Destination
- // Tag of the inbound proxy that handles the connection.
- Tag string
- // User is the user that authencates for the inbound. May be nil if the protocol allows anounymous traffic.
- User *protocol.MemoryUser
- }
- // Outbound is the metadata of an outbound connection.
- type Outbound struct {
- // Target address of the outbound connection.
- Target net.Destination
- // Gateway address
- Gateway net.Address
- // ResolvedIPs is the resolved IP addresses, if the Targe is a domain address.
- ResolvedIPs []net.IP
- }
- type SniffingRequest struct {
- OverrideDestinationForProtocol []string
- Enabled bool
- }
- // Content is the metadata of the connection content.
- type Content struct {
- // Protocol of current content.
- Protocol string
- SniffingRequest SniffingRequest
- attr map[string]interface{}
- }
- func (c *Content) SetAttribute(name string, value interface{}) {
- if c.attr == nil {
- c.attr = make(map[string]interface{})
- }
- c.attr[name] = value
- }
- func (c *Content) Attribute(name string) interface{} {
- if c.attr == nil {
- return nil
- }
- return c.attr[name]
- }
|