Debian Codegraph @

cmd/atomdns/internal/dnsctx/dnsctx.go

raw
1 // package dnsctx helps with setting and getting data from the context of the current query.
2 // See the [Match] function source for what types are currently supported. When adding something to the
3 // context there is no check done on the type to not slow down the handler.
4 package dnsctx
5
6 import (
7 "context"
8 "net/netip"
9 "slices"
10 "strings"
11
12 "codeberg.org/miekg/dns"
13 )
14
15 type Keyer interface {
16 // Key returns the "key" of the handler.
17 Key() string
18 }
19
20 // Func is a function that can be set in the context and operates on a [dns.Msg].
21 type Func func(*dns.Msg) *dns.Msg
22
23 type funcsKey struct{}
24
25 // WithFunc appends the Func f in the context under the funcsKey. It is not possible to retrieve a specific
26 // Func. You can only run through them using [Funcs].
27 func WithFunc(ctx context.Context, handler Keyer, f Func) context.Context {
28 v := ctx.Value(funcsKey{})
29 if v == nil {
30 return context.WithValue(ctx, funcsKey{}, []Func{f})
31 }
32 return context.WithValue(ctx, funcsKey{}, append(v.([]Func), f))
33 }
34
35 // Predefined context keys.
36 const KeyStatus = "status"
37
38 // Key creates a key from the keyer and string.
39 func Key(handler Keyer, key string) string { return handler.Key() + "/" + key }
40
41 // Funcs iterates over all functions that are set in the context over the message. The possibly
42 // modified message is returned.
43 func Funcs(ctx context.Context, m *dns.Msg) *dns.Msg {
44 v := ctx.Value(funcsKey{})
45 if v == nil {
46 return m
47 }
48 for _, f := range v.([]Func) {
49 m = f(m)
50 }
51 return m
52 }
53
54 // WithValue stores value under the string value key, key must contain a slash and be formatted like
55 // "<handler>/xxx". If key does not contain a slash, this function is noop.
56 func WithValue(ctx context.Context, key string, value any) context.Context {
57 if !Valid(key) {
58 return ctx
59 }
60 return context.WithValue(ctx, key, value)
61 }
62
63 // Value returns the value under key. If key does not contain a slash nil is returned.
64 func Value(ctx context.Context, key string) any {
65 if !Valid(key) {
66 return ""
67 }
68 v := ctx.Value(key)
69 if v == nil {
70 return nil
71 }
72 return v
73 }
74
75 // Valid returns a boolean indicating if the key is a valid context key.
76 func Valid(key string) bool {
77 if len(key) < 3 {
78 return false
79 }
80 return strings.Contains(key, "/")
81 }
82
83 // Match checks the value under key and see if it matches any of the elements in the list.
84 // This function handles strings, string slices, ints, flaot64s, net.IPs and bools. A nil value for key returns true.
85 func Match(ctx context.Context, key string, values []any) bool {
86 value := Value(ctx, key)
87 if value == nil {
88 return true
89 }
90 switch x := value.(type) {
91 case bool:
92 for _, v := range values {
93 if b, ok := v.(bool); ok && b == x {
94 return true
95 }
96 }
97 case string:
98 for _, v := range values {
99 if s, ok := v.(string); ok && s == x {
100 return true
101 }
102 }
103 case []string:
104 for _, v := range values {
105 if slices.Contains(value.([]string), v.(string)) {
106 return true
107 }
108 }
109 case int:
110 for _, v := range values {
111 if i, ok := v.(int); ok && i == x {
112 return true
113 }
114 }
115 case float64:
116 for _, v := range values {
117 if f, ok := v.(float64); ok && f == x {
118 return true
119 }
120 }
121 case netip.Addr:
122 for _, v := range values {
123 if ip, ok := v.(netip.Addr); ok && ip == x {
124 return true
125 }
126 }
127 }
128 return false
129 }