-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
593 lines (507 loc) · 15.7 KB
/
http.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
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
// http.go
package main
import (
"crypto/tls"
"crypto/x509"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/http/httputil"
"net/url"
"strconv"
"strings"
"time"
"github.com/golang/glog"
)
var htmlEscaper = strings.NewReplacer(
`&`, "&",
`'`, "'",
`<`, "<",
`>`, ">",
`"`, """,
)
// -----------------------------------------------
const htmlHeader = `
<!-- HEADER -->
<html>
<head>
<title>goHome</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="description" content="goHome">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body
style="background-image: url(/images/PlageMoorea.jpg);background-repeat: no-repeat;background-size: 100%%;"
onload="%s"
>
<div align="center" onclick="window.location.reload(true);" >%s</div>
<br><br>
<div id="maindiv" align="center">
`
const htmlUserCode = `
<form action="#" method="POST">
<input type="password" id="usercode" name="usercode" value="%s">
<input type="submit" value="Submit">
</form>
`
const htmlAction = `
<table><tr>
<td align="center">
<form action="#" method="POST">
<input type="hidden" id="usercode" name="usercode" value="%s">
<input type="hidden" id="objectid" name="objectid" value="3">
<input type="image" src="/images/portail.jpg" border="1px" width="120" height="120">
</form>
</td>
<td align="center">
<form action="#" method="POST">
<input type="hidden" id="usercode" name="usercode" value="%s">
<input type="hidden" id="objectid" name="objectid" value="4">
<input type="image" src="/images/garage.jpg" border="1px" width="120" height="120">
</form>
</td>
</tr><tr>
<td align="center" colspan="2" >
<br><br>
<form action="#" method="POST">
<input type="hidden" id="usercode" name="usercode" value="%s">
<input type="hidden" id="objectid" name="objectid" value="0">
<input type="image" src="/capture/simpleAlarm.jpg" border="2px" width="512" height="384">
</form>
</td>
</tr></table>
`
const htmlFooter = `
</div>
<!-- FOOTER -->
<p align="center">-*-</p>
<p align="center">%s</p>
</body>
</html>
`
// -----------------------------------------------
func getFormStrVal(form url.Values, key string, idx int) (strVal string, err error) {
val, found := form[key]
if !found {
err = errors.New(fmt.Sprintf(`{"error":"Missing '%s' in form data (%s)"}`, key, form))
return
}
strVal = val[idx]
return
}
func writeApiError(w http.ResponseWriter, errMsg string) {
if glog.V(1) {
glog.Infof("writeApiError : '%s'", errMsg)
}
w.Write(apiError(errMsg))
return
}
// -----------------------------------------------
// defaultResponse : for testing only
//func defaultResponse(w http.ResponseWriter, r *http.Request) {
// const header = `<!-- HEADER -->
//<html>
//<head>
//<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
//<meta name="viewport" content="width=device-width, initial-scale=1">
//<title>goHome</title>
//</head>
//<body>
//`
// const footer = `<!-- FOOTER -->
//<br><br>
//<p align="center">-*-</p>
//<p align="center">%s</p>
//</body>
//</html>
//`
// fmt.Fprintf(w, header)
// fmt.Fprintf(w, "<p>goHome HTTPS server<p>\n")
// fmt.Fprintf(w, "<p>goHome version %s</p>\n", goHomeVersion)
// fmt.Fprintf(w, "<p>URL requested : %s </p>\n", r.URL.Path)
// fmt.Fprintf(w, "<p>Post params : %s</p>\n", r.Form)
// fmt.Fprintf(w, footer, time.Now().String())
//}
// -----------------------------------------------
// sendInitPage : landing page for simpleResponse
func sendInitPage(w http.ResponseWriter) {
fmt.Fprintf(w, htmlHeader, "", "Utilisateur inconnu")
fmt.Fprintf(w, htmlUserCode, "")
fmt.Fprintf(w, htmlFooter, time.Now().Format("2 Jan 2006 15:04:05") )
return
}
// sendActionPage : returning defaut action page
func sendActionPage(w http.ResponseWriter, onload string, userName string, userCode string ) {
fmt.Fprintf(w, htmlHeader, onload, userName)
fmt.Fprintf(w, htmlAction, userCode, userCode, userCode)
fmt.Fprintf(w, htmlFooter, time.Now().Format("2 Jan 2006 15:04:05") )
return
}
// -----------------------------------------------
// simpleResponse : for simple HTTP client that can't handle modern css
func simpleResponse(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Strict-Transport-Security", "max-age=63072000; includeSubDomains")
// Parse received request
if err := r.ParseForm(); err != nil {
writeApiError(w, fmt.Sprintf("Form parse error '%s' for (%s)", err, r.Form))
return
}
if glog.V(2) {
glog.Infof("Paramaters = %v",r.Form)
}
// Look for a user code
userCode, err := getFormStrVal(r.Form, "usercode", 0)
if err != nil {
// No userCode found => return page with form to input userCode
if glog.V(2) {
glog.Infof("No userCode found : %v",err)
}
sendInitPage(w)
return
}
// Check userCode validity
userObj, err := getUserFromCode(nil, userCode)
if err != nil {
// Invalid userCode
if glog.V(2) {
glog.Infof("Invalid userCode %v",err)
}
sendInitPage(w)
return
}
userName, err := userObj.getStrVal("FirstName")
if err != nil {
if glog.V(2) {
glog.Infof("Get user FirstName : %v",err)
}
userName = "FirstName"
}
// -------------------------- Action --------------------------
// Found a user for given userCode, get profil
profil, err := checkApiUser(userObj)
if err != nil {
if glog.V(2) {
glog.Infof("Fail to check Api access : %v",err)
}
sendActionPage(w, "alert('Ready')", userName, userCode)
return
}
// Check if any action (objectid) received
objectidStr, err := getFormStrVal(r.Form, "objectid", 0)
if err != nil {
// No objectid found => return page with form to input userCode
if glog.V(2) {
glog.Infof("No valid ObjectId found : %s - %v", objectidStr, err)
}
sendActionPage(w, "alert('Ready')", userName, userCode)
return
}
objectid, err := strconv.Atoi(objectidStr)
if err != nil {
// Not an int objectidStr
if glog.V(2) {
glog.Infof("Bad objectid found : %v",err)
}
sendActionPage(w, "alert('Ready')", userName, userCode)
return
}
// Check user access to object
if err := checkAccessToObjectId(profil, objectid); err != nil {
if glog.V(2) {
glog.Infof("Acces api check fail : %v",err)
}
sendActionPage(w, "", userName, userCode)
return
}
// Trigger asked action
result, err := triggerActorById(objectid, userObj.getId(), "")
if err != nil {
if glog.V(2) {
glog.Infof("Trigger actionfail : %v",err)
}
sendActionPage(w, fmt.Sprintf("alert('Action %d : failed')",objectid), userName, userCode)
return
}
if glog.V(2) {
glog.Infof("Actor result : %s",result)
}
sendActionPage(w, fmt.Sprintf("alert('Action %d : %s')",objectid,htmlEscaper.Replace(result)), userName, userCode)
return
}
// -----------------------------------------------
// apiHandler : handle API requests
func apiHandler(w http.ResponseWriter, r *http.Request) {
var userObj HomeObject
w.Header().Add("Strict-Transport-Security", "max-age=63072000; includeSubDomains")
// Parse received request
if err := r.ParseForm(); err != nil {
writeApiError(w, fmt.Sprintf("Form parse error '%s' for (%s)", err, r.Form))
return
}
command, err := getFormStrVal(r.Form, "command", 0)
if err != nil {
writeApiError(w, err.Error())
return
}
var jsonCmde apiCommandSruct
err = json.Unmarshal([]byte(command), &jsonCmde)
if err != nil {
writeApiError(w, fmt.Sprintf("Fail to unmarshal apiCommandSruct (%s) : %s", command, err))
return
}
if glog.V(2) {
glog.Info(jsonCmde)
}
// Handle user info
if glog.V(2) {
glog.Infof("User code = %s", jsonCmde.UserCode)
}
userCert := r.TLS.PeerCertificates
if ( len(userCert) > 0 ) {
userObj, err = getUserFromCert(r.TLS.PeerCertificates)
} else { // if no user cetificat provided, fall back to user code
userObj, err = getUserFromCode(nil, jsonCmde.UserCode)
}
if err != nil {
writeApiError(w, err.Error())
return
}
profil, err := checkApiUser(userObj)
if err != nil {
writeApiError(w, err.Error())
return
}
if glog.V(2) {
glog.Infof("User profil %d, Form=%v", profil, r.Form)
}
// Handle received command
switch jsonCmde.Command {
case apiReadRefList:
if glog.V(2) {
glog.Infof("%s (name=%s)", jsonCmde.Command, jsonCmde.Jsonparam)
}
w.Write(fctApiRefList(jsonCmde))
return
case apiReadCurrentUser:
if glog.V(2) {
glog.Infof("%s (user objectid=%d)", jsonCmde.Command, userObj.Values[0].IdObject)
}
w.Write(apiObjectResponse(profil, userObj))
return
case apiReadItem:
if glog.V(2) {
glog.Infof("%s (item=%d)", jsonCmde.Command, jsonCmde.Itemid)
}
w.Write(fctApiReadItem(profil, jsonCmde))
return
case apiReadObject:
if glog.V(2) {
glog.Infof("%s (item=%d, obj=%d)", jsonCmde.Command, jsonCmde.Itemid, jsonCmde.Objectid)
}
w.Write(fctApiReadObject(profil, jsonCmde))
return
case apiReadSensor:
if glog.V(2) {
glog.Infof("%s (objectid=%d)", jsonCmde.Command, jsonCmde.Objectid)
}
w.Write(fctApiGetSensorVal(profil, jsonCmde, true))
return
case apiGetSensorLastVal:
if glog.V(2) {
glog.Infof("%s (objectid=%d)", jsonCmde.Command, jsonCmde.Objectid)
}
w.Write(fctApiGetSensorVal(profil, jsonCmde, false))
return
case apiReadHistoVal:
if glog.V(2) {
glog.Infof("%s (obj=%d, start=%d, end=%d)", jsonCmde.Command, jsonCmde.Objectid, jsonCmde.Startts, jsonCmde.Endts)
}
w.Write(fctApiReadHistoVal(profil, jsonCmde))
return
case apiReadActorRes:
if glog.V(2) {
glog.Infof("%s (objectid=%d, start=%d, end=%d)", jsonCmde.Command, jsonCmde.Objectid, jsonCmde.Startts, jsonCmde.Endts)
}
w.Write(fctApiReadActorRes(profil, jsonCmde))
return
case apiSaveItem:
writeApiError(w, fmt.Sprintf("Command %s not ready", jsonCmde.Command))
return
case apiSaveObject:
if glog.V(2) {
glog.Infof("%s (item=%d, obj=%d)", jsonCmde.Command, jsonCmde.Itemid, jsonCmde.Objectid)
}
w.Write(fctApiSaveObject(profil, jsonCmde))
return
case apiDeleteItem, apiDeleteObject:
writeApiError(w, "Delete not available : use apiSave* to toggle IsActive flag or use manual access to DB")
return
case apiSendSensorVal: // TODO
// if glog.V(1) {
// glog.Infof("%s (type=%d, item=%d, obj=%d)", jsonCmde.Command, jsonCmde.Objectid, jsonCmde.Value)
// }
// objs, err := getHomeObjects(nil, ItemNone, ItemIdNone, jsonCmde.Objectid)
// if err != nil {
// writeJsonError(w, fmt.Sprintf("%s failed for (%s) : %s", jsonCmde.Command, r.Form, err))
// return
// }
// Unmarchal jsonparam to SensorVal
// load sensor using getHomeObjects
// Call handleSensorValue(ts, sensor, value)
writeApiError(w, fmt.Sprintf("Command %s not ready", jsonCmde.Command))
return
case apiTriggerActor:
if glog.V(2) {
glog.Infof("%s (item=%d, obj=%d)", jsonCmde.Command, jsonCmde.Itemid, jsonCmde.Objectid)
}
w.Write(fctApiTriggerActor(profil, userObj.getId(), jsonCmde))
return
default:
writeApiError(w, fmt.Sprintf("Unhandle command '%s' in (%s)", jsonCmde.Command, r.Form))
return
}
// return
}
// -----------------------------------------------
// startHTTPS is used to start the HTTPS server part.
// It takes a chanel as parameter used to signal error (which is suppose to cause goHome exit after cleanup)
// PI : Simple cert generation for TLS
// $ openssl req -x509 -nodes -newkey rsa:2048 -keyout certificats/server.key.pem -out certificats/server.crt.pem -days 3650
// or
// $ go run $GOROOT/src/crypto/tls/generate_cert.go --host localhost # will generate key.pem and cert.pem for you.
func startHTTPS(chanExit chan bool) {
db, err := openDB()
if err != nil {
glog.Errorf("Error in startHTTPS ... exiting", err)
chanExit <- true
return
}
// defer db.Close() don't use that, if ListenAndServeTLS run fine, it wont return !
//-----------------------------
// Read global param from DB
fileServerRoot, err := getGlobalParam(db, "Http", "fileserver_root")
if err != nil {
glog.Errorf("Error in startHTTPS ... exiting : %s", err)
chanExit <- true
return
}
if glog.V(1) {
glog.Infof("FileServer root dir = '%s'", fileServerRoot)
}
strPortNum, err := getGlobalParam(db, "Http", "https_port")
if err != nil {
glog.Errorf("Error in startHTTPS ... exiting : %s", err)
chanExit <- true
return
}
strPortNumSimple, err := getGlobalParam(db, "Http", "simple_port")
if err != nil {
glog.Infof("Cant get simple_port, not starting simple server : %s", err)
strPortNumSimple = ""
}
serverCrtFileName, err := getGlobalParam(db, "Http", "server_crt")
if err != nil {
glog.Errorf("Error in startHTTPS ... exiting : %s", err)
chanExit <- true
return
}
serverKeyFileName, err := getGlobalParam(db, "Http", "server_key")
if err != nil {
glog.Errorf("Error in startHTTPS ... exiting : %s", err)
chanExit <- true
return
}
caCertFileName, err := getGlobalParam(db, "Http", "ca_crt")
if err != nil {
glog.Errorf("Error in startHTTPS ... exiting : %s", err)
chanExit <- true
return
}
//-----------------------------
serverMux := http.NewServeMux() // Create dedicated ServeMux, rather than using http.defaultServeMux
//-----------------------------
proxyMap, err := getGlobalParamList(db, "Proxy")
if err != nil {
glog.Errorf("Error in startHTTPS ... exiting : %s", err)
chanExit <- true
return
}
for source, target := range proxyMap {
urlTarget, err := url.Parse(target)
if err != nil {
glog.Errorf("startHTTPS fail to parse proxy url target(%s) : %s", target, err)
}
if glog.V(1) {
glog.Infof("Adding proxy (from=%s, to=%s)", source, target)
}
serverMux.Handle(source, http.StripPrefix(source, httputil.NewSingleHostReverseProxy(urlTarget)))
}
// Note : access to "/api", apiHandler required a registered user in DB
serverMux.HandleFunc("/api", apiHandler)
//serverMux.HandleFunc("/tst/", defaultResponse)
serverMux.Handle("/", http.FileServer(http.Dir(fileServerRoot)))
//-----------------------------
db.Close()
//-----------------------------
caCert, err := ioutil.ReadFile(caCertFileName)
if err != nil {
glog.Errorf("Error reading CA cert (%s) ... exiting : %s", caCertFileName, err)
chanExit <- true
return
}
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(caCert)
// Setup HTTPS client
tlsConfig := &tls.Config{
ClientCAs: caCertPool,
// NoClientCert
// RequestClientCert
// RequireAnyClientCert
// VerifyClientCertIfGiven
// RequireAndVerifyClientCert
ClientAuth: tls.RequireAndVerifyClientCert,
}
tlsConfig.BuildNameToCertificate()
server := &http.Server{
Addr: ":" + strPortNum,
Handler: serverMux,
TLSConfig: tlsConfig,
// TODO : use custom logger => ErrorLog: goHomeHttpLogger,
}
if ( strPortNumSimple != "" ) {
serverMuxSimple := http.NewServeMux() // Create dedicated ServeMux, rather than using http.defaultServeMux
serverMuxSimple.HandleFunc("/simple", simpleResponse)
serverMuxSimple.HandleFunc("/api", apiHandler)
serverMuxSimple.Handle("/", http.FileServer(http.Dir(fileServerRoot)))
// Setup HTTPS client for "simple Client"
tlsConfigSimple := &tls.Config{
ClientCAs: caCertPool,
// NoClientCert
// RequestClientCert
// RequireAnyClientCert
// VerifyClientCertIfGiven
// RequireAndVerifyClientCert
ClientAuth: tls.VerifyClientCertIfGiven,
}
tlsConfigSimple.BuildNameToCertificate()
serverSimple := &http.Server{
Addr: ":" + strPortNumSimple,
Handler: serverMuxSimple,
TLSConfig: tlsConfigSimple,
// TODO : use custom logger => ErrorLog: goHomeHttpLogger,
}
if glog.V(1) {
glog.Infof("Starting ListenAndServeTLS (https://*:%s)", strPortNumSimple)
}
go serverSimple.ListenAndServeTLS(serverCrtFileName, serverKeyFileName)
}
if glog.V(1) {
glog.Infof("Starting ListenAndServeTLS (https://*:%s)", strPortNum)
}
if err = server.ListenAndServeTLS(serverCrtFileName, serverKeyFileName); err != nil {
glog.Errorf("Error starting HTTPS ListenAndServeTLS : %s ... exiting", err)
chanExit <- true
}
}