-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
337 lines (291 loc) · 8.98 KB
/
main.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
// Copyright 2017 GlobalR LLC. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// ---------------------------------------------------------
// WHOIS server
// ---------------------------------------------------------
// Author Khachatur Ashtotyan <khachatur.ashotyan@gmail.com>
package main
import (
"net"
"time"
"strings"
"text/template"
"bytes"
"regexp"
"log"
)
const (
// Servers listen to requests on the well-known port number 43
port = ":43"
// Set maximum connection time (deadline)
maxConnTime = 10 * time.Second
// Set maximum request length
maxReqLength = 64
)
var (
// Initializing help template
helpTemplate = InitHelp()
// Initializing top level error template
topErrTemplate = InitTopError()
// Initializing second level error template
secondErrTemplate = InitSecondError()
// Initializing syntax error template
syntaxErrTemplate = InitSyntaxError()
// Initializing domain name length error template
lenErrTemplate = InitLenError()
// Initializing success template
successTemplate = InitSuccess()
// Initializing success more template
successMoreTemplate = InitSuccessMore()
// Buffer initializing
tpl bytes.Buffer
)
func main() {
// Resolve TCP address
tcpAddr, err := net.ResolveTCPAddr("tcp4", port)
checkError(err)
// Initialize TCP Listener
listener, err := net.ListenTCP("tcp", tcpAddr)
checkError(err)
for {
conn, err := listener.Accept() // waits for the next call and returns a generic Conn
if err != nil {
continue
}
// Handle TCP queries
go handleClient(conn)
}
}
// Initialize Template by name and path
// @param {string} name name of template
// @param {string} path path of template
// @return {*template.Template} template representation pointer
func InitTemplate( name string, path string ) *template.Template {
t := template.New(name) // allocates a new, undefined template with the given name.
tmpl, err := t.ParseFiles(path) // parses the named files and associates the resulting templates with t
checkError(err)
tpl.Reset() // reset buffer story
return tmpl
}
// Initialize Help template
// @return {string} help template
func InitHelp() string {
helpTemplate := InitTemplate("Help", "./tmpl/help") // initialize template pointer
err := helpTemplate.ExecuteTemplate(&tpl,"help", nil) // write template with given parameter
checkError(err)
return tpl.String()
}
// Initialize Length Error template
// @return {string} length error template
func InitLenError() string {
helpTemplate := InitTemplate("Error", "./tmpl/error/len_error") // initialize template pointer
err := helpTemplate.ExecuteTemplate(&tpl,"len_error", nil) // write template with given parameter
checkError(err)
return tpl.String()
}
// Initialize Syntax Error template
// @return {string} syntax error template
func InitSyntaxError() string {
helpTemplate := InitTemplate("Error", "./tmpl/error/syntax_error") // initialize template pointer
err := helpTemplate.ExecuteTemplate(&tpl,"syntax_error", nil) // write template with given parameter
checkError(err)
return tpl.String()
}
// Initialize Top Level Error template
// @return {string} top level error template
func InitTopError() string {
helpTemplate := InitTemplate("Error", "./tmpl/error/top_level_error") // initialize template pointer
err := helpTemplate.ExecuteTemplate(&tpl,"top_level_error", nil) // write template with given parameter
checkError(err)
return tpl.String()
}
// Initialize Second Level Error template
// @return {string} second level error template
func InitSecondError() string {
helpTemplate := InitTemplate("Error", "./tmpl/error/second_level_error") // initialize template pointer
err := helpTemplate.ExecuteTemplate(&tpl,"second_level_error", nil) // write template with given parameter
checkError(err)
return tpl.String()
}
// Initialize Success Template for handling success queries
// @return {*template.Template} template representation pointer
func InitSuccess() *template.Template {
return InitTemplate("Success", "./tmpl/success/success")
}
// Initialize Success More Template for handling success queries
// @return {*template.Template} template representation pointer
func InitSuccessMore() *template.Template {
return InitTemplate("Success", "./tmpl/success/success_more")
}
// Handle Client WHOIS queries
// @param {net.Conn} conn generic stream-oriented network connection.
func handleClient(conn net.Conn) {
conn.SetReadDeadline(time.Now().Add(maxConnTime)) // set maximum connection time 10 second
request := make([]byte, maxReqLength) // set maximum request length to 128B to prevent flood based attacks
defer conn.Close() // close connection before exit
for {
readLen, err := conn.Read(request) // read client query
if err != nil {
logError(err)
break
}
if readLen == 0 {
break // connection already closed by client
} else {
req := strings.TrimSpace(string(request[:readLen])) // client query object
if req == "help" {
conn.Write([]byte(helpTemplate)) // client receive help
conn.Close()
} else {
if checkDomain(req, conn) { // check query correction
handleSuccess(req, conn) // all is correct send to client WHOIS data
}
}
}
}
}
// Check domain name correctness
// @param {string} domain generic stream-oriented network connection.
// @param {net.Conn} conn generic stream-oriented network connection.
// @return {bool} if all is OK. return true, if not receive specific error
func checkDomain(domain string, conn net.Conn) bool {
domain = strings.ToLower(domain) // domain name to lowercase
match, _ := regexp.MatchString(`^([a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,}$`, domain ) // check domain name correctness by regex
if ! match {
conn.Write([]byte(syntaxErrTemplate)) // client have syntax error
conn.Close()
} else {
domainParts := strings.Split(domain, ".") // check domain name parts
// check this domain name is belongs to us
if ( len(domainParts) == 2 && domainParts[1] == "ge" ) ||
( len(domainParts) == 3 && (
( domainParts[1] == "com" ||
domainParts[1] == "edu" ||
domainParts[1] == "gov" ||
domainParts[1] == "org" ||
domainParts[1] == "mil" ||
domainParts[1] == "net" ||
domainParts[1] == "pvt" ) && domainParts[2] == "ge" ) ) {
if len(domainParts[0]) < 2 {
conn.Write([]byte(lenErrTemplate)) // for finish check domain name length correctness
conn.Close()
}
return true
} else {
// send specific error by domain name
if len(domainParts) == 2 {
conn.Write([]byte(topErrTemplate)) // send top level error
conn.Close()
} else {
conn.Write([]byte(secondErrTemplate)) // send second level serror
conn.Close()
}
}
}
return true
}
type SuccessData struct {
DomainName string
Registrar string
Status string
Registrant Registrant
}
type Registrant struct {
Name string
Address1 string
Address2 string
ZIP string
Country string
}
type SuccessMoreData struct {
Administrative Administrative
Technical Technical
DNS []string
Registered string
Modified string
Expires string
}
type Administrative struct {
Name string
Address1 string
Address2 string
ZIP string
Country string
Email string
Phone string
}
type Technical struct {
Name string
Address1 string
Address2 string
ZIP string
Country string
Email string
Phone string
}
// TODO::Connect to database and send request
// If all is correct send to client success request
// @param {string} req client request (domain name)
// @param {net.Conn} generic stream-oriented network connection.
func handleSuccess(req string, conn net.Conn) {
tpl.Reset()
err := successTemplate.ExecuteTemplate(&tpl,"success", SuccessData{
DomainName: req,
Registrar: "RegNest.com (RegNest LLC)",
Status: "active, registrar locked",
Registrant: Registrant{
Name: "John Doe",
Address1: "123 6th St.",
Address2: "Melbourne, FL",
ZIP: "32904",
Country: "US",
},
} )
checkError(err)
conn.Write([]byte(tpl.String()))
tpl.Reset()
err = successMoreTemplate.ExecuteTemplate(&tpl,"success_more", SuccessMoreData{
Administrative: Administrative{
Name: "John Doe",
Address1: "123 6th St.",
Address2: "Melbourne, FL",
ZIP: "32904",
Country: "US",
Phone:"6503491051",
Email:"john@doe.ge",
},
Technical: Technical{
Name: "John Doe",
Address1: "123 6th St.",
Address2: "Melbourne, FL",
ZIP: "32904",
Country: "US",
Phone:"6503491051",
Email:"john@doe.ge",
},
DNS: []string{"coco.ns.cloudflare.com","todd.ns.cloudflare.com"},
Registered: "2004-12-28",
Modified: "2017-06-14",
Expires: "2022-12-28",
} )
checkError(err)
conn.Write([]byte(tpl.String()))
conn.Close()
}
// Fatal Error handling
// Log the message and turn off server
// @param {error} err error interface
func checkError(err error) {
if err != nil {
log.Fatalf("Fatal error: %s", err.Error())
}
}
// Warnings handling
// Log the server messages (for example, client closed connection)
// @param {error} err error interface
func logError(err error) {
if err != nil {
log.Printf("Warning: %s", err.Error())
}
}