You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
2.2 KiB

  1. var Express = require('express');
  2. var Http = require('http');
  3. var Https = require('https');
  4. var Fs = require('fs');
  5. var WebSocketServer = require('ws').Server;
  6. var ChainPadSrv = require('./ChainPadSrv');
  7. var Storage = require('./Storage');
  8. var config = require('./config');
  9. config.websocketPort = config.websocketPort || config.httpPort;
  10. var app = Express();
  11. app.use(Express.static(__dirname + '/www'));
  12. // Bower is broken and does not allow components nested within components...
  13. // And jquery.sheet expects it!
  14. // *Workaround*
  15. app.use("/bower_components/jquery.sheet/bower_components",
  16. Express.static(__dirname + '/www/bower_components'));
  17. var httpsOpts;
  18. if (config.privKeyAndCertFiles) {
  19. var privKeyAndCerts = '';
  20. config.privKeyAndCertFiles.forEach(function (file) {
  21. privKeyAndCerts = privKeyAndCerts + Fs.readFileSync(file);
  22. });
  23. var array = privKeyAndCerts.split('\n-----BEGIN ');
  24. for (var i = 1; i < array.length; i++) { array[i] = '-----BEGIN ' + array[i] }
  25. var privKey;
  26. for (var i = 0; i < array.length; i++) {
  27. if (array[i].indexOf('PRIVATE KEY-----\n') !== -1) {
  28. privKey = array[i];
  29. array.splice(i, 1);
  30. break;
  31. }
  32. }
  33. if (!privKey) { throw new Error("cannot find private key"); }
  34. httpsOpts = {
  35. cert: array.shift(),
  36. key: privKey,
  37. ca: array
  38. }
  39. }
  40. app.get('/api/config', function(req, res){
  41. var host = req.headers.host.replace(/\:[0-9]+/, '');
  42. res.setHeader('Content-Type', 'text/javascript');
  43. res.send('define(' + JSON.stringify({
  44. websocketURL:'ws' + ((httpsOpts) ? 's' : '') + '://' + host + ':' +
  45. config.websocketPort + '/cryptpad_websocket'
  46. }) + ');');
  47. });
  48. var httpServer = httpsOpts ? Https.createServer(httpsOpts, app) : Http.createServer(app);
  49. httpServer.listen(config.httpPort);
  50. console.log('listening on port ' + config.httpPort);
  51. var wsConfig = { server: httpServer };
  52. if (config.websocketPort !== config.httpPort) {
  53. wsConfig = { port: config.websocketPort };
  54. }
  55. var wsSrv = new WebSocketServer(wsConfig);
  56. Storage.create(config, function (store) {
  57. console.log('DB connected');
  58. ChainPadSrv.create(wsSrv, store);
  59. });