-
Notifications
You must be signed in to change notification settings - Fork 27
/
main.go
70 lines (61 loc) · 1.79 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
package main
import (
"log"
"regexp"
"github.com/miekg/dns"
)
func main() {
appConfigs, err := InitConfig()
if err != nil {
log.Fatalf("Failed to load configs: %s", err)
}
dnsCache := InitCache(appConfigs.CacheExpiration)
domains, _ := appConfigs.DNSConfigs["domains"]
servers, _ := appConfigs.DNSConfigs["servers"]
dnsProxy := DNSProxy{
Cache: &dnsCache,
domains: domains.(map[string]interface{}),
servers: servers.(map[string]interface{}),
defaultServer: appConfigs.DNSConfigs["defaultDns"].(string),
}
logger := NewLogger(appConfigs.LogLevel)
host := ""
if appConfigs.UseOutbound {
ip, err := GetOutboundIP()
if err != nil {
logger.Fatalf("Failed to get outbound address: %s\n ", err.Error())
}
host = ip.String() + ":53"
} else {
host = appConfigs.DNSConfigs["host"].(string)
}
dns.HandleFunc(".", func(w dns.ResponseWriter, r *dns.Msg) {
switch r.Opcode {
case dns.OpcodeQuery:
m, err := dnsProxy.getResponse(r)
if err != nil {
logger.Errorf("Failed lookup for %s with error: %s\n", r, err.Error())
m.SetReply(r)
w.WriteMsg(m)
return
}
if len(m.Answer) > 0 {
pattern := regexp.MustCompile(`(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}`)
ipAddress := pattern.FindAllString(m.Answer[0].String(), -1)
if len(ipAddress) > 0 {
logger.Infof("Lookup for %s with ip %s\n", m.Answer[0].Header().Name, ipAddress[0])
} else {
logger.Infof("Lookup for %s with response %s\n", m.Answer[0].Header().Name, m.Answer[0])
}
}
m.SetReply(r)
w.WriteMsg(m)
}
})
server := &dns.Server{Addr: host, Net: "udp"}
logger.Infof("Starting at %s\n", host)
err = server.ListenAndServe()
if err != nil {
logger.Errorf("Failed to start server: %s\n ", err.Error())
}
}