1
mirror of https://github.com/XTLS/Xray-core.git synced 2025-12-13 05:14:17 +04:00
Files

123 lines
3.8 KiB
Go
Raw Permalink Normal View History

2020-11-25 19:01:53 +08:00
// Package session provides functions for sessions of incoming requests.
2020-12-04 09:36:16 +08:00
package session // import "github.com/xtls/xray-core/common/session"
2020-11-25 19:01:53 +08:00
import (
"context"
"math/rand"
c "github.com/xtls/xray-core/common/ctx"
2020-12-04 09:36:16 +08: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 11:58:10 +00:00
"github.com/xtls/xray-core/common/signal"
2020-11-25 19:01:53 +08: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.
func NewID() c.ID {
2020-11-25 19:01:53 +08:00
for {
id := c.ID(rand.Uint32())
2020-11-25 19:01:53 +08: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 {
id := c.IDFromContext(ctx)
2020-11-25 19:01:53 +08: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
// Local address of the inbound connection.
Local net.Destination
2021-10-13 00:49:05 +08:00
// Gateway address.
2020-11-25 19:01:53 +08:00
Gateway net.Destination
// Tag of the inbound proxy that handles the connection.
Tag string
// Name of the inbound proxy that handles the connection.
Name string
// User is the user that authenticates for the inbound. May be nil if the protocol allows anonymous traffic.
2020-11-25 19:01:53 +08:00
User *protocol.MemoryUser
// VlessRoute is the user-sent VLESS UUID's 7th<<8 | 8th bytes.
VlessRoute net.Port
// Used by splice copy. Conn is actually internet.Connection. May be nil.
2020-12-04 09:36:16 +08:00
Conn net.Conn
// Used by splice copy. Timer of the inbound buf copier. May be nil.
2020-12-05 11:58:10 +00:00
Timer *signal.ActivityTimer
// 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 19:01:53 +08:00
// Outbound is the metadata of an outbound connection.
type Outbound struct {
// Target address of the outbound connection.
OriginalTarget net.Destination
Target net.Destination
RouteTarget net.Destination
2020-11-25 19:01:53 +08:00
// Gateway address
Gateway net.Address
// Tag of the outbound proxy that handles the connection.
Tag string
// Name of the outbound proxy that handles the connection.
Name string
// Unused. Conn is actually internet.Connection. May be nil. It is currently nil for outbound with proxySettings
Conn net.Conn
// 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 19:01:53 +08:00
}
// SniffingRequest controls the behavior of content sniffing. They are from inbound config. Read-only
2020-11-25 19:01:53 +08:00
type SniffingRequest struct {
ExcludeForDomain []string
OverrideDestinationForProtocol []string
2020-11-25 19:01:53 +08:00
Enabled bool
MetadataOnly bool
2021-09-16 15:05:48 +08:00
RouteOnly bool
2020-11-25 19:01:53 +08:00
}
// Content is the metadata of the connection content. Mainly used for routing.
2020-11-25 19:01:53 +08:00
type Content struct {
// Protocol of current content.
Protocol string
SniffingRequest SniffingRequest
// HTTP traffic sniffed headers
2020-11-25 19:01:53 +08:00
Attributes map[string]string
// SkipDNSResolve is set from DNS module. the DOH remote server maybe a domain name, this prevents cycle resolving dead loop
Merge dns (#722) * DNS: add clientip for specific nameserver * Refactoring: DNS App * DNS: add DNS over QUIC support * Feat: add disableCache option for DNS * Feat: add queryStrategy option for DNS * Feat: add disableFallback & skipFallback option for DNS * Feat: DNS hosts support multiple addresses * Feat: DNS transport over TCP * DNS: fix typo & refine code * DNS: refine code * Add disableFallbackIfMatch dns option * Feat: routing and freedom outbound ignore Fake DNS Turn off fake DNS for request sent from Routing and Freedom outbound. Fake DNS now only apply to DNS outbound. This is important for Android, where VPN service take over all system DNS traffic and pass it to core. "UseIp" option can be used in Freedom outbound to avoid getting fake IP and fail connection. * Fix test * Fix dns return * Fix local dns return empty * Apply timeout to dns outbound * Update app/dns/config.go Co-authored-by: Loyalsoldier <10487845+loyalsoldier@users.noreply.github.com> Co-authored-by: Ye Zhihao <vigilans@foxmail.com> Co-authored-by: maskedeken <52683904+maskedeken@users.noreply.github.com> Co-authored-by: V2Fly Team <51714622+vcptr@users.noreply.github.com> Co-authored-by: CalmLong <37164399+calmlong@users.noreply.github.com> Co-authored-by: Shelikhoo <xiaokangwang@outlook.com> Co-authored-by: 秋のかえで <autmaple@protonmail.com> Co-authored-by: 朱聖黎 <digglife@gmail.com> Co-authored-by: rurirei <72071920+rurirei@users.noreply.github.com> Co-authored-by: yuhan6665 <1588741+yuhan6665@users.noreply.github.com> Co-authored-by: Arthur Morgan <4637240+badO1a5A90@users.noreply.github.com>
2021-10-16 21:02:51 +08:00
SkipDNSResolve bool
2020-11-25 19:01:53 +08:00
}
// Sockopt is the settings for socket connection.
type Sockopt struct {
// Mark of the socket connection.
Mark int32
}
2022-02-03 08:57:32 +08:00
// SetAttribute attaches additional string attributes to content.
2020-11-25 19:01:53 +08:00
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]
}