2020-11-25 14:01:53 +03:00
|
|
|
// Package session provides functions for sessions of incoming requests.
|
2020-12-04 04:36:16 +03:00
|
|
|
package session // import "github.com/xtls/xray-core/common/session"
|
2020-11-25 14:01:53 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"math/rand"
|
2024-10-15 07:22:32 +03:00
|
|
|
"sync"
|
2020-11-25 14:01:53 +03:00
|
|
|
|
2024-06-29 21:32:57 +03:00
|
|
|
c "github.com/xtls/xray-core/common/ctx"
|
2020-12-04 04:36:16 +03:00
|
|
|
"github.com/xtls/xray-core/common/errors"
|
|
|
|
"github.com/xtls/xray-core/common/net"
|
|
|
|
"github.com/xtls/xray-core/common/protocol"
|
2020-12-05 14:58:10 +03:00
|
|
|
"github.com/xtls/xray-core/common/signal"
|
2020-11-25 14:01:53 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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.
|
2024-06-29 21:32:57 +03:00
|
|
|
func NewID() c.ID {
|
2020-11-25 14:01:53 +03:00
|
|
|
for {
|
2024-06-29 21:32:57 +03:00
|
|
|
id := c.ID(rand.Uint32())
|
2020-11-25 14:01:53 +03:00
|
|
|
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 {
|
2024-06-29 21:32:57 +03:00
|
|
|
id := c.IDFromContext(ctx)
|
2020-11-25 14:01:53 +03:00
|
|
|
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
|
2021-10-12 19:49:05 +03:00
|
|
|
// Gateway address.
|
2020-11-25 14:01:53 +03:00
|
|
|
Gateway net.Destination
|
|
|
|
// Tag of the inbound proxy that handles the connection.
|
|
|
|
Tag string
|
2023-04-06 13:21:35 +03:00
|
|
|
// Name of the inbound proxy that handles the connection.
|
|
|
|
Name string
|
2024-04-11 12:51:12 +03:00
|
|
|
// User is the user that authenticates for the inbound. May be nil if the protocol allows anonymous traffic.
|
2020-11-25 14:01:53 +03:00
|
|
|
User *protocol.MemoryUser
|
2020-12-05 14:58:10 +03:00
|
|
|
// Conn is actually internet.Connection. May be nil.
|
2020-12-04 04:36:16 +03:00
|
|
|
Conn net.Conn
|
2020-12-05 14:58:10 +03:00
|
|
|
// Timer of the inbound buf copier. May be nil.
|
|
|
|
Timer *signal.ActivityTimer
|
2024-05-14 04:52:24 +03:00
|
|
|
// CanSpliceCopy is a property for this connection
|
2023-05-04 05:21:45 +03:00
|
|
|
// 1 = can, 2 = after processing protocol info should be able to, 3 = cannot
|
|
|
|
CanSpliceCopy int
|
|
|
|
}
|
|
|
|
|
2020-11-25 14:01:53 +03:00
|
|
|
// Outbound is the metadata of an outbound connection.
|
|
|
|
type Outbound struct {
|
|
|
|
// Target address of the outbound connection.
|
2023-08-29 10:12:36 +03:00
|
|
|
OriginalTarget net.Destination
|
|
|
|
Target net.Destination
|
|
|
|
RouteTarget net.Destination
|
2020-11-25 14:01:53 +03:00
|
|
|
// Gateway address
|
|
|
|
Gateway net.Address
|
2024-05-14 04:52:24 +03:00
|
|
|
// Tag of the outbound proxy that handles the connection.
|
|
|
|
Tag string
|
2023-05-04 05:21:45 +03:00
|
|
|
// Name of the outbound proxy that handles the connection.
|
|
|
|
Name string
|
2024-10-15 07:22:32 +03:00
|
|
|
// Conn is actually internet.Connection. May be nil. It is currently nil for outbound with proxySettings
|
2023-05-04 05:21:45 +03:00
|
|
|
Conn net.Conn
|
2024-05-14 04:52:24 +03:00
|
|
|
// CanSpliceCopy is a property for this connection
|
|
|
|
// 1 = can, 2 = after processing protocol info should be able to, 3 = cannot
|
|
|
|
CanSpliceCopy int
|
2020-11-25 14:01:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// SniffingRequest controls the behavior of content sniffing.
|
|
|
|
type SniffingRequest struct {
|
2021-01-21 23:50:09 +03:00
|
|
|
ExcludeForDomain []string
|
2020-11-25 14:01:53 +03:00
|
|
|
OverrideDestinationForProtocol []string
|
|
|
|
Enabled bool
|
2021-03-07 07:39:50 +03:00
|
|
|
MetadataOnly bool
|
2021-09-16 10:05:48 +03:00
|
|
|
RouteOnly bool
|
2020-11-25 14:01:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Content is the metadata of the connection content.
|
|
|
|
type Content struct {
|
|
|
|
// Protocol of current content.
|
|
|
|
Protocol string
|
|
|
|
|
|
|
|
SniffingRequest SniffingRequest
|
|
|
|
|
|
|
|
Attributes map[string]string
|
|
|
|
|
2021-10-16 16:02:51 +03:00
|
|
|
SkipDNSResolve bool
|
2024-10-15 07:22:32 +03:00
|
|
|
|
|
|
|
mu sync.Mutex
|
|
|
|
|
|
|
|
isLocked bool
|
2020-11-25 14:01:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sockopt is the settings for socket connection.
|
|
|
|
type Sockopt struct {
|
|
|
|
// Mark of the socket connection.
|
|
|
|
Mark int32
|
|
|
|
}
|
|
|
|
|
2024-10-15 07:22:32 +03:00
|
|
|
// Some how when using mux, there will be a same ctx between different requests
|
|
|
|
// This will cause problem as it's designed for single request, like concurrent map writes
|
|
|
|
// Add a Mutex as a temp solution
|
|
|
|
|
2022-02-03 03:57:32 +03:00
|
|
|
// SetAttribute attaches additional string attributes to content.
|
2020-11-25 14:01:53 +03:00
|
|
|
func (c *Content) SetAttribute(name string, value string) {
|
2024-10-15 07:22:32 +03:00
|
|
|
if c.isLocked {
|
|
|
|
errors.LogError(context.Background(), "Multiple goroutines are tring to access one routing content, tring to write ", name, ":", value)
|
|
|
|
}
|
|
|
|
c.mu.Lock()
|
|
|
|
c.isLocked = true
|
|
|
|
defer func() {
|
|
|
|
c.isLocked = false
|
|
|
|
c.mu.Unlock()
|
|
|
|
}()
|
|
|
|
|
2020-11-25 14:01:53 +03:00
|
|
|
if c.Attributes == nil {
|
|
|
|
c.Attributes = make(map[string]string)
|
|
|
|
}
|
|
|
|
c.Attributes[name] = value
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attribute retrieves additional string attributes from content.
|
|
|
|
func (c *Content) Attribute(name string) string {
|
2024-10-30 05:26:43 +03:00
|
|
|
c.mu.Lock()
|
|
|
|
c.isLocked = true
|
|
|
|
defer func() {
|
|
|
|
c.isLocked = false
|
|
|
|
c.mu.Unlock()
|
|
|
|
}()
|
2020-11-25 14:01:53 +03:00
|
|
|
if c.Attributes == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return c.Attributes[name]
|
|
|
|
}
|
2024-10-30 05:26:43 +03:00
|
|
|
|
|
|
|
func (c *Content) AttributeLen() int {
|
|
|
|
c.mu.Lock()
|
|
|
|
c.isLocked = true
|
|
|
|
defer func() {
|
|
|
|
c.isLocked = false
|
|
|
|
c.mu.Unlock()
|
|
|
|
}()
|
|
|
|
return len(c.Attributes)
|
|
|
|
}
|