forked from katakonst/go-dns-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dns_proxy.go
133 lines (108 loc) · 2.91 KB
/
dns_proxy.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
package main
import (
"fmt"
"net"
"regexp"
"github.com/miekg/dns"
)
type DNSProxy struct {
Cache *Cache
domains map[string]interface{}
servers map[string]interface{}
defaultServer string
}
func (proxy *DNSProxy) getResponse(requestMsg *dns.Msg) (*dns.Msg, error) {
responseMsg := new(dns.Msg)
if len(requestMsg.Question) > 0 {
question := requestMsg.Question[0]
dnsServer := proxy.getIPFromConfigs(question.Name, proxy.servers)
if dnsServer == "" {
dnsServer = proxy.defaultServer
}
switch question.Qtype {
case dns.TypeA:
answer, err := proxy.processTypeA(dnsServer, &question, requestMsg)
if err != nil {
return responseMsg, err
}
responseMsg.Answer = append(responseMsg.Answer, *answer)
default:
answer, err := proxy.processOtherTypes(dnsServer, &question, requestMsg)
if err != nil {
return responseMsg, err
}
responseMsg.Answer = append(responseMsg.Answer, *answer)
}
}
return responseMsg, nil
}
func (proxy *DNSProxy) processOtherTypes(dnsServer string, q *dns.Question, requestMsg *dns.Msg) (*dns.RR, error) {
queryMsg := new(dns.Msg)
requestMsg.CopyTo(queryMsg)
queryMsg.Question = []dns.Question{*q}
msg, err := lookup(dnsServer, queryMsg)
if err != nil {
return nil, err
}
if len(msg.Answer) > 0 {
return &msg.Answer[0], nil
}
return nil, fmt.Errorf("not found")
}
func (proxy *DNSProxy) processTypeA(dnsServer string, q *dns.Question, requestMsg *dns.Msg) (*dns.RR, error) {
ip := proxy.getIPFromConfigs(q.Name, proxy.domains)
cacheMsg, found := proxy.Cache.Get(q.Name)
if ip == "" && !found {
queryMsg := new(dns.Msg)
requestMsg.CopyTo(queryMsg)
queryMsg.Question = []dns.Question{*q}
msg, err := lookup(dnsServer, queryMsg)
if err != nil {
return nil, err
}
fmt.Println("Test")
fmt.Println(msg.Answer)
fmt.Println("Test2")
fmt.Println(msg.Answer[len(msg.Answer)-1])
if len(msg.Answer) > 0 {
proxy.Cache.Set(q.Name, &msg.Answer[len(msg.Answer)-1])
return &msg.Answer[len(msg.Answer)-1], nil
}
} else if found {
return cacheMsg.(*dns.RR), nil
} else if ip != "" {
answer, err := dns.NewRR(fmt.Sprintf("%s A %s", q.Name, ip))
if err != nil {
return nil, err
}
return &answer, nil
}
return nil, fmt.Errorf("not found")
}
func (dnsProxy *DNSProxy) getIPFromConfigs(domain string, configs map[string]interface{}) string {
for k, v := range configs {
match, _ := regexp.MatchString(k+"\\.", domain)
if match {
return v.(string)
}
}
return ""
}
func GetOutboundIP() (net.IP, error) {
conn, err := net.Dial("udp", "8.8.8.8:80")
if err != nil {
return nil, err
}
defer conn.Close()
localAddr := conn.LocalAddr().(*net.UDPAddr)
return localAddr.IP, nil
}
func lookup(server string, m *dns.Msg) (*dns.Msg, error) {
dnsClient := new(dns.Client)
dnsClient.Net = "udp"
response, _, err := dnsClient.Exchange(m, server)
if err != nil {
return nil, err
}
return response, nil
}