forked from dillonhafer/garage-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
137 lines (120 loc) · 3.47 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
package main
import (
"encoding/base64"
"encoding/json"
"fmt"
"net/http"
"os"
"strconv"
"time"
)
func apiLogHandler(event string) {
fmt.Fprintln(os.Stderr, event, "-", time.Now())
}
func VersionHandler(logger func(string)) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
logger("Version")
var jsonResp struct {
Text string `json:"version"`
}
jsonResp.Text = Version
message, err := json.Marshal(jsonResp)
if err != nil {
logger(fmt.Sprintf("%s", err))
}
w.Write(message)
})
}
func CreateVersionHandler(logger func(string)) http.HandlerFunc {
return AuthenticatedHandler(VersionHandler(logger))
}
func DoorStatusHandler(doorStatus func(int) (string, error), logger func(string), statusPin int) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
var jsonResp struct {
Text string `json:"doorStatus"`
}
status, err := doorStatus(statusPin)
if err != nil {
errMessage := fmt.Sprintf("Could not read pin '%d' on Raspberry Pi", statusPin)
logger(errMessage)
jsonResp.Text = errMessage
w.WriteHeader(422)
}
jsonResp.Text = status
message, err := json.Marshal(jsonResp)
if err != nil {
logger(fmt.Sprintf("%s", err))
}
w.Write(message)
})
}
func CreateDoorStatusHandler(doorStatus func(int) (string, error), logger func(string), statusPin int) http.HandlerFunc {
return AuthenticatedHandler(DoorStatusHandler(doorStatus, logger, statusPin))
}
func RelayHandle(toggleSwitch func(int, int) error, logger func(string), pinNumber int, sleepTimeout int) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
logger("TOGGLE DOOR")
err := toggleSwitch(pinNumber, sleepTimeout)
if err != nil {
errMessage := "Could not write to pin"
logger(errMessage)
w.WriteHeader(500)
return
}
var resp struct {
Status string `json:"status"`
}
resp.Status = "signal received"
message, err := json.Marshal(resp)
if err != nil {
logger(fmt.Sprintf("%s", err))
}
w.Write(message)
})
}
func CreateRelayHandle(toggleSwitch func(int, int) error, logger func(string), pinNumber int, sleepTimeout int) http.HandlerFunc {
return AuthenticatedHandler(RelayHandle(toggleSwitch, logger, pinNumber, sleepTimeout))
}
func LogsHandler(logger func(string), logFile string) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
logger("Logs")
logs := ParseLogs(logFile)
entries, err := json.Marshal(logs)
if err != nil {
logger(fmt.Sprintf("%s", err))
}
w.Write(entries)
})
}
func CreateLogsHandler(logger func(string), logFile string) http.HandlerFunc {
return AuthenticatedHandler(LogsHandler(logger, logFile))
}
func AuthenticatedHandler(f http.HandlerFunc) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
signature := req.Header.Get("signature")
timestamp := req.Header.Get("timestamp")
decodedSignature, err := base64.URLEncoding.DecodeString(signature)
if err != nil {
w.WriteHeader(http.StatusForbidden)
return
}
verified := VerifySignature([]byte(timestamp), decodedSignature)
if verified {
// Verify time
i, err := strconv.ParseInt(timestamp, 10, 64)
if err != nil {
w.WriteHeader(http.StatusForbidden)
return
}
_, err = VerifyTime(i)
if err != nil {
w.WriteHeader(http.StatusForbidden)
return
}
} else {
w.WriteHeader(http.StatusForbidden)
return
}
f(w, req)
})
}