mirror of
https://github.com/v2fly/v2ray-core.git
synced 2026-10-09 12:11:45 -07:00
move session based info into session package
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
package session
|
||||
|
||||
import "context"
|
||||
|
||||
type sessionKey int
|
||||
|
||||
const (
|
||||
idSessionKey sessionKey = iota
|
||||
inboundSessionKey
|
||||
outboundSessionKey
|
||||
)
|
||||
|
||||
// ContextWithID returns a new context with the given ID.
|
||||
func ContextWithID(ctx context.Context, id ID) context.Context {
|
||||
return context.WithValue(ctx, idSessionKey, id)
|
||||
}
|
||||
|
||||
// IDFromContext returns ID in this context, or 0 if not contained.
|
||||
func IDFromContext(ctx context.Context) ID {
|
||||
if id, ok := ctx.Value(idSessionKey).(ID); ok {
|
||||
return id
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func ContextWithInbound(ctx context.Context, inbound *Inbound) context.Context {
|
||||
return context.WithValue(ctx, inboundSessionKey, inbound)
|
||||
}
|
||||
|
||||
func InboundFromContext(ctx context.Context) *Inbound {
|
||||
if inbound, ok := ctx.Value(inboundSessionKey).(*Inbound); ok {
|
||||
return inbound
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func ContextWithOutbound(ctx context.Context, outbound *Outbound) context.Context {
|
||||
return context.WithValue(ctx, outboundSessionKey, outbound)
|
||||
}
|
||||
|
||||
func OutboundFromContext(ctx context.Context) *Outbound {
|
||||
if outbound, ok := ctx.Value(outboundSessionKey).(*Outbound); ok {
|
||||
return outbound
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
package session
|
||||
|
||||
type Request struct {
|
||||
//Destination net.Destination
|
||||
DecodedLayers []interface{}
|
||||
}
|
||||
+13
-19
@@ -6,6 +6,7 @@ import (
|
||||
"math/rand"
|
||||
|
||||
"v2ray.com/core/common/errors"
|
||||
"v2ray.com/core/common/net"
|
||||
)
|
||||
|
||||
// ID of a session.
|
||||
@@ -22,28 +23,21 @@ func NewID() ID {
|
||||
}
|
||||
}
|
||||
|
||||
type sessionKey int
|
||||
|
||||
const (
|
||||
idSessionKey sessionKey = iota
|
||||
)
|
||||
|
||||
// ContextWithID returns a new context with the given ID.
|
||||
func ContextWithID(ctx context.Context, id ID) context.Context {
|
||||
return context.WithValue(ctx, idSessionKey, id)
|
||||
}
|
||||
|
||||
// IDFromContext returns ID in this context, or 0 if not contained.
|
||||
func IDFromContext(ctx context.Context) ID {
|
||||
if id, ok := ctx.Value(idSessionKey).(ID); ok {
|
||||
return id
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func ExportIDToError(ctx context.Context) errors.ExportOption {
|
||||
id := IDFromContext(ctx)
|
||||
return func(h *errors.ExportOptionHolder) {
|
||||
h.SessionID = uint32(id)
|
||||
}
|
||||
}
|
||||
|
||||
type Inbound struct {
|
||||
Source net.Destination
|
||||
Gateway net.Destination
|
||||
Tag string
|
||||
}
|
||||
|
||||
type Outbound struct {
|
||||
Target net.Destination
|
||||
Gateway net.Address
|
||||
ResolvedIPs []net.IP
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user