-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
379 lines (358 loc) · 9.51 KB
/
Copy pathmain.go
File metadata and controls
379 lines (358 loc) · 9.51 KB
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
package main
import (
cRand "crypto/rand"
"errors"
"fmt"
"github.com/gorilla/mux"
"io/ioutil"
"math/big"
"net/http"
"os"
"reflect"
"strconv"
)
const (
SussiePath = "/etc/capatica/sussyphotos/"
NormalPath = "/etc/capatica/safephotos/"
Host = "0.0.0.0"
Port = "4444"
)
var capaticasOpen []*Capatica
var normalImages []Image
var sussyImages []Image
func initialise() {
// create required directories
err := os.MkdirAll(SussiePath, 0755)
if err != nil {
panic(err)
}
err = os.MkdirAll(NormalPath, 0755)
if err != nil {
panic(err)
}
fmt.Println("directories created, please add images before starting again")
os.Exit(0)
}
func setup() {
// read all files in sussy directory
files, err := ioutil.ReadDir(SussiePath)
if err != nil {
panic(err)
}
for _, f := range files {
// check if file is a jpg
if f.Name()[len(f.Name())-3:] == "jpg" {
// create image
img := Image{
RealName: f.Name(),
Sus: true,
}
// add image to sussyImages
sussyImages = append(sussyImages, img)
}
}
// read all files in normal directory
files, err = ioutil.ReadDir(NormalPath)
if err != nil {
panic(err)
}
for _, f := range files {
// check if file is a jpg
if f.Name()[len(f.Name())-3:] == "jpg" {
// create image
img := Image{
RealName: f.Name(),
Sus: false,
}
// add image to normalImages
normalImages = append(normalImages, img)
}
}
}
func generateRequestID() string {
// generate a random string
b := make([]byte, 16)
cRand.Read(b)
return fmt.Sprintf("%x", b)
}
func createNewCapatica(capaticaChan chan *Capatica, errorChan chan error) {
// seed random number generator from /dev/urandom
// generate a request id
requestID := generateRequestID()
// choose 8 normal images and 1 sussy image
var capaticaImages [9]*Image
// choose a random image
randomA, err := cRand.Int(cRand.Reader, big.NewInt(int64(len(normalImages))))
if err != nil {
errorChan <- err
}
// choose a random index to replace
randomIndex, err := cRand.Int(cRand.Reader, big.NewInt(int64(len(normalImages))))
if err != nil {
errorChan <- err
}
for i := 0; i < 9; i++ {
// if i is the random index, add the sussy image
if int64(i) == randomIndex.Int64() {
capaticaImages[i] = &sussyImages[randomA.Int64()]
} else {
// choose a random image
randomB, err := cRand.Int(cRand.Reader, big.NewInt(int64(len(normalImages))))
if err != nil {
errorChan <- err
}
// add the image
capaticaImages[i] = &normalImages[randomB.Int64()]
}
}
fmt.Println("sussy image is " + sussyImages[randomA.Int64()].RealName)
funny := fmt.Sprintf("at index %d", randomIndex.Int64())
fmt.Println(funny)
// create capatica
capatica := Capatica{
RequestID: requestID,
ImagesSent: capaticaImages,
}
// send capatica to capaticaChan
capaticaChan <- &capatica
// send error to errorChan
errorChan <- nil
}
func errorPrinter(errorChan chan error) {
err := <-errorChan
if err != nil {
fmt.Println(err)
} else {
return
}
}
func httpServer(capaticaChan chan *Capatica, requestChan chan MainRoutineRequest) {
r := mux.NewRouter()
// get request to /genrequest to generate a new capatica
r.HandleFunc("/genrequest", func(w http.ResponseWriter, r *http.Request) {
// create error channel
errorChan := make(chan error)
// create capatica channel
ourCapaticaChan := make(chan *Capatica)
go createNewCapatica(ourCapaticaChan, errorChan)
// wait for capatica to be created
capatica := <-ourCapaticaChan
// send capatica to capaticaChan
capaticaChan <- capatica
// wait for error
errorPrinter(errorChan)
// write response
_, err := w.Write([]byte(capatica.RequestID))
if err != nil {
return
}
})
// get request to /(requestID)/1-9 to get images
r.HandleFunc("/{requestID}/{imageNumber}", func(w http.ResponseWriter, r *http.Request) {
// get request id
requestID := mux.Vars(r)["requestID"]
fmt.Println(requestID)
// get image number
imageNumber := mux.Vars(r)["imageNumber"]
// send request to requestChan for capatica
returnChan := make(chan interface{})
requestChan <- MainRoutineRequest{
DemandType: DemandGetCapatica,
Data: nil,
AssociatedRequest: requestID,
ResponseChan: returnChan,
}
// wait for capatica
capatica := <-returnChan
// check type
if capatica == nil {
// capatica not found
w.WriteHeader(http.StatusNotFound)
return
}
if reflect.TypeOf(capatica).String() == "main.Capatica" {
// cast capatica
capatica := capatica.(Capatica)
// cast image number
imageNumberInt, err := strconv.Atoi(imageNumber)
if err != nil {
fmt.Println(err)
return
}
// check if image number is valid
if imageNumberInt < 1 || imageNumberInt > 9 {
fmt.Println("image number is not valid")
return
}
// get image
image := capatica.ImagesSent[imageNumberInt-1]
// check if image is sussy
if image.Sus {
// send image to sussy
fmt.Println("serving sussy image: " + image.RealName)
http.ServeFile(w, r, SussiePath+image.RealName)
} else {
// send image to normal
fmt.Println("serving normal image: " + image.RealName)
http.ServeFile(w, r, NormalPath+image.RealName)
}
} else {
// send error
_, err := w.Write([]byte("error"))
fmt.Println("type of capatica is not capatica, it is: " + reflect.TypeOf(capatica).String())
if err != nil {
fmt.Println(err)
}
}
})
// post request to /(requestID) with body of number 1-9 to verify capatica
r.HandleFunc("/{requestID}/verify/{imageNumber}", func(w http.ResponseWriter, r *http.Request) {
// get request id
requestID := mux.Vars(r)["requestID"]
// send request to requestChan for capatica
returnChan := make(chan interface{})
requestChan <- MainRoutineRequest{
DemandType: DemandGetCapatica,
Data: nil,
AssociatedRequest: requestID,
ResponseChan: returnChan,
}
// wait for capatica
capatica := <-returnChan
// check type
if capatica == nil {
// capatica not found
w.WriteHeader(http.StatusNotFound)
return
}
if reflect.TypeOf(capatica).String() == "main.Capatica" {
// cast capatica
capatica := capatica.(Capatica)
// get image number
imageNumber := mux.Vars(r)["imageNumber"]
// cast image number
imageNumberInt, err := strconv.Atoi(imageNumber)
if err != nil {
fmt.Println(err)
return
}
// check if body is valid
if imageNumberInt < 1 || imageNumberInt > 9 {
fmt.Println("body is not valid")
return
}
// check if number is the sussy image
if capatica.ImagesSent[imageNumberInt-1].Sus {
// send response
_, err := w.Write([]byte("sussy"))
if err != nil {
return
}
} else {
// send response
_, err := w.Write([]byte("normal"))
if err != nil {
return
}
}
} else {
// send response
_, err := w.Write([]byte("invalid"))
if err != nil {
return
}
}
})
// start server
err := http.ListenAndServe(Host+":"+Port, r)
if err != nil {
fmt.Println(err)
}
}
func main() {
// check if directories exist
if _, err := os.Stat(SussiePath); os.IsNotExist(err) {
fmt.Println("no sussy directory found, initializing")
initialise()
}
if _, err := os.Stat(NormalPath); os.IsNotExist(err) {
fmt.Println("no normal directory found, initializing")
initialise()
}
// setup capatica images
setup()
// create capatica channel
capaticaChan := make(chan *Capatica, 10000)
// create mainroutinerequest channel
requestChan := make(chan MainRoutineRequest, 1)
// start http server
go httpServer(capaticaChan, requestChan)
for {
if len(capaticaChan) > 0 {
// add capatica to capaticasOpen
tmp := <-capaticaChan
capaticasOpen = append(capaticasOpen, tmp)
fmt.Println("added capatica to capaticasOpen")
fmt.Println("capaticasOpen length: " + strconv.Itoa(len(capaticasOpen)))
fmt.Println(tmp)
}
if len(requestChan) > 0 {
tmp := <-requestChan
switch tmp.DemandType {
case DemandVerifyCapatica:
// check if capatica is in capaticasOpen
resolved := false
for i, capatica := range capaticasOpen {
if capatica.RequestID == tmp.AssociatedRequest {
// make sure tmp.Data is an int between 1 and 9 (the image that was clicked)
if reflect.TypeOf(tmp.Data) == reflect.TypeOf(int(0)) {
if tmp.Data.(int) > 0 && tmp.Data.(int) < 9 {
// check if image is correct
if capatica.ImagesSent[tmp.Data.(int)].Sus == true {
// user picked sussy image
tmp.ResponseChan <- true
} else {
// user picked bad image (they're an imposter)
tmp.ResponseChan <- false
}
} else {
// invalid image clicked
fmt.Println("out of bounds")
tmp.ResponseChan <- errors.New("out of bounds")
}
} else {
// invalid data type
fmt.Println("invalid data type")
tmp.ResponseChan <- errors.New("invalid data type")
}
resolved = true
capaticasOpen = append(capaticasOpen[:i], capaticasOpen[i+1:]...)
break
}
}
if !resolved {
// capatica not found
fmt.Println("capatica not found")
tmp.ResponseChan <- errors.New("capatica not found")
} else {
// capatica found
fmt.Println("capatica found")
tmp.ResponseChan <- nil
}
case DemandGetCapatica:
found := false
for _, capatica := range capaticasOpen {
if capatica.RequestID == tmp.AssociatedRequest {
// send this one back over the return channel
tmp.ResponseChan <- *capatica
found = true
break
}
}
if !found {
tmp.ResponseChan <- nil
}
}
}
}
}