forked from katakonst/go-dns-proxy
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
46 lines (39 loc) · 981 Bytes
/
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
package main
import (
"github.com/miekg/dns"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
func main() {
if err := CreateConfig(); err != nil {
panic(err)
}
ConfigLogging()
domains := viper.Get("domains")
dnsProxy := DNSProxy{
domains: domains.(map[string]interface{}),
defaultServer: viper.GetString("default_dns"),
}
dns.HandleFunc(".", func(w dns.ResponseWriter, r *dns.Msg) {
switch r.Opcode {
case dns.OpcodeQuery:
m, err := dnsProxy.getResponse(r)
if err != nil {
log.Errorf("Failed lookup for %v with error:\n %v\n", r, err.Error())
m.SetReply(r)
w.WriteMsg(m)
return
}
m.SetReply(r)
w.WriteMsg(m)
}
})
host := viper.GetString("listen_host")
port := viper.GetString("listen_port")
server := &dns.Server{Addr: host + ":" + port, Net: "udp"}
log.Infoln("Starting at", host+":"+port)
err := server.ListenAndServe()
if err != nil {
log.Errorln("Failed to start server:", err.Error())
}
}