Xray-core/common/session/session.go

105 lines
2.8 KiB
Go
Raw Permalink Normal View History

2020-11-25 11:01:53 +00:00
// Package session provides functions for sessions of incoming requests.
2020-12-04 01:36:16 +00:00
package session // import "github.com/xtls/xray-core/common/session"
2020-11-25 11:01:53 +00:00
import (
"context"
"math/rand"
2020-12-04 01:36:16 +00:00
"github.com/xtls/xray-core/common/errors"
2021-03-26 09:30:24 +00:00
"github.com/xtls/xray-core/common/matcher/domain"
"github.com/xtls/xray-core/common/matcher/geoip"
2020-12-04 01:36:16 +00:00
"github.com/xtls/xray-core/common/net"
"github.com/xtls/xray-core/common/protocol"
2020-12-05 11:58:10 +00:00
"github.com/xtls/xray-core/common/signal"
2020-11-25 11:01:53 +00:00
)
// 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
2020-12-04 01:36:16 +00:00
// Getaway address.
2020-11-25 11:01:53 +00:00
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
2020-12-05 11:58:10 +00:00
// Conn is actually internet.Connection. May be nil.
2020-12-04 01:36:16 +00:00
Conn net.Conn
2020-12-05 11:58:10 +00:00
// Timer of the inbound buf copier. May be nil.
Timer *signal.ActivityTimer
2020-11-25 11:01:53 +00:00
}
// 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
}
// SniffingRequest controls the behavior of content sniffing.
type SniffingRequest struct {
2021-03-26 08:56:43 +00:00
ExcludedDomainMatcher *domain.DomainMatcher
ExcludedIPMatcher *geoip.MultiGeoIPMatcher
2020-11-25 11:01:53 +00:00
OverrideDestinationForProtocol []string
Enabled bool
MetadataOnly bool
2020-11-25 11:01:53 +00: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-03-18 15:24:24 +00:00
SkipDNSResolve bool
2020-11-25 11:01:53 +00:00
}
// Sockopt is the settings for socket connection.
type Sockopt struct {
// Mark of the socket connection.
Mark int32
}
// SetAttribute attachs additional string attributes to content.
func (c *Content) SetAttribute(name string, value string) {
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 {
if c.Attributes == nil {
return ""
}
return c.Attributes[name]
}