forked from v2ray/v2ray-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
72 lines (56 loc) · 1.85 KB
/
context.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package proxy
import (
"context"
"v2ray.com/core/common/net"
)
type key int
const (
sourceKey key = iota
targetKey
originalTargetKey
inboundEntryPointKey
inboundTagKey
resolvedIPsKey
)
func ContextWithSource(ctx context.Context, src net.Destination) context.Context {
return context.WithValue(ctx, sourceKey, src)
}
func SourceFromContext(ctx context.Context) (net.Destination, bool) {
v, ok := ctx.Value(sourceKey).(net.Destination)
return v, ok
}
func ContextWithOriginalTarget(ctx context.Context, dest net.Destination) context.Context {
return context.WithValue(ctx, originalTargetKey, dest)
}
func OriginalTargetFromContext(ctx context.Context) (net.Destination, bool) {
v, ok := ctx.Value(originalTargetKey).(net.Destination)
return v, ok
}
func ContextWithTarget(ctx context.Context, dest net.Destination) context.Context {
return context.WithValue(ctx, targetKey, dest)
}
func TargetFromContext(ctx context.Context) (net.Destination, bool) {
v, ok := ctx.Value(targetKey).(net.Destination)
return v, ok
}
func ContextWithInboundEntryPoint(ctx context.Context, dest net.Destination) context.Context {
return context.WithValue(ctx, inboundEntryPointKey, dest)
}
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)
}
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
}