github.com/xmplusdev/xmcore@v1.8.11-0.20240412132628-5518b55526af/common/session/session.go (about)

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