From b220a971f6252ba31b41e797919e839d74ca956d Mon Sep 17 00:00:00 2001 From: who-biz <37732338+who-biz@users.noreply.github.com> Date: Wed, 26 Apr 2023 22:00:59 +0000 Subject: [PATCH] Add same changes to app_mongo_many.js --- app_mongo_many.js | 213 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 157 insertions(+), 56 deletions(-) diff --git a/app_mongo_many.js b/app_mongo_many.js index e7d2819..7417dfe 100644 --- a/app_mongo_many.js +++ b/app_mongo_many.js @@ -30,6 +30,14 @@ const { WebSocket, WebSocketServer } = require('ws') // Mogodb driver for externally store messageid and slates for ver 2.0.0. of epicbox, tp work with answer by wallet with slate id message const { MongoClient } = require('mongodb'); +// Ledger Integration API support +const mime = require('mime-types') +const url = require('url') +const path = require('path') + +// where is subfolder with your public files like index.html +const baseDirectory = __dirname +"/public" + // Connection URL for mongo, please change if your mongo server is on other location, you can add authorization and open firewall for it. // each instance of epicbox which you run on your domain must use the same mongodb -- so change to correct ip and remeber open ports in firewall // it's good idea to add one field (createdat:new Date()) in stored document and set index with timeout which which can delete documents which are old about 3-4 days @@ -209,66 +217,159 @@ setInterval(()=>{ },60*60*1000); +const requestListener = function (req, res) { + if(req.method=="GET") { + + 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 "/": { + res.writeHead(200, { 'Content-Type':'text/html'}); + res.end(`\n\ + Epicbox\n\ + \n\ +

Epicbox servers. Local server number 1

Protocol 2.0.0

\n\ + epic-wallet to build with protocol 2.0.0\n\ +

Asia, Australia - epicbox.hyperbig.com

North America, South America - epicbox.epic.tech

\n\ +

US East Cost - epicbox.epicnet.us

Africa, Europe - epicbox.fastepic.eu


\n\ +

More about Epic

Epic Cash main webpage

\n\ + Example use in toml file:\n\ +
\n\[epicbox]\n\epicbox_domain = 'epicbox.fastepic.eu'\n\epicbox_port = 443\n\epicbox_protocol_unsecure = false\n\epicbox_address_index = 0\n\epicbox_listener_interval = 10\n\
+				
\n\ +

start listen: epic-wallet listen -m epicbox


\n\ +

Epicbox Statistics from ${statistics.from.toUTCString()}:

\n\ +

connections: ${statistics.connectionsInHour}
active connections: ${statistics.activeconnections}
\n\ + subscribes: ${statistics.connectionsInHour}
received slates: ${statistics.slatesReceivedInHour}
\n\ + relayed slates: ${statistics.slatesRelayedInHour}
sending slate attempts: ${statistics.slatesAttempt}
\n\ +

\n\ + `); + break; + } // case '/' + + case "/timenow": { + + res.setHeader("Content-Type", "application/json") + res.writeHead(200) + var timestamp =Date.now(); + res.end(JSON.stringify({time: timestamp})) + + break; + + } // case 'timenow + + case "/listener": { + listener(requestUrl, res) + break; + } // case '/listener' + + 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('No that file') + }) + } // default + } -const requestListener = function (req, res) { - res.writeHead(200) - res.end(`\n\ -\n\ -\n\ -Epicbox\n\ -\n\ -\n\ -\n\ -\n\ -

Epicbox servers. Local server number 1

\n\ -

Protocol 2.0.0

\n\ -epic-wallet to build with protocol 2.0.0\n\ -

Asia, Australia - epicbox.hyperbig.com

\n\ -

North America, South America - epicbox.epic.tech

\n\ -

US East Cost - epicbox.epicnet.us

\n\ -

Africa, Europe - epicbox.fastepic.eu

\n\ -
\n\ -

More about Epic

\n\ -Epic Cash main webpage\n\ -
\n\ -
\n\ - Example use in toml file.\n\ -\n\ -
\n\
-\n\
-\n\
-[epicbox]\n\
-epicbox_domain = 'epicbox.fastepic.eu'\n\
-epicbox_port = 443\n\
-epicbox_protocol_unsecure = false\n\
-epicbox_address_index = 0\n\
-epicbox_listener_interval = 10\n\
-\n\
-\n\
-
\n\ -

start listen: epic-wallet listen -m epicbox

\n\ -
\n\ -

\n\ -Epicbox Statistics from ${statistics.from.toUTCString()}:\n\ -

\n\ -

\n\ -connections: ${statistics.connectionsInHour}
\n\ -active connections: ${statistics.activeconnections}
\n\ -subscribes: ${statistics.connectionsInHour}
\n\ -received slates: ${statistics.slatesReceivedInHour}
\n\ -relayed slates: ${statistics.slatesRelayedInHour}
\n\ -sending slate attempts: ${statistics.slatesAttempt}
\n\ -

\n\ -\n\ -`); + } catch(e) { + res.writeHead(500) + res.end() // end the response so browsers don't hang + console.log(e.stack) + } + } } - +function listener(requestUrl, res){ + + try { + // trick + let json = JSON.parse(JSON.stringify(requestUrl.query)) + + console.log(json) + + if(json.hasOwnProperty("address") && json.hasOwnProperty("signature") && json.hasOwnProperty("timenow") ){ + + console.log("OK") + + var from; + let split = json.address.search('@'); + if (split >= 0) { + from = json.address.split('@') + from = from[0] + } else { + from = json.address; + } + console.log("from = " + from); + + // 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', json.address, from], (erroradr, stdoutadr, stderradr) => + { + if (erroradr) { + throw erroradr + } + + var isTrueSetadr = (stdoutadr === 'true'); + + if(isTrueSetadr) { + // use rust program to verify signatures if they signet timenow by private key of address public key + const child = execFile(pathtoepicboxlib, ["verifysignature", from , json.timenow, json.signature], (error, stdout, stderr) => { + + if (error) { + throw error; + } + var isTrueSet = (stdout === 'true'); + + if(isTrueSet){ + const db = mongoclient.db(dbName); + 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) + } +} // //