1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-14 00:07:09 -05:00
v2fly/common/session/session.go

98 lines
2.4 KiB
Go
Raw Normal View History

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"
"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 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().
func ExportIDToError(ctx context.Context) errors.ExportOption {
id := IDFromContext(ctx)
return func(h *errors.ExportOptionHolder) {
h.SessionID = uint32(id)
}
}
2018-10-15 02:51:24 -04:00
// Inbound is the metadata of an inbound connection.
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
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-10-15 02:51:24 -04:00
// Outbound is the metadata of an outbound connection.
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
// Domain resolver to use when dialing
Resolver func(ctx context.Context, domain string) net.Address
}
2019-02-22 10:58:16 -05:00
// SniffingRequest controls the behavior of content sniffing.
2019-02-22 18:27:21 -05:00
type SniffingRequest struct {
OverrideDestinationForProtocol []string
Enabled bool
Feature: Fake DNS support (#406) * Add fake dns A new config object "fake" in DnsObject for toggling fake dns function Compare with sniffing, fake dns is not limited to http and tls traffic. It works across all inbounds. For example, when dns request come from one inbound, the local DNS server of v2ray will response with a unique fake IP for every unique domain name. Then later on v2ray received a request to one of the fake IP from any inbounds, it will override the request destination with the previously saved domain. By default, v2ray cache up to 65535 addresses. The old records will be discarded bases on LRU. The fake IP will be 240.x.x.x * fix an edge case when encounter a fake IP in use * Move lru to common.cache package * Added the necessary change to obtain request IP from sniffer * Refactor the code so that it may stop depending on global variables in the future. * Replace string manipulation code with more generic codes, hopefully this will work for both IPv4 and IPv6 networks. * Try to use IPv4 version of address if possible * Added Test Case for Fake Dns * Added More Test Case for Fake Dns * Stop user from creating a instance with LRU size more than subnet size, it will create a infinite loop * Move Fake DNS to a separate package * Generated Code for fakedns * Encapsulate Fake DNS as a Instance wide service * Added Support for metadata sniffer, which will be used for Fake DNS * Dependency injection for fake dns * Fake DNS As a Sniffer * Remove stub object * Remove global variable * Update generated protobuf file for metadata only sniffing * Apply Fake DNS config to session * Loading for fake dns settings * Bug fix * Include fake dns in all * Fix FakeDns Lint Condition * Fix sniffer config * Fix lint message * Fix dependency resolution * Fix fake dns not loaded as sniffer * reduce ttl for fake dns * Apply Coding Style * Apply Coding Style * Apply Coding Style * Apply Coding Style * Apply Coding Style * Fix crashed when no fake dns * Apply Coding Style * Fix Fake DNS do not apply to UDP socket * Fixed a bug prevent FakeDNS App Setting from become effective * Fixed a caveat prevent FakeDNS App Setting from become effective * Use log comparison to reduce in issue when it comes to really high value typical for ipv6 subnet * Add build tag for fakedns * Removal of FakeDNS specific logic at DNS client: making it a standard dns client * Regenerate auto generated file * Amended version of configure file * Bug fixes for fakeDNS * Bug fixes for fakeDNS * Fix test: remove reference to removed attribute * Test: fix codacy issue * Conf: Remove old field support * Test: fix codacy issue * Change test scale for TestFakeDnsHolderCreateMappingAndRollOver * Test: fix codacy issue Co-authored-by: yuhan6665 <1588741+yuhan6665@users.noreply.github.com> Co-authored-by: loyalsoldier <10487845+Loyalsoldier@users.noreply.github.com> Co-authored-by: kslr <kslrwang@gmail.com>
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
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
}
// 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 08:39:50 -05:00
if c.Attributes == nil {
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
}
// 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 {
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
}