use httpolyglot
This commit is contained in:
parent
3a1efa7cdf
commit
f3fb34236b
|
@ -39,6 +39,7 @@
|
||||||
"homepage": "https://github.com/Daplie/serve-https#readme",
|
"homepage": "https://github.com/Daplie/serve-https#readme",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"finalhandler": "^0.4.0",
|
"finalhandler": "^0.4.0",
|
||||||
|
"httpolyglot": "^0.1.1",
|
||||||
"ipify": "^1.1.0",
|
"ipify": "^1.1.0",
|
||||||
"livereload": "^0.4.0",
|
"livereload": "^0.4.0",
|
||||||
"localhost.daplie.com-certificates": "^1.2.0",
|
"localhost.daplie.com-certificates": "^1.2.0",
|
||||||
|
|
36
serve.js
36
serve.js
|
@ -2,10 +2,12 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var PromiseA = global.Promise; // require('bluebird');
|
var PromiseA = global.Promise; // require('bluebird');
|
||||||
var https = require('https');
|
var https = require('httpolyglot');
|
||||||
var http = require('http');
|
var http = require('http');
|
||||||
var fs = require('fs');
|
var fs = require('fs');
|
||||||
var path = require('path');
|
var path = require('path');
|
||||||
|
var httpPort = 80;
|
||||||
|
var httpsPort = 443;
|
||||||
var portFallback = 8443;
|
var portFallback = 8443;
|
||||||
var insecurePortFallback = 4080;
|
var insecurePortFallback = 4080;
|
||||||
|
|
||||||
|
@ -76,7 +78,7 @@ function createServer(port, pubdir, content, opts) {
|
||||||
|
|
||||||
server2.watch(pubdir);
|
server2.watch(pubdir);
|
||||||
|
|
||||||
if ('false' !== opts.insecurePort) {
|
if ('false' !== opts.insecurePort && httpPort !== opts.insecurePort) {
|
||||||
return createInsecureServer(opts.insecurePort, pubdir, opts).then(resolve);
|
return createInsecureServer(opts.insecurePort, pubdir, opts).then(resolve);
|
||||||
} else {
|
} else {
|
||||||
resolve();
|
resolve();
|
||||||
|
@ -90,6 +92,17 @@ function createServer(port, pubdir, content, opts) {
|
||||||
}
|
}
|
||||||
|
|
||||||
server.on('request', function (req, res) {
|
server.on('request', function (req, res) {
|
||||||
|
if (!req.socket.encrypted) {
|
||||||
|
res.statusCode = 301;
|
||||||
|
res.setHeader(
|
||||||
|
'Location'
|
||||||
|
, 'https://' + (req.headers.host || 'localhost')
|
||||||
|
+ (httpsPort === opts.port ? '' : ':' + opts.port)
|
||||||
|
);
|
||||||
|
res.end();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if ('function' === typeof app) {
|
if ('function' === typeof app) {
|
||||||
app(req, res);
|
app(req, res);
|
||||||
return;
|
return;
|
||||||
|
@ -110,7 +123,7 @@ function run() {
|
||||||
var defaultServername = 'localhost.daplie.com';
|
var defaultServername = 'localhost.daplie.com';
|
||||||
var minimist = require('minimist');
|
var minimist = require('minimist');
|
||||||
var argv = minimist(process.argv.slice(2));
|
var argv = minimist(process.argv.slice(2));
|
||||||
var port = argv.p || argv.port || argv._[0] || 443;
|
var port = parseInt(argv.p || argv.port || argv._[0], 10) || httpsPort;
|
||||||
var livereload = argv.livereload;
|
var livereload = argv.livereload;
|
||||||
var pubdir = path.resolve(argv.d || argv._[1] || process.cwd());
|
var pubdir = path.resolve(argv.d || argv._[1] || process.cwd());
|
||||||
var content = argv.c;
|
var content = argv.c;
|
||||||
|
@ -201,7 +214,10 @@ function run() {
|
||||||
if (argv.i || argv['insecure-port']) {
|
if (argv.i || argv['insecure-port']) {
|
||||||
opts.manualInsecurePort = true;
|
opts.manualInsecurePort = true;
|
||||||
}
|
}
|
||||||
opts.insecurePort = argv.i || argv['insecure-port'] || 80;
|
opts.insecurePort = parseInt(argv.i || argv['insecure-port'], 10)
|
||||||
|
|| argv.i || argv['insecure-port']
|
||||||
|
|| httpPort
|
||||||
|
;
|
||||||
opts.livereload = livereload;
|
opts.livereload = livereload;
|
||||||
|
|
||||||
if (argv['express-app']) {
|
if (argv['express-app']) {
|
||||||
|
@ -218,7 +234,7 @@ function run() {
|
||||||
msg = 'Serving ' + pubdir + ' at ';
|
msg = 'Serving ' + pubdir + ' at ';
|
||||||
httpsUrl = 'https://' + opts.servername;
|
httpsUrl = 'https://' + opts.servername;
|
||||||
p = opts.port;
|
p = opts.port;
|
||||||
if (443 !== p) {
|
if (httpsPort !== p) {
|
||||||
httpsUrl += ':' + p;
|
httpsUrl += ':' + p;
|
||||||
}
|
}
|
||||||
console.info('');
|
console.info('');
|
||||||
|
@ -228,7 +244,7 @@ function run() {
|
||||||
|
|
||||||
// Insecure Port
|
// Insecure Port
|
||||||
p = '';
|
p = '';
|
||||||
if (80 !== p) {
|
if (httpPort !== p) {
|
||||||
p = ':' + opts.insecurePort;
|
p = ':' + opts.insecurePort;
|
||||||
}
|
}
|
||||||
msg = '\thttp://' + opts.servername + p + ' (redirecting to https)';
|
msg = '\thttp://' + opts.servername + p + ' (redirecting to https)';
|
||||||
|
@ -264,14 +280,14 @@ function run() {
|
||||||
opts.matchingIps.forEach(function (ip) {
|
opts.matchingIps.forEach(function (ip) {
|
||||||
if ('IPv4' === ip.family) {
|
if ('IPv4' === ip.family) {
|
||||||
httpsUrl = 'https://' + ip.address;
|
httpsUrl = 'https://' + ip.address;
|
||||||
if (443 !== opts.port) {
|
if (httpsPort !== opts.port) {
|
||||||
httpsUrl += ':' + opts.port;
|
httpsUrl += ':' + opts.port;
|
||||||
}
|
}
|
||||||
console.info('\t' + httpsUrl);
|
console.info('\t' + httpsUrl);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
httpsUrl = 'https://[' + ip.address + ']';
|
httpsUrl = 'https://[' + ip.address + ']';
|
||||||
if (443 !== opts.port) {
|
if (httpsPort !== opts.port) {
|
||||||
httpsUrl += ':' + opts.port;
|
httpsUrl += ':' + opts.port;
|
||||||
}
|
}
|
||||||
console.info('\t' + httpsUrl);
|
console.info('\t' + httpsUrl);
|
||||||
|
@ -287,13 +303,13 @@ function run() {
|
||||||
console.info(iname + ':');
|
console.info(iname + ':');
|
||||||
|
|
||||||
httpsUrl = 'https://' + iface.ipv4[0].address;
|
httpsUrl = 'https://' + iface.ipv4[0].address;
|
||||||
if (443 !== opts.port) {
|
if (httpsPort !== opts.port) {
|
||||||
httpsUrl += ':' + opts.port;
|
httpsUrl += ':' + opts.port;
|
||||||
}
|
}
|
||||||
console.info('\t' + httpsUrl);
|
console.info('\t' + httpsUrl);
|
||||||
|
|
||||||
httpsUrl = 'https://[' + iface.ipv6[0].address + ']';
|
httpsUrl = 'https://[' + iface.ipv6[0].address + ']';
|
||||||
if (443 !== opts.port) {
|
if (httpsPort !== opts.port) {
|
||||||
httpsUrl += ':' + opts.port;
|
httpsUrl += ':' + opts.port;
|
||||||
}
|
}
|
||||||
if (iface.ipv6.length) {
|
if (iface.ipv6.length) {
|
||||||
|
|
Loading…
Reference in New Issue