-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
100 lines (88 loc) · 2.15 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package main
import (
"flag"
"io/ioutil"
"log"
"os"
"os/signal"
"runtime"
"runtime/pprof"
"syscall"
"github.com/miekg/dns"
"gopkg.in/yaml.v2"
)
var (
configPath = flag.String("c", "./config.yml", "path of configuration file")
config = &Config{
Netbox: netboxConfig{
UseTLS: false,
VerifyTLS: true,
Mode: "description",
Interval: "1m",
},
}
)
func serve(net string, listen string, secret *map[string]string, soreuseport bool) {
server := &dns.Server{Addr: listen, Net: net, TsigSecret: *secret, ReusePort: soreuseport}
if err := server.ListenAndServe(); err != nil {
log.Printf("Failed to setup the "+net+" server: %s\n", err.Error())
}
}
func main() {
flag.Usage = func() {
flag.PrintDefaults()
}
flag.Parse()
buf, err := ioutil.ReadFile(*configPath)
if err != nil {
log.Fatal(err)
}
if err := yaml.Unmarshal(buf, config); err != nil {
log.Fatal(err)
}
if config.Server.CPUProfile != nil {
f, err := os.Create(*config.Server.CPUProfile)
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
if config.Server.CPU != nil {
runtime.GOMAXPROCS(*config.Server.CPU)
}
zms := map[string]*zoneManager{}
for _, zoneConfig := range config.Zones {
zone, err := zoneMerge(&zoneConfig, &config.ZoneDefault)
if err != nil {
log.Fatal(err)
}
zm := newZoneManager(zone)
dns.HandleFunc(zone.Origin, zm.handler)
zms[zm.ZoneConfig.Suffix] = zm
}
if err := startNetboxSync(config, &zms); err != nil {
log.Fatal(err)
}
secret := map[string]string{}
for _, ts := range config.TsigSecrets {
secret[dns.Fqdn(ts.Name)] = ts.Secret
}
if config.Server.SoReuseport != nil {
for i := uint32(0); i < *config.Server.SoReuseport; i++ {
for _, listen := range config.Server.Listen {
go serve("tcp", listen, &secret, true)
go serve("udp", listen, &secret, true)
}
}
} else {
for _, listen := range config.Server.Listen {
go serve("tcp", listen, &secret, false)
go serve("udp", listen, &secret, false)
}
}
sig := make(chan os.Signal)
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
s := <-sig
log.Printf("Signal (%s) received, stopping\n", s)
}