-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
session.go
118 lines (103 loc) · 3.33 KB
/
session.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Package session provides functions for sessions of incoming requests.
package session // import "github.com/xtls/xray-core/common/session"
import (
"context"
"math/rand"
"github.com/xtls/xray-core/common/errors"
"github.com/xtls/xray-core/common/net"
"github.com/xtls/xray-core/common/protocol"
"github.com/xtls/xray-core/common/signal"
)
// 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
// Gateway address.
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.
User *protocol.MemoryUser
// Conn is actually internet.Connection. May be nil.
Conn net.Conn
// Timer of the inbound buf copier. May be nil.
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
}
// 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
// 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
// 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
}
// SniffingRequest controls the behavior of content sniffing.
type SniffingRequest struct {
ExcludeForDomain []string
OverrideDestinationForProtocol []string
Enabled bool
MetadataOnly bool
RouteOnly bool
}
// Content is the metadata of the connection content.
type Content struct {
// Protocol of current content.
Protocol string
SniffingRequest SniffingRequest
Attributes map[string]string
SkipDNSResolve bool
}
// Sockopt is the settings for socket connection.
type Sockopt struct {
// Mark of the socket connection.
Mark int32
}
// SetAttribute attaches 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]
}