Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

adding https support for the server (including socket.io support)

  • Loading branch information...
commit 35b637e06ef5ea8ecf832a70d6deef14ca23aef8 1 parent 296604d
Joachim Kainz authored
View
17 certificate.pem
@@ -0,0 +1,17 @@
+-----BEGIN CERTIFICATE-----
+MIICqTCCAhICCQCmxObN5gQsQzANBgkqhkiG9w0BAQUFADCBmDELMAkGA1UEBhMC
+VVMxCzAJBgNVBAgTAkNBMRYwFAYDVQQHEw1TYW4gRnJhbmNpc2NvMRkwFwYDVQQK
+ExBTbmFrZU9pbCBMaW1pdGVkMQ4wDAYDVQQLEwVTY2FtczEXMBUGA1UEAxQOKi5z
+bmFrZW9pbC5vcmcxIDAeBgkqhkiG9w0BCQEWEWluZm9Ac25ha2VvaWwuY29tMB4X
+DTEyMDYxODIzMjk1NVoXDTEyMDcxODIzMjk1NVowgZgxCzAJBgNVBAYTAlVTMQsw
+CQYDVQQIEwJDQTEWMBQGA1UEBxMNU2FuIEZyYW5jaXNjbzEZMBcGA1UEChMQU25h
+a2VPaWwgTGltaXRlZDEOMAwGA1UECxMFU2NhbXMxFzAVBgNVBAMUDiouc25ha2Vv
+aWwub3JnMSAwHgYJKoZIhvcNAQkBFhFpbmZvQHNuYWtlb2lsLmNvbTCBnzANBgkq
+hkiG9w0BAQEFAAOBjQAwgYkCgYEAsgnqMy6SZPR8PEQrPfBJmm/edH+UfoeKjRHZ
+Q1tVnDvFLdCHaQOWeI1HM+1MJKhgJcdBiG6lhA9m1FgWhrJX669jWoIgVzVZl0xa
+ilh6l4LDeAz2RGKtF+c33eN/GHXef+6YrxgnAMdicPq2l4MQfWxN9DbZDCYmUSiQ
+fziPKpUCAwEAATANBgkqhkiG9w0BAQUFAAOBgQBUw3DE0TGMRNduZZ2ObtoCQAkV
+M06UZpQLdN7H+1DXuQsEzpr4scqICiiJo1e2wjBLauUzcWY0v0bwZK91SAaYarvv
+cyTIit3of4AD4bOCZXsAYjRJ0P0U8Rv4xRp2lbvw7llM+wB0PY8rt/wcE/CLum7a
+pHn1p8e3DGzwqsea3w==
+-----END CERTIFICATE-----
View
15 key.pem
@@ -0,0 +1,15 @@
+-----BEGIN RSA PRIVATE KEY-----
+MIICXQIBAAKBgQCyCeozLpJk9Hw8RCs98Emab950f5R+h4qNEdlDW1WcO8Ut0Idp
+A5Z4jUcz7UwkqGAlx0GIbqWED2bUWBaGslfrr2NagiBXNVmXTFqKWHqXgsN4DPZE
+Yq0X5zfd438Ydd5/7pivGCcAx2Jw+raXgxB9bE30NtkMJiZRKJB/OI8qlQIDAQAB
+AoGBAKEfU/IJ3ba7g4tO7gtZ4GKBh0LLCuoPZBYS5xqMF8gSV3oE0+b3tj9XK+8U
+e+pY7tqBnw8Hg4cmIRtetNQlpmZDnRXwT1yN4sbysVj7OKyNkxGNj3YcPBpa0z/4
+uX0G3y5mTdcRJpXS/OiVDNt21vE+47vvFKWaq+uSC6g2y9DBAkEA5ZprIQVfHqtC
+uhUhCkyYUvZTBewuwSAaDdi3Krs4UAf3fvoRSvWTBdrBIZj62MMbUPvWBVEnZxCB
+uUOj3/wkmQJBAMaB35WRmDxAwv5MXuIk4bsvYPykL8WbtSiWEYdfJsCaPvdSutiS
+hiu73T9GwLUrQhbPcU4B3o0Yh00DzEWBN10CQQCtdgQJZm1+VhSLUPstNMC/rKj8
+iHqqHP6SB3AaMGdSzzIEFBiMcQsqGSJfp5ubt4sjm58NVUpXavkWZiugRHXBAkBC
+OLtE40A9AYHLYeDZYmJnlqOtRHynWI1zz7RjS6va3toYmeG5NBBMcKybWlgZWCgB
+Ag0Tb+41dbRVTeoMWdIFAkBhSuLVMJiZJDo/5rfVoE0UM5nIResuQa/5FOdH+kiA
+v13EAVzsKYZmJ8znNDG0dYVpLQBj72NU6dVGSOhgp6sb
+-----END RSA PRIVATE KEY-----
View
153 lib/launcher.js
@@ -5,9 +5,12 @@
var LOG_STRING = ':remote-addr - - [:date] :req[host] ":method :url HTTP/:http-version" :status ' +
':res[content-length] ":referrer" ":user-agent"',
fs = require("fs"),
+ Batch = require('batch'),
+ http = require('http'),
+ https = require('https'),
cluster = require('cluster'),
startSte = require("./site"),
- connect = require("express"),
+ express = require("express"),
reporter = require("404project"),
Batch = require("batch"),
path = require("path");
@@ -30,8 +33,8 @@
return process.env[(process.platform === 'win32') ? 'USERPROFILE' : 'HOME'];
}
- function startSite(app, config, multiSite, cb, options, logger, httpServer) {
- var vsite = multiSite ? connect.createServer() : app;
+ function startSite(app, config, multiSite, cb, options) {
+ var vsite = multiSite ? express.createServer() : app;
return startSte(vsite, config, function (err, site) {
if (err) {
@@ -39,13 +42,17 @@
}
if (multiSite) {
- var vhost = connect.vhost(site.module.hostname, vsite);
+ if (app.secureApp) {
+ return cb(new Error("nulti-sites do not support https at this point."));
+ }
+
+ var vhost = express.vhost(site.module.hostname, vsite);
app.use(vhost);
}
cb();
- }, options, logger, httpServer);
+ }, options);
}
function resetRequireCache(siteDir) {
@@ -139,13 +146,13 @@
}, logger);
}
- function startSites(app, sites, multiSite, cb, options, logger, httpServer) {
+ function startSites(app, sites, multiSite, cb, options) {
var started = [],
batch = new Batch();
sites.forEach(function (config) {
batch.push(function (done) {
- return startSite(app, config, multiSite, done, options, logger, httpServer);
+ return startSite(app, config, multiSite, done, options);
});
});
@@ -154,15 +161,76 @@
function Launcher(dir, logger) {
this.dir = dir;
- this.httpServers = {};
+ this.apps = [];
this.logger = logger;
}
- Launcher.prototype.start = function (listenPort, cb) {
- if (this.httpServers[listenPort]) {
- return cb(new Error("already started"));
+ function loadKeyCert(sslKeyFile, sslCertificateFile, cb) {
+ var keyFile = sslKeyFile || path.join(__dirname, "..", "key.pem"),
+ certFile = sslCertificateFile || path.join(__dirname, "..", "certificate.pem");
+
+ return fs.readFile(keyFile, "utf-8", function (err, key) {
+ if (err) {
+ return cb(err);
+ }
+ return fs.readFile(certFile, "utf-8", function (err, certificate) {
+ if (err) {
+ return cb(err);
+ }
+
+ return cb(undefined, { key: key, cert: certificate});
+ });
+ });
+ }
+
+ function createServer(logger, listenPort, sslPort, sslKey, sslCert, cb) {
+ var batch = new Batch(),
+ result = {
+ logger: logger,
+ app: express(),
+ use: function(handler) {
+ if (this.secureApp) {
+ this.secureApp.use(handler);
+ }
+
+ return this.app.use(handler);
+ }
+ };
+
+ batch.push(function(done){
+ result.httpServer = http.createServer(result.app);
+ result.httpServer.on("listening", done);
+ result.httpServer.on("error", function (err) {
+ return logger.error("http server error", err);
+ });
+ result.httpServer.listen(listenPort);
+ });
+
+ if (sslPort) {
+ batch.push(function(done) {
+ return loadKeyCert(sslKey, sslCert, function(err, credentials) {
+ if (err) {
+ return done(err);
+ }
+
+ result.httpsServer = https.createServer(credentials, result.app);
+ result.httpsServer.on("listening", done);
+ result.httpsServer.on("error", function (err) {
+ return logger.error("https server error", err);
+ });
+ result.httpsServer.listen(sslPort);
+
+ return done();
+ });
+ });
}
+ batch.end(function(err) {
+ return cb(err, result);
+ });
+ }
+
+ Launcher.prototype.start = function (listenPort, sslPort, sslKey, sslCert, cb) {
var self = this,
home = getUserHome(),
globalConfig = path.join(home, '.site-manager.json');
@@ -177,18 +245,17 @@
return cb(err);
}
- var app = connect.createServer(),
- httpServer = app.listen(listenPort),
- logger = connect.logger(LOG_STRING);
+ return createServer(self.logger, listenPort, sslPort, sslKey, sslCert, function(err, app) {
+ if (err) {
+ return cb(err);
+ }
- app.use(logger);
+ self.logger("listening to", listenPort, sslPort);
+ self.apps.push(app);
- httpServer.on("error", function (err) {
- return cb(err);
- });
- httpServer.on("listening", function (err) {
- self.logger("listening to", listenPort);
- self.httpServers[listenPort] = httpServer;
+ var reqLogger = express.logger(LOG_STRING);
+
+ app.use(reqLogger);
return startSites(app, sites, multiSite, function (err, started) {
if (err) {
@@ -202,56 +269,32 @@
app.use(reporter(properties.iscID, properties.iscKey));
}
- var icon = connect.favicon();
+ var icon = express.favicon();
app.use(icon);
return cb();
- }, properties, self.logger, httpServer);
+ }, properties);
});
}, self.logger);
}, self.logger);
};
- Launcher.prototype.stop = function (/*[port1 [, port2 [, …]]], cb*/) {
+ Launcher.prototype.stop = function (cb) {
var self = this,
- batch = new Batch(),
- ports = [],
- idx,
- cb;
-
- for (idx in arguments) {
- if (cb) {
- ports.push(cb);
- }
- cb = arguments[idx];
- }
-
- var allActivePorts = Object.keys(this.httpServers);
-
- if (allActivePorts.length === 0) {
- return cb && cb(new Error("nothing to stop as there are no " +
- "active ports"));
- }
-
- if (ports.length === 0) {
- // stop all
- ports = allActivePorts;
- }
+ batch = new Batch();
- ports.forEach(function (port) {
+ this.apps.forEach(function (app) {
batch.push(function (done) {
- var server = self.httpServers[port];
-
- self.logger("closing ", port);
- server.close();
-
- delete self.httpServers[port];
+ app.httpServer.close();
+ if (app.httpsServer) {
+ app.httpsServer.close();
+ }
done();
});
});
batch.end(function () {
- self.logger("still running", self.httpServers);
+ this.apps = [];
return cb && cb();
});
};
View
4 lib/manifest.js
@@ -22,12 +22,12 @@
return next();
}
- util.locate(mod["public"], "manifest.appcache", function (err, manifestFile) {
+ return util.locate(mod["public"], "manifest.appcache", function (err, manifestFile) {
if (err) {
return cb(err);
}
- fs.readFile(manifestFile, "utf-8", function (err, content) {
+ return fs.readFile(manifestFile, "utf-8", function (err, content) {
if (err) {
throw err;
}
View
15 lib/parse-cli.js
@@ -10,12 +10,15 @@
function usage() {
console.error("usage: %s %s [options]", process.argv[0], process.argv[1]);
console.error(" site-manager options are:");
- console.error(" --help print this message");
- console.error(" --sites=<directory> look for sites in this directory; defaults to ./sites");
- console.error(" --port=<portnumber> set the listen port; defaults to 3000");
- console.error(" --cluster[=<workers>] enable clustering with <workers>");
- console.error(" --debug[=true|false] enable/disable debugging");
- console.error(" --restart-delay=<ms> restart delay in ms; default to 1.5 seconds");
+ console.error(" --help print this message");
+ console.error(" --sites=<directory> look for sites in this directory; defaults to ./sites");
+ console.error(" --port=<portnumber> set the listen port; defaults to 3000");
+ console.error(" --secure-port=<portnumber> set the listen port; defaults to 3443");
+ console.error(" --secure-key=<file> the key file to be used for the https port");
+ console.error(" --secure-cert=<file> the certificate file to be used for the https port");
+ console.error(" --cluster[=<workers>] enable clustering with <workers>");
+ console.error(" --debug[=true|false] enable/disable debugging");
+ console.error(" --restart-delay=<ms> restart delay in ms; default to 1.5 seconds");
console.error(" --watch-dirs[=true|false] auto-restart if content changes");
console.error(" There may be other, site-specific options");
}
View
28 lib/site.js
@@ -35,7 +35,7 @@
cb(undefined, self);
}
- Site.prototype.start = function (site, cb) {
+ Site.prototype.start = function (app, cb) {
if (!this.module.hostname) {
return cb(new Error("no hostname defined for module " + this.module));
}
@@ -46,7 +46,7 @@
var self = this;
- site.use(connect.compress({
+ app.use(connect.compress({
/* TODO: filter: function () {
return true; // compress it all
}*/
@@ -54,15 +54,15 @@
var publicDirs = self.module["public"];
- site.use(less(publicDirs, {
+ app.use(less(publicDirs, {
maxAge: 86400000
}, self.logger));
if (self.module.dispatcher) {
- site.use(dispatcher(self.module.dispatcher, self.logger));
+ app.use(dispatcher(self.module.dispatcher, self.logger));
}
- site.use(htmlcompiler(self.module, self.logger));
+ app.use(htmlcompiler(self.module, self.logger));
locate(publicDirs, "favicon.ico", function (err, icon) {
if (err) {
@@ -70,19 +70,19 @@
}
self.logger.debug(self.module.hostname, "using", icon);
- site.use(connect.favicon(icon));
- site.use(manifest(self.module, {
+ app.use(connect.favicon(icon));
+ app.use(manifest(self.module, {
maxAge: 86400000
}, self.logger));
- startStaticEtc(self, site, cb);
+ startStaticEtc(self, app, cb);
});
};
- function startSite(site, cb, globalOptions, app, logger, httpServer) {
+ function startSite(site, cb, globalOptions, app) {
if (typeof site.site === "function") {
- var clonedDefaults = clone(defaults);
+ var _defaults = clone(defaults);
- return site.site(clonedDefaults, cb, site.properties, globalOptions, app, logger, httpServer);
+ return site.site(_defaults, cb, site.properties, globalOptions, app);
}
var mySite = clone(site.site),
@@ -108,15 +108,15 @@
return cb(undefined, mySite);
}
- module.exports = function (app, mod, cb, options, logger, httpServer) {
+ module.exports = function (app, mod, cb, options) {
startSite(mod, function (err, conf) {
if (err) {
return cb(err);
}
- var site = new Site(conf, logger);
+ var site = new Site(conf, app.logger);
return site.start(app, cb);
- }, options, app, logger, httpServer);
+ }, options, app);
};
})(module, __dirname);
View
2  package.json
@@ -1,6 +1,6 @@
{
"name":"site-manager",
- "version":"0.6.4",
+ "version":"0.7.0",
"description":"Create & manage mobile & desktop sites using HTML5 & CSS3",
"keywords":["site", "html5"],
"contributors": [
View
5 server.js
@@ -20,6 +20,9 @@
RESTART_DELAY = conf["restart-delay"] || 1500,
PORT = conf.port || getInt(process.env.PORT) || getInt(process.env.C9_PORT) || 3e3,
WORKERS = conf.cluster === true ? os.cpus().length * 2 : getInt(conf.cluster),
+ SSL_PORT = conf['secure-port'],
+ SSL_CERT = conf['secure-cert'],
+ SSL_KEY = conf['secure-key'],
watchAll,
launcher;
@@ -101,7 +104,7 @@
launcher = theLauncher(SITES, logger);
function start(){
- launcher.start(PORT, function (err) {
+ launcher.start(PORT, SSL_PORT, SSL_KEY, SSL_CERT, function (err) {
logger("Started...");
handleError(err);
watchAll();
Please sign in to comment.
Something went wrong with that request. Please try again.