2017-07-16 19:37:33 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const config = require('wild-config');
|
|
|
|
const fs = require('fs');
|
|
|
|
|
|
|
|
const certs = new Map();
|
2017-10-05 20:14:43 +08:00
|
|
|
const servers = [];
|
2017-07-16 19:37:33 +08:00
|
|
|
|
2017-10-05 20:14:43 +08:00
|
|
|
module.exports.reload = () => {
|
|
|
|
// load certificate files
|
2017-11-06 23:32:45 +08:00
|
|
|
[false, 'imap', 'lmtp', 'pop3', 'api', 'api.mobileconfig'].forEach(type => {
|
2017-10-05 20:14:43 +08:00
|
|
|
let tlsconf = config.tls;
|
|
|
|
|
|
|
|
if (type) {
|
|
|
|
let path = (type + '.tls').split('.');
|
|
|
|
tlsconf = config;
|
|
|
|
for (let i = 0; i < path.length; i++) {
|
|
|
|
let key = path[i];
|
|
|
|
if (!tlsconf[key]) {
|
|
|
|
tlsconf = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
tlsconf = tlsconf[key];
|
|
|
|
}
|
|
|
|
if (!tlsconf || !tlsconf.key) {
|
|
|
|
tlsconf = config.tls;
|
2017-07-25 20:50:16 +08:00
|
|
|
}
|
|
|
|
}
|
2017-10-05 20:14:43 +08:00
|
|
|
|
|
|
|
if (!tlsconf) {
|
|
|
|
return;
|
2017-07-25 20:50:16 +08:00
|
|
|
}
|
2017-07-16 19:37:33 +08:00
|
|
|
|
2017-10-05 20:14:43 +08:00
|
|
|
let key, cert, ca;
|
2017-07-16 19:37:33 +08:00
|
|
|
|
2017-10-05 20:14:43 +08:00
|
|
|
if (tlsconf.key) {
|
|
|
|
key = fs.readFileSync(tlsconf.key, 'ascii');
|
|
|
|
}
|
2017-07-16 19:37:33 +08:00
|
|
|
|
2017-10-05 20:14:43 +08:00
|
|
|
if (!key) {
|
|
|
|
return;
|
|
|
|
}
|
2017-07-16 19:37:33 +08:00
|
|
|
|
2017-10-05 20:14:43 +08:00
|
|
|
if (tlsconf.cert) {
|
|
|
|
cert = fs.readFileSync(tlsconf.cert, 'ascii');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tlsconf.ca) {
|
|
|
|
ca = [].concat(tlsconf.ca || []).map(ca => fs.readFileSync(ca, 'ascii'));
|
|
|
|
if (!ca.length) {
|
|
|
|
ca = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
certs.set(type || 'default', {
|
|
|
|
key,
|
|
|
|
cert,
|
|
|
|
ca
|
|
|
|
});
|
|
|
|
});
|
2017-07-16 19:37:33 +08:00
|
|
|
|
2017-10-05 20:14:43 +08:00
|
|
|
if (!certs.has('default')) {
|
|
|
|
certs.set('default', {
|
|
|
|
key: fs.readFileSync(__dirname + '/../certs/example.key', 'ascii'),
|
|
|
|
cert: fs.readFileSync(__dirname + '/../certs/example.cert', 'ascii'),
|
|
|
|
ca: false
|
|
|
|
});
|
2017-07-16 19:37:33 +08:00
|
|
|
}
|
2017-10-05 20:14:43 +08:00
|
|
|
};
|
2017-07-16 19:37:33 +08:00
|
|
|
|
2017-10-05 20:14:43 +08:00
|
|
|
module.exports.reload();
|
|
|
|
|
|
|
|
module.exports.get = type => (certs.has(type) ? certs.get(type) : certs.get('default')) || false;
|
|
|
|
|
|
|
|
module.exports.loadTLSOptions = (serverOptions, name) => {
|
|
|
|
Object.keys(config[name].tls || {}).forEach(key => {
|
|
|
|
if (!['key', 'cert', 'ca'].includes(key)) {
|
|
|
|
serverOptions[key] = config[name].tls[key];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
let serverCerts = certs.get(name);
|
|
|
|
|
|
|
|
if (serverCerts) {
|
|
|
|
serverOptions.key = serverCerts.key;
|
|
|
|
if (serverCerts.ca) {
|
|
|
|
serverOptions.ca = serverCerts.ca;
|
2017-07-16 19:37:33 +08:00
|
|
|
}
|
2017-10-05 20:14:43 +08:00
|
|
|
serverOptions.cert = serverCerts.cert;
|
2017-07-16 19:37:33 +08:00
|
|
|
}
|
2017-10-05 20:14:43 +08:00
|
|
|
};
|
2017-07-16 19:37:33 +08:00
|
|
|
|
2017-10-05 20:14:43 +08:00
|
|
|
module.exports.registerReload = (server, name) => {
|
|
|
|
servers.push({ server, name });
|
|
|
|
};
|
2017-07-16 19:37:33 +08:00
|
|
|
|
2017-10-05 20:14:43 +08:00
|
|
|
config.on('reload', () => {
|
|
|
|
module.exports.reload();
|
|
|
|
servers.forEach(entry => {
|
|
|
|
let serverCerts = certs.get(entry.name);
|
|
|
|
let certOptions = {};
|
|
|
|
if (serverCerts) {
|
|
|
|
certOptions.key = serverCerts.key;
|
|
|
|
if (serverCerts.ca) {
|
|
|
|
certOptions.ca = serverCerts.ca;
|
|
|
|
}
|
|
|
|
certOptions.cert = serverCerts.cert;
|
|
|
|
entry.server.updateSecureContext(certOptions);
|
|
|
|
}
|
2017-07-25 20:50:16 +08:00
|
|
|
});
|
2017-10-05 20:14:43 +08:00
|
|
|
});
|