-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
218 lines (186 loc) · 4.85 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package main
import (
"context"
"crypto/tls"
"flag"
"fmt"
"github.com/miekg/dns"
"io/ioutil"
"log"
"math/rand"
"os"
"strconv"
"sync"
"time"
)
var (
confFile = flag.String("conf", "", "location of the funkyd configuration file")
versionFlag = flag.Bool("version", false, "output version")
)
func validateConfFile() error {
file := *confFile
if file == "" {
return fmt.Errorf("no configuration file specified")
}
if _, err := os.Stat(file); err != nil {
return err
}
return nil
}
func validateFlags() error {
if err := validateConfFile(); err != nil {
return err
}
return nil
}
func runBlackholeServer() error {
config := GetConfiguration()
switch config.ListenProtocol {
case "tcp-tls":
srv := &dns.Server{Addr: ":" + strconv.Itoa(config.DnsPort), Net: "tcp-tls", MaxTCPQueries: -1, ReusePort: true}
log.Printf("starting tls blackhole server")
if (config.TlsConfig == tlsConfig{}) {
log.Fatalf("attempted to listen for TLS connections, but no tls config was defined")
}
if config.TlsConfig.CertificateFile == "" {
log.Fatalf("invalid certificate file in configuration")
}
if config.TlsConfig.PrivateKeyFile == "" {
log.Fatalf("invalid private key in configuration")
}
cert, err := tls.LoadX509KeyPair(config.TlsConfig.CertificateFile, config.TlsConfig.PrivateKeyFile)
if err != nil {
log.Fatalf("could not load tls files")
}
srv.TLSConfig = &tls.Config{
Certificates: []tls.Certificate{cert},
}
srv.Handler = &BlackholeServer{}
return srv.ListenAndServe()
default:
return fmt.Errorf("unsupported protocol [%s]", config.ListenProtocol)
}
}
func loadLocalZones(server Server) {
config := GetConfiguration()
// read in zone files, if configured to do so
for _, file := range config.ZoneFiles {
file, err := ioutil.ReadFile(file)
if err != nil {
log.Fatalf("could not read zone file [%s]: %s\n", file, err)
}
responses, err := ParseZoneFile(string(file))
if err != nil {
log.Fatalf("could not parse zone file [%s]: %s\n", file, err)
}
for _, response := range responses {
log.Printf("adding [%v]\n", response)
// TODO one function to make the keys, please
server.GetHostedCache().Add(response)
}
}
}
var servers []*dns.Server = []*dns.Server{}
func addServer(s *dns.Server) {
servers = append(servers, s)
}
var shutdownMutex *sync.Mutex = &sync.Mutex{}
func Shutdown() {
shutdownMutex.Lock()
defer shutdownMutex.Unlock()
for _, s := range servers {
ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Second)
defer cancel()
if err := s.ShutdownContext(ctx); err != nil {
log.Printf("error shutting down server [%v] : %s", s, err)
}
}
}
func main() {
flag.Parse()
if *versionFlag {
fmt.Printf("current version: %s\n", GetVersion().String())
os.Exit(0)
}
if err := validateFlags(); err != nil {
fmt.Printf("error validating flags: %s\n", err.Error())
os.Exit(1)
}
rand.Seed(time.Now().UnixNano())
BuildInfoGauge.WithLabelValues(GetVersion().String()).Set(1)
log.Printf("reading configuration from [%s]", *confFile)
// read in configuration
if err := InitConfiguration(*confFile); err != nil {
log.Fatalf("could not open configuration: %s\n", err)
}
config := GetConfiguration()
InitLoggers()
InitApi()
server, err := NewMutexServer(nil, nil)
if err != nil {
Logger.Log(LogMessage{
Level: CRITICAL,
Context: LogContext{
"what": "could not build mutex server",
"error": err.Error(),
},
})
os.Exit(1)
}
loadLocalZones(server)
dnsPort := config.DnsPort
if dnsPort == 0 {
dnsPort = 53
}
// set up DNS server
srvUDP := &dns.Server{Addr: ":" + strconv.Itoa(dnsPort), Net: "udp", MaxTCPQueries: -1, ReusePort: true}
srvTCP := &dns.Server{Addr: ":" + strconv.Itoa(dnsPort), Net: "tcp", MaxTCPQueries: -1, ReusePort: true}
srvUDP.Handler, srvTCP.Handler = server, server
addServer(srvUDP)
addServer(srvTCP)
if config.Blackhole {
// PSYCH!
if err := runBlackholeServer(); err != nil {
Logger.Log(LogMessage{
Level: CRITICAL,
Context: LogContext{
"what": "failed to start blackhole server",
"error": err.Error(),
},
})
os.Exit(1)
}
}
Logger.Log(LogMessage{
Level: CRITICAL,
Context: LogContext{
"what": "starting up TCP and UDP servers",
"version": GetVersion().String(),
"port": fmt.Sprintf("%d", dnsPort),
},
})
go func(srvUDP *dns.Server) {
if err := srvUDP.ListenAndServe(); err != nil {
Logger.Log(LogMessage{
Level: CRITICAL,
Context: LogContext{
"what": "error serving UDP",
"error": err.Error(),
},
})
}
}(srvUDP)
if err := srvTCP.ListenAndServe(); err != nil {
Logger.Log(LogMessage{
Level: CRITICAL,
Context: LogContext{
"what": "error serving TCP",
"error": err.Error(),
},
})
// bail here so it doesn't deadlock on the shutdown mutex
os.Exit(1)
}
// wait until all shutdowns are complete
shutdownMutex.Lock()
}