-
Notifications
You must be signed in to change notification settings - Fork 1
/
web.go
187 lines (151 loc) · 3.79 KB
/
web.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
package main
import (
"bytes"
"encoding/json"
"log"
"net/http"
"github.com/gin-gonic/gin"
"github.com/labstack/echo/v4"
"github.com/sirupsen/logrus"
"go.bug.st/serial"
)
type OpenRequest struct {
CardID string `json:"card_id"`
CardPIN string `json:"card_pin"`
}
func checkRequest(c echo.Context) error {
conf := GetConfig()
var req OpenRequest
if err := c.Bind(&req); err != nil {
return c.JSON(400, gin.H{"error": err.Error()})
}
data, err := json.Marshal(req)
if err != nil {
return c.JSON(400, gin.H{"error": err.Error()})
}
logrus.Debug("POST /auth/card: ", string(data))
rdr := bytes.NewReader(data)
// Http client with X-Local-Token
client := &http.Client{}
request, err := http.NewRequest("POST", conf.ApiURL+"/auth/card", rdr)
if err != nil {
return c.JSON(400, gin.H{"error": err.Error()})
}
request.Header.Add("X-Local-Token", conf.LocalToken)
request.Header.Add("Content-Type", "application/json")
// post the same request to API_URL
r, err := client.Do(request)
if err != nil {
return c.JSON(400, gin.H{"error": err.Error()})
}
if r.StatusCode != http.StatusOK {
logrus.Debug("/auth/card returned ", r.StatusCode)
return c.JSON(400, gin.H{"error": "invalid card"})
}
request, err = http.NewRequest("GET", conf.ApiURL+"/account/admin", nil)
if err != nil {
return c.JSON(400, gin.H{"error": err.Error()})
}
request.Header.Add("X-Local-Token", conf.LocalToken)
for _, cookie := range r.Cookies() {
request.AddCookie(cookie)
}
r2, err := client.Do(request)
if err != nil {
return c.JSON(400, gin.H{"error": err.Error()})
}
if r2.StatusCode != http.StatusOK {
logrus.Debug("/account/admin returned ", r2.StatusCode)
return c.JSON(400, gin.H{"error": "invalid card"})
}
type Resp struct {
IsAllowed bool `json:"is_allowed"`
}
var resp Resp
if err := json.NewDecoder(r2.Body).Decode(&resp); err != nil {
return c.JSON(400, gin.H{"error": err.Error()})
}
if !resp.IsAllowed {
return c.JSON(400, gin.H{"error": "invalid card"})
}
return nil
}
func sendToACM(c echo.Context, data []byte) error {
ports, err := serial.GetPortsList()
if err != nil {
log.Fatal(err)
}
for _, port := range ports {
mode := &serial.Mode{}
port, err := serial.Open(port, mode)
if err != nil {
logrus.Error(err)
continue
}
n, err := port.Write([]byte(data))
if err != nil {
logrus.Error(err)
continue
}
logrus.Debug("Wrote ", n, " bytes")
port.Close()
}
// Write 1 to ttyACM*
// files := os.DirFS("/dev")
// err := fs.WalkDir(files, ".", func(path string, d fs.DirEntry, err error) error {
// if err != nil {
// return err
// }
// if strings.HasPrefix(path, "ttyACM") {
// options := serial.RawOptions
// options.BitRate = 115200
// p, err := options.Open("/dev/tty")
// if err != nil {
// log.Panic(err)
// }
// _, err = s.Write([]byte(data))
// if err != nil {
// log.Fatal("Cannot write bytes. ", err)
// }
// f.Write([]byte())
// f.Close()
// }
// return nil
// })
// if err != nil {
// return c.JSON(400, gin.H{"error": err.Error()})
// }
return nil
}
func routes(e *echo.Echo) {
e.POST("/porte", func(c echo.Context) error {
if err := checkRequest(c); err != nil {
return err
}
// Check if context has been closed
if c.Response().Committed {
return nil
}
return sendToACM(c, []byte("1"))
})
e.POST("/caisse", func(c echo.Context) error {
if err := checkRequest(c); err != nil {
return err
}
// Check if context has been closed
if c.Response().Committed {
return nil
}
return sendToACM(c, []byte("2"))
})
e.POST("/ventilo", func(c echo.Context) error {
if err := checkRequest(c); err != nil {
return err
}
// Check if context has been closed
if c.Response().Committed {
return nil
}
return sendToACM(c, []byte("3"))
})
}