|
| 1 | +package proxy |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/AdguardTeam/dnsproxy/upstream" |
| 8 | + "github.com/miekg/dns" |
| 9 | +) |
| 10 | + |
| 11 | +// upstreamWithStats is a wrapper around the [upstream.Upstream] interface that |
| 12 | +// gathers statistics. |
| 13 | +type upstreamWithStats struct { |
| 14 | + // upstream is the upstream DNS resolver. |
| 15 | + upstream upstream.Upstream |
| 16 | + |
| 17 | + // err is the DNS lookup error, if any. |
| 18 | + err error |
| 19 | + |
| 20 | + // queryDuration is the duration of the successful DNS lookup. |
| 21 | + queryDuration time.Duration |
| 22 | +} |
| 23 | + |
| 24 | +// type check |
| 25 | +var _ upstream.Upstream = (*upstreamWithStats)(nil) |
| 26 | + |
| 27 | +// Exchange implements the [upstream.Upstream] for *upstreamWithStats. |
| 28 | +func (u *upstreamWithStats) Exchange(req *dns.Msg) (resp *dns.Msg, err error) { |
| 29 | + start := time.Now() |
| 30 | + resp, err = u.upstream.Exchange(req) |
| 31 | + u.err = err |
| 32 | + u.queryDuration = time.Since(start) |
| 33 | + |
| 34 | + return resp, err |
| 35 | +} |
| 36 | + |
| 37 | +// Address implements the [upstream.Upstream] for *upstreamWithStats. |
| 38 | +func (u *upstreamWithStats) Address() (addr string) { |
| 39 | + return u.upstream.Address() |
| 40 | +} |
| 41 | + |
| 42 | +// Close implements the [upstream.Upstream] for *upstreamWithStats. |
| 43 | +func (u *upstreamWithStats) Close() (err error) { |
| 44 | + return u.upstream.Close() |
| 45 | +} |
| 46 | + |
| 47 | +// upstreamsWithStats takes a list of upstreams, wraps each upstream with |
| 48 | +// [upstreamWithStats] to gather statistics, and returns the wrapped upstreams. |
| 49 | +func upstreamsWithStats(upstreams []upstream.Upstream) (wrapped []upstream.Upstream) { |
| 50 | + wrapped = make([]upstream.Upstream, 0, len(upstreams)) |
| 51 | + for _, u := range upstreams { |
| 52 | + wrapped = append(wrapped, &upstreamWithStats{upstream: u}) |
| 53 | + } |
| 54 | + |
| 55 | + return wrapped |
| 56 | +} |
| 57 | + |
| 58 | +// QueryStatistics contains the DNS query statistics for both the upstream and |
| 59 | +// fallback DNS servers. |
| 60 | +type QueryStatistics struct { |
| 61 | + main []*UpstreamStatistics |
| 62 | + fallback []*UpstreamStatistics |
| 63 | +} |
| 64 | + |
| 65 | +// cachedQueryStatistics returns the DNS query statistics for cached queries. |
| 66 | +func cachedQueryStatistics(addr string) (s *QueryStatistics) { |
| 67 | + return &QueryStatistics{ |
| 68 | + main: []*UpstreamStatistics{{ |
| 69 | + Address: addr, |
| 70 | + IsCached: true, |
| 71 | + }}, |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +// Main returns the DNS query statistics for the upstream DNS servers. |
| 76 | +func (s *QueryStatistics) Main() (us []*UpstreamStatistics) { |
| 77 | + return s.main |
| 78 | +} |
| 79 | + |
| 80 | +// Fallback returns the DNS query statistics for the fallback DNS servers. |
| 81 | +func (s *QueryStatistics) Fallback() (us []*UpstreamStatistics) { |
| 82 | + return s.fallback |
| 83 | +} |
| 84 | + |
| 85 | +// collectQueryStats gathers the statistics from the wrapped upstreams. |
| 86 | +// resolver is an upstream DNS resolver that successfully resolved the request, |
| 87 | +// if any. Provided upstreams must be of type [*upstreamWithStats]. unwrapped |
| 88 | +// is the unwrapped resolver, see [upstreamWithStats.upstream]. The returned |
| 89 | +// statistics depend on whether the DNS request was successfully resolved and |
| 90 | +// the upstream mode, see [DNSContext.QueryStatistics]. |
| 91 | +func collectQueryStats( |
| 92 | + mode UpstreamMode, |
| 93 | + resolver upstream.Upstream, |
| 94 | + upstreams []upstream.Upstream, |
| 95 | + fallbacks []upstream.Upstream, |
| 96 | +) (unwrapped upstream.Upstream, stats *QueryStatistics) { |
| 97 | + var wrapped *upstreamWithStats |
| 98 | + if resolver != nil { |
| 99 | + var ok bool |
| 100 | + wrapped, ok = resolver.(*upstreamWithStats) |
| 101 | + if !ok { |
| 102 | + // Should never happen. |
| 103 | + panic(fmt.Errorf("unexpected type %T", resolver)) |
| 104 | + } |
| 105 | + |
| 106 | + unwrapped = wrapped.upstream |
| 107 | + } |
| 108 | + |
| 109 | + // The DNS query was not resolved. |
| 110 | + if wrapped == nil { |
| 111 | + return nil, &QueryStatistics{ |
| 112 | + main: collectUpstreamStats(upstreams...), |
| 113 | + fallback: collectUpstreamStats(fallbacks...), |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + // The DNS query was successfully resolved by main resolver and the upstream |
| 118 | + // mode is [UpstreamModeFastestAddr]. |
| 119 | + if mode == UpstreamModeFastestAddr && len(fallbacks) == 0 { |
| 120 | + return unwrapped, &QueryStatistics{ |
| 121 | + main: collectUpstreamStats(upstreams...), |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + // The DNS query was resolved by fallback resolver. |
| 126 | + if len(fallbacks) > 0 { |
| 127 | + return unwrapped, &QueryStatistics{ |
| 128 | + main: collectUpstreamStats(upstreams...), |
| 129 | + fallback: collectUpstreamStats(wrapped), |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + // The DNS query was successfully resolved by main resolver. |
| 134 | + return unwrapped, &QueryStatistics{ |
| 135 | + main: collectUpstreamStats(wrapped), |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +// UpstreamStatistics contains the DNS query statistics. |
| 140 | +type UpstreamStatistics struct { |
| 141 | + // Error is the DNS lookup error, if any. |
| 142 | + Error error |
| 143 | + |
| 144 | + // Address is the address of the upstream DNS resolver. |
| 145 | + // |
| 146 | + // TODO(s.chzhen): Use [upstream.Upstream] when [cacheItem] starts to |
| 147 | + // contain one. |
| 148 | + Address string |
| 149 | + |
| 150 | + // QueryDuration is the duration of the successful DNS lookup. |
| 151 | + QueryDuration time.Duration |
| 152 | + |
| 153 | + // IsCached indicates whether the response was served from a cache. |
| 154 | + IsCached bool |
| 155 | +} |
| 156 | + |
| 157 | +// collectUpstreamStats gathers the upstream statistics from the list of wrapped |
| 158 | +// upstreams. upstreams must be of type *upstreamWithStats. |
| 159 | +func collectUpstreamStats(upstreams ...upstream.Upstream) (stats []*UpstreamStatistics) { |
| 160 | + stats = make([]*UpstreamStatistics, 0, len(upstreams)) |
| 161 | + |
| 162 | + for _, u := range upstreams { |
| 163 | + w, ok := u.(*upstreamWithStats) |
| 164 | + if !ok { |
| 165 | + // Should never happen. |
| 166 | + panic(fmt.Errorf("unexpected type %T", u)) |
| 167 | + } |
| 168 | + |
| 169 | + stats = append(stats, &UpstreamStatistics{ |
| 170 | + Error: w.err, |
| 171 | + Address: w.Address(), |
| 172 | + QueryDuration: w.queryDuration, |
| 173 | + }) |
| 174 | + } |
| 175 | + |
| 176 | + return stats |
| 177 | +} |
0 commit comments