/
ipconfig.go
529 lines (458 loc) · 13.9 KB
/
ipconfig.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
package main
import (
"fmt"
"regexp"
"strings"
"net"
"net/http"
"io/ioutil"
"github.com/zenazn/goji"
"github.com/zenazn/goji/web"
"github.com/zenazn/goji/bind"
)
// compile a few regex to keep things going smoothl
var ipRE = regexp.MustCompile("^[0-9]+.[0-9]+.[0-9]+.[0-9]+")
var portRE = regexp.MustCompile("[0-9]+$")
var curlRE = regexp.MustCompile("curl")
// This array is to set the order we want things output in
var dataOrder = []string{
"ip_addr",
"remote_host",
"user_agent",
"protocol",
"port",
"language",
"referrer",
"connection",
"method",
"encoding",
"mime",
"charset",
"via",
"forwarded",
}
/////////////////////////////////////////////////
//
// This is wher all the magic happens.
// We are treating any subpage as a parameter
// and parsing it here
//
/////////////////////////////////////////////////
func index (c web.C, w http.ResponseWriter, r *http.Request) {
// grab param named context.
// returns empty string if context param doesn't exist
var context = strings.TrimSpace(c.URLParams["context"])
// if there is no context,
// but the user-agent matches curl, print the ip
if context == "" {
if curlRE.MatchString(ua(r)) {
fmt.Fprintf(w, "%s\n", ip(r))
// if the user-agent is anything but curl, assume its a browser
} else {
var output = fullPage(r)
if output == "err" {
http.NotFound(w, r)
} else {
fmt.Fprintf(w, "%s", fullPage(r))
}
}
// matches the context to the function
// all of the following functions should
// only return strings, which are then
// printed in plain text
} else if context == "ip" {
fmt.Fprintf(w, "%s\n", ip(r))
} else if context == "host" {
fmt.Fprintf(w, "%s\n", host(r))
} else if context == "ua" {
fmt.Fprintf(w, "%s\n", ua(r))
} else if context == "port" {
fmt.Fprintf(w, "%s\n", port(r))
} else if context == "proto" {
fmt.Fprintf(w, "%s\n", proto(r))
} else if context == "lang" {
fmt.Fprintf(w, "%s\n", lang(r))
} else if context == "ref" {
fmt.Fprintf(w, "%s\n", referrer(r))
} else if context == "connection" {
fmt.Fprintf(w, "%s\n", connection(r))
} else if context == "encoding" {
fmt.Fprintf(w, "%s\n", encoding(r))
} else if context == "charset" {
fmt.Fprintf(w, "%s\n", charset(r))
} else if context == "via" {
fmt.Fprintf(w, "%s\n", via(r))
} else if context == "forwarded" {
fmt.Fprintf(w, "%s\n", forwarded(r))
} else if context == "mime" {
fmt.Fprintf(w, "%s\n", mime(r))
} else if context == "all" {
fmt.Fprintf(w, "%s", allTxt(r))
} else if context == "all.xml" {
fmt.Fprintf(w, "%s\n", allXml(r))
} else if context == "all.json" {
fmt.Fprintf(w, "%s\n", allJson(r))
// we did our best, but no function has been defined
} else {
http.NotFound(w, r)
}
}
//////////////////////////////////////
//
// Function Definitions for Context
//
//////////////////////////////////////
//
// returns ip (8.8.8.8)
//
func ip (r *http.Request) string {
var ip = ipRE.FindString(r.RemoteAddr)
var realIP = getHeader(r, "X-Forwarded-For")
if realIP != "" {
return realIP
}
return ip
}
//
// returns result of reverse dns lookup
// if error, return whatever was
// in the Host request field (probably the IP)
// else returns a string of all returned hosts
// (google-public-dns-b.google.com.)
//
func host (r *http.Request) string {
var list []string
hosts,err := net.LookupAddr(ipRE.FindString(ip(r)))
if err != nil {
return ip(r)
}
for _,host := range hosts {
list = append(list, host)
}
return strings.Join(list, ", ")
}
//
// returns whatever the client reported as its user-agent
// (Mozilla/3.01Gold (X11; I; SunOS 5.5.1 sun4m))
//
func ua (r *http.Request) string {
var header = r.Header
return fmt.Sprintf("%s", header["User-Agent"])
}
//
// returns client port. In most cases its a random negotiatd port
// and better be greater than 1024: (37906)
//
func port (r *http.Request) string {
return fmt.Sprintf("%s", portRE.FindString(r.RemoteAddr))
}
//
// returns the negotiated protocol (HTTP/1.1)
func proto (r *http.Request) string {
return fmt.Sprintf("%s", r.Proto)
}
//
// returns the accepted-language header
// (en-US,en;q=0.5)
//
func lang (r *http.Request) string {
return fmt.Sprintf("%s", getHeader(r, "Accept-Language"))
}
//
// returns the referrer if any
// (https://www.google.com)
// yes, you have to check for both
// because someone mispelled it in
// the original spec
//
func referrer (r *http.Request) string {
var iDontSpellGood = r.Referer()
if iDontSpellGood != "" {
return fmt.Sprintf("%s", r.Referer())
} else {
return fmt.Sprintf("%s", getHeader(r, "Referrer"))
}
}
//
// returns method verb
// (GET)
//
func method (r *http.Request) string {
return fmt.Sprintf("%s", r.Method)
}
//
// returns the connection header
// (keep-alive)
//
func connection (r *http.Request) string {
return fmt.Sprintf("%s", getHeader(r, "Connection"))
}
//
// returns string of acceptable encodings
// (gzip, deflate)
//
func encoding (r *http.Request) string {
return fmt.Sprintf("%s", getHeader(r, "Accept-Encoding"))
}
//
// retruns string of acceptable character sets
// (utf-8)
//
func charset (r *http.Request) string {
return fmt.Sprintf("%s", getHeader(r, "Accept-Charset"))
}
//
// returns any known proxies through which the request was sent
// (1.0 fred, 1.1 example.com (Apache/1.1))
//
func via (r *http.Request) string {
return fmt.Sprintf("%s", getHeader(r, "Via"))
}
//
// original information of client connecting through
// an HTTP prox
// (for=192.0.2.60;proto=http;by=203.0.113.43)
func forwarded (r *http.Request) string {
return fmt.Sprintf("%s", getHeader(r, "X-Forwarded-For"))
}
//
// returns mime-type header
// (text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8)
//
func mime (r *http.Request) string {
return fmt.Sprintf("%s", getHeader(r, "Content-Type"))
}
//
// runs all functions, and returns them in a map
// this is used when rendering html, xml, json, and text
//
func all (r *http.Request) map[string]string {
var allData map[string]string
allData = make(map[string]string)
allData["ip_addr"] = ip(r)
allData["port"] = port(r)
allData["user_agent"] = ua(r)
allData["remote_host"] = host(r)
allData["language"] = lang(r)
allData["referrer"] = referrer(r)
allData["encoding"] = encoding(r)
allData["mime"] = mime(r)
allData["protocol"] = proto(r)
allData["method"] = method(r)
allData["connection"] = connection(r)
allData["charset"] = charset(r)
allData["via"] = via(r)
allData["forwarded"] = forwarded(r)
return allData
}
//
// formats the map from the function all for text output
//
func allTxt (r *http.Request) string {
var allData = all(r)
var stringData []string
for _, key := range dataOrder {
stringData = append(
stringData,
strings.TrimSpace(key),
": ",
strings.TrimSpace(allData[key]),"\n",
)
}
return strings.Join(stringData, "")
}
//
// formats the map from the function all for text output
// overloaded so we don't have to call all()
//
func allTxtnc (allData map[string]string) string {
var stringData []string
for _, key := range dataOrder {
stringData = append(
stringData,
strings.TrimSpace(key),
": ",
strings.TrimSpace(allData[key]),"\n",
)
}
return strings.Join(stringData, "")
}
//
// formats the map from the function all for XML output
//
func allXml (r *http.Request) string {
var allData = all(r)
var stringData []string
stringData = append(
stringData,
"<info>\n",
)
for _, key := range dataOrder {
stringData = append(
stringData,
" <",
strings.TrimSpace(key),
">",
strings.TrimSpace(allData[key]),
"</",
strings.TrimSpace(key),
">\n",
)
}
stringData = append(
stringData,
"</info>",
)
return strings.Join(stringData, "")
}
//
// formats the map from the function all for XML output
// overloaded to not call back to all()
//
func allXmlnc (allData map[string]string) string {
var stringData []string
stringData = append(
stringData,
"<info>\n",
)
for _, key := range dataOrder {
stringData = append(
stringData,
" <",
strings.TrimSpace(key),
">",
strings.TrimSpace(allData[key]),
"</",
strings.TrimSpace(key),
">\n",
)
}
stringData = append(
stringData,
"</info>",
)
return strings.Join(stringData, "")
}
//
// formats the map from the function all for JSON output
//
func allJson (r *http.Request) string {
var allData = all(r)
var stringData []string
stringData = append(stringData, "{")
for _, key := range dataOrder {
stringData = append(
stringData,
"\"",
strings.TrimSpace(key),
"\":\"",
strings.TrimSpace(allData[key]),
"\",",
)
}
// remove the last character of teh last slice
stringData[len(stringData)-1] = strings.TrimSuffix(stringData[len(stringData)-1], ",")
stringData = append(stringData, "}")
return strings.Join(stringData, "")
}
//
//
// formats the map from the function all for JSON output
// overloaded to not call back to all()
//
func allJsonnc (allData map[string]string) string {
var stringData []string
stringData = append(stringData, "{")
for _, key := range dataOrder {
stringData = append(
stringData,
"\"",
strings.TrimSpace(key),
"\":\"",
strings.TrimSpace(allData[key]),
"\",",
)
}
// remove the last character of teh last slice
stringData[len(stringData)-1] = strings.TrimSuffix(stringData[len(stringData)-1], ",")
stringData = append(stringData, "}")
return strings.Join(stringData, "")
}
// facilitates grabbing known headers
// returns all known values for header as a single csv string
//
func getHeader (r *http.Request, key string) string {
return r.Header.Get(key)
}
//
// format the map from the fuction all for HTML output
//
func fullPage (r *http.Request) string {
var allData = all(r)
templateBytes,err := ioutil.ReadFile("template.html")
if err != nil {
return "err"
}
// compile some more regex for templating the html
var ip_addrRE = regexp.MustCompile("##ip_addr##")
var remote_hostRE = regexp.MustCompile("##remote_host##")
var user_agentRE = regexp.MustCompile("##user_agent##")
var protocolRE = regexp.MustCompile("##protocol##")
var portRE = regexp.MustCompile("##port##")
var languageRE = regexp.MustCompile("##language##")
var referrerRE = regexp.MustCompile("##referrer##")
var connectionRE = regexp.MustCompile("##connection##")
var methodRE = regexp.MustCompile("##method##")
var encodingRE = regexp.MustCompile("##encoding##")
var mimeRE = regexp.MustCompile("##mime##")
var charsetRE = regexp.MustCompile("##charset##")
var viaRE = regexp.MustCompile("##via##")
var forwardedRE = regexp.MustCompile("##forwarded##")
// create a few regex we know we will need
var urlRE = regexp.MustCompile("##URL##")
var allRE = regexp.MustCompile("##all##")
var allxmlRE = regexp.MustCompile("##allxml##")
var alljsonRE = regexp.MustCompile("##alljson##")
// convert the byte array into a string
template := string(templateBytes)
// apply our few known regex
template = urlRE.ReplaceAllString(template, r.Host)
template = allRE.ReplaceAllString(template, allTxtnc(allData))
template = allxmlRE.ReplaceAllString(template, allXmlnc(allData))
template = alljsonRE.ReplaceAllString(template, allJsonnc(allData))
template = ip_addrRE.ReplaceAllString(template, allData["ip_addr"])
template = remote_hostRE.ReplaceAllString(template, allData["remote_host"])
template = user_agentRE.ReplaceAllString(template, allData["user_agent"])
template = protocolRE.ReplaceAllString(template, allData["protocol"])
template = portRE.ReplaceAllString(template, allData["port"])
template = languageRE.ReplaceAllString(template, allData["language"])
template = referrerRE.ReplaceAllString(template, allData["referrer"])
template = connectionRE.ReplaceAllString(template, allData["connection"])
template = methodRE.ReplaceAllString(template, allData["method"])
template = encodingRE.ReplaceAllString(template, allData["encoding"])
template = mimeRE.ReplaceAllString(template, allData["mime"])
template = charsetRE.ReplaceAllString(template, allData["charset"])
template = viaRE.ReplaceAllString(template, allData["via"])
template = forwardedRE.ReplaceAllString(template, allData["forwarded"])
// we should probably do it like this commented out code,
// but it wasn't working for some reason
/* // loop through our special list
// and apply the rest of the regex
for _, key := range dataOrder {
var stringData []string
stringData = append(stringData, "##", key, "##")
var re = regexp.MustCompile(strings.Join(stringData, ""))
re.ReplaceAllString(template, allData[key])
fmt.Println(fmt.Sprintf("##%s: %s##", key, allData[key]))
}
*/
return template
}
//
// Yay, we are gonna do the mains
//
func main() {
goji.Get("/", index)
goji.Get("/:context", index)
goji.ServeListener(bind.Socket("0.0.0.0:8080"))
}