| 1 |
package global |
| 2 |
|
| 3 |
import ( |
| 4 |
"crypto/tls" |
| 5 |
"io" |
| 6 |
"net" |
| 7 |
"sync" |
| 8 |
"time" |
| 9 |
|
| 10 |
"codeberg.org/miekg/dns" |
| 11 |
"codeberg.org/miekg/dns/cmd/atomdns/internal/conffile" |
| 12 |
"github.com/caddyserver/certmagic" |
| 13 |
) |
| 14 |
|
| 15 |
type Global struct { |
| 16 |
// Root |
| 17 |
Root string |
| 18 |
// Logging |
| 19 |
Debug bool |
| 20 |
Quiet bool |
| 21 |
Disable bool |
| 22 |
// Metrics |
| 23 |
MetricsN uint64 |
| 24 |
MetricsListener net.Listener |
| 25 |
// Health |
| 26 |
Lameduck time.Duration |
| 27 |
HealthListener net.Listener |
| 28 |
// Pprof |
| 29 |
PprofListener net.Listener |
| 30 |
PprofWriter io.WriteCloser |
| 31 |
// dns |
| 32 |
Addr string |
| 33 |
Limits Limits |
| 34 |
// doh |
| 35 |
HttpAddr string |
| 36 |
HttpLimits Limits |
| 37 |
// dot |
| 38 |
TlsAddr string |
| 39 |
TlsLimits Limits |
| 40 |
// dou |
| 41 |
UnixAddr string |
| 42 |
UnixLimits Limits |
| 43 |
// tls |
| 44 |
TlsConfig *tls.Config // manual |
| 45 |
TlsCertConfig *certmagic.Config |
| 46 |
TlsIPs []string // lets-encrypt, IP to get certs for |
| 47 |
TlsContact string // lets-encrypt |
| 48 |
TlsPath string // lets-encrypt |
| 49 |
|
| 50 |
onceStartup sync.Once |
| 51 |
onceShutdown sync.Once |
| 52 |
onceReset sync.Once |
| 53 |
onStartup []func() error // Functions to execute on startup |
| 54 |
onShutdown []func() error // Function to execute on shutdown |
| 55 |
onReset []func() // Function to execute after shutdown has been called |
| 56 |
|
| 57 |
Config string // path to config file |
| 58 |
Registered map[conffile.ZoneClass]struct{} // registered zones and classes |
| 59 |
} |
| 60 |
|
| 61 |
func (g *Global) HandlerFunc(next dns.HandlerFunc) dns.HandlerFunc { return nil } |
| 62 |
|
| 63 |
func (g *Global) OnStartup(fn func() error) { g.onStartup = append(g.onStartup, fn) } |
| 64 |
func (g *Global) OnShutdown(fn func() error) { g.onShutdown = append(g.onShutdown, fn) } |
| 65 |
func (g *Global) OnReset(fn func()) { g.onReset = append(g.onReset, fn) } |
| 66 |
|
| 67 |
func (g *Global) Startup() error { |
| 68 |
errs := []error{} |
| 69 |
wg := sync.WaitGroup{} |
| 70 |
g.onceStartup.Do(func() { |
| 71 |
wg.Go(func() { |
| 72 |
for _, fn := range g.onStartup { |
| 73 |
if err := fn(); err != nil { |
| 74 |
errs = append(errs, err) |
| 75 |
} |
| 76 |
} |
| 77 |
}) |
| 78 |
}) |
| 79 |
wg.Wait() |
| 80 |
for _, e := range errs { |
| 81 |
if e != nil { |
| 82 |
return e |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
return nil |
| 87 |
} |
| 88 |
|
| 89 |
func (g *Global) Shutdown() error { |
| 90 |
errs := []error{} |
| 91 |
wg := sync.WaitGroup{} |
| 92 |
g.onceShutdown.Do(func() { |
| 93 |
for _, fn := range g.onShutdown { |
| 94 |
wg.Go(func() { |
| 95 |
if err := fn(); err != nil { |
| 96 |
errs = append(errs, err) |
| 97 |
} |
| 98 |
}) |
| 99 |
} |
| 100 |
}) |
| 101 |
wg.Wait() |
| 102 |
for _, e := range errs { |
| 103 |
if e != nil { |
| 104 |
return e |
| 105 |
} |
| 106 |
} |
| 107 |
g.onceReset.Do(func() { |
| 108 |
for _, fn := range g.onReset { |
| 109 |
fn() |
| 110 |
} |
| 111 |
}) |
| 112 |
g.onceReset = sync.Once{} |
| 113 |
return nil |
| 114 |
} |
| 115 |
|
| 116 |
func (g *Global) HasTls() bool { |
| 117 |
return g.TlsCertConfig != nil || g.TlsConfig != nil |
| 118 |
} |