-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
74 lines (64 loc) · 1.64 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
package main
import (
"fmt"
"io/ioutil"
"os"
"os/signal"
"syscall"
serf_client "github.com/hashicorp/serf/client"
"github.com/miekg/dns"
)
func handle(writer dns.ResponseWriter, request *dns.Msg, serfClient *serf_client.RPCClient) {
message := new(dns.Msg)
message.SetReply(request)
for _, question := range request.Question {
filter := parseDomainName(question.Name, SerfFilterTable)
hosts, err := getSerfMembers(serfClient, filter)
if err != nil {
fmt.Println(err.Error())
}
message.Answer = addHostsToAnswer(hosts, question.Name, message.Answer)
}
err := writer.WriteMsg(message)
if err != nil {
fmt.Println(err.Error())
}
}
func serve(net string, address string) {
server := &dns.Server{Addr: address, Net: net, TsigSecret: nil}
err := server.ListenAndServe()
if err != nil {
fmt.Printf("Failed to setup the %s server at %s: %s\n", net, address, err.Error())
}
}
func main() {
config.Parse()
if *configCustomDomainNameFile != "" {
customDNData, err := ioutil.ReadFile(*configCustomDomainNameFile)
if err != nil {
fmt.Println(err.Error())
} else {
SerfFilterTable = loadCustomDomainName(customDNData)
}
}
serfClient, err := connectSerfAgent(*configSerfRPCAddress, *configSerfRPCAuthKey)
defer closeSerfConnection(serfClient)
if err != nil {
fmt.Println(err.Error())
}
dns.HandleFunc(*configDomainName,
func(writer dns.ResponseWriter, request *dns.Msg) {
handle(writer, request, serfClient)
})
go serve("tcp", *configBind)
go serve("udp", *configBind)
sig := make(chan os.Signal)
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
forever:
for {
select {
case <-sig:
break forever
}
}
}