2014-06-02 14:00:24 -07:00
|
|
|
var express = require('express');
|
2014-06-24 08:36:32 -07:00
|
|
|
var http = require('http');
|
|
|
|
var app = express();
|
2014-08-04 10:11:12 -07:00
|
|
|
var request = require('request');
|
2014-04-20 12:46:03 -07:00
|
|
|
|
2014-07-16 13:59:28 -07:00
|
|
|
app.use('/', express.static(__dirname + '/'));
|
|
|
|
app.get('*', function(req, res) {
|
|
|
|
return res.sendfile('index.html');
|
|
|
|
});
|
|
|
|
|
2014-06-02 14:00:24 -07:00
|
|
|
app.start = function(port, callback) {
|
2014-04-20 12:46:03 -07:00
|
|
|
|
2014-06-02 14:00:24 -07:00
|
|
|
app.set('port', port);
|
|
|
|
app.use(express.static(__dirname));
|
2014-04-20 12:46:03 -07:00
|
|
|
|
2014-08-04 10:11:12 -07:00
|
|
|
// XHR'ing from a site with a self-signed
|
|
|
|
// cert on the same port seems to work.
|
2014-07-30 19:14:29 -07:00
|
|
|
if (process.env.USE_HTTPS) {
|
|
|
|
var path = require('path');
|
2014-08-04 10:11:12 -07:00
|
|
|
|
2014-07-30 19:14:29 -07:00
|
|
|
var bc = path.dirname(require.resolve('bitcore/package.json'));
|
|
|
|
var pserver = require(bc + '/examples/PayPro/server.js');
|
2014-08-04 10:11:12 -07:00
|
|
|
|
2014-07-31 11:00:22 -07:00
|
|
|
pserver.removeListener('request', pserver.app);
|
2014-08-04 10:11:12 -07:00
|
|
|
|
2014-07-31 11:00:22 -07:00
|
|
|
pserver.on('request', function(req, res) {
|
2014-07-31 14:41:50 -07:00
|
|
|
if (req.url.indexOf('/-/') === 0) {
|
|
|
|
return pserver.app(req, res);
|
|
|
|
}
|
2014-07-31 11:00:22 -07:00
|
|
|
return app(req, res);
|
2014-07-30 19:14:29 -07:00
|
|
|
});
|
2014-08-04 10:11:12 -07:00
|
|
|
|
2014-07-31 11:00:22 -07:00
|
|
|
pserver.listen(port, function() {
|
2014-07-30 19:14:29 -07:00
|
|
|
callback('https://localhost:' + port);
|
|
|
|
});
|
2014-08-04 10:11:12 -07:00
|
|
|
|
2014-07-30 19:14:29 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-08-04 10:11:12 -07:00
|
|
|
if (process.env.USE_REQUEST_PROXY) {
|
|
|
|
// Disable strict SSL
|
|
|
|
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0';
|
|
|
|
|
|
|
|
// NOTE: Should be .use(), but the router is invoked
|
|
|
|
// to early above, which puts it on the stack.
|
|
|
|
// Only allow proxy requests from localhost
|
|
|
|
app.post('/_request', function(req, res, next) {
|
|
|
|
var address = req.socket.remoteAddress;
|
|
|
|
if (address !== '127.0.0.1' && address !== '::1') {
|
|
|
|
res.statusCode = 403;
|
|
|
|
res.end();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return next();
|
|
|
|
});
|
|
|
|
|
|
|
|
// NOTE: Should be .use(), but the router is invoked
|
|
|
|
// to early above, which puts it on the stack.
|
|
|
|
app.post('/_request', function(req, res, next) {
|
|
|
|
var buf = '';
|
|
|
|
|
|
|
|
req.setEncoding('utf8');
|
|
|
|
|
|
|
|
req.on('error', function(err) {
|
|
|
|
try {
|
|
|
|
req.socket.destroy();
|
|
|
|
} catch (e) {
|
|
|
|
;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
req.on('data', function(data) {
|
|
|
|
buf += data;
|
|
|
|
});
|
|
|
|
|
|
|
|
req.on('end', function(data) {
|
|
|
|
if (data) buf += data;
|
|
|
|
try {
|
|
|
|
req.reqOptions = JSON.parse(buf);
|
|
|
|
} catch (e) {
|
|
|
|
req.reqOptions = {};
|
|
|
|
}
|
|
|
|
return next();
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
app.post('/_request', function(req, res, next) {
|
|
|
|
var options = req.reqOptions;
|
|
|
|
if (options.body) {
|
|
|
|
options.body = new Buffer(options.body, 'hex');
|
|
|
|
}
|
|
|
|
request(options, function(err, response, body) {
|
|
|
|
if (err) {
|
|
|
|
res.statusCode = 500;
|
|
|
|
res.setHeader('Content-Type', 'text/plain; charset=utf-8');
|
|
|
|
res.end(err.stack + '');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
res.writeHead(response.statusCode, response.headers);
|
|
|
|
res.end(body);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-06-02 14:00:24 -07:00
|
|
|
app.listen(port, function() {
|
|
|
|
callback('http://localhost:' + port);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = app;
|
2014-06-06 11:23:40 -07:00
|
|
|
|
|
|
|
// if we are running in the copay shell context, initialize the shell bindings
|
|
|
|
if (process.versions && process.versions['atom-shell']) {
|
|
|
|
require('./shell')(app);
|
|
|
|
}
|