Files
v2fly/common/session/session.go
T

98 lines
2.4 KiB
Go
Raw Normal View History

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