2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2021-12-21 21:52:50 +00:00
|
|
|
|
|
|
|
package resolver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"html"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"tailscale.com/health"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
health.RegisterDebugHandler("dnsfwd", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
n, _ := strconv.Atoi(r.FormValue("n"))
|
|
|
|
if n <= 0 {
|
|
|
|
n = 100
|
|
|
|
} else if n > 10000 {
|
|
|
|
n = 10000
|
|
|
|
}
|
2022-08-04 04:31:40 +00:00
|
|
|
fl := fwdLogAtomic.Load()
|
|
|
|
if fl == nil || n != len(fl.ent) {
|
2021-12-21 21:52:50 +00:00
|
|
|
fl = &fwdLog{ent: make([]fwdLogEntry, n)}
|
|
|
|
fwdLogAtomic.Store(fl)
|
|
|
|
}
|
|
|
|
fl.ServeHTTP(w, r)
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2022-08-04 04:31:40 +00:00
|
|
|
var fwdLogAtomic atomic.Pointer[fwdLog]
|
2021-12-21 21:52:50 +00:00
|
|
|
|
|
|
|
type fwdLog struct {
|
|
|
|
mu sync.Mutex
|
|
|
|
pos int // ent[pos] is next entry
|
|
|
|
ent []fwdLogEntry
|
|
|
|
}
|
|
|
|
|
|
|
|
type fwdLogEntry struct {
|
|
|
|
Domain string
|
|
|
|
Time time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fl *fwdLog) addName(name string) {
|
|
|
|
if fl == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fl.mu.Lock()
|
|
|
|
defer fl.mu.Unlock()
|
|
|
|
if len(fl.ent) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fl.ent[fl.pos] = fwdLogEntry{Domain: name, Time: time.Now()}
|
|
|
|
fl.pos++
|
|
|
|
if fl.pos == len(fl.ent) {
|
|
|
|
fl.pos = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fl *fwdLog) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
fl.mu.Lock()
|
|
|
|
defer fl.mu.Unlock()
|
|
|
|
|
|
|
|
fmt.Fprintf(w, "<html><h1>DNS forwards</h1>")
|
|
|
|
now := time.Now()
|
2024-04-16 20:15:13 +00:00
|
|
|
for i := range len(fl.ent) {
|
2021-12-21 21:52:50 +00:00
|
|
|
ent := fl.ent[(i+fl.pos)%len(fl.ent)]
|
|
|
|
if ent.Domain == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
fmt.Fprintf(w, "%v ago: %v<br>\n", now.Sub(ent.Time).Round(time.Second), html.EscapeString(ent.Domain))
|
|
|
|
}
|
|
|
|
}
|