This repository has been archived by the owner on Mar 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dns.go
146 lines (105 loc) · 3.41 KB
/
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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
* VC5 load balancer. Copyright (C) 2021-present David Coles
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package mon
import (
"bufio"
"fmt"
"net"
"time"
)
var dialer *net.Dialer
func init() {
dialer = &net.Dialer{Timeout: 1 * time.Second}
}
// dig chaos txt version.bind @80.80.80.80
func dnsudp(addr string, port uint16) (bool, string) {
if port == 0 {
return false, "Port is 0"
}
conn, err := dialer.Dial("udp", fmt.Sprintf("%s:%d", addr, port))
if err != nil {
return false, err.Error()
}
defer conn.Close()
query := []byte{
0x00, 0x00, 0x01, 0x20, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x07, 0x76, 0x65, 0x72,
0x73, 0x69, 0x6f, 0x6e, 0x04, 0x62, 0x69, 0x6e, 0x64, 0x00, 0x00, 0x10, 0x00, 0x03, 0x00, 0x00,
0x29, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x0a, 0x00, 0x08, 0x56, 0x16, 0xf6,
0x23, 0xc3, 0xf6, 0x35, 0x9c,
}
var tid uint16 = uint16(time.Now().Unix() % 65536)
query[0] = byte(tid >> 8)
query[1] = byte(tid & 0xff)
conn.SetWriteDeadline(time.Now().Add(1 * time.Second))
_, err = conn.Write(query)
if err != nil {
return false, "Write to server failed:" + err.Error()
}
conn.SetReadDeadline(time.Now().Add(1 * time.Second))
var buff [256]byte
read, err := bufio.NewReader(conn).Read(buff[:])
if err != nil {
return false, "Read from server failed:" + err.Error()
}
if read < 2 {
return false, "Malformed packet: Too short"
}
if tid != (uint16(buff[0])<<8 + uint16(buff[1])) {
return false, "Malformed packet: Incorrect transaction ID"
}
return true, ""
}
func dnstcp(addr string, port uint16) (bool, string) {
if port == 0 {
return false, "Port is 0"
}
conn, err := dialer.Dial("tcp", fmt.Sprintf("%s:%d", addr, port))
if err != nil {
return false, err.Error()
}
defer conn.Close()
query := []byte{
0x00, 0x1e, 0xf4, 0xda, 0x01, 0x20, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x76,
0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x04, 0x62, 0x69, 0x6e, 0x64, 0x00, 0x00, 0x10, 0x00, 0x03,
}
var tid uint16 = uint16(time.Now().UnixNano() % 65536)
query[2] = byte(tid >> 8)
query[3] = byte(tid & 0xff)
conn.SetWriteDeadline(time.Now().Add(1 * time.Second))
_, err = conn.Write(query)
if err != nil {
return false, "Write to server failed:" + err.Error()
}
conn.SetReadDeadline(time.Now().Add(1 * time.Second))
var buff [256]byte
read, err := conn.Read(buff[:])
if err != nil {
return false, "Read from server failed:" + err.Error()
}
if read < 4 {
return false, "Malformed packet: Too short"
}
length := int(buff[0])<<8 + int(buff[1])
if length+2 != read {
return false, "Malformed packet: Length mismatch"
}
if tid != (uint16(buff[2])<<8 + uint16(buff[3])) {
return false, "Malformed packet: Incorrect transaction ID"
}
return true, ""
}