/
startPublicServer.go
562 lines (446 loc) · 14.1 KB
/
startPublicServer.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
package mainhelpers
import (
"encoding/base64"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"net/http/httputil"
"net/url"
"os"
"strings"
"time"
"github.com/awootton/knotfreeiot/iot"
"github.com/awootton/knotfreeiot/tokens"
"github.com/gorilla/websocket"
"github.com/prometheus/client_golang/prometheus/promhttp"
"golang.org/x/crypto/nacl/box"
)
type ApiHandler struct {
ce *iot.ClusterExecutive
staticStuffHandler webHandler
}
func (api ApiHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
fmt.Println("ApiHandler ServeHTTP", req.RequestURI, req.Host)
w.Header().Add("Access-Control-Allow-Origin", "*")
const proxyApiPath = "/api1/rawgithubusercontentproxy/"
if strings.HasPrefix(req.RequestURI, proxyApiPath) {
path := req.RequestURI[len(proxyApiPath):]
fmt.Println("proxy path", path)
if strings.HasSuffix(path, ".png") || strings.HasSuffix(path, ".jpg") {
w.Header().Set("Content-Type", "image/png")
}
resp, err := http.Get("https://raw.githubusercontent.com/" + path)
if err != nil {
w.Write([]byte("error " + err.Error()))
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
w.Write([]byte("error " + err.Error()))
} else {
w.Write(body)
}
} else if req.RequestURI == "/api1/getallstats" {
stats := api.ce.Aides[0].ClusterStatsString
w.Write([]byte(stats))
} else if req.RequestURI == "/api1/getstats" {
stats := api.ce.Aides[0].GetExecutiveStats()
bytes, err := json.Marshal(stats)
if err != nil {
fmt.Println("GetExecutiveStats marshal", err)
}
w.Write(bytes)
} else if req.RequestURI == "/api1/getToken" {
// if isLocal(req) {
// w.Header().Set("Access-Control-Allow-Origin", "http://localhost:3000")
// } else {
// w.Header().Set("Access-Control-Allow-Origin", "http://knotfree.io")
// }
api.ServeMakeToken(w, req)
} else if req.RequestURI == "/api1/getPublicKey" {
//sss := base64.RawURLEncoding.EncodeToString([]byte(tokens.FindPublicKey("yRst")))
sss := base64.RawURLEncoding.EncodeToString(api.ce.PublicKeyTemp[:])
fmt.Println("serve /api1/getPublicKey ", sss)
//w.Header().Set("Access-Control-Allow-Origin", "*")
w.Write([]byte(sss))
} else if req.RequestURI == "/api1/getGiantPassword" {
sss := tokens.MakeRandomPhrase(14)
w.Write([]byte(sss))
} else if req.RequestURI == "/api1/help" {
// w.Header().Set("Access-Control-Allow-Origin", "*")
sss := "/api1/getallstats\n"
sss += "/api1/getstats\n"
sss += "/api1/getToken\n"
sss += "/api1/getPublicKey\n"
sss += "/api1/getGiantPassword\n"
w.Write([]byte(sss))
} else {
// http.NotFound(w, req)
// fmt.Fprintf(w, "expected known path "+req.RequestURI)
// iot.HTTPServe404.Inc()
api.staticStuffHandler.ServeHTTP(w, req)
}
}
type SuperMux struct {
ce *iot.ClusterExecutive
//super,
sub *http.ServeMux
}
// type pinfo struct {
// // these are the reply buffers
// buff []byte
// }
// type RequestReplyStruct struct {
// originalRequest []byte
// firstLine string
// replyParts []pinfo
// }
// var servedMap = map[string]*RequestReplyStruct{}
func IsLocal(r *http.Request) bool {
fmt.Println("host is ", r.Host)
if os.Getenv("KNOT_KUNG_FOO") == "atw" {
return true
}
if strings.Contains(r.Host, "local") {
return true
}
return false
}
func (superMux *SuperMux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
//fmt.Println("giant token ", tokens.GetImpromptuGiantToken())
// if !strings.Contains(r.Host, "knotfree.") {
// fmt.Println("unknown host ", r.Host)
// http.NotFound(w, r)
// return
// }
// fmt.Println("ServeHTTP from host ", r.Host)
if r.Host == "212.2.245.112:80" { // the ip address of knotfree.io
r.Host = "knotfree.io"
}
domainParts := strings.Split(r.Host, ".")
if len(domainParts) == 4 {
fmt.Println("unknown host ", r.Host)
http.NotFound(w, r)
return
}
// eg [knotfree net]
// eg [subdomain knotfree net]
// eg [subdomain knotfree io]
// fmt.Println("serving domainParts ", domainParts)
isApiRequest := strings.HasPrefix(r.RequestURI, "/api1/") // len(r.RequestURI) >= 9 && r.RequestURI[:9] == "/api1/get"
isApiRequest = isApiRequest || r.RequestURI == "/mqtt"
if !isApiRequest && len(domainParts) > 2 && domainParts[0] != "www" {
// we have a subdomain
subDomain := domainParts[0]
HandleHttpSubdomainRequest(w, r, superMux.ce.Aides[0], subDomain)
} else {
// it's not a subdomain pass it to the api.
superMux.sub.ServeHTTP(w, r)
}
}
type ProxyHandler struct {
p *httputil.ReverseProxy
}
func (ph *ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
fmt.Println(r.URL)
w.Header().Set("X-Ben", "Rad")
ph.p.ServeHTTP(w, r)
}
func StartPublicServer(ce *iot.ClusterExecutive) {
// an http server and reverse proxy.
go startPublicServer3100(ce)
// for prometheus webpage
go startPublicServer9090(ce) // eliminate?
// go startPublicServer8000(ce) // was for libra
go startPublicServer9102(ce) // promhttp.Handler for getting metrics
go func() { // generate heartbeat
for {
// mt.Println("Heartbeat ", ce.Aides[0].Name)
ce.Aides[0].Heartbeat(uint32(time.Now().Unix()))
// fmt.Println("Heartbeat DONE", ce.Aides[0].Name)
time.Sleep(10 * time.Second)
}
}()
supermux := &SuperMux{}
supermux.ce = ce
supermux.sub = http.NewServeMux()
staticStuffHandler := webHandler{ce,
http.FileServer(http.Dir("./docs"))} // FIXME: points to gotohere static assets (a react build)
supermux.sub.Handle("/mqtt", wsAPIHandler{ce})
supermux.sub.Handle("/", ApiHandler{ce, staticStuffHandler})
s := &http.Server{
Addr: ":8085",
Handler: supermux,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 13,
}
go func(s *http.Server) {
fmt.Println("http service for ws " + s.Addr)
err := s.ListenAndServe()
_ = err
fmt.Println("ListenAndServe 8085 returned !!!!! arrrrg", err)
}(s)
}
func startPublicServer9102(ce *iot.ClusterExecutive) {
fmt.Println("http metrics service 9102")
http.Handle("/metrics", promhttp.Handler())
http.ListenAndServe(":9102", nil)
fmt.Println("http service 9102 FAIL")
}
func startPublicServer3100(ce *iot.ClusterExecutive) {
// an http server and reverse proxy.
mux := http.NewServeMux()
if true {
origin, _ := url.Parse("http://grafana.monitoring:3100/")
director := func(req *http.Request) {
req.Header.Add("X-Forwarded-Host", req.Host)
req.Header.Add("X-Origin-Host", origin.Host)
req.URL.Scheme = "http"
req.URL.Host = origin.Host
//fmt.Println("fwd graf:", req.URL.Host, req.URL.Port(), req.URL.Path)
iot.ForwardsCount3100.Inc()
}
proxy := &httputil.ReverseProxy{Director: director}
mux.Handle("/", proxy)
}
s := &http.Server{
Addr: ":3100",
Handler: mux,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 13,
}
go func(s *http.Server) {
fmt.Println("http grafana service " + s.Addr)
err := s.ListenAndServe()
_ = err
fmt.Println("ListenAndServe 3100 returned !!!!! arrrrg", err)
}(s)
}
func startPublicServer9090(ce *iot.ClusterExecutive) {
// an http server and reverse proxy.
mux := http.NewServeMux()
if true {
origin, _ := url.Parse("http://prometheus-operated.monitoring:9090/")
director := func(req *http.Request) {
req.Header.Add("X-Forwarded-Host", req.Host)
req.Header.Add("X-Origin-Host", origin.Host)
req.URL.Scheme = "http"
req.URL.Host = origin.Host
//fmt.Println("fwd prom:", req.URL.Host, req.URL.Port(), req.URL.Path)
iot.ForwardsCount9090.Inc()
}
proxy := &httputil.ReverseProxy{Director: director}
mux.Handle("/", proxy)
}
s := &http.Server{
Addr: ":9090",
Handler: mux,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 13,
}
go func(s *http.Server) {
fmt.Println("http service " + s.Addr)
err := s.ListenAndServe()
_ = err
fmt.Println("ListenAndServe 9090 returned !!!!! arrrrg", err)
}(s)
}
var upgrader = websocket.Upgrader{}
type wsAPIHandler struct {
ce *iot.ClusterExecutive
}
func (api wsAPIHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
//fmt.Println("ws ServeHTTP", r.RequestURI)
allowAll := func(r *http.Request) bool {
return true
}
upgrader.WriteBufferSize = 4096
upgrader.ReadBufferSize = 4096
upgrader.CheckOrigin = allowAll
upgrader.Subprotocols = []string{"mqtt", "mqttv5", "mqttv3.1"}
wsConn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Print("upgrade:", err)
return
}
iot.WebSocketLoop(wsConn, api.ce.Aides[0].Config)
}
func ParsePayload(httpBytes string) (string, map[string]string, string) {
headerMap := make(map[string]string)
pos := strings.Index(httpBytes, "\r\n\r\n")
if pos <= 0 {
fmt.Println("isWhat? no header end!!! this is bad ", httpBytes)
return "", headerMap, ""
}
payload := httpBytes[pos+4:]
headers := httpBytes[0:pos]
headerParts := strings.Split(headers, "\r\n")
firstLine := headerParts[0]
headerParts = headerParts[1:]
for _, head := range headerParts {
pos = strings.Index(head, ":")
if pos > 0 && len(head) > 3 {
key := strings.Trim(head[0:pos], " ")
val := strings.Trim(head[pos+1:], " ")
headerMap[key] = val
} else {
fmt.Println("weird header found " + head)
}
}
return firstLine, headerMap, payload
}
var bootTimeSec int64 = 0
var tokensServed int64 = 0
func (api ApiHandler) ServeMakeToken(w http.ResponseWriter, req *http.Request) {
if bootTimeSec == 0 {
bootTimeSec = time.Now().Unix()
}
remoteAddr := req.RemoteAddr
parts := strings.Split(remoteAddr, ":") // eg [::1]:49326
// lose the port
parts = parts[0 : len(parts)-1]
remoteAddr = strings.Join(parts, ":")
// remoteAddr += req.Header.Get("HTTP_X_FORWARDED_FOR")
ctx := req.Context()
// what is IP or id of sender?
fmt.Println("token req RemoteAddr", remoteAddr) // F I X M E: use db see below
now := time.Now().Unix()
numberOfMinutesPassed := (now - bootTimeSec) / 6 // now it's 10 sec
if tokensServed > numberOfMinutesPassed {
iot.BadTokenRequests.Inc()
http.Error(w, "Token dispenser is too busy now. Try in a minute, or, you could subscribe and get better tokens", 500)
return
}
if numberOfMinutesPassed > 60 {
// reset the allocator every hour
bootTimeSec = now
tokensServed = 0
}
var buff1024 [1024]byte
n, err := req.Body.Read(buff1024[:])
if err != nil {
iot.BadTokenRequests.Inc()
}
buf := buff1024[:n]
fmt.Println("token request read body", string(buf), n)
_ = buf
tokenRequest := &tokens.TokenRequest{}
err = json.Unmarshal(buf, tokenRequest)
if err != nil {
iot.BadTokenRequests.Inc()
fmt.Println("TokenRequest err", err.Error())
http.Error(w, err.Error(), 500)
return
} else {
// todo: calc cost of this token and have limit.
// move this phat routine somewhere else TODO:
clientPublicKey := tokenRequest.Pkey
if len(clientPublicKey) != 64 {
iot.BadTokenRequests.Inc()
http.Error(w, "bad client key", 500)
return
}
// not using the payload . we always hand out Tiny4
payload := tokens.KnotFreeTokenPayload{}
payload.Issuer = "_9sh"
payload.JWTID = tokens.GetRandomB36String()
nonce := payload.JWTID
payload.ExpirationTime = uint32(time.Now().Unix()) + 60*60*24*30 // a month
priceThing := tokens.GetTokenStatsAndPrice(tokens.TinyX4)
payload.KnotFreeContactStats = priceThing.Stats
parts := strings.Split(req.Host, ".")
partslen := len(parts)
if partslen < 2 {
parts = strings.Split("local.localhost", ".")
partslen = len(parts)
}
targetSite := parts[partslen-2] + "." + parts[partslen-1]
payload.URL = targetSite
exp := payload.ExpirationTime
if exp > uint32(time.Now().Unix()+60*60*24*365) {
// more than a year in the future not allowed now.
exp = uint32(time.Now().Unix() + 60*60*24*365)
fmt.Println("had long token ", string(payload.JWTID)) // TODO: store in db
}
cost := priceThing.Price // tokens.CalcTokenPrice(&payload, uint32(time.Now().Unix()))
fmt.Println("token cost is " + fmt.Sprintf("%f", cost))
// if cost > 0.012 {
// http.Error(w, "token too expensive at "+fmt.Sprintf("%f", cost), 500)
// return
// }
signingKey := tokens.GetPrivateKey("_9sh")
tokenString, err := tokens.MakeToken(&payload, []byte(signingKey))
if err != nil {
iot.BadTokenRequests.Inc()
http.Error(w, err.Error(), 500)
return
}
signingKey = "unused now"
when := time.Unix(int64(exp), 0)
year, month, day := when.Date()
comments := make([]interface{}, 3)
tmp := fmt.Sprintf(" expires: %v-%v-%v", year, int(month), day)
comments[0] = tokenRequest.Comment + tmp
comments[1] = "" //payload
comments[2] = string(tokenString)
//returnval, err := json.Marshal(comments)
_ = err
//returnval = []byte(strings.ReplaceAll(string(returnval), `"`, ``))
// returnval = []byte(strings.ReplaceAll(string(returnval), ` `, `_`))
//fmt.Println("sending token package ", string(returnval))
returnval := tokenString
err = tokens.LogNewToken(ctx, &payload, remoteAddr)
if err != nil {
iot.BadTokenRequests.Inc()
http.Error(w, err.Error(), 500)
return
}
// box it up
boxout := make([]byte, len(returnval)+box.Overhead)
boxout = boxout[:0]
var jwtid [24]byte
copy(jwtid[:], []byte(nonce))
var clipub [32]byte
n, err := hex.Decode(clipub[:], []byte(clientPublicKey))
_ = err
if n != 32 {
iot.BadTokenRequests.Inc()
http.Error(w, "bad size2", 500)
return
}
sealed := box.Seal(boxout, returnval, &jwtid, &clipub, api.ce.PrivateKeyTemp)
reply := tokens.TokenReply{}
reply.Nonce = nonce
reply.Pkey = hex.EncodeToString(api.ce.PublicKeyTemp[:])
reply.Payload = hex.EncodeToString(sealed)
bytes, err := json.Marshal(reply)
if err != nil {
iot.BadTokenRequests.Inc()
http.Error(w, err.Error(), 500)
return
}
//time.Sleep(8 * time.Second)
time.Sleep(1 * time.Second)
w.Write(bytes)
tokensServed++
fmt.Println("done sending free token")
}
}
type webHandler struct { // is this even used?
ce *iot.ClusterExecutive
// fs1 http.Handler
fs2 http.Handler
//fs := http.FileServer(http.Dir("./docs/_site"))
// supermux.sub.Handle("/", fs)
}
// webHandler.ServeHTTP serves the static content
func (api webHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
fmt.Println("webHandler ServeHTTP", r.RequestURI)
api.fs2.ServeHTTP(w, r)
}