1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-09 03:37:37 -05:00
v2fly/proxy/context.go

73 lines
1.9 KiB
Go
Raw Normal View History

package proxy
import (
"context"
"v2ray.com/core/common/net"
)
type key int
const (
sourceKey key = iota
2017-02-09 16:49:38 -05:00
targetKey
originalTargetKey
inboundEntryPointKey
inboundTagKey
resolvedIPsKey
)
func ContextWithSource(ctx context.Context, src net.Destination) context.Context {
return context.WithValue(ctx, sourceKey, src)
}
2017-02-09 16:49:38 -05:00
func SourceFromContext(ctx context.Context) (net.Destination, bool) {
v, ok := ctx.Value(sourceKey).(net.Destination)
return v, ok
}
2017-01-14 18:48:37 -05:00
2017-02-09 16:49:38 -05:00
func ContextWithOriginalTarget(ctx context.Context, dest net.Destination) context.Context {
return context.WithValue(ctx, originalTargetKey, dest)
2017-01-14 18:48:37 -05:00
}
2017-02-09 16:49:38 -05:00
func OriginalTargetFromContext(ctx context.Context) (net.Destination, bool) {
v, ok := ctx.Value(originalTargetKey).(net.Destination)
return v, ok
}
2017-02-09 16:49:38 -05:00
func ContextWithTarget(ctx context.Context, dest net.Destination) context.Context {
return context.WithValue(ctx, targetKey, dest)
}
2017-02-09 16:49:38 -05:00
func TargetFromContext(ctx context.Context) (net.Destination, bool) {
v, ok := ctx.Value(targetKey).(net.Destination)
return v, ok
}
2017-02-09 16:49:38 -05:00
func ContextWithInboundEntryPoint(ctx context.Context, dest net.Destination) context.Context {
return context.WithValue(ctx, inboundEntryPointKey, dest)
}
2017-02-09 16:49:38 -05:00
func InboundEntryPointFromContext(ctx context.Context) (net.Destination, bool) {
v, ok := ctx.Value(inboundEntryPointKey).(net.Destination)
return v, ok
}
func ContextWithInboundTag(ctx context.Context, tag string) context.Context {
return context.WithValue(ctx, inboundTagKey, tag)
}
2017-02-09 16:49:38 -05:00
func InboundTagFromContext(ctx context.Context) (string, bool) {
v, ok := ctx.Value(inboundTagKey).(string)
return v, ok
}
func ContextWithResolveIPs(ctx context.Context, ips []net.Address) context.Context {
return context.WithValue(ctx, resolvedIPsKey, ips)
}
func ResolvedIPsFromContext(ctx context.Context) ([]net.Address, bool) {
ips, ok := ctx.Value(resolvedIPsKey).([]net.Address)
return ips, ok
}