-
Notifications
You must be signed in to change notification settings - Fork 10
/
forward_dns.go
39 lines (31 loc) · 983 Bytes
/
forward_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
package easyss
import (
"github.com/miekg/dns"
"github.com/nange/easyss/v2/log"
)
func NewDNSForwardServer(dnsServer string) *dns.Server {
srv := &dns.Server{Addr: ":53", Net: "udp"}
c := &dns.Client{Timeout: DefaultDNSTimeout, UDPSize: 8192}
srv.Handler = dns.HandlerFunc(func(writer dns.ResponseWriter, msg *dns.Msg) {
if len(msg.Question) > 0 {
log.Info("[DNS_FORWARD]", "domain", msg.Question[0].Name)
}
r, _, err := c.Exchange(msg, dnsServer)
if err != nil {
log.Error("[DNS_FORWARD] exchange", "err", err)
return
}
if err := writer.WriteMsg(r); err != nil {
log.Error("[DNS_FORWARD] write msg back", "err", err)
}
})
return srv
}
func (ss *Easyss) LocalDNSForward() {
server := NewDNSForwardServer(ss.DirectDNSServer())
ss.SetForwardDNSServer(server)
log.Info("[DNS_FORWARD] starting local dns forward server at :53")
if err := server.ListenAndServe(); err != nil {
log.Warn("[DNS_FORWARD] local forward server", "err", err)
}
}