Add native SSL support
This commit is contained in:
40
app.js
40
app.js
@@ -1,6 +1,8 @@
|
||||
'use strict';
|
||||
const config = require('./config');
|
||||
const app = require('./lib/endpoints');
|
||||
const https = require('https');
|
||||
const fs = require('fs');
|
||||
|
||||
/**
|
||||
* Naming:
|
||||
@@ -9,17 +11,43 @@ const app = require('./lib/endpoints');
|
||||
* fid: {sid}++{key}
|
||||
*/
|
||||
|
||||
const server = app.listen(config.port, config.iface, () => {
|
||||
console.log(`PsiTransfer listening on http://${config.iface}:${config.port}`);
|
||||
});
|
||||
let server;
|
||||
if(config.port) {
|
||||
// HTTP Server
|
||||
server = app.listen(config.port, config.iface, () => {
|
||||
console.log(`PsiTransfer listening on http://${config.iface}:${config.port}`);
|
||||
});
|
||||
}
|
||||
|
||||
let httpsServer;
|
||||
if(config.sslPort && config.sslKeyFile && config.sslCertFile) {
|
||||
// HTTPS Server
|
||||
const sslOpts = {
|
||||
key: fs.readFileSync(config.sslKeyFile),
|
||||
cert: fs.readFileSync(config.sslCertFile)
|
||||
};
|
||||
httpsServer = https.createServer(sslOpts, app)
|
||||
.listen(config.sslPort, config.iface, () => {
|
||||
console.log(`PsiTransfer listening on https://${config.iface}:${config.sslPort}`);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
// graceful shutdown
|
||||
function shutdown() {
|
||||
console.log('PsiTransfer shutting down...');
|
||||
server.close(() => {
|
||||
process.exit(0);
|
||||
});
|
||||
if(server) {
|
||||
server.close(() => {
|
||||
server = false;
|
||||
if(!server && !httpsServer) process.exit(0);
|
||||
});
|
||||
}
|
||||
if(httpsServer) {
|
||||
httpsServer.close(() => {
|
||||
httpsServer = false;
|
||||
if(!server && !httpsServer) process.exit(0);
|
||||
});
|
||||
}
|
||||
setTimeout(function() {
|
||||
console.log('Could not close connections in time, forcefully shutting down');
|
||||
process.exit(0);
|
||||
|
||||
Reference in New Issue
Block a user