github.com/xraypb/xray-core@v1.6.6/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  	// 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  	RouteTarget net.Destination
    58  	// Gateway address
    59  	Gateway net.Address
    60  }
    61  
    62  // SniffingRequest controls the behavior of content sniffing.
    63  type SniffingRequest struct {
    64  	ExcludeForDomain               []string
    65  	OverrideDestinationForProtocol []string
    66  	Enabled                        bool
    67  	MetadataOnly                   bool
    68  	RouteOnly                      bool
    69  }
    70  
    71  // Content is the metadata of the connection content.
    72  type Content struct {
    73  	// Protocol of current content.
    74  	Protocol string
    75  
    76  	SniffingRequest SniffingRequest
    77  
    78  	Attributes map[string]string
    79  
    80  	SkipDNSResolve bool
    81  }
    82  
    83  // Sockopt is the settings for socket connection.
    84  type Sockopt struct {
    85  	// Mark of the socket connection.
    86  	Mark int32
    87  }
    88  
    89  // SetAttribute attaches additional string attributes to content.
    90  func (c *Content) SetAttribute(name string, value string) {
    91  	if c.Attributes == nil {
    92  		c.Attributes = make(map[string]string)
    93  	}
    94  	c.Attributes[name] = value
    95  }
    96  
    97  // Attribute retrieves additional string attributes from content.
    98  func (c *Content) Attribute(name string) string {
    99  	if c.Attributes == nil {
   100  		return ""
   101  	}
   102  	return c.Attributes[name]
   103  }