forked from duedil-ltd/discodns
/
main.go
164 lines (139 loc) · 5.41 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package main
import (
"github.com/coreos/go-etcd/etcd"
"github.com/jessevdk/go-flags"
"github.com/miekg/dns"
"github.com/rcrowley/go-metrics"
"log"
"net"
"os"
"os/signal"
"runtime"
"strings"
"time"
)
var (
logger = log.New(os.Stderr, "[discodns] ", log.Ldate|log.Ltime)
log_debug = false
// Define all of the command line arguments
Options struct {
ListenAddress string `short:"l" long:"listen" description:"Listen IP address" default:"0.0.0.0"`
ListenPort int `short:"p" long:"port" description:"Port to listen on" default:"53"`
EtcdHosts []string `short:"e" long:"etcd" description:"host:port[,host:port] for etcd hosts" default:"127.0.0.1:4001"`
Prefix string `short:"P" long:"prefix" description:"Etcd prefix" default:"discodns"`
Cert string `short:"c" long:"cert" description:"path to cert"`
Key string `short:"k" long:"key" description:"path to private key"`
CaCert string `short:"C" long:"caCert" description:"path to CA cert"`
Debug bool `short:"v" long:"debug" description:"Enable debug logging"`
MetricsDuration int `short:"m" long:"metrics" description:"Dump metrics to stderr every N seconds" default:"30"`
GraphiteServer string `long:"graphite" description:"Graphite server to send metrics to"`
GraphiteDuration int `long:"graphite-duration" description:"Duration to periodically send metrics to the graphite server" default:"10"`
DefaultTtl uint32 `short:"t" long:"default-ttl" description:"Default TTL to return on records without an explicit TTL" default:"300"`
Accept []string `long:"accept" description:"Limit DNS queries to a set of domain:[type,...] pairs"`
Reject []string `long:"reject" description:"Limit DNS queries to a set of domain:[type,...] pairs"`
}
)
func main() {
_, err := flags.ParseArgs(&Options, os.Args[1:])
if err != nil {
os.Exit(1)
}
if Options.Debug {
log_debug = true
debugMsg("Debug mode enabled")
}
// Create an ETCD client
var e *etcd.Client
if Options.Cert != "" && Options.Key != "" {
debugMsg("Connecting using TLS: ", Options.EtcdHosts, Options.Cert, Options.Key, Options.CaCert)
e, err = etcd.NewTLSClient(Options.EtcdHosts, Options.Cert, Options.Key, Options.CaCert)
if err != nil {
logger.Fatalf("Failed to connect to Etcd server using TLS: ", err)
}
} else {
e = etcd.NewClient(Options.EtcdHosts)
}
if !e.SyncCluster() {
logger.Printf("[WARNING] Failed to connect to etcd cluster at launch time")
}
// Register the metrics writer
if len(Options.GraphiteServer) > 0 {
addr, err := net.ResolveTCPAddr("tcp", Options.GraphiteServer)
if err != nil {
logger.Fatalf("Failed to parse graphite server: ", err)
}
prefix := "discodns"
hostname, err := os.Hostname()
if err != nil {
logger.Fatalf("Unable to get hostname: ", err)
}
prefix = prefix + "." + strings.Replace(hostname, ".", "_", -1)
go metrics.Graphite(metrics.DefaultRegistry, time.Duration(Options.GraphiteDuration)*time.Second, prefix, addr)
} else if Options.MetricsDuration > 0 {
go metrics.Log(metrics.DefaultRegistry, time.Duration(Options.MetricsDuration)*time.Second, logger)
// Register a bunch of debug metrics
metrics.RegisterDebugGCStats(metrics.DefaultRegistry)
metrics.RegisterRuntimeMemStats(metrics.DefaultRegistry)
go metrics.CaptureDebugGCStats(metrics.DefaultRegistry, time.Duration(Options.MetricsDuration))
go metrics.CaptureRuntimeMemStats(metrics.DefaultRegistry, time.Duration(Options.MetricsDuration))
} else {
logger.Printf("Metric logging disabled")
}
// Start up the DNS resolver server
server := &Server{
addr: Options.ListenAddress,
port: Options.ListenPort,
etcd: e,
prefix: Options.Prefix,
rTimeout: time.Duration(5) * time.Second,
wTimeout: time.Duration(5) * time.Second,
defaultTtl: Options.DefaultTtl,
queryFilterer: &QueryFilterer{acceptFilters: parseFilters(Options.Accept),
rejectFilters: parseFilters(Options.Reject)}}
server.Run()
logger.Printf("Listening on %s:%d\n", Options.ListenAddress, Options.ListenPort)
sig := make(chan os.Signal)
signal.Notify(sig, os.Interrupt)
forever:
for {
select {
case <-sig:
logger.Printf("Bye bye :(\n")
break forever
}
}
}
func debugMsg(v ...interface{}) {
if log_debug {
vars := []interface{}{"[", runtime.NumGoroutine(), "]"}
vars = append(vars, v...)
logger.Println(vars...)
}
}
// parseFilters will convert a string into a Query Filter structure. The accepted
// format for input is [domain]:[type,type,...]. For example...
//
// - "domain:A,AAAA" # Match all A and AAAA queries within `domain`
// - ":TXT" # Matches only TXT queries for any domain
// - "domain:" # Matches any query within `domain`
func parseFilters(filters []string) []QueryFilter {
parsedFilters := make([]QueryFilter, 0)
for _, filter := range filters {
components := strings.Split(filter, ":")
if len(components) != 2 {
logger.Printf("Expected only one colon ([domain]:[type,type...])")
continue
}
domain := dns.Fqdn(components[0])
types := strings.Split(components[1], ",")
if len(types) == 1 && len(types[0]) == 0 {
types = make([]string, 0)
}
debugMsg("Adding filter with domain '" + domain + "' and types '" + strings.Join(types, ",") + "'")
parsedFilters = append(parsedFilters, QueryFilter{domain, types})
}
return parsedFilters
}
func init() {
runtime.GOMAXPROCS(runtime.NumCPU())
}