This commit is contained in:
Arthur-Coppey 2022-10-22 13:41:38 +02:00
parent 0b0738efae
commit c73d4db733
7 changed files with 102 additions and 0 deletions

5
.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/

12
.idea/ctf10k-rpn.iml Normal file
View File

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/temp" />
<excludeFolder url="file://$MODULE_DIR$/.tmp" />
<excludeFolder url="file://$MODULE_DIR$/tmp" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="JavaScriptLibraryMappings">
<includedPredefinedLibrary name="Node.js Core" />
</component>
</project>

8
.idea/modules.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/ctf10k-rpn.iml" filepath="$PROJECT_DIR$/.idea/ctf10k-rpn.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

59
main.js Normal file
View File

@ -0,0 +1,59 @@
const Net = require('net');
const port = 8002
const host = "ctf10k.root-me.org"
const client = new Net.Socket
client.on('data', (data) => {
console.log("received data:\n", data.toString())
regex =/(?<equation>^\d+.*$)/m
let message = regex.exec(data.toString()).groups["equation"]
console.log("equation:", message)
let response = interpretRPN(message)
console.log("response:", response)
let buf = Buffer.from(response+"\n")
client.write(buf)
})
client.on('end', () => {
console.log("connection terminated by remote host")
})
client.connect({port: port, host: host}, () => {
console.log("connected")
})
function interpretRPN(message){
const operators = ['-', '+', 'x', '/']
let elements = message.split(' ')
for (let i = 0; i < elements.length; i++) {
if (operators.includes(elements[i])) {
let operation = elements.slice(i-2, i+1)
let result = computeOperation(operation)
elements.splice(i-2, 3, result)
i = 0
}
}
return elements.toString()
}
function computeOperation(operation) {
switch (operation[2]) {
case '-':
return BigInt(operation[0]) - BigInt(operation[1])
case 'x':
return BigInt(operation[0]) * BigInt(operation[1])
case '/':
return BigInt(operation[0]) / BigInt(operation[1])
case '+':
return BigInt(operation[0]) + BigInt(operation[1])
}
}
// const example = "994 611 + 357 733 + 82 543 - x x 740 + 136 x 996 +"
// console.log("response:", interpretRPN(example))

6
package.json Normal file
View File

@ -0,0 +1,6 @@
{
"name": "ctf10k-rpn",
"version": "1.0.0",
"dependencies": {
}
}