move session based info into session package

This commit is contained in:
Darien Raymond
2018-09-18 23:09:54 +02:00
parent f589769947
commit 94b880d060
28 changed files with 212 additions and 194 deletions
+46
View File
@@ -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
}
-6
View File
@@ -1,6 +0,0 @@
package session
type Request struct {
//Destination net.Destination
DecodedLayers []interface{}
}
+13 -19
View File
@@ -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
}