2023-03-21 06:53:39 +00:00
|
|
|
const config = require('./lib/config');
|
2020-02-19 04:55:06 +00:00
|
|
|
const logger = require('./logger').setup;
|
2020-10-17 10:25:36 +00:00
|
|
|
const certificateModel = require('./models/certificate');
|
2020-02-19 04:55:06 +00:00
|
|
|
const userModel = require('./models/user');
|
|
|
|
const userPermissionModel = require('./models/user_permission');
|
2020-10-17 10:25:36 +00:00
|
|
|
const utils = require('./lib/utils');
|
2020-02-19 04:55:06 +00:00
|
|
|
const authModel = require('./models/auth');
|
2020-07-19 14:35:13 +00:00
|
|
|
const settingModel = require('./models/setting');
|
2024-01-18 05:13:16 +00:00
|
|
|
const certbot = require('./lib/certbot');
|
2020-08-05 22:58:20 +00:00
|
|
|
/**
|
|
|
|
* Creates a default admin users if one doesn't already exist in the database
|
|
|
|
*
|
|
|
|
* @returns {Promise}
|
|
|
|
*/
|
|
|
|
const setupDefaultUser = () => {
|
|
|
|
return userModel
|
2020-07-19 14:35:13 +00:00
|
|
|
.query()
|
|
|
|
.select(userModel.raw('COUNT(`id`) as `count`'))
|
|
|
|
.where('is_deleted', 0)
|
|
|
|
.first()
|
2020-08-05 22:58:20 +00:00
|
|
|
.then((row) => {
|
|
|
|
if (!row.count) {
|
|
|
|
// Create a new user and set password
|
|
|
|
logger.info('Creating a new user: admin@example.com with password: changeme');
|
2020-07-19 14:35:13 +00:00
|
|
|
|
2020-08-05 22:58:20 +00:00
|
|
|
let data = {
|
|
|
|
is_deleted: 0,
|
|
|
|
email: 'admin@example.com',
|
|
|
|
name: 'Administrator',
|
|
|
|
nickname: 'Admin',
|
|
|
|
avatar: '',
|
|
|
|
roles: ['admin'],
|
|
|
|
};
|
2020-07-19 14:35:13 +00:00
|
|
|
|
2020-08-05 22:58:20 +00:00
|
|
|
return userModel
|
|
|
|
.query()
|
|
|
|
.insertAndFetch(data)
|
|
|
|
.then((user) => {
|
|
|
|
return authModel
|
|
|
|
.query()
|
|
|
|
.insert({
|
|
|
|
user_id: user.id,
|
|
|
|
type: 'password',
|
|
|
|
secret: 'changeme',
|
|
|
|
meta: {},
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
return userPermissionModel.query().insert({
|
2020-07-19 14:35:13 +00:00
|
|
|
user_id: user.id,
|
|
|
|
visibility: 'all',
|
|
|
|
proxy_hosts: 'manage',
|
|
|
|
redirection_hosts: 'manage',
|
|
|
|
dead_hosts: 'manage',
|
|
|
|
streams: 'manage',
|
|
|
|
access_lists: 'manage',
|
2020-08-05 22:58:20 +00:00
|
|
|
certificates: 'manage',
|
2020-07-19 14:35:13 +00:00
|
|
|
});
|
2020-08-05 22:58:20 +00:00
|
|
|
});
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
logger.info('Initial admin setup completed');
|
|
|
|
});
|
2023-03-21 06:53:39 +00:00
|
|
|
} else if (config.debug()) {
|
|
|
|
logger.info('Admin user setup not required');
|
2020-08-05 22:58:20 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
2020-02-19 04:55:06 +00:00
|
|
|
|
2020-08-05 22:58:20 +00:00
|
|
|
/**
|
|
|
|
* Creates default settings if they don't already exist in the database
|
|
|
|
*
|
|
|
|
* @returns {Promise}
|
|
|
|
*/
|
|
|
|
const setupDefaultSettings = () => {
|
2020-07-19 14:35:13 +00:00
|
|
|
return settingModel
|
|
|
|
.query()
|
2020-08-05 22:58:20 +00:00
|
|
|
.select(settingModel.raw('COUNT(`id`) as `count`'))
|
|
|
|
.where({id: 'default-site'})
|
2020-07-19 14:35:13 +00:00
|
|
|
.first()
|
2020-08-05 22:58:20 +00:00
|
|
|
.then((row) => {
|
2020-07-19 14:35:13 +00:00
|
|
|
if (!row.count) {
|
|
|
|
settingModel
|
2020-02-19 04:55:06 +00:00
|
|
|
.query()
|
2020-07-19 14:35:13 +00:00
|
|
|
.insert({
|
|
|
|
id: 'default-site',
|
|
|
|
name: 'Default Site',
|
|
|
|
description: 'What to show when Nginx is hit with an unknown Host',
|
|
|
|
value: 'congratulations',
|
2020-08-05 22:58:20 +00:00
|
|
|
meta: {},
|
|
|
|
})
|
|
|
|
.then(() => {
|
2020-07-19 14:35:13 +00:00
|
|
|
logger.info('Default settings added');
|
2020-02-19 04:55:06 +00:00
|
|
|
});
|
2020-08-05 22:58:20 +00:00
|
|
|
}
|
2023-03-21 06:53:39 +00:00
|
|
|
if (config.debug()) {
|
|
|
|
logger.info('Default setting setup not required');
|
2020-02-19 04:55:06 +00:00
|
|
|
}
|
|
|
|
});
|
2020-08-05 22:58:20 +00:00
|
|
|
};
|
2020-07-19 14:35:13 +00:00
|
|
|
|
2020-10-17 10:13:08 +00:00
|
|
|
/**
|
|
|
|
* Installs all Certbot plugins which are required for an installed certificate
|
|
|
|
*
|
|
|
|
* @returns {Promise}
|
|
|
|
*/
|
|
|
|
const setupCertbotPlugins = () => {
|
|
|
|
return certificateModel
|
2020-10-17 10:25:36 +00:00
|
|
|
.query()
|
|
|
|
.where('is_deleted', 0)
|
|
|
|
.andWhere('provider', 'letsencrypt')
|
|
|
|
.then((certificates) => {
|
|
|
|
if (certificates && certificates.length) {
|
2022-11-20 22:46:42 +00:00
|
|
|
let plugins = [];
|
|
|
|
let promises = [];
|
2020-10-17 10:25:36 +00:00
|
|
|
|
|
|
|
certificates.map(function (certificate) {
|
|
|
|
if (certificate.meta && certificate.meta.dns_challenge === true) {
|
2024-01-18 06:06:09 +00:00
|
|
|
if (plugins.indexOf(certificate.meta.dns_provider) === -1) {
|
|
|
|
plugins.push(certificate.meta.dns_provider);
|
|
|
|
}
|
2020-10-17 10:25:36 +00:00
|
|
|
|
|
|
|
// Make sure credentials file exists
|
2022-11-14 21:48:57 +00:00
|
|
|
const credentials_loc = '/etc/letsencrypt/credentials/credentials-' + certificate.id;
|
2021-12-29 15:30:49 +00:00
|
|
|
// Escape single quotes and backslashes
|
|
|
|
const escapedCredentials = certificate.meta.dns_provider_credentials.replaceAll('\'', '\\\'').replaceAll('\\', '\\\\');
|
|
|
|
const credentials_cmd = '[ -f \'' + credentials_loc + '\' ] || { mkdir -p /etc/letsencrypt/credentials 2> /dev/null; echo \'' + escapedCredentials + '\' > \'' + credentials_loc + '\' && chmod 600 \'' + credentials_loc + '\'; }';
|
2020-10-17 10:25:36 +00:00
|
|
|
promises.push(utils.exec(credentials_cmd));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-01-18 05:13:16 +00:00
|
|
|
return certbot.installPlugins(plugins)
|
|
|
|
.then(() => {
|
|
|
|
if (promises.length) {
|
|
|
|
return Promise.all(promises)
|
|
|
|
.then(() => {
|
|
|
|
logger.info('Added Certbot plugins ' + plugins.join(', '));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2020-10-17 10:25:36 +00:00
|
|
|
}
|
|
|
|
});
|
2020-10-17 10:13:08 +00:00
|
|
|
};
|
|
|
|
|
2021-06-29 18:40:56 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Starts a timer to call run the logrotation binary every two days
|
|
|
|
* @returns {Promise}
|
|
|
|
*/
|
|
|
|
const setupLogrotation = () => {
|
|
|
|
const intervalTimeout = 1000 * 60 * 60 * 24 * 2; // 2 days
|
|
|
|
|
|
|
|
const runLogrotate = async () => {
|
2021-07-22 12:05:21 +00:00
|
|
|
try {
|
|
|
|
await utils.exec('logrotate /etc/logrotate.d/nginx-proxy-manager');
|
|
|
|
logger.info('Logrotate completed.');
|
|
|
|
} catch (e) { logger.warn(e); }
|
2021-06-29 18:40:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
logger.info('Logrotate Timer initialized');
|
|
|
|
setInterval(runLogrotate, intervalTimeout);
|
|
|
|
// And do this now as well
|
|
|
|
return runLogrotate();
|
|
|
|
};
|
|
|
|
|
2020-07-19 14:35:13 +00:00
|
|
|
module.exports = function () {
|
2023-03-21 06:53:39 +00:00
|
|
|
return setupDefaultUser()
|
2020-10-17 10:13:08 +00:00
|
|
|
.then(setupDefaultSettings)
|
2021-06-29 18:40:56 +00:00
|
|
|
.then(setupCertbotPlugins)
|
|
|
|
.then(setupLogrotation);
|
2020-02-19 04:55:06 +00:00
|
|
|
};
|