| 1 |
package global |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"fmt" |
| 6 |
"io" |
| 7 |
"log/slog" |
| 8 |
"net" |
| 9 |
"net/http" |
| 10 |
pp "net/http/pprof" |
| 11 |
"os" |
| 12 |
"path/filepath" |
| 13 |
"runtime/pprof" |
| 14 |
"strconv" |
| 15 |
"strings" |
| 16 |
"sync" |
| 17 |
"time" |
| 18 |
|
| 19 |
"codeberg.org/miekg/dns/cmd/atomdns/internal/conffile" |
| 20 |
"codeberg.org/miekg/dns/cmd/atomdns/internal/dnslog" |
| 21 |
"github.com/caddyserver/certmagic" |
| 22 |
"github.com/prometheus/client_golang/prometheus/promhttp" |
| 23 |
) |
| 24 |
|
| 25 |
func (g *Global) Setup(d conffile.Dispenser) error { |
| 26 |
if d.Next() { |
| 27 |
switch d.Val() { |
| 28 |
case "log": |
| 29 |
for d.NextBlock(0) { |
| 30 |
switch d.Val() { |
| 31 |
case "debug": |
| 32 |
g.Debug = true |
| 33 |
slog.SetLogLoggerLevel(slog.LevelDebug) |
| 34 |
case "json": |
| 35 |
jlog := slog.New(slog.NewJSONHandler(os.Stderr, nil)) |
| 36 |
slog.SetDefault(jlog) |
| 37 |
case "quiet": |
| 38 |
g.Quiet = true |
| 39 |
case "disable": |
| 40 |
g.Disable = true |
| 41 |
case "enable": |
| 42 |
g.Disable = false |
| 43 |
default: |
| 44 |
return d.ArgErr() |
| 45 |
} |
| 46 |
} |
| 47 |
case "root": |
| 48 |
if !d.NextArg() { |
| 49 |
return d.ArgErr() |
| 50 |
} |
| 51 |
g.Root = d.Val() |
| 52 |
g.Root = conffile.Tilde(g.Root) |
| 53 |
if !filepath.IsAbs(g.Root) { |
| 54 |
pwd, _ := os.Getwd() |
| 55 |
g.Root = filepath.Join(pwd, g.Root) |
| 56 |
} |
| 57 |
g.OnStartup(func() error { |
| 58 |
log().Info("Startup", "root", g.Root) |
| 59 |
_, err := os.Stat(g.Root) |
| 60 |
return err |
| 61 |
}) |
| 62 |
case "tls": |
| 63 |
args := d.RemainingArgs() |
| 64 |
if len(args) != 1 { |
| 65 |
return d.ArgErr() |
| 66 |
} |
| 67 |
if args[0] != "manual" && args[0] != "lets-encrypt" { |
| 68 |
return d.PropErr(fmt.Errorf("expected %s or %s, got: %s", "manual", "lets-encrypt", args[0])) |
| 69 |
} |
| 70 |
if err := g.SetupTLS(&d); err != nil { |
| 71 |
return err |
| 72 |
} |
| 73 |
switch args[0] { |
| 74 |
case "manual": |
| 75 |
g.OnStartup(func() error { |
| 76 |
log().Info("Startup", "tls", args[0]) |
| 77 |
return nil |
| 78 |
}) |
| 79 |
case "lets-encrypt": |
| 80 |
if len(g.TlsIPs) != 0 && g.TlsContact != "" { |
| 81 |
g.TlsCertConfig = certmagic.NewDefault() |
| 82 |
ctx, cancel := context.WithCancel(context.Background()) |
| 83 |
g.OnStartup(func() error { |
| 84 |
log().Info("Startup", "tls", args[0], "IPs", dnslog.GroupValues("IP", g.TlsIPs)) |
| 85 |
err := certmagic.ManageAsync(ctx, g.TlsIPs) |
| 86 |
if err != nil { |
| 87 |
return err |
| 88 |
} |
| 89 |
return nil |
| 90 |
}) |
| 91 |
g.OnShutdown(func() error { |
| 92 |
log().Info("Shutdown", "tls", args[0], "IPs", dnslog.GroupValues("IP", g.TlsIPs)) |
| 93 |
cancel() |
| 94 |
return nil |
| 95 |
}) |
| 96 |
} |
| 97 |
} |
| 98 |
case "dns": |
| 99 |
g.Limits.Servers = 1 |
| 100 |
for d.NextBlock(0) { |
| 101 |
switch d.Val() { |
| 102 |
case "addr": |
| 103 |
addrs, err := d.RemainingAddrs() |
| 104 |
if err != nil { |
| 105 |
return d.PropErr(err) |
| 106 |
} |
| 107 |
if len(addrs) != 1 { |
| 108 |
return d.PropErr(fmt.Errorf("need single address")) |
| 109 |
} |
| 110 |
g.Addr = addrs[0] |
| 111 |
case "limits": |
| 112 |
l, err := g.SetupLimits(&d) |
| 113 |
if err != nil { |
| 114 |
return err |
| 115 |
} |
| 116 |
g.Limits.MaxTCPQueries = l.MaxTCPQueries |
| 117 |
if l.Servers != -1 { |
| 118 |
g.Limits.Servers = l.Servers |
| 119 |
} |
| 120 |
default: |
| 121 |
return d.ArgErr() |
| 122 |
} |
| 123 |
} |
| 124 |
g.OnStartup(func() error { |
| 125 |
log().Info("Startup", "dns", g.Addr, "tcp", g.Limits.MaxTCPQueries, "run", g.Limits.Servers) |
| 126 |
return nil |
| 127 |
}) |
| 128 |
|
| 129 |
case "dot": |
| 130 |
g.TlsLimits.Servers = 1 |
| 131 |
for d.NextBlock(0) { |
| 132 |
switch d.Val() { |
| 133 |
case "addr": |
| 134 |
addrs, err := d.RemainingAddrs() |
| 135 |
if err != nil { |
| 136 |
return d.PropErr(err) |
| 137 |
} |
| 138 |
if len(addrs) != 1 { |
| 139 |
return d.PropErr(fmt.Errorf("need single address")) |
| 140 |
} |
| 141 |
g.TlsAddr = addrs[0] |
| 142 |
case "limits": |
| 143 |
l, err := g.SetupLimits(&d) |
| 144 |
if err != nil { |
| 145 |
return err |
| 146 |
} |
| 147 |
g.TlsLimits.MaxTCPQueries = l.MaxTCPQueries |
| 148 |
if l.Servers != -1 { |
| 149 |
g.TlsLimits.Servers = l.Servers |
| 150 |
} |
| 151 |
if l.MaxInflight != -1 { |
| 152 |
g.TlsLimits.MaxInflight = l.MaxInflight |
| 153 |
} |
| 154 |
default: |
| 155 |
return d.ArgErr() |
| 156 |
} |
| 157 |
} |
| 158 |
inf := slog.Attr{} |
| 159 |
if g.TlsLimits.MaxInflight > 0 { |
| 160 |
inf = slog.Int("inflight", g.TlsLimits.MaxInflight) |
| 161 |
} |
| 162 |
g.OnStartup(func() error { |
| 163 |
log().Info("Startup", "dot", g.TlsAddr, "tcp", g.TlsLimits.MaxTCPQueries, "run", g.TlsLimits.Servers, inf) |
| 164 |
return nil |
| 165 |
}) |
| 166 |
|
| 167 |
case "doh": |
| 168 |
g.HttpLimits.Servers = 1 |
| 169 |
for d.NextBlock(0) { |
| 170 |
switch d.Val() { |
| 171 |
case "addr": |
| 172 |
addrs, err := d.RemainingAddrs() |
| 173 |
if err != nil { |
| 174 |
return d.PropErr(err) |
| 175 |
} |
| 176 |
if len(addrs) != 1 { |
| 177 |
return d.PropErr(fmt.Errorf("need single address")) |
| 178 |
} |
| 179 |
g.HttpAddr = addrs[0] |
| 180 |
case "limits": |
| 181 |
l, err := g.SetupLimits(&d) |
| 182 |
if err != nil { |
| 183 |
return err |
| 184 |
} |
| 185 |
if l.Servers != -1 { |
| 186 |
g.HttpLimits.Servers = l.Servers |
| 187 |
} |
| 188 |
if l.MaxInflight != -1 { |
| 189 |
g.HttpLimits.MaxInflight = l.MaxInflight |
| 190 |
} |
| 191 |
default: |
| 192 |
return d.ArgErr() |
| 193 |
} |
| 194 |
} |
| 195 |
inf := slog.Attr{} |
| 196 |
if g.HttpLimits.MaxInflight > 0 { |
| 197 |
inf = slog.Int("inflight", g.HttpLimits.MaxInflight) |
| 198 |
} |
| 199 |
g.OnStartup(func() error { |
| 200 |
log().Info("Startup", "doh", g.HttpAddr, "run", g.HttpLimits.Servers, inf, "path", "/dns-query") |
| 201 |
return nil |
| 202 |
}) |
| 203 |
case "dou": |
| 204 |
g.UnixLimits.Servers = 1 |
| 205 |
g.UnixLimits.MaxTCPQueries = -1 |
| 206 |
for d.NextBlock(0) { |
| 207 |
switch d.Val() { |
| 208 |
case "addr": |
| 209 |
files := d.RemainingArgs() |
| 210 |
if len(files) != 1 { |
| 211 |
return d.PropErr(fmt.Errorf("need single socket")) |
| 212 |
} |
| 213 |
g.UnixAddr = conffile.Tilde(files[0]) |
| 214 |
if !filepath.IsAbs(g.UnixAddr) { |
| 215 |
g.UnixAddr = filepath.Join(g.Root, g.UnixAddr) |
| 216 |
} |
| 217 |
default: |
| 218 |
return d.ArgErr() |
| 219 |
} |
| 220 |
} |
| 221 |
g.OnStartup(func() error { |
| 222 |
log().Info("Startup", "dou", g.UnixAddr, "tcp", "-1", "run", g.UnixLimits.Servers) |
| 223 |
return nil |
| 224 |
}) |
| 225 |
g.OnShutdown(func() error { |
| 226 |
log().Info("Shutdown", "dou", g.UnixAddr) |
| 227 |
os.Remove(g.UnixAddr) |
| 228 |
return nil |
| 229 |
}) |
| 230 |
|
| 231 |
case "metrics": |
| 232 |
g.MetricsN = 10 |
| 233 |
addr := "localhost:9153" |
| 234 |
if d.NextArg() { |
| 235 |
if !strings.HasPrefix(d.Val(), "/") { |
| 236 |
addr = d.Val() |
| 237 |
} else { |
| 238 |
n, err := strconv.Atoi(d.Val()[1:]) |
| 239 |
if err != nil || n < 0 { |
| 240 |
return d.PropErr(fmt.Errorf("not a (positive) number: %s", d.Val()[1:])) |
| 241 |
} |
| 242 |
} |
| 243 |
} |
| 244 |
if d.NextArg() { |
| 245 |
addr = d.Val() |
| 246 |
} |
| 247 |
g.OnStartup(func() error { |
| 248 |
log().Info("Startup", "/metrics", addr, slog.Int64("/N", int64(g.MetricsN))) |
| 249 |
ln, err := net.Listen("tcp", addr) |
| 250 |
if err != nil { |
| 251 |
return err |
| 252 |
} |
| 253 |
mux := http.NewServeMux() |
| 254 |
mux.Handle("/metrics", promhttp.Handler()) |
| 255 |
server := &http.Server{Handler: mux, ReadTimeout: 5 * time.Second} |
| 256 |
go func() { server.Serve(ln) }() |
| 257 |
g.MetricsListener = ln |
| 258 |
return nil |
| 259 |
}) |
| 260 |
g.OnShutdown(func() error { |
| 261 |
log().Info("Shutdown", "/metrics", addr) |
| 262 |
g.MetricsListener.Close() |
| 263 |
return nil |
| 264 |
}) |
| 265 |
case "health": |
| 266 |
addr := ":8080" |
| 267 |
if d.Next() { |
| 268 |
addr = d.Val() |
| 269 |
} |
| 270 |
if d.Next() { |
| 271 |
delay, err := time.ParseDuration(d.Val()) |
| 272 |
if err != nil || delay < 0 { |
| 273 |
return d.PropErr(fmt.Errorf("not a (positive) number: %s", d.Val())) |
| 274 |
} |
| 275 |
g.Lameduck = delay |
| 276 |
} |
| 277 |
g.OnStartup(func() error { |
| 278 |
log().Info("Startup", "/health", addr) |
| 279 |
ln, err := net.Listen("tcp", addr) |
| 280 |
if err != nil { |
| 281 |
return err |
| 282 |
} |
| 283 |
mux := http.NewServeMux() |
| 284 |
mux.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) { |
| 285 |
w.WriteHeader(http.StatusOK) |
| 286 |
io.WriteString(w, http.StatusText(http.StatusOK)) |
| 287 |
}) |
| 288 |
|
| 289 |
server := &http.Server{Handler: mux, ReadTimeout: 5 * time.Second} |
| 290 |
go func() { server.Serve(ln) }() |
| 291 |
g.HealthListener = ln |
| 292 |
return nil |
| 293 |
}) |
| 294 |
|
| 295 |
g.OnShutdown(func() error { |
| 296 |
log().Info("Shutdown", "/health", addr) |
| 297 |
g.HealthListener.Close() |
| 298 |
return nil |
| 299 |
}) |
| 300 |
if g.Lameduck > 0 { |
| 301 |
g.OnShutdown(func() error { |
| 302 |
log().Info("Shutdown", "lameduck", g.Lameduck) |
| 303 |
g.HealthListener.Close() |
| 304 |
time.Sleep(g.Lameduck) |
| 305 |
return nil |
| 306 |
}) |
| 307 |
} |
| 308 |
ctx := context.Background() |
| 309 |
ctx, cancel := context.WithCancel(ctx) |
| 310 |
g.OnStartup(func() error { |
| 311 |
log().Info("Startup", "health", "overload check") |
| 312 |
go overload(ctx, addr) |
| 313 |
return nil |
| 314 |
}) |
| 315 |
g.OnShutdown(func() error { |
| 316 |
log().Info("Shutdown", "health", "overload check") |
| 317 |
cancel() |
| 318 |
return nil |
| 319 |
}) |
| 320 |
case "pprof": |
| 321 |
addr := "localhost:6053" |
| 322 |
path := false |
| 323 |
if d.NextArg() { |
| 324 |
addr = d.Val() |
| 325 |
} |
| 326 |
if _, err := net.ResolveTCPAddr("tcp", addr); err != nil { // not an address, thus file |
| 327 |
addr = conffile.Tilde(addr) |
| 328 |
if !filepath.IsAbs(addr) { |
| 329 |
addr = filepath.Join(g.Root, addr) |
| 330 |
} |
| 331 |
path = true |
| 332 |
} |
| 333 |
|
| 334 |
g.OnStartup(func() (err error) { |
| 335 |
switch path { |
| 336 |
case false: |
| 337 |
log().Info("Startup", "/debug/pprof", addr) |
| 338 |
ln, err := net.Listen("tcp", addr) |
| 339 |
if err != nil { |
| 340 |
return err |
| 341 |
} |
| 342 |
mux := http.NewServeMux() |
| 343 |
mux.Handle("/metrics", promhttp.Handler()) |
| 344 |
mux.HandleFunc("/debug/pprof/", pp.Index) |
| 345 |
mux.HandleFunc("/debug/pprof/cmdline", pp.Cmdline) |
| 346 |
mux.HandleFunc("/debug/pprof/profile", pp.Profile) |
| 347 |
mux.HandleFunc("/debug/pprof/symbol", pp.Symbol) |
| 348 |
mux.HandleFunc("/debug/pprof/trace", pp.Trace) |
| 349 |
server := &http.Server{Handler: mux, ReadTimeout: 5 * time.Second} |
| 350 |
go func() { server.Serve(ln) }() |
| 351 |
g.PprofListener = ln |
| 352 |
case true: |
| 353 |
log().Info("Startup", "pprof", filepath.Base(addr)) |
| 354 |
if g.PprofWriter, err = os.Create(addr); err != nil { |
| 355 |
return err |
| 356 |
} |
| 357 |
pprof.StartCPUProfile(g.PprofWriter) |
| 358 |
} |
| 359 |
return nil |
| 360 |
}) |
| 361 |
g.OnShutdown(func() error { |
| 362 |
switch path { |
| 363 |
case false: |
| 364 |
log().Info("Shutdown", "/debug/pprof", addr) |
| 365 |
g.PprofListener.Close() |
| 366 |
case true: |
| 367 |
log().Info("Shutdown", "pprof", filepath.Base(addr)) |
| 368 |
pprof.StopCPUProfile() |
| 369 |
g.PprofWriter.Close() |
| 370 |
} |
| 371 |
return nil |
| 372 |
}) |
| 373 |
default: |
| 374 |
return d.PropErr() |
| 375 |
} |
| 376 |
} |
| 377 |
g.OnReset(func() { |
| 378 |
g.onceStartup = sync.Once{} |
| 379 |
g.onStartup = []func() error{} |
| 380 |
|
| 381 |
g.onceShutdown = sync.Once{} |
| 382 |
g.onShutdown = []func() error{} |
| 383 |
}) |
| 384 |
|
| 385 |
return nil |
| 386 |
} |