-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
138 lines (125 loc) · 2.53 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
package main
import (
"errors"
"fmt"
"io/fs"
"log"
"os"
"strconv"
"strings"
"github.com/Mehloul-Mohamed/ally/app"
)
var Url, Token string
func help(f string) {
switch f {
case "start":
fmt.Println("usage: ally start name url token\n\n" +
"positional arguments:\n" +
" name\t\t\tCTF name\n" +
" url\t\t\tCTF url\n" +
" token\t\t\tYour API token")
case "list":
fmt.Println("usage: ally list")
case "attempt":
fmt.Println("usage: ally attempt id\n\n" +
"positional arguments:\n" +
" id\t\t\tChallenge id")
case "fetch":
fmt.Println("usage: ally fetch")
case "info":
fmt.Println("usage: ally info")
case "main":
fmt.Println("usage: ally {start,list,attempt,info}\n" +
"positional arguments:\n" +
" {start,list,attempt}\n" +
"\tstart\t\t\tStart a CTF\n" +
"\tlist\t\t\tShow challenge list\n" +
"\tattempt\t\t\tAttempt a challenge\n" +
"\tinfo\t\t\tShow scoreboard & team stats\n" +
"\tfetch\t\t\tFetch all challenges")
}
}
func main() {
if len(os.Args) < 2 {
help("main")
return
}
if os.Args[1] == "start" {
if len(os.Args) != 5 {
help("start")
return
}
name := os.Args[2]
url := strings.TrimSuffix(os.Args[3], "/")
token := "token " + os.Args[4]
if name == "" || url == "" || token == "" {
help("main")
}
err := app.StartCtf(name, url, token)
if errors.Is(err, fs.ErrExist) {
log.Fatalln("Ctf already started")
}
if err != nil {
panic(err)
}
return
}
// Other Options
// Read Credentials
wd, _ := os.Getwd()
bytes, err := os.ReadFile(wd + "/credentials.txt")
if errors.Is(err, os.ErrNotExist) {
log.Fatalln("Credentials file not found")
}
if err != nil {
panic(err)
}
slice := strings.Split(string(bytes), "\n")
Url = slice[0]
Token = slice[1]
switch os.Args[1] {
case "list":
if len(os.Args) != 2 {
help("list")
return
}
err := app.DisplayChallList(Url, Token)
if err != nil {
panic(err)
}
case "attempt":
if len(os.Args) != 3 || os.Args[2] == "-h" {
help("attempt")
return
}
id, err := strconv.Atoi(os.Args[2])
if err != nil {
log.Fatalln("id must be an integer")
}
err = app.Attempt(id, Url, Token)
if err != nil {
panic(err)
}
case "fetch":
if len(os.Args) != 2 {
help("fetch")
return
}
err := app.FetchAll(Url, Token)
if err != nil {
panic(err)
}
// Credit to shadow1004 on GitHub for the idea
case "info":
if len(os.Args) != 2 {
help("info")
return
}
err := app.DisplayTeamInfo(Url, Token)
if err != nil {
panic(err)
}
default:
help("main")
}
}