2018-04-03 20:34:59 +00:00
|
|
|
// Package session provides functions for sessions of incoming requests.
|
|
|
|
package session // import "v2ray.com/core/common/session"
|
2018-02-22 14:26:00 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"math/rand"
|
2018-06-24 23:09:02 +00:00
|
|
|
|
|
|
|
"v2ray.com/core/common/errors"
|
2018-09-18 21:09:54 +00:00
|
|
|
"v2ray.com/core/common/net"
|
2018-10-15 06:36:50 +00:00
|
|
|
"v2ray.com/core/common/protocol"
|
2018-02-22 14:26:00 +00:00
|
|
|
)
|
|
|
|
|
2018-04-03 20:34:59 +00:00
|
|
|
// ID of a session.
|
2018-02-22 14:26:00 +00:00
|
|
|
type ID uint32
|
|
|
|
|
2018-04-03 20:34:59 +00: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 14:26:00 +00:00
|
|
|
func NewID() ID {
|
|
|
|
for {
|
|
|
|
id := ID(rand.Uint32())
|
|
|
|
if id != 0 {
|
|
|
|
return id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-15 06:51:24 +00:00
|
|
|
// ExportIDToError transfers session.ID into an error object, for logging purpose.
|
|
|
|
// This can be used with error.WriteToLog().
|
2018-06-24 23:09:02 +00:00
|
|
|
func ExportIDToError(ctx context.Context) errors.ExportOption {
|
|
|
|
id := IDFromContext(ctx)
|
|
|
|
return func(h *errors.ExportOptionHolder) {
|
|
|
|
h.SessionID = uint32(id)
|
|
|
|
}
|
|
|
|
}
|
2018-09-18 21:09:54 +00:00
|
|
|
|
2018-10-15 06:51:24 +00:00
|
|
|
// Inbound is the metadata of an inbound connection.
|
2018-09-18 21:09:54 +00:00
|
|
|
type Inbound struct {
|
2018-10-15 06:51:24 +00:00
|
|
|
// Source address of the inbound connection.
|
|
|
|
Source net.Destination
|
|
|
|
// Getaway address
|
2018-09-18 21:09:54 +00:00
|
|
|
Gateway net.Destination
|
2018-10-15 06:51:24 +00:00
|
|
|
// Tag of the inbound proxy that handles the connection.
|
|
|
|
Tag string
|
2018-10-15 06:36:50 +00: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 21:09:54 +00:00
|
|
|
}
|
|
|
|
|
2018-10-15 06:51:24 +00:00
|
|
|
// Outbound is the metadata of an outbound connection.
|
2018-09-18 21:09:54 +00:00
|
|
|
type Outbound struct {
|
2018-10-15 06:51:24 +00:00
|
|
|
// Target address of the outbound connection.
|
|
|
|
Target net.Destination
|
|
|
|
// Gateway address
|
|
|
|
Gateway net.Address
|
|
|
|
// ResolvedIPs is the resolved IP addresses, if the Targe is a domain address.
|
2018-09-18 21:09:54 +00:00
|
|
|
ResolvedIPs []net.IP
|
|
|
|
}
|
2019-02-22 15:58:16 +00:00
|
|
|
|
2019-02-22 23:27:21 +00:00
|
|
|
type SniffingRequest struct {
|
|
|
|
OverrideDestinationForProtocol []string
|
|
|
|
Enabled bool
|
|
|
|
}
|
|
|
|
|
2019-02-22 15:58:16 +00:00
|
|
|
// Content is the metadata of the connection content.
|
|
|
|
type Content struct {
|
|
|
|
// Protocol of current content.
|
|
|
|
Protocol string
|
2019-02-22 23:27:21 +00:00
|
|
|
|
|
|
|
SniffingRequest SniffingRequest
|
2019-02-28 10:45:06 +00:00
|
|
|
|
2019-02-28 13:39:50 +00:00
|
|
|
Attributes map[string]interface{}
|
2019-02-28 10:45:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Content) SetAttribute(name string, value interface{}) {
|
2019-02-28 13:39:50 +00:00
|
|
|
if c.Attributes == nil {
|
|
|
|
c.Attributes = make(map[string]interface{})
|
2019-02-28 10:45:06 +00:00
|
|
|
}
|
2019-02-28 13:39:50 +00:00
|
|
|
c.Attributes[name] = value
|
2019-02-28 10:45:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Content) Attribute(name string) interface{} {
|
2019-02-28 13:39:50 +00:00
|
|
|
if c.Attributes == nil {
|
2019-02-28 10:45:06 +00:00
|
|
|
return nil
|
|
|
|
}
|
2019-02-28 13:39:50 +00:00
|
|
|
return c.Attributes[name]
|
2019-02-22 15:58:16 +00:00
|
|
|
}
|