2017-01-12 18:56:21 -05:00
|
|
|
package proxy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2017-01-26 14:46:44 -05:00
|
|
|
|
|
|
|
"v2ray.com/core/common/net"
|
2017-01-12 18:56:21 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type key int
|
|
|
|
|
|
|
|
const (
|
2017-02-03 16:35:09 -05:00
|
|
|
sourceKey key = iota
|
2017-01-26 14:46:44 -05:00
|
|
|
destinationKey
|
|
|
|
originalDestinationKey
|
|
|
|
inboundDestinationKey
|
|
|
|
inboundTagKey
|
|
|
|
outboundTagKey
|
|
|
|
resolvedIPsKey
|
|
|
|
allowPassiveConnKey
|
2017-02-03 16:35:09 -05:00
|
|
|
dispatcherKey
|
2017-01-12 18:56:21 -05:00
|
|
|
)
|
|
|
|
|
2017-01-26 14:46:44 -05:00
|
|
|
func ContextWithSource(ctx context.Context, src net.Destination) context.Context {
|
|
|
|
return context.WithValue(ctx, sourceKey, src)
|
2017-01-12 18:56:21 -05:00
|
|
|
}
|
|
|
|
|
2017-01-26 14:46:44 -05:00
|
|
|
func SourceFromContext(ctx context.Context) net.Destination {
|
|
|
|
v := ctx.Value(sourceKey)
|
2017-01-12 18:56:21 -05:00
|
|
|
if v == nil {
|
2017-01-26 14:46:44 -05:00
|
|
|
return net.Destination{}
|
2017-01-12 18:56:21 -05:00
|
|
|
}
|
2017-01-26 14:46:44 -05:00
|
|
|
return v.(net.Destination)
|
2017-01-12 18:56:21 -05:00
|
|
|
}
|
2017-01-14 18:48:37 -05:00
|
|
|
|
2017-01-26 14:46:44 -05:00
|
|
|
func ContextWithOriginalDestination(ctx context.Context, dest net.Destination) context.Context {
|
|
|
|
return context.WithValue(ctx, originalDestinationKey, dest)
|
2017-01-14 18:48:37 -05:00
|
|
|
}
|
|
|
|
|
2017-01-26 14:46:44 -05:00
|
|
|
func OriginalDestinationFromContext(ctx context.Context) net.Destination {
|
|
|
|
v := ctx.Value(originalDestinationKey)
|
2017-01-14 18:48:37 -05:00
|
|
|
if v == nil {
|
2017-01-26 14:46:44 -05:00
|
|
|
return net.Destination{}
|
2017-01-14 18:48:37 -05:00
|
|
|
}
|
2017-01-26 14:46:44 -05:00
|
|
|
return v.(net.Destination)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ContextWithDestination(ctx context.Context, dest net.Destination) context.Context {
|
|
|
|
return context.WithValue(ctx, destinationKey, dest)
|
|
|
|
}
|
|
|
|
|
|
|
|
func DestinationFromContext(ctx context.Context) net.Destination {
|
|
|
|
v := ctx.Value(destinationKey)
|
|
|
|
if v == nil {
|
|
|
|
return net.Destination{}
|
|
|
|
}
|
|
|
|
return v.(net.Destination)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ContextWithInboundDestination(ctx context.Context, dest net.Destination) context.Context {
|
|
|
|
return context.WithValue(ctx, inboundDestinationKey, dest)
|
|
|
|
}
|
|
|
|
|
|
|
|
func InboundDestinationFromContext(ctx context.Context) net.Destination {
|
|
|
|
v := ctx.Value(inboundDestinationKey)
|
|
|
|
if v == nil {
|
|
|
|
return net.Destination{}
|
|
|
|
}
|
|
|
|
return v.(net.Destination)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ContextWithInboundTag(ctx context.Context, tag string) context.Context {
|
|
|
|
return context.WithValue(ctx, inboundTagKey, tag)
|
|
|
|
}
|
|
|
|
|
|
|
|
func InboundTagFromContext(ctx context.Context) string {
|
|
|
|
v := ctx.Value(inboundTagKey)
|
|
|
|
if v == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return v.(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ContextWithOutboundTag(ctx context.Context, tag string) context.Context {
|
|
|
|
return context.WithValue(ctx, outboundTagKey, tag)
|
|
|
|
}
|
|
|
|
|
|
|
|
func OutboundTagFromContext(ctx context.Context) string {
|
|
|
|
v := ctx.Value(outboundTagKey)
|
|
|
|
if v == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return v.(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func ContextWithAllowPassiveConnection(ctx context.Context, allowPassiveConnection bool) context.Context {
|
|
|
|
return context.WithValue(ctx, allowPassiveConnKey, allowPassiveConnection)
|
|
|
|
}
|
|
|
|
|
|
|
|
func AllowPassiveConnectionFromContext(ctx context.Context) (bool, bool) {
|
|
|
|
allow, ok := ctx.Value(allowPassiveConnKey).(bool)
|
|
|
|
return allow, ok
|
2017-01-14 18:48:37 -05:00
|
|
|
}
|