/
rputn-srv.go
567 lines (479 loc) · 14.4 KB
/
rputn-srv.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
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
// File: rputn-srv.go
//
// Copyright (c) 2013 Charles Perkins
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
package main
import (
"fmt"
"net/url"
"runtime"
"bytes"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"strings"
"time"
"rputbl.com/hashbase"
)
var hostname []byte
var hb HBstats
var mstore hashbase.HashPersister
func pwdfile(f string) string {
out := "File Read Error"
content, err := ioutil.ReadFile(f)
if err == nil {
out = string(content)
}
return out
}
func reply2request(req string) string {
return req
}
func rootHandler(w http.ResponseWriter, r *http.Request) {
log.Printf("%s GET-HTML", hostname)
w.Header().Set("Content-Type", "text/html")
fmt.Fprintf(w, "%s", pwdfile("rputbl.html"))
}
func isAmpersand(r rune) bool {
if r == '&' {
return true
}
return false
}
func isSpace(r rune) bool {
if r == ' ' {
return true
}
return false
}
func assertHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
if len(r.URL.String()) < 4 {
cmc <- CM{i: CAssertBad, n: 1}
fmt.Fprintf(w, "%s", "{\"error\" : \"malformed query (too small)\"}")
log.Printf("%s BADASSERT-QUERY-FORMAT %s ", hostname, r.URL.String())
return
}
qline := r.URL.String()[3:]
qFields := strings.FieldsFunc(qline, isAmpersand)
if len(qFields) < 4 {
cmc <- CM{i: CAssertBad, n: 1}
fmt.Fprintf(w, "%s", "{\"error\" : \"malformed query (too few fields)\"}")
log.Printf("%s BADASSERT-QUERY-FORMAT %s ", hostname, r.URL.String())
return
}
fh64val := qFields[0]
fhbv, err := hashbase.Un64(fh64val)
if err != nil {
cmc <- CM{i: CAssertBad, n: 1}
fmt.Fprintf(w, "%s", "{\"error\" : \"malformed file hash\"}")
log.Printf("%s BADASSERT-BASE64-DECODE %s ", hostname, qline)
return
}
if len(fhbv) != 28 {
cmc <- CM{i: CAssertBad, n: 1}
fmt.Fprintf(w, "%s", "{\"error\" : \"this is not a SHA224 (28 byte) file hash\"}")
log.Printf("%s BADASSERT-SHA224-SIZE %s ", hostname, qline)
return
}
ph64val := qFields[1]
phbv, err := hashbase.Un64(ph64val)
if err != nil {
cmc <- CM{i: CAssertBad, n: 1}
fmt.Fprintf(w, "%s", "{\"error\" : \"malformed asserter hash\"}")
log.Printf("%s BADASSERT-BASE64-DECODE %s ", hostname, qline)
return
}
if len(phbv) != 28 {
cmc <- CM{i: CAssertBad, n: 1}
fmt.Fprintf(w, "%s", "{\"error\" : \"this is not a SHA224 (28 byte) asserter hash\"}")
log.Printf("%s BADASSERT-SHA224-SIZE %s ", hostname, qline)
return
}
assertstr := qFields[2]
if len(assertstr) < 1 {
cmc <- CM{i: CAssertBad, n: 1}
fmt.Fprintf(w, "%s", "{\"error\" : \"malformed assert content\"}")
log.Printf("%s BADASSERT-ASSERT-FORMAT %s ", hostname, qline)
return
}
sigstr := qFields[3]
if len(sigstr) < 1 {
cmc <- CM{i: CAssertBad, n: 1}
fmt.Fprintf(w, "%s", "{\"error\" : \"malformed signature content\"}")
log.Printf("%s BADASSERT-SIGNATURE-FORMAT %s ", hostname, qline)
return
}
cmc <- CM{i: CAssertGood, n: 1}
err = mstore.Assert(fh64val,ph64val,assertstr)
if err != nil {
fmt.Fprintf(w, "{\"error\" : \"%s\"}",err.Error())
log.Printf("%s %s %s ", hostname, err.Error(), qline)
return
}
cmc <- CM{i: CAssertion, n: 1}
fmt.Fprintf(w, "%s", "{\"ok\" : \"query understood\"}")
log.Printf("%s ASSERT %s by %s is %s", hostname, fh64val, ph64val, assertstr)
}
func queryHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
hash64val := r.URL.String()[3:]
hbv, err := hashbase.Un64(hash64val)
if err != nil {
cmc <- CM{i: CQueryBad, n: 1}
fmt.Fprintf(w, "%s", "{\"error\" : \"malformed base64 encoding\"}")
log.Printf("%s BADQUERY-BASE64-DECODE %s ", hostname, r.URL.String()[3:])
} else {
if len(hbv) != 28 {
cmc <- CM{i: CQueryBad, n: 1}
fmt.Fprintf(w, "%s", "{\"error\" : \"this is not a SHA224 (28 byte) hash\"}")
log.Printf("%s BADQUERY-SHA224-SIZE %s ", hostname, r.URL.String()[3:])
} else {
assert, err := mstore.Query(hash64val)
if err == nil {
cmc <- CM{i: CQueryGood, n: 1}
fmt.Fprintf(w, "%s%s%s", "{\"ok\" : \"success\",\"asserted\" : \"", assert, "\"}")
log.Printf("%s QUERY %s ", hostname, r.URL.String()[3:])
} else {
cmc <- CM{i: CQueryGood, n: 1}
fmt.Fprintf(w, "%s", "{\"ok\" : \"no such hash\"}")
log.Printf("%s QUERY %s ", hostname, r.URL.String()[3:])
}
}
}
}
func introHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
// log.Printf("DEBUG-YEEK")
// fmt.Fprintf(w, "%s", "{\"debug\" : \"YEEK\"}")
if len(r.URL.String()) < 4 {
// cmc <- CM{ i: CAssertBad, n:1 }
fmt.Fprintf(w, "%s", "{\"error\" : \"malformed query (too small)\"}")
log.Printf("%s BADINTRO-QUERY-FORMAT %s ", hostname, r.URL.String())
return
}
qline := r.URL.String()[3:]
qFields := strings.FieldsFunc(qline, isAmpersand)
if len(qFields) < 2 {
// cmc <- CM{ i: CAssertBad, n:1 }
fmt.Fprintf(w, "%s", "{\"error\" : \"malformed query (too few fields)\"}")
log.Printf("%s BADINTRO-QUERY-FORMAT %s ", hostname, r.URL.String())
return
}
ph64val := qFields[0]
phbv, err := hashbase.Un64(ph64val)
if err != nil {
// cmc <- CM{ i: CAssertBad, n:1 }
fmt.Fprintf(w, "%s", "{\"error\" : \"malformed file hash\"}")
log.Printf("%s BADINTRO-BASE64-DECODE %s ", hostname, qline)
return
}
if len(phbv) != 28 {
// cmc <- CM{ i: CAssertBad, n:1 }
fmt.Fprintf(w, "%s", "{\"error\" : \"this is not a SHA224 (28 byte) public key hash\"}")
log.Printf("%s BADINTRO-SHA224-SIZE %s ", hostname, qline)
return
}
rsa_pub, err := url.QueryUnescape(qFields[1])
if err != nil {
// cmc <- CM{ i: CAssertBad, n:1 }
fmt.Fprintf(w, "%s", "{\"error\" : \"malformed escaped pubkey\"}")
log.Printf("%s BADINTRO-PUBKEY-UNESCAPE %s ", hostname, qline)
return
}
rsa_pub_stored, err := mstore.Query(ph64val)
if err == nil {
lspi := strings.LastIndex(rsa_pub_stored, " ")
if lspi < 1 {
// cmc <- CM{ i: CAssertBad, n:1 }
fmt.Fprintf(w, "%s", "{\"error\" : \"malformed stored user record\"}")
log.Printf("%s BADINTRO-STOREDKEY-DECODE %s ", hostname, qline)
return
}
rsa_pub_name := strings.Trim(rsa_pub_stored[lspi+1:], " \n")
fmt.Fprintf(w, "%s%s%s", "{\"ok\" : \"success\",\"remembered\" : \"", rsa_pub_name, "\"}")
log.Printf("%s INTRO-ALREADY %s ", hostname, rsa_pub_name)
} else {
lspi := strings.LastIndex(rsa_pub, " ")
if lspi < 1 {
// cmc <- CM{ i: CAssertBad, n:1 }
fmt.Fprintf(w, "%s", "{\"error\" : \"malformed stored user record\"}")
log.Printf("%s BADINTRO-STOREDKEY-DECODE %s ", hostname, qline)
return
}
rsa_pub_name := strings.Trim(rsa_pub[lspi+1:], " \n")
cmc <- CM{i: CIntroduction, n: 1}
err = mstore.Assert(ph64val,"INTRODUCING",rsa_pub)
if err != nil {
fmt.Fprintf(w, "{\"error\" : \"%s\"}",err.Error())
log.Printf("%s %s %s ", hostname, err.Error(), qline)
return
}
fmt.Fprintf(w, "%s%s%s", "{\"ok\" : \"success\",\"introduced\" : \"", rsa_pub_name, "\"}")
log.Printf("%s INTRO-NEW %s ", hostname, rsa_pub_name)
}
}
func ieCssHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/css")
fmt.Fprintf(w, "%s", pwdfile("ie.css"))
}
func printCssHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/css")
fmt.Fprintf(w, "%s", pwdfile("print.css"))
}
func screenCssHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/css")
fmt.Fprintf(w, "%s", pwdfile("screen.css"))
}
func buttonScreenCssHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
fmt.Fprintf(w, "%s", pwdfile("button-screen.css"))
}
func loginJsHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
fmt.Fprintf(w, "%s", pwdfile("login.js"))
}
func jqueryJsHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
fmt.Fprintf(w, "%s", pwdfile("jquery-1.4.4.min.js"))
}
func tickPngHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
fmt.Fprintf(w, "%s", pwdfile("tick.png"))
}
func authenticateHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
if 1 == 1 {
log.Printf("AUTH-SUCCESS")
fmt.Fprintf(w, "%s", "{\"success\" : \"login is successful\", \"userid\" : \"33\"}")
} else {
fmt.Fprintf(w, "%s", "{{\"error\" : \"username or password is wrong\"}}")
}
}
func echoHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
fmt.Fprintf(w, "THIS IS FROM AJAX")
// ctx.Request.ParseForm()
// data := ctx.Request.FormValue("data")
// return reply2request(data)
}
type HBstats struct {
Hostname [32]byte
Pid int
Assertions uint32
Identities uint32
Hashcap uint32
Hashload uint32
Disksize uint32
Diskuse uint32
Repclan uint32
Shardspan uint32
Connections uint32
Qgsec [60]int
Agsec [60]int
Qbsec [60]int
Absec [60]int
Respms [60]int
Cpuload string
Memuse uint32
Netuse uint32
}
func Acc60(itm [60]int) int {
var acc int
acc = 0
for i := 0; i < 60; i++ {
acc = acc + itm[i]
}
return acc
}
func statusHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
fmt.Fprintf(w, "{")
fmt.Fprintf(w, "\"hostname\" : \"%s\",", hostname)
fmt.Fprintf(w, "\"pid\" : \"%d\",", hb.Pid)
fmt.Fprintf(w, "\"assertions\" : \"%d\",", hb.Assertions)
fmt.Fprintf(w, "\"identities\" : \"%d\",", hb.Identities)
fmt.Fprintf(w, "\"hashcap\" : \"%d\",", hb.Hashcap)
fmt.Fprintf(w, "\"hashload\" : \"%d\",", hb.Hashload)
fmt.Fprintf(w, "\"disksize\" : \"%d\",", hb.Disksize)
fmt.Fprintf(w, "\"diskuse\" : \"%d\",", hb.Diskuse)
fmt.Fprintf(w, "\"repclan\" : \"%d\",", hb.Repclan)
fmt.Fprintf(w, "\"shardspan\" : \"%d\",", hb.Shardspan)
fmt.Fprintf(w, "\"connections\" : \"%d\",", hb.Connections)
fmt.Fprintf(w, "\"qgsec\" : \"%d\",", Acc60(hb.Qgsec))
fmt.Fprintf(w, "\"agsec\" : \"%d\",", Acc60(hb.Agsec))
fmt.Fprintf(w, "\"qbsec\" : \"%d\",", Acc60(hb.Qbsec))
fmt.Fprintf(w, "\"absec\" : \"%d\",", Acc60(hb.Absec))
fmt.Fprintf(w, "\"respms\" : \"%d\",", Acc60(hb.Respms))
fmt.Fprintf(w, "\"cpuload\" : \"%s\",", hb.Cpuload)
fmt.Fprintf(w, "\"memuse\" : \"%d\",", hb.Memuse)
fmt.Fprintf(w, "\"netuse\" : \"%d\"", hb.Netuse)
fmt.Fprintf(w, "}")
}
func getHostname() {
cmd := exec.Command("hostname")
bs, err := cmd.Output()
if err != nil {
}
offset := bytes.IndexRune(bs, '.')
if offset < 1 {
offset = len(bs)
}
hostname = bs[:offset]
offset = bytes.IndexRune(hostname, '\n')
if offset < 1 {
offset = len(hostname)
}
hostname = hostname[:offset]
}
func getLoad() string {
switch runtime.GOOS {
case "windows": // ...
return "Windows"
case "linux": // ...
cmd := exec.Command("cat", "/proc/loadavg")
bs, err := cmd.Output()
if err != nil {
return "top error"
}
offset := bytes.IndexRune(bs, '\n')
if offset < 1 {
offset = len(bs)
}
return string(bs[:offset])
case "freebsd": // ...
return "freebsd"
case "darwin": // ...
cmd := exec.Command("sysctl", "-n", "vm.loadavg")
bs, err := cmd.Output()
if err != nil {
return "top error"
}
offset := bytes.IndexRune(bs, '\n')
if offset < 1 {
offset = len(bs)
}
return string(bs[:offset])
}
return "dunno!"
}
func listenForClientsSSL() {
log.Printf("Listening for clients on 8081/SSL")
err := http.ListenAndServeTLS(":8081", "cert.pem", "key.pem", nil)
if err != nil {
log.Fatal(err)
}
}
func listenForClientsHTTP() {
log.Printf("Listening for clients on 8082/HTTP")
err := http.ListenAndServe(":8082", nil)
if err != nil {
log.Fatal(err)
}
}
func housekeeping() {
for {
hb.Cpuload = getLoad()
time.Sleep(1 * time.Second)
}
}
type CM struct {
i int
n int
}
var cmc chan CM
var qgc chan int
var agc chan int
var qbc chan int
var abc chan int
var rmc chan int
const (
CAssertion = iota
CIntroduction
CQueryGood
CQueryBad
CAssertGood
CAssertBad
CResponseMs
)
func cmrecord() {
var cm CM
for {
cm = <-cmc
switch cm.i {
case CAssertion:
hb.Assertions = hb.Assertions + uint32(cm.n)
case CIntroduction:
hb.Identities = hb.Identities + uint32(cm.n)
case CQueryGood:
hb.Qgsec[0] = hb.Qgsec[0] + cm.n
case CQueryBad:
hb.Qbsec[0] = hb.Qbsec[0] + cm.n
case CAssertGood:
hb.Agsec[0] = hb.Agsec[0] + cm.n
case CAssertBad:
hb.Absec[0] = hb.Absec[0] + cm.n
case CResponseMs:
hb.Respms[0] = hb.Respms[0] + cm.n
}
}
}
func main() {
mstore = new(hashbase.LocalMapHashes)
mstore.Init()
mstore.Load(nil)
getHostname()
go housekeeping()
hb.Pid = os.Getpid()
hb.Assertions = 0
hb.Identities = 0
hb.Hashcap = 0
hb.Hashload = 0
hb.Disksize = 0
hb.Diskuse = 0
hb.Repclan = 0
hb.Shardspan = 0
hb.Connections = 0
hb.Memuse = 0
hb.Netuse = 0
cmc = make(chan CM)
go cmrecord()
http.HandleFunc("/", rootHandler)
http.HandleFunc("/a", assertHandler)
http.HandleFunc("/q", queryHandler)
http.HandleFunc("/i", introHandler)
http.HandleFunc("/ie.css", ieCssHandler)
http.HandleFunc("/print.css", printCssHandler)
http.HandleFunc("/screen.css", screenCssHandler)
http.HandleFunc("/buttons-screen.css", buttonScreenCssHandler)
http.HandleFunc("/login.js", loginJsHandler)
http.HandleFunc("/jquery-1.4.4.min.js", jqueryJsHandler)
http.HandleFunc("/tick.png", tickPngHandler)
http.HandleFunc("/status", statusHandler)
http.HandleFunc("/authenticate", authenticateHandler)
http.HandleFunc("/ejax", echoHandler)
go listenForClientsSSL()
listenForClientsHTTP()
}