Skip to content

Commit

Permalink
Add '/sender' endpoint, compat for POST methods
Browse files Browse the repository at this point in the history
- Not completely functioning yet, but basics are
  • Loading branch information
who-biz committed May 2, 2023
1 parent 2e461bf commit 03ab787
Showing 1 changed file with 181 additions and 4 deletions.
185 changes: 181 additions & 4 deletions app_mongo.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const { MongoClient } = require('mongodb');
const mime = require('mime-types')
const url = require('url')
const path = require('path')
const qs = require('querystring')

// where is subfolder with your public files like index.html
const baseDirectory = __dirname +"/public"
Expand Down Expand Up @@ -182,9 +183,9 @@ setInterval(()=>{

const requestListener = function (req, res) {

if(req.method=="GET") {
if(req.method=="GET") {

try {
try {

console.log(req.url)

Expand Down Expand Up @@ -234,9 +235,16 @@ const requestListener = function (req, res) {

case "/listener": {
listener(requestUrl, res)
break;
break;
} // case '/listener'

case "/sender": {
res.writeHead(400)
res.end("HTTP method GET is not supported by this URL")
console.log("Error: GET is not permitted on \"sender\" URL, use POST instead")
break;
} // case '/sender'

default: {
var fileStream = fs.createReadStream(fsPath)
res.setHeader("Content-Type",mime.contentType(path.extname(fsPath)))
Expand All @@ -245,7 +253,7 @@ const requestListener = function (req, res) {
res.writeHead(200)
})
fileStream.on('error',function(e) {
res.end('No that file')
res.end('File does not exist')
})
} // default
}
Expand All @@ -255,6 +263,59 @@ const requestListener = function (req, res) {
res.end() // end the response so browsers don't hang
console.log(e.stack)
}

} else if (req.method=="POST") {

try {
console.log(req.url)

var requestUrl = url.parse(req.url,true)

// need to use path.normalize so people can't access directories underneath baseDirectory
var fsPath = baseDirectory+path.normalize(requestUrl.pathname)

console.log(fsPath)
console.log(requestUrl)

switch (requestUrl.pathname) {

case "/sender": {
var requestBody = '';
req.on('data', function(data) {
requestBody += data;
if(requestBody.length > 1e7) {
res.writeHead(413, 'Request Entity Too Large', {'Content-Type': 'text/html'});
res.end('<!doctype html><html><head><title>413</title></head><body>413: Request Entity Too Large</body></html>');
}
});
req.on('end', function() {
var formData = qs.parse(requestBody);
var obj = JSON.parse(JSON.stringify(formData));
console.log("requestBody = " + requestBody);
console.log("formData =" + obj);
sender(requestUrl, formData, res)
});
break;
} // case '/sender'

default: {
var fileStream = fs.createReadStream(fsPath)
res.setHeader("Content-Type",mime.contentType(path.extname(fsPath)))
fileStream.pipe(res)
fileStream.on('open', function() {
res.writeHead(200)
})
fileStream.on('error',function(e) {
res.end('File does not exist')
})
} // default
} // switch

} catch(e) {
res.writeHead(500)
res.end() // end the response so browsers don't hang
console.log(e.stack)
}
}
}

Expand Down Expand Up @@ -334,6 +395,122 @@ function listener(requestUrl, res){
}
}

function sender(requestUrl, requestBody, res) {

try {
// trick
let jsonUrl = JSON.parse(JSON.stringify(requestUrl.query))

console.log(jsonUrl)

if(jsonUrl.hasOwnProperty("address")) {

console.log("OK")

var destination;
let split = jsonUrl.address.search('@');
if (split >= 0) {

This comment has been minimized.

Copy link
@who-biz

who-biz May 2, 2023

Author

Need to figure out if we are actually handling @ symbol here.

Domain/port resolution in request is also not handled yet

destination = jsonUrl.address.split('@')
destination = destination[0]
} else {
destination = jsonUrl.address;
}
console.log("destination = " + destination);

// here we check address!!!

// use externally rust program to verify addresses - it is the same which is used to verify signatures
const childadd = execFile(pathtoepicboxlib, ['verifyaddress', jsonUrl.address, destination], (erroradr, stdoutadr, stderradr) =>
{
if (erroradr) {
throw erroradr
}

var destinationValid = (stdoutadr === 'true');

if(destinationValid) {
console.log("Destination address is valid, moving on...");
// nothing else in URL, move onto checking request body
}
}) // end child
}

console.log(requestBody);
if (requestBody.hasOwnProperty("mapmessage") && requestBody.hasOwnProperty("from") && requestBody.hasOwnProperty("signature")) {

console.log("OK")

var fromAddress;
let split = requestBody.from.search('@');
if (split >= 0) {
fromAddress = requestBody.from.split('@')
fromAddress = fromAddress[0]
} else {
fromAddress = requestBody.from;
}
console.log("fromAddress = " + fromAddress);

// here we check address!!!

// use externally rust program to verify addresses - it is the same which is used to verify signatures
const childadd = execFile(pathtoepicboxlib, ['verifyaddress', requestBody.address, fromAddress], (erroradr, stdoutadr, stderradr) =>
{
if (erroradr) {
throw erroradr
}

var senderAddressValid = (stdoutadr === 'true');

if(senderAddressValid) {

// use rust program to verify signatures if they signet timenow by private key of address public key
const child = execFile(pathtoepicboxlib, ["verifysignature", fromAddress, requestBody.mapmessage, requestBody.signature], (error, stdout, stderr) => {

if (error) {
throw error;
}
var signatureValid = (stdout === 'true');

if(signatureValid){
// TODO: add encrypted data to DB
const db = mongoclient.db(dbName);
console.log("Signature OK - Valid");

res.writeHead(200)
res.end("lastSeen: 1311110615")

//const collection = db.collection(collectionname);

// show all slates where address is from query - sender and receiver
//collection.find({queue:from, replyto:json.address}).project({
// _id:0, queue:1, replyto:1, made:1, payload:1, createdat:1, expiration:1 }
// ).toArray().then((SlatesMany =>
//{
// res.setHeader("Content-Type", "application/json")
// res.writeHead(200)
// res.end(JSON.stringify({slates:SlatesMany}))
//}))
} else {
res.writeHead(200)
res.end(JSON.stringify({error:true, message:"wrong signature"}))
}
}) // end child
} else {
res.writeHead(200)
res.end(JSON.stringify({error:true, message:"wrong address"}))
}
}) // end childad
} else {
res.writeHead(200)
res.end(JSON.stringify({error:true, message:"not enough data"}))
}
} catch (e) {
res.writeHead(500)
res.end() // end the response so browsers don't hang
console.log(e.stack)
}
}

//
// HTTMl server creation with function for receives requests
// Used by WebSocketServer
Expand Down

0 comments on commit 03ab787

Please sign in to comment.