From 1ed53741fee788650068e053fa2a3ede122e8ced Mon Sep 17 00:00:00 2001 From: David Weiss Date: Wed, 27 Sep 2017 10:23:30 +0000 Subject: [PATCH] fixed with help from ipfs team https://github.com/ipfs/js-ipfs/issues/1024 --- storageServer.js | 153 +++++++++++++++++++---------------------------- 1 file changed, 60 insertions(+), 93 deletions(-) diff --git a/storageServer.js b/storageServer.js index 8d27784..9921273 100644 --- a/storageServer.js +++ b/storageServer.js @@ -21,108 +21,75 @@ var HOST = '0.0.0.0'; // Nodejs encryption of buffers var privateKey = new Buffer('my secret'); -var aes = crypto.createCipher('aes-256-cbc', privateKey); +var ipfsNode = new IPFS() -https.createServer(https_options, function (req, res) { - if (req.url == '/fileupload' && req.method == 'POST') { - var form = new formidable.IncomingForm(); +ipfsNode.on('ready', (err) => { + if (err) { + console.error(err) + process.exit(1) + } + + + var server = https.createServer(https_options, function (req, res) { + if (req.url == '/fileupload' && req.method == 'POST') { + var form = new formidable.IncomingForm(); - // form parsing - form.parse(req, function (err, fields, files) { - if(typeof(ipfsNode)==='undefined'){console.log('IPFS is undefined.')}else{console.log('IPFS is defined.')} - var ipfsNode = new IPFS() + // form parsing + form.parse(req, function (err, fields, files) { - console.log('New IPFS node spawned!') - console.log('\nNode info on property isOnline:', ipfsNode.isOnline() ) - // Move to upload directory - var oldpath = files.filetoupload.path; - var newpath = '/home/davidweisss/DNA-IDstorage/uploads/' + files.filetoupload.name; - fs.rename(oldpath, newpath, function (err) { - if (err) throw err; - }); + // create read stream to encrypt and send to ipfs + var rstream = fs + .createReadStream(files.filetoupload.path) + .pipe(crypto.createCipher('aes-256-cbc', privateKey)) - // create read stream to encrypt and send to ipfs - var rstream = fs.createReadStream(newpath); - - // Upload to IPFS - // Create the File to add, a file consists of a path + content. More details on - // https://github.com/ipfs/interface-ipfs-core/tree/master/API/files - // asynchronous server startup and upload - let fileMultihash - series( - [ - // 1. load ipfs server - (cb) => ipfsNode.on('ready', (cb)), - // 2. After loading ipfs - (cb) => ipfsNode.files.add({ - path: 'private_data_if_no_permission_destroy.encr', - content: rstream.pipe(aes) - }, - // When file loaded - (err, result) => { - if (err) { return cb(err) } - console.log('\nAdded file:', result[0].path, result[0].hash) - fileMultihash = result[0].hash - var b = - '

Your information is now encrypted in the interplanetary file system

' - +'

Your data is immortalized, and encrypted here. This is the Multi hash of your file:'+fileMultihash+'

' - +'

Only you have its password. If shared with this location, it gives access to your data. Share wisely.

'; - - res.write(b) - // IPFS Core exposed components - // // - for booting up a node - //console.log('\nNode info on property init:', ipfsNode.init() ) - //console.log('\nNode info on property preStart:', ipfsNode.preStart() ) - //console.log('\nNode info on property start:', ipfsNode.start() ) - //console.log('\nNode info on property stop:', ipfsNode.stop() ) - console.log('Node info on property isOnline:', ipfsNode.isOnline() ) - // - interface-ipfs-core defined API - console.log('\nNode info on property version:', ipfsNode.version() ) - console.log('\nNode info on property id:', ipfsNode.id() ) - // console.log('\nNode info on property repo:', ipfsNode.repo ) - // console.log('\nNode info on property bootstrap:', ipfsNode.bootstrap) - // console.log('\nNode info on property config:', ipfsNode.config ) - // console.log('\nNode info on property block:', ipfsNode.block ) - // console.log('\nNode info on property object:', ipfsNode.object ) - // console.log('\nNode info on property dag:', ipfsNode.dag ) - // console.log('\nNode info on property libp2p:', ipfsNode.libp2p ) - // console.log('\nNode info on property swarm:', ipfsNode.swarm ) - // console.log('\nNode info on property files:', ipfsNode.files ) - // console.log('\nNode info on property bitswap:', ipfsNode.bitswap ) - // console.log('\nNode info on property ping:', ipfsNode.ping) - // console.log('\nNode info on property pubsub:', ipfsNode.pubsub ) - // console.log('\nNode info on property dht:', ipfsNode.dht ) + // Upload to IPFS + // Create the File to add, a file consists of a path + content. More details on + // https://github.com/ipfs/interface-ipfs-core/tree/master/API/files + // asynchronous server startup and upload + let fileMultihash - cb() - } - ) - ] - , - () => { - if(typeof(ipfsNode)==='undefined'){console.log('In callback: IPFS is undefined.')}else{console.log('In callback: IPFS is defined.')} - return res.end()} - ) + ipfsNode.files.add({ + path: 'private_data_if_no_permission_destroy.encr', + content: rstream + }, + // When file loaded + (err, result) => { + if (err) { return cb(err) } + console.log('\nAdded file:', result[0].path, result[0].hash) + fileMultihash = result[0].hash + res.write( + '

Your information is now encrypted in the interplanetary file system

' + + '

Your data is immortalized, and encrypted here. This is the Multi hash of your file:' + fileMultihash + '

' + + '

Only you have its password. If shared with this location, it gives access to your data. Share wisely.

', + res.end.bind(res)) + } + ) + }) + }else { + // Upload form + res.writeHead(200, {'Content-Type': 'text/html'}); + res.write('

Store and send encrypted content to anyone

'); + res.write('

Encrypt and store in always-available, non-erasable peer-to-peer storage
Access and share with keys on www.

'); + res.write('
'); + res.write('
'); + res.write(''); + res.write('
'); + res.write('

secure https server encrypting your info then uploading it to permanent peer to peer storage network (ipfs).

Gives you a web adress (ipfs) and a password to decrypt your data

'); + res.write('

Only you, and whomever you share the key with, has access

'); + res.write('

* this service is used by https://dnavid.com through its API to upload personal DNA information while retaining control.

'); + return res.end(); + } + }).on('listening', (err) => { + if (err) { + console.error(err) + process.exit(1) } - ) - } - else { - // Upload form - res.writeHead(200, {'Content-Type': 'text/html'}); - res.write('

Store and send encrypted content to anyone

'); - res.write('

Encrypt and store in always-available, non-erasable peer-to-peer storage
Access and share with keys on www.

'); - res.write('
'); - res.write('
'); - res.write(''); - res.write('
'); - res.write('

secure https server encrypting your info then uploading it to permanent peer to peer storage network (ipfs).

Gives you a web adress (ipfs) and a password to decrypt your data

'); - res.write('

Only you, and whomever you share the key with, has access

'); - res.write('

* this service is used by https://dnavid.com through its API to upload personal DNA information while retaining control.

'); - return res.end(); - } -}).listen(PORT, HOST); + console.log('HTTPS Server listening on %s:%s', HOST, PORT) + }).listen(PORT, HOST); +}) // start server //server = https.createServer(https_options, app).listen(PORT, HOST); console.log('HTTPS Server listening on %s:%s', HOST, PORT);