-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
123 lines (101 loc) · 2.93 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package proxy
import (
"context"
"v2ray.com/core/common/net"
)
type key int
const (
dialerKey key = iota
sourceKey
destinationKey
originalDestinationKey
inboundDestinationKey
inboundTagKey
outboundTagKey
resolvedIPsKey
allowPassiveConnKey
)
func ContextWithDialer(ctx context.Context, dialer Dialer) context.Context {
return context.WithValue(ctx, dialerKey, dialer)
}
func DialerFromContext(ctx context.Context) Dialer {
v := ctx.Value(dialerKey)
if v == nil {
return nil
}
return v.(Dialer)
}
func ContextWithSource(ctx context.Context, src net.Destination) context.Context {
return context.WithValue(ctx, sourceKey, src)
}
func SourceFromContext(ctx context.Context) net.Destination {
v := ctx.Value(sourceKey)
if v == nil {
return net.Destination{}
}
return v.(net.Destination)
}
func ContextWithOriginalDestination(ctx context.Context, dest net.Destination) context.Context {
return context.WithValue(ctx, originalDestinationKey, dest)
}
func OriginalDestinationFromContext(ctx context.Context) net.Destination {
v := ctx.Value(originalDestinationKey)
if v == nil {
return net.Destination{}
}
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
}