-
Notifications
You must be signed in to change notification settings - Fork 76
/
utils.go
239 lines (214 loc) · 4.58 KB
/
utils.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package utils
import (
"bytes"
"encoding/json"
"github.com/mageddo/dns-proxy-server/flags"
"github.com/mageddo/dns-proxy-server/utils/env"
"io"
"os"
"os/signal"
"path/filepath"
"strings"
"syscall"
"time"
)
var QTypeCodes = map[uint16] string {
0 : "TypeNone",
1 : "TypeA",
2 : "TypeNS",
3 : "TypeMD",
4 : "TypeMF",
5 : "TypeCNAME",
6 : "TypeSOA",
7 : "TypeMB",
8 : "TypeMG",
9 : "TypeMR",
10 : "TypeNULL",
12 : "TypePTR",
13 : "TypeHINFO",
14 : "TypeMINFO",
15 : "TypeMX",
16 : "TypeTXT",
17 : "TypeRP",
18 : "TypeAFSDB",
19 : "TypeX25",
20 : "TypeISDN",
21 : "TypeRT",
23 : "TypeNSAPPTR",
24 : "TypeSIG",
25 : "TypeKEY",
26 : "TypePX",
27 : "TypeGPOS",
28 : "TypeAAAA",
29 : "TypeLOC",
30 : "TypeNXT",
31 : "TypeEID",
32 : "TypeNIMLOC",
33 : "TypeSRV",
34 : "TypeATMA",
35 : "TypeNAPTR",
36 : "TypeKX",
37 : "TypeCERT",
39 : "TypeDNAME",
41 : "TypeOPT", // EDNS
43 : "TypeDS",
44 : "TypeSSHFP",
46 : "TypeRRSIG",
47 : "TypeNSEC",
48 : "TypeDNSKEY",
49 : "TypeDHCID",
50 : "TypeNSEC3",
51 : "TypeNSEC3PARAM",
52 : "TypeTLSA",
53 : "TypeSMIMEA",
55 : "TypeHIP",
56 : "TypeNINFO",
57 : "TypeRKEY",
58 : "TypeTALINK",
59 : "TypeCDS",
60 : "TypeCDNSKEY",
61 : "TypeOPENPGPKEY",
99 : "TypeSPF",
100 : "TypeUINFO",
101 : "TypeUID",
102 : "TypeGID",
103 : "TypeUNSPEC",
104 : "TypeNID",
105 : "TypeL32",
106 : "TypeL64",
107 : "TypeLP",
108 : "TypeEUI48",
109 : "TypeEUI64",
256 : "TypeURI",
257 : "TypeCAA",
249 : "TypeTKEY",
250 : "TypeTSIG",
// valid Question.Qtype only,
251 : "TypeIXFR",
252 : "TypeAXFR",
253 : "TypeMAILB",
254 : "TypeMAILA",
255 : "TypeANY",
32768 : "TypeTA",
32769 : "TypeDLV",
65535 : "TypeReserved",
}
var QClassCodes = map[uint16] string {
// valid Question.Qclass,
1 : "ClassINET",
2 : "ClassCSNET",
3 : "ClassCHAOS",
4 : "ClassHESIOD",
254 : "ClassNONE",
255 : "ClassANY",
}
var RCodes = map[uint16] string {
// Message Response Codes.,
0 : "RcodeSuccess",
1 : "RcodeFormatError",
2 : "RcodeServerFailure",
3 : "RcodeNameError",
4 : "RcodeNotImplemented",
5 : "RcodeRefused",
6 : "RcodeYXDomain",
7 : "RcodeYXRrset",
8 : "RcodeNXRrset",
9 : "RcodeNotAuth",
10 : "RcodeNotZone",
16 : "RcodeBadSig", // TSIG
//16 : "RcodeBadVers", // EDNS0
17 : "RcodeBadKey",
18 : "RcodeBadTime",
19 : "RcodeBadMode", // TKEY
20 : "RcodeBadName",
21 : "RcodeBadAlg",
22 : "RcodeBadTrunc", // TSIG
23 : "RcodeBadCookie", // DNS Cookies
}
var opCodes = map[uint16] string {
// Message Opcodes. There is no 3.,
0 : "OpcodeQuery",
1 : "OpcodeIQuery",
2 : "OpcodeStatus",
4 : "OpcodeNotify",
5 : "OpcodeUpdate",
}
var Sig = make(chan os.Signal)
func init(){
signal.Notify(Sig, syscall.SIGINT, syscall.SIGTERM)
}
func DnsQTypeCodeToName(code uint16) string {
return QTypeCodes[code]
}
func GetCurrentPath() string {
currDIr := os.Getenv(env.MG_WORK_DIR)
if len(currDIr) != 0 {
return currDIr
}
currentPath, _ := filepath.Abs(filepath.Dir(os.Args[0]))
return currentPath
}
func SolveRelativePath(path string) string {
if !strings.HasPrefix(path, "/") {
path = "/" + path
}
currentPath := GetCurrentPath()
if strings.HasSuffix(currentPath, "/") {
currentPath = currentPath[0:len(currentPath)-1]
}
return currentPath + path
}
func GetPath(path string) string {
if strings.HasPrefix(path, "/") {
return path
}
return SolveRelativePath(path)
}
func GetJsonEncoder(w io.Writer) *json.Encoder {
enconder := json.NewEncoder(w)
if !flags.IsTestVersion() {
enconder.SetIndent("", "\t")
}
return enconder
}
func GetUUID() int64 {
return time.Now().UnixNano()
}
func Copy(src, dst string) error {
in, err := os.Open(src)
if err != nil { return err }
defer in.Close()
out, err := os.OpenFile(dst, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
if err != nil { return err }
defer out.Close()
_, err = io.Copy(out, in)
cerr := out.Close()
if err != nil { return err }
return cerr
}
func CreateExecutableFile(sourceData, dst string) error {
out, err := os.OpenFile(dst, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
if err != nil { return err }
defer out.Close()
_, err = io.Copy(out, bytes.NewReader([]byte(sourceData)))
cerr := out.Close()
if err != nil { return err }
return cerr
}
func WriteToFile(sourceData, dst string) error {
out, err := os.OpenFile(dst, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0766)
if err != nil { return err }
defer out.Close()
_, err = io.Copy(out, bytes.NewReader([]byte(sourceData)))
cerr := out.Close()
if err != nil { return err }
return cerr
}
// Difference between b and a in milliseconds
//
// b must be greater than a
//
func DiffMillis(a, b time.Time) int64 {
na, nb := a.UnixNano(), b.UnixNano()
return (nb - na) / int64(time.Nanosecond * time.Millisecond)
}