Skip to content

Commit 920c5a1

Browse files
committedMar 10, 2021
Add dns cache
1 parent f79ded4 commit 920c5a1

File tree

5 files changed

+42
-9
lines changed

5 files changed

+42
-9
lines changed
 

‎README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ DNS proxy server over socks5(udp)
55
# Usage
66
```
77
Usage of ./bin/dns2socks:
8+
-c cache dns type a (default true)
89
-d string
910
remote dns server address (default "8.8.8.8:53")
1011
-l string
1112
local dns server address (default "127.0.0.1:53")
1213
-s string
1314
socks5(udp) proxy address (default "127.0.0.1:1080")
1415
15-
1616
```
1717
# Run on docker
1818
```

‎cmd/dns2socks.go

+36-7
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@ import (
44
"io"
55
"log"
66
"net"
7+
"time"
78

89
"github.com/miekg/dns"
10+
"github.com/patrickmn/go-cache"
911
"github.com/thinkgos/go-socks5/ccsocks5"
1012
)
1113

14+
var c = cache.New(30*time.Minute, 10*time.Minute)
15+
1216
//StartServer acts start a dns proxy server
13-
func StartServer(localAddr *string, socksAddr *string, dnsAddr *string) {
17+
func StartServer(localAddr *string, socksAddr *string, dnsAddr *string, cached *bool) {
1418
addr, err := net.ResolveUDPAddr("udp", *localAddr)
1519
if nil != err {
1620
log.Fatalln("Unable to get UDP socket:", err)
@@ -21,6 +25,7 @@ func StartServer(localAddr *string, socksAddr *string, dnsAddr *string) {
2125
}
2226
defer conn.Close()
2327
log.Printf("dns2socks started on %v", *localAddr)
28+
2429
client := ccsocks5.NewClient(*socksAddr)
2530
defer client.Close()
2631
buf := make([]byte, 4096)
@@ -30,7 +35,17 @@ func StartServer(localAddr *string, socksAddr *string, dnsAddr *string) {
3035
continue
3136
}
3237
b := buf[:n]
33-
printQuestion(b)
38+
if *cached {
39+
answer := getAnswerFromCache(b)
40+
if answer != nil {
41+
respMsg := new(dns.Msg)
42+
respMsg.Unpack(b)
43+
respMsg.Answer = append(respMsg.Answer, *answer)
44+
respData, _ := respMsg.Pack()
45+
conn.WriteToUDP(respData, fromAddr)
46+
continue
47+
}
48+
}
3449
proxyConn, err := client.Dial("udp", *dnsAddr)
3550
if err != nil {
3651
log.Println(err)
@@ -45,21 +60,35 @@ func StartServer(localAddr *string, socksAddr *string, dnsAddr *string) {
4560
break
4661
}
4762
b := buf[:n]
48-
printAnswer(b)
63+
if *cached {
64+
setAnswerCache(b)
65+
}
4966
conn.WriteToUDP(b, fromAddr)
5067
}
5168
}()
5269
}
5370
}
5471

55-
func printQuestion(data []byte) {
72+
func getAnswerFromCache(data []byte) *dns.RR {
5673
msg := new(dns.Msg)
5774
msg.Unpack(data)
58-
log.Printf("dns question:%v", msg.Question)
75+
q := msg.Question[0]
76+
if q.Qtype == dns.TypeA {
77+
if v, found := c.Get(q.Name); found {
78+
log.Printf("query dns from cache:%v", msg.Question)
79+
ret := v.(*dns.RR)
80+
return ret
81+
}
82+
}
83+
return nil
5984
}
6085

61-
func printAnswer(data []byte) {
86+
func setAnswerCache(data []byte) {
6287
msg := new(dns.Msg)
6388
msg.Unpack(data)
64-
log.Printf("dns answer:%v", msg.Answer)
89+
q := msg.Question[0]
90+
if q.Qtype == dns.TypeA && len(msg.Answer) > 0 {
91+
c.Set(q.Name, &msg.Answer[len(msg.Answer)-1], cache.DefaultExpiration)
92+
log.Printf("set dns cache:%v", q.Name)
93+
}
6594
}

‎go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.16
44

55
require (
66
github.com/miekg/dns v1.1.40
7+
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
78
github.com/thinkgos/go-socks5 v0.2.2 // indirect
89
golang.org/x/net v0.0.0-20210224082022-3d97a244fca7
910
)

‎go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
22
github.com/miekg/dns v1.1.40 h1:pyyPFfGMnciYUk/mXpKkVmeMQjfXqt3FAJ2hy7tPiLA=
33
github.com/miekg/dns v1.1.40/go.mod h1:KNUDUusw/aVsxyTYZM1oqvCicbwhgbNgztCETuNZ7xM=
4+
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
5+
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
46
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
57
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
68
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=

‎main.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ func main() {
1010
localAddr := flag.String("l", "127.0.0.1:53", "local dns server address")
1111
socksAddr := flag.String("s", "127.0.0.1:1080", "socks5(udp) proxy address")
1212
dnsAddr := flag.String("d", "8.8.8.8:53", "remote dns server address")
13+
cached := flag.Bool("c", true, "cache dns type a")
1314
flag.Parse()
14-
cmd.StartServer(localAddr, socksAddr, dnsAddr)
15+
cmd.StartServer(localAddr, socksAddr, dnsAddr, cached)
1516
}

0 commit comments

Comments
 (0)
Please sign in to comment.