Skip to content

Commit

Permalink
fix issue with p2p task
Browse files Browse the repository at this point in the history
  • Loading branch information
paulo-ocean committed Jul 30, 2024
1 parent 789cd36 commit e7e0bc7
Showing 1 changed file with 25 additions and 8 deletions.
33 changes: 25 additions & 8 deletions src/components/P2P/handleProtocolCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,22 @@ export async function handleProtocolCommands(connection: any) {
let statusStream
let sendStream = null

const buildWrongCommandStatus = function (errorCode: number, message: string) {
status = {
httpStatus: errorCode,
error: message
}
return status
}

const denyList = await (await getConfiguration()).denyList
if (denyList.peers.length > 0) {
if (denyList.peers.includes(remotePeer.toString())) {
P2P_LOGGER.error(`Incoming request denied to peer: ${remotePeer}`)
status = {
httpStatus: 403,
error: 'Unauthorized request'
}
statusStream = new ReadableString(JSON.stringify(status))

statusStream = new ReadableString(
JSON.stringify(buildWrongCommandStatus(403, 'Unauthorized request'))
)
pipe(statusStream, connection.stream.sink)
return
}
Expand All @@ -58,14 +65,24 @@ export async function handleProtocolCommands(connection: any) {
const str = uint8ArrayToString(chunk.subarray())
task = JSON.parse(str) as Command
} catch (e) {
status = { httpStatus: 400, error: 'Invalid command' }
statusStream = new ReadableString(JSON.stringify(status))
statusStream = new ReadableString(
JSON.stringify(buildWrongCommandStatus(400, 'Invalid command'))
)
pipe(statusStream, connection.stream.sink)
return
}
break
}
P2P_LOGGER.logMessage('Performing task: ' + JSON.stringify(task), true)
if (!task) {
P2P_LOGGER.error('Invalid or missing task/command data!')
status = { httpStatus: 400, error: 'Invalid command' }
statusStream = new ReadableString(JSON.stringify(status))
pipe(statusStream, connection.stream.sink)
return
} else {
P2P_LOGGER.logMessage('Performing task: ' + JSON.stringify(task), true)
}

// we get the handler from the running instance
// no need to create a new instance of Handler on every request
const handler: Handler = this.getCoreHandlers().getHandler(task.command)
Expand Down

0 comments on commit e7e0bc7

Please sign in to comment.