-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
568 lines (479 loc) · 13 KB
/
main.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
package main
import (
"bytes"
"crypto/rand"
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"encoding/json"
"encoding/pem"
"fmt"
"io"
"log"
"net/http"
"os"
"path/filepath"
"github.com/urfave/cli/v2"
"golang.org/x/crypto/bcrypt"
)
// The address it makes requests to
var address string = retrieveAddress()
// Allows it to make https requests to localhost mocking https
var transport = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
var client = &http.Client{Transport: transport}
func main() {
app := &cli.App{
Commands: []*cli.Command{
{
Name: "setKey",
Aliases: []string{"sk"},
Usage: "Setting the event key",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "key",
Aliases: []string{"k"},
Usage: "TBA Event Key",
},
},
Action: func(cCtx *cli.Context) error {
performChecks()
KeyChangeRequest(cCtx.String("key"))
return nil
},
},
{
Name: "login",
Aliases: []string{"L"},
Usage: "Login to the app",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "username",
Aliases: []string{"u"},
Usage: "Username",
},
&cli.StringFlag{
Name: "password",
Aliases: []string{"p"},
Usage: "Password",
},
},
Action: func(cCtx *cli.Context) error {
checkForAddress()
sendPassword(cCtx.String("username"), cCtx.String("password"))
return nil
},
},
{
Name: "validate",
Aliases: []string{"v"},
Usage: "Validates the server is on",
Action: func(cCtx *cli.Context) error {
checkForAddress()
validateOn()
return nil
},
},
{
Name: "getSchedule",
Usage: "Gets the schedule for the current selected event",
Action: func(cCtx *cli.Context) error {
checkForAddress()
requestSchedule()
return nil
},
},
{
Name: "update-address",
Usage: "Updates the address the CLI will go to.",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "address",
Aliases: []string{"a"},
Usage: "The new address",
},
},
Action: func(cCtx *cli.Context) error {
updateAddress(cCtx.String("address"))
return nil
},
},
{
Name: "update-sheet",
Usage: "Updates the sheet the backend will use.",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "sheet",
Aliases: []string{"s"},
Usage: "The new sheet ID",
},
},
Action: func(cCtx *cli.Context) error {
performChecks()
updateSheet(cCtx.String("sheet"))
return nil
},
},
{
Name: "getScouterSchedule",
Usage: "Gets the schedule of a single scouter.",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "scouter",
Usage: "The scouter's name",
},
},
Action: func(cCtx *cli.Context) error {
performChecks()
getScouterSchedule(cCtx.String("scouter"))
return nil
},
},
{
Name: "getLeaderboard",
Usage: "Gets the leaderboard from the backend",
Action: func(cCtx *cli.Context) error {
performChecks()
getLeaderboard()
return nil
},
},
{
Name: "getAddress",
Usage: "Gets the cached address",
Action: func(cCtx *cli.Context) error {
println(retrieveAddress())
return nil
},
},
{
Name: "getUsers",
Usage: "Gets the list of all users",
Action: func(cCtx *cli.Context) error {
performChecks()
getUsers()
return nil
},
},
{
Name: "genPassword",
Usage: "Generates a new password using bcrypt",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "password",
Aliases: []string{"p"},
Usage: "Name of scouter to modify",
},
},
Action: func(cCtx *cli.Context) error {
encrypted, _ := bcrypt.GenerateFromPassword([]byte(cCtx.String("password")), 6)
fmt.Println(string(encrypted))
return nil
},
},
{
Name: "modify-leaderboard",
Usage: "Modify the leaderboard",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "name",
Aliases: []string{"n"},
Usage: "Name of scouter to modify",
},
&cli.StringFlag{
Name: "Modification",
Aliases: []string{"m"},
Usage: "The type of modification: Increase, Decrease, or Set",
},
&cli.IntFlag{
Name: "By",
Aliases: []string{"b"},
Usage: "How much to modify by",
},
},
Action: func(cCtx *cli.Context) error {
performChecks()
modifyLeaderboard(cCtx.String("name"), Modification(cCtx.String("Modification")), cCtx.Int("By"))
return nil
},
},
{
Name: "addBadge",
Usage: "Add a badge",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "name",
Aliases: []string{"n"},
Usage: "Name of scouter to modify",
},
&cli.StringFlag{
Name: "badge",
Aliases: []string{"b"},
Usage: "The Badge name to add",
},
&cli.StringFlag{
Name: "description",
Aliases: []string{"d"},
Usage: "The badge description",
},
},
Action: func(cCtx *cli.Context) error {
performChecks()
addBadge(cCtx.String("name"), cCtx.String("badge"), cCtx.String("description"))
return nil
},
},
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}
// Checks for a valid address and certificate
func performChecks() {
checkForAddress()
checkForValidCert()
}
// Sends a request to change the event key
func KeyChangeRequest(newKey string) {
keyBytes := []byte(newKey)
body := bytes.NewReader(keyBytes)
request, err := http.NewRequest("GET", address+"/keyChange", body)
if err != nil {
log.Fatalln(err)
}
request.Header.Add("Certificate", retrieveCredentials().Certificate)
resp, err := client.Do(request)
if err != nil {
log.Fatalln(err)
}
newBody, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatalln(err)
}
sb := string(newBody)
log.Print(sb)
}
// Validates the certificate
func validateInternally() bool {
request, _ := http.NewRequest("GET", address+"/", bytes.NewBufferString(""))
request.Header.Add("Certificate", retrieveCredentials().Certificate)
resp, _ := client.Do(request)
return resp != nil
}
// Validates the server is on
func validateOn() {
if validateInternally() {
println("Server validated to be on!")
} else {
fmt.Printf("Server offline. Please make sure %v is the right address.", address)
}
}
// Sends a request for the event schedule
func requestSchedule() {
response, _ := client.Get(address + "/schedule")
if response == nil {
log.Println("Server did not return a response.")
} else {
newBody, _ := io.ReadAll(response.Body)
sb := string(newBody)
log.Print(sb)
}
}
// Logs in
func sendPassword(username string, password string) {
pub := getPublicKey()
encryptedPassword, _ := rsa.EncryptPKCS1v15(rand.Reader, pub, []byte(password))
request := LoginRequest{
Username: username,
EncryptedPassword: encryptedPassword,
}
jsonLogin, _ := json.Marshal(request)
response, err := client.Post(address+"/login", "application/json", bytes.NewBuffer(jsonLogin))
if err != nil {
log.Fatalln(err)
}
newBody, err := io.ReadAll(response.Body)
if err != nil {
log.Fatalln(err)
}
sb := string(newBody)
log.Println(sb)
saveCredentials(response.Header.Get("uuid"), response.Header.Get("certificate"))
}
// Gets the RSA public key from the server
func getPublicKey() *rsa.PublicKey {
response, _ := client.Get(address + "/pub")
newBody, _ := io.ReadAll(response.Body)
block, _ := pem.Decode(newBody)
key, _ := x509.ParsePKCS1PublicKey(block.Bytes)
return key
}
// A request to log in
type LoginRequest struct {
Username string // The username
EncryptedPassword []byte // The password, encrypted with the RSA public key
}
// User credentials
type Credentials struct {
UUID string // The uuid
Certificate string // The certificate
}
// The user configuration directory
var appdata, _ = os.UserConfigDir()
// The path to the GreenScoutCLI config directory
var configDir = filepath.Join(appdata, "GreenScoutCLI")
// Saves credentials to the file system
func saveCredentials(uuid string, certificate string) {
os.Mkdir(configDir, os.ModePerm) // Not checking error here bcs the only real error is it alr existing
credentialPath := filepath.Join(configDir, "credentials.json")
file, _ := os.Create(credentialPath)
defer file.Close()
myCreds := Credentials{UUID: uuid, Certificate: certificate}
json.NewEncoder(file).Encode(myCreds)
}
// Gets the credentials from the file system
func retrieveCredentials() Credentials {
file, _ := os.Open(filepath.Join(configDir, "credentials.json"))
defer file.Close()
var credentials Credentials
json.NewDecoder(file).Decode(&credentials)
return credentials
}
// Updates the address it attempts to make requests to
func updateAddress(newAddress string) {
os.Mkdir(configDir, os.ModePerm) // Not checking error here bcs the only real error is it alr existing
addressPath := filepath.Join(configDir, "address.txt")
file, _ := os.Create(addressPath)
defer file.Close()
file.WriteString(newAddress)
address = newAddress
}
// Retrieves the address to make requests to
func retrieveAddress() string {
file, _ := os.Open(filepath.Join(configDir, "address.txt"))
defer file.Close()
dataBytes, _ := io.ReadAll(file)
return string(dataBytes)
}
// Sends a sheet update request
func updateSheet(newSheet string) {
response, _ := client.Post(address+"/sheetChange", "text/plain", bytes.NewBufferString(newSheet))
if response == nil {
log.Println("Server did not return a response.")
} else {
newBody, _ := io.ReadAll(response.Body)
sb := string(newBody)
log.Println("Server Returned: " + sb)
}
}
// Gets the schedule of a specific scouter
func getScouterSchedule(name string) {
request, _ := http.NewRequest("GET", address+"/singleSchedule", bytes.NewBufferString(""))
request.Header.Add("Certificate", retrieveCredentials().Certificate)
request.Header.Add("userInput", name)
resp, _ := client.Do(request)
if resp == nil {
log.Println("Server did not return a response.")
} else {
newBody, _ := io.ReadAll(resp.Body)
sb := string(newBody)
log.Println("Server Returned: " + sb)
}
}
// A leaderboard modification request
type ModRequest struct {
Name string
By int
Mod Modification
}
// A badge
type Badge struct {
ID string
Description string
}
type Modification string
const ( // Modification type enum
Increase Modification = "Increase"
Decrease Modification = "Decrease"
Set Modification = "Set"
)
// Gets the leaderboard from the server
func getLeaderboard() {
response, _ := client.Get(address + "/leaderboard")
if response == nil {
log.Println("Server did not return a response.")
} else {
newBody, _ := io.ReadAll(response.Body)
sb := string(newBody)
log.Print(sb)
}
}
// Sends a request to modify the leaderboard
func modifyLeaderboard(name string, mod Modification, by int) {
jsonBytes, _ := json.Marshal(ModRequest{Name: name, Mod: mod, By: by})
request, _ := http.NewRequest("POST", address+"/modScore", bytes.NewBuffer(jsonBytes))
request.Header.Add("Certificate", retrieveCredentials().Certificate)
resp, _ := client.Do(request)
if resp == nil {
log.Println("Server did not return a response.")
} else {
newBody, _ := io.ReadAll(resp.Body)
sb := string(newBody)
log.Println("Server Returned: " + sb)
}
}
// Adds a badge to the leaderboard
func addBadge(name string, badge string, description string) {
jsonBytes, _ := json.Marshal(Badge{ID: badge, Description: description})
request, _ := http.NewRequest("POST", address+"/addBadge", bytes.NewBuffer(jsonBytes))
request.Header.Add("Certificate", retrieveCredentials().Certificate)
request.Header.Add("Username", name)
resp, _ := client.Do(request)
if resp == nil {
log.Println("Server did not return a response.")
} else {
newBody, _ := io.ReadAll(resp.Body)
sb := string(newBody)
log.Println("Server Returned: " + sb)
}
}
// Gets all users from the server
func getUsers() {
request, err := http.NewRequest("GET", address+"/allUsers", bytes.NewBufferString(""))
request.Header.Add("Certificate", retrieveCredentials().Certificate)
if err != nil {
log.Fatalln(err)
}
response, _ := client.Do(request)
if response == nil {
log.Println("Server did not return a response.")
} else {
newBody, _ := io.ReadAll(response.Body)
sb := string(newBody)
log.Print(sb)
}
}
// Ensures the address is not null
func checkForAddress() {
if address == "" {
log.Fatalln("Please enter an address for the server using ./GreenScoutCLI update-address", address)
}
}
// Checks the certificate is valid
func checkForValidCert() {
request, err := http.NewRequest("GET", address+"/certificateValid", bytes.NewBufferString(""))
request.Header.Add("Certificate", retrieveCredentials().Certificate)
if err != nil {
log.Fatalln(err)
}
response, _ := client.Do(request)
if response.StatusCode != 200 {
if validateInternally() {
log.Fatalln("Certificate Invalid. Please log in with ./GreenScoutCLI login")
} else {
log.Fatalln("Server offline. Please make sure " + address + " is the right address.")
}
}
}