forked from SagerNet/sing-dns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extensions.go
56 lines (43 loc) · 1.38 KB
/
extensions.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
package dns
import (
"context"
"net/netip"
)
type disableCacheKey struct{}
func ContextWithDisableCache(ctx context.Context, val bool) context.Context {
return context.WithValue(ctx, (*disableCacheKey)(nil), val)
}
func DisableCacheFromContext(ctx context.Context) bool {
val := ctx.Value((*disableCacheKey)(nil))
if val == nil {
return false
}
return val.(bool)
}
type rewriteTTLKey struct{}
func ContextWithRewriteTTL(ctx context.Context, val uint32) context.Context {
return context.WithValue(ctx, (*rewriteTTLKey)(nil), val)
}
func RewriteTTLFromContext(ctx context.Context) (uint32, bool) {
val := ctx.Value((*rewriteTTLKey)(nil))
if val == nil {
return 0, false
}
return val.(uint32), true
}
type transportKey struct{}
func contextWithTransportName(ctx context.Context, transportName string) context.Context {
return context.WithValue(ctx, transportKey{}, transportName)
}
func transportNameFromContext(ctx context.Context) (string, bool) {
value, loaded := ctx.Value(transportKey{}).(string)
return value, loaded
}
type clientSubnetKey struct{}
func ContextWithClientSubnet(ctx context.Context, clientSubnet netip.Prefix) context.Context {
return context.WithValue(ctx, clientSubnetKey{}, clientSubnet)
}
func ClientSubnetFromContext(ctx context.Context) (netip.Prefix, bool) {
clientSubnet, ok := ctx.Value(clientSubnetKey{}).(netip.Prefix)
return clientSubnet, ok
}