2018-04-03 16:34:59 -04:00
|
|
|
// Package session provides functions for sessions of incoming requests.
|
2021-02-16 15:31:50 -05:00
|
|
|
package session
|
2018-02-22 09:26:00 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"math/rand"
|
2018-06-24 19:09:02 -04:00
|
|
|
|
2021-02-16 15:31:50 -05:00
|
|
|
"github.com/v2fly/v2ray-core/v4/common/errors"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/common/net"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/common/protocol"
|
2018-02-22 09:26:00 -05:00
|
|
|
)
|
|
|
|
|
2018-04-03 16:34:59 -04:00
|
|
|
// ID of a session.
|
2018-02-22 09:26:00 -05:00
|
|
|
type ID uint32
|
|
|
|
|
2018-04-03 16:34:59 -04: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.
|
2018-02-22 09:26:00 -05:00
|
|
|
func NewID() ID {
|
|
|
|
for {
|
|
|
|
id := ID(rand.Uint32())
|
|
|
|
if id != 0 {
|
|
|
|
return id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-15 02:51:24 -04:00
|
|
|
// ExportIDToError transfers session.ID into an error object, for logging purpose.
|
|
|
|
// This can be used with error.WriteToLog().
|
2018-06-24 19:09:02 -04:00
|
|
|
func ExportIDToError(ctx context.Context) errors.ExportOption {
|
|
|
|
id := IDFromContext(ctx)
|
|
|
|
return func(h *errors.ExportOptionHolder) {
|
|
|
|
h.SessionID = uint32(id)
|
|
|
|
}
|
|
|
|
}
|
2018-09-18 17:09:54 -04:00
|
|
|
|
2018-10-15 02:51:24 -04:00
|
|
|
// Inbound is the metadata of an inbound connection.
|
2018-09-18 17:09:54 -04:00
|
|
|
type Inbound struct {
|
2018-10-15 02:51:24 -04:00
|
|
|
// Source address of the inbound connection.
|
|
|
|
Source net.Destination
|
2021-04-08 21:47:36 -04:00
|
|
|
// Gateway address
|
2018-09-18 17:09:54 -04:00
|
|
|
Gateway net.Destination
|
2018-10-15 02:51:24 -04:00
|
|
|
// Tag of the inbound proxy that handles the connection.
|
|
|
|
Tag string
|
2018-10-15 02:36:50 -04:00
|
|
|
// User is the user that authencates for the inbound. May be nil if the protocol allows anounymous traffic.
|
|
|
|
User *protocol.MemoryUser
|
2018-09-18 17:09:54 -04:00
|
|
|
}
|
|
|
|
|
2018-10-15 02:51:24 -04:00
|
|
|
// Outbound is the metadata of an outbound connection.
|
2018-09-18 17:09:54 -04:00
|
|
|
type Outbound struct {
|
2018-10-15 02:51:24 -04:00
|
|
|
// Target address of the outbound connection.
|
|
|
|
Target net.Destination
|
|
|
|
// Gateway address
|
|
|
|
Gateway net.Address
|
2018-09-18 17:09:54 -04:00
|
|
|
}
|
2019-02-22 10:58:16 -05:00
|
|
|
|
2020-09-12 12:34:35 -04:00
|
|
|
// SniffingRequest controls the behavior of content sniffing.
|
2019-02-22 18:27:21 -05:00
|
|
|
type SniffingRequest struct {
|
|
|
|
OverrideDestinationForProtocol []string
|
|
|
|
Enabled bool
|
2021-02-08 05:18:52 -05:00
|
|
|
MetadataOnly bool
|
2019-02-22 18:27:21 -05:00
|
|
|
}
|
|
|
|
|
2019-02-22 10:58:16 -05:00
|
|
|
// Content is the metadata of the connection content.
|
|
|
|
type Content struct {
|
|
|
|
// Protocol of current content.
|
|
|
|
Protocol string
|
2019-02-22 18:27:21 -05:00
|
|
|
|
|
|
|
SniffingRequest SniffingRequest
|
2019-02-28 05:45:06 -05:00
|
|
|
|
2020-09-12 12:34:35 -04:00
|
|
|
Attributes map[string]string
|
2020-03-02 05:07:43 -05:00
|
|
|
|
2020-12-30 05:35:19 -05:00
|
|
|
SkipDNSResolve bool
|
2019-02-28 05:45:06 -05:00
|
|
|
}
|
|
|
|
|
2020-07-31 14:04:06 -04:00
|
|
|
// Sockopt is the settings for socket connection.
|
|
|
|
type Sockopt struct {
|
|
|
|
// Mark of the socket connection.
|
|
|
|
Mark int32
|
|
|
|
}
|
|
|
|
|
2020-09-12 12:34:35 -04:00
|
|
|
// SetAttribute attachs additional string attributes to content.
|
|
|
|
func (c *Content) SetAttribute(name string, value string) {
|
2019-02-28 08:39:50 -05:00
|
|
|
if c.Attributes == nil {
|
2020-09-12 12:34:35 -04:00
|
|
|
c.Attributes = make(map[string]string)
|
2019-02-28 05:45:06 -05:00
|
|
|
}
|
2019-02-28 08:39:50 -05:00
|
|
|
c.Attributes[name] = value
|
2019-02-28 05:45:06 -05:00
|
|
|
}
|
|
|
|
|
2020-09-12 12:34:35 -04:00
|
|
|
// Attribute retrieves additional string attributes from content.
|
|
|
|
func (c *Content) Attribute(name string) string {
|
2019-02-28 08:39:50 -05:00
|
|
|
if c.Attributes == nil {
|
2020-09-12 12:34:35 -04:00
|
|
|
return ""
|
2019-02-28 05:45:06 -05:00
|
|
|
}
|
2019-02-28 08:39:50 -05:00
|
|
|
return c.Attributes[name]
|
2019-02-22 10:58:16 -05:00
|
|
|
}
|