Commit 41651701 authored by Adrien Béraud's avatar Adrien Béraud Committed by Guillaume Roguez

nodejs: add https support

Launch an HTTPs server on port 443 with the
when the argument --https is passed.

Change-Id: Ifd8235faded0cc095d1b016fd8ddecda1b2a085d
Reviewed-by: Guillaume Roguez's avatarGuillaume Roguez <guillaume.roguez@savoirfairelinux.com>
parent 44ddcbb8
......@@ -23,8 +23,10 @@ var bodyParser = require('body-parser');
var BigNumber = require('bignumber.js');
var fs = require('fs');
var http = require('http');
var https = require('https');
var Web3 = require('web3');
var web3 = new Web3();
var argv = require('minimist')(process.argv.slice(2));
Object.getPrototypeOf(web3.eth).awaitConsensus = function(txhash, mined_cb) {
var ethP = this;
......@@ -191,6 +193,19 @@ function formatAddress(s) {
return undefined;
}
function readCertificateChain(path) {
var cert = [];
var ca = [];
fs.readFileSync(path, 'utf8').split("\n").forEach(function(line) {
cert.push(line);
if (line.match(/-END CERTIFICATE-/)) {
ca.push(cert.join("\n"));
cert = [];
}
});
return ca;
}
function startServer() {
console.log("Starting web server");
var app = express();
......@@ -359,7 +374,23 @@ function startServer() {
http_res.status(500).end(JSON.stringify({"error": "server error"}));
}
});
http.createServer(app).listen(80);
try {
http.createServer(app).listen(80);
} catch (err) {
console.log("Error starting HTTP server: " + err);
}
if (argv.https) {
try {
var options = {
key : fs.readFileSync('/etc/ssl/private/star_ring_cx.key'),
cert : fs.readFileSync('/etc/ssl/certs/cert_star_ring_cx.pem'),
ca : readCertificateChain('/etc/ssl/certs/chain_star_ring_cx.pem')
};
https.createServer(options, app).listen(443);
} catch (err) {
console.log("Error starting HTTPS server: " + err);
}
}
}
unlockAccount();
......
......@@ -10,6 +10,7 @@
"web3" : ">=0.15" ,
"express" : ">=4.1.0",
"body-parser" : ">=1.8.1",
"bignumber.js": ">=2.0.0"
"bignumber.js": ">=2.0.0",
"minimist" : ">=1.0.0"
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment