-
Notifications
You must be signed in to change notification settings - Fork 6
/
trickydns.go
executable file
·123 lines (121 loc) · 2.56 KB
/
trickydns.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
package main
import (
"encoding/binary"
"encoding/json"
"io"
"io/ioutil"
"net"
"os"
"sync"
"time"
)
func main() {
config := struct {
BindAddr string
Servers []string
Timeout time.Duration
ExtraPtrs int
BufSize int
}{}
func() {
buf, err := ioutil.ReadFile(os.Args[1])
if err != nil {
panic(err)
}
if err := json.Unmarshal(buf, &config); err != nil {
panic(err)
}
config.Timeout *= time.Second
}()
udp := func() *net.UDPConn {
bindAddr, err := net.ResolveUDPAddr("udp", config.BindAddr)
if err != nil {
panic(err)
}
udp, err := net.ListenUDP("udp", bindAddr)
if err != nil {
panic(err)
}
return udp
}()
bufPool := sync.Pool{New: func() interface{} {
return make([]byte, config.BufSize)
}}
buf := bufPool.Get().([]byte)
domain := bufPool.Get().([]byte)
for {
buf = buf[:cap(buf)]
size, addr, err := udp.ReadFromUDP(buf)
if err != nil {
continue
}
err = func() (err error) {
defer func() {
if recover() != nil {
err = io.EOF
}
}()
buf = buf[:size]
for idx := 12; ; {
if buf[idx] == 0 {
domain = domain[:idx-11]
break
}
if buf[idx] > 0x3f { // already has pointer or malformed
return io.EOF
}
idx += int(buf[idx]) + 1
}
copy(domain, buf[12:])
copy(buf[14:], buf[12+len(domain):])
buf = buf[:len(buf)-len(domain)+2]
binary.BigEndian.PutUint16(buf[12:], uint16(len(buf)))
buf[12] |= 0xc0
for i := 0; i < config.ExtraPtrs; i++ {
buf = buf[:len(buf)+2]
binary.BigEndian.PutUint16(buf[len(buf)-2:], uint16(len(buf)))
buf[len(buf)-2] |= 0xc0
}
copy(buf[len(buf):cap(buf)], domain)
buf = buf[:len(buf)+len(domain)]
return nil
}()
if err != nil {
continue
}
cli := func() *net.UDPConn {
cliAddr, err := net.ResolveUDPAddr("udp", "")
if err != nil {
return nil
}
cli, err := net.ListenUDP("udp", cliAddr)
if err != nil {
return nil
}
for _, server := range config.Servers {
serverAddr, err := net.ResolveUDPAddr("udp", server)
if err != nil {
continue
}
cli.WriteToUDP(buf, serverAddr)
}
return cli
}()
if cli == nil {
continue
}
go func() {
defer cli.Close()
buf := bufPool.Get().([]byte)
defer bufPool.Put(buf)
cli.SetReadDeadline(time.Now().Add(config.Timeout))
buf = buf[:cap(buf)]
size, _, err := cli.ReadFromUDP(buf)
if err != nil {
return
}
buf = buf[:size]
udp.WriteToUDP(buf, addr)
}()
}
}