forked from mageddo/dns-proxy-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dns.go
165 lines (140 loc) · 4.6 KB
/
dns.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
package main
import (
"github.com/mageddo/dns-proxy-server/log"
"fmt"
"os"
"runtime/pprof"
"strings"
"github.com/miekg/dns"
"github.com/mageddo/dns-proxy-server/proxy"
"reflect"
"github.com/mageddo/dns-proxy-server/utils"
"github.com/mageddo/dns-proxy-server/events/local"
"github.com/mageddo/dns-proxy-server/events/docker"
"net/http"
"github.com/mageddo/dns-proxy-server/controller"
"github.com/mageddo/dns-proxy-server/conf"
"github.com/mageddo/dns-proxy-server/utils/exitcodes"
"github.com/mageddo/dns-proxy-server/service"
"github.com/mageddo/go-logging"
"runtime/debug"
"github.com/mageddo/dns-proxy-server/cache/store"
"github.com/mageddo/dns-proxy-server/resolvconf"
)
func init(){
log.SetLevel(conf.LogLevel())
log.SetOutput(conf.LogFile())
}
func handleQuestion(respWriter dns.ResponseWriter, reqMsg *dns.Msg) {
ctx := logging.NewContext()
logger := logging.NewLog(ctx)
defer func() {
err := recover()
if err != nil {
logger.Errorf("status=error, error=%v, stack=%s", err, string(debug.Stack()))
}
}()
var firstQuestion dns.Question
questionsQtd := len(reqMsg.Question)
if questionsQtd != 0 {
firstQuestion = reqMsg.Question[0]
}else{
logger.Error("status=question-is-nil")
return
}
logger.Debugf("status=begin, reqId=%d, questions=%d, question=%s, type=%s", reqMsg.Id,
questionsQtd, firstQuestion.Name, utils.DnsQTypeCodeToName(firstQuestion.Qtype))
// loading the solvers and try to solve the hostname in that order
solvers := []proxy.DnsSolver{
proxy.NewDockerSolver(docker.GetCache()), proxy.NewLocalDNSSolver(store.GetInstance()), proxy.NewRemoteDnsSolver(),
}
for _, solver := range solvers {
solverID := reflect.TypeOf(solver).String()
logger.Debugf("status=begin, solver=%s", solverID)
// loop through questions
resp, err := solver.Solve(ctx, firstQuestion)
if err == nil {
var firstAnswer dns.RR
answerLenth := len(resp.Answer)
logger.Debugf("status=answer-found, solver=%s, length=%d", solverID, answerLenth)
if answerLenth != 0 {
firstAnswer = resp.Answer[0]
}
logger.Debugf("status=resolved, solver=%s, alength=%d, answer=%v", solverID, answerLenth, firstAnswer)
resp.SetReply(reqMsg)
resp.Compress = conf.Compress()
respWriter.WriteMsg(resp)
break
}
logger.Debugf("status=not-resolved, solver=%s, err=%v", solverID, err)
}
}
func serve(net, name, secret string, logger logging.Log) {
port := fmt.Sprintf(":%d", conf.DnsServerPort())
logger.Debugf("status=begin, port=%d", conf.DnsServerPort())
switch name {
case "":
server := &dns.Server{Addr: port, Net: net, TsigSecret: nil}
if err := server.ListenAndServe(); err != nil {
fmt.Printf("Failed to setup the %s server: %s\n", net, err.Error())
exitcodes.Exit(exitcodes.FAIL_START_DNS_SERVER)
}
default:
server := &dns.Server{Addr: port, Net: net, TsigSecret: map[string]string{name: secret}}
if err := server.ListenAndServe(); err != nil {
fmt.Printf("Failed to setup the %s server: %s\n", net, err.Error())
exitcodes.Exit(exitcodes.FAIL_START_DNS_SERVER)
}
}
}
func main() {
ctx := logging.NewContext()
logger := logging.NewLog(ctx)
service.NewService(ctx).Install()
var name, secret string
if conf.Tsig() != "" {
a := strings.SplitN(conf.Tsig(), ":", 2)
name, secret = dns.Fqdn(a[0]), a[1] // fqdn the name, which everybody forgets...
}
if conf.CpuProfile() != "" {
f, err := os.Create(conf.CpuProfile())
if err != nil {
logger.Fatal(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
dns.HandleFunc(".", handleQuestion)
local.LoadConfiguration(ctx)
go docker.HandleDockerEvents()
go serve("tcp", name, secret, logger)
go serve("udp", name, secret, logger)
go func(){
webPort := conf.WebServerPort()
logger.Infof("status=web-server-starting, port=%d", webPort)
if err := http.ListenAndServe(fmt.Sprintf(":%d", webPort), nil); err != nil {
logger.Errorf("status=failed-start-web-server, err=%v, port=%d", err, webPort)
exitcodes.Exit(exitcodes.FAIL_START_WEB_SERVER)
}else{
logger.Infof("status=web-server-started, port=%d", webPort)
}
}()
go func() {
logger.Infof("status=setup-requests")
controller.MapRequests()
if conf.SetupResolvConf() {
logger.Infof("status=setResolvconf")
err := resolvconf.SetCurrentDNSServerToMachine()
if err != nil {
logger.Errorf("status=setResolvconf, err=%v", err)
exitcodes.Exit(exitcodes.FAIL_SET_DNS_AS_DEFAULT)
}
}
}()
logger.Infof("status=listing-signals")
fmt.Printf("server started\n")
s := <- utils.Sig
logger.Infof("status=exiting..., s=%s", s)
resolvconf.RestoreResolvconfToDefault()
logger.Warningf("status=exiting, signal=%v", s)
}