slowly switch configs to db and only a small amount of things like secrets remain in a config file

merge-requests/218/head
Thomas Lynch 3 years ago
parent d14dcdcc76
commit 60cddef23a
  1. 15
      controllers/forms/globalsettings.js
  2. 9
      getconfig.js
  3. 2
      gulpfile.js
  4. 2
      helpers/setting.js
  5. 29
      models/forms/changeglobalsettings.js
  6. 10
      redis.js
  7. 18
      server.js
  8. 45
      views/pages/globalmanagesettings.pug

@ -20,7 +20,7 @@ module.exports = async (req, res, next) => {
//at some point in future the schemas can stored in a common place and updated along with config in redis sub
const schema = [
{ result: lengthBody(req.body.filters, 0, 5000), expected: false, error: 'Filter text cannot exceed 5000 characters' },
{ result: numberBody(req.body.req.body.filter_mode, 0, 2), expected: false, error: 'Filter mode must be a number from 0-2' },
{ result: numberBody(req.body.filter_mode, 0, 2), expected: false, error: 'Filter mode must be a number from 0-2' },
{ result: numberBody(req.body.ban_duration), expected: false, error: 'Invalid filter auto ban duration' },
{ result: lengthBody(req.body.allowed_hosts, 0, 100), expected: false, error: 'Allowed hosts must not exceed 100 entries' },
{ result: lengthBody(req.body.country_code_header, 0, 100), expected: false, error: 'Country code header length must not exceed 100 characters' },
@ -31,16 +31,15 @@ module.exports = async (req, res, next) => {
{ result: numberBody(req.body.captcha_options_generate_limit, 1), expected: false, error: 'Captcha options generate limit must be a number > 0' },
{ result: numberBody(req.body.captcha_options_grid_size, 2, 6), expected: false, error: 'Captcha options grid size must be a number from 2-8' },
{ result: numberBody(req.body.captcha_options_image_size, 50, 500), expected: false, error: 'Captcha options image size must be a number from 50-500' },
// { result: xx, expected: false, error: 'xx' },
{ result: numberBody(req.body.captcha_options_grid_icon_y_offset, 0, 50), expected: false, error: 'Captcha options icon y offset must be a number from 0-50' },
{ result: numberBody(req.body.captcha_options_num_distorts_min, 0, 10), expected: false, error: 'Captcha options min distorts must be a number from 0-10' },
{ result: numberBody(req.body.captcha_options_num_distorts_max, 0, 10), expected: false, error: 'Captcha options max distorts must be a number from 0-10' },
{ result: minmaxBody(req.body.captcha_options_num_distorts_min, req.body.captcha_options_num_distorts_max), expected: false, error: 'Captcha options distorts min must be less than max' },
{ result: numberBody(req.body.captcha_options_distortion, 0, 50), expected: false, error: 'Captcha options distortion must be a number from 0-50' },
];
/*
captchaOptions: {
grid: {
size: trimSetting(req.body.captcha_options_grid_size, oldSettings.captchaOptions.grid.size),
imageSize: trimSetting(req.body.captcha_options_grid_image_size, oldSettings.captchaOptions.grid.imageSize),
iconYOffset: trimSetting(req.body.captcha_options_grid_icon_y_offset, oldSettings.captchaOptions.grid.iconYOffset),
},
numDistorts: {
min: numberSetting(req.body.captcha_options_num_distorts_min, oldSettings.captchaOptions.numDistorts.min),
max: numberSetting(req.body.captcha_options_num_distorts_max, oldSettings.captchaOptions.numDistorts.max),
@ -181,7 +180,7 @@ module.exports = async (req, res, next) => {
};
*/
const errors = checkSchema(settingSchema)
const errors = checkSchema(schema);
if (errors.length > 0) {
return dynamicResponse(req, res, 400, 'message', {

@ -1,15 +1,12 @@
'use strict';
const { addCallback } = require(__dirname+'/redis.js');
let config = require(__dirname+'/configs/main.js');
const redis = require(__dirname+'/redis.js');
const loadConfig = (message) => {
delete require.cache[__dirname+'/configs/main.js'];
config = /*message*/ require(__dirname+'/configs/main.js');
config = message || redis.get('globalsettings');
}
loadConfig();
addCallback('config', loadConfig);
redis.addCallback('config', loadConfig);
module.exports = () => { return config };

@ -5,7 +5,7 @@ const gulp = require('gulp')
, semver = require('semver')
, formatSize = require(__dirname+'/helpers/files/formatsize.js')
, uploadDirectory = require(__dirname+'/helpers/files/uploadDirectory.js')
, configs = require(__dirname+'/configs/main.js')
, secrets = require(__dirname+'/configs/secrets.js')
, { themes, codeThemes } = require(__dirname+'/helpers/themes.js')
, commit = require(__dirname+'/helpers/commit.js')
, less = require('gulp-less')

@ -14,7 +14,7 @@ module.exports = {
if (setting !== null) {
const split = setting
.split(/\r?\n/)
.fitler(n => n);
.filter(n => n);
return split
.slice(0, limit || split.length);
}

@ -4,7 +4,7 @@ const { Boards, Posts, Accounts } = require(__dirname+'/../../db/')
, dynamicResponse = require(__dirname+'/../../helpers/dynamic.js')
, uploadDirectory = require(__dirname+'/../../helpers/files/uploadDirectory.js')
, buildQueue = require(__dirname+'/../../queue.js')
, { redisPublisher } = require(__dirname+'/../../redis.js')
, redis = require(__dirname+'/../../redis.js')
, getConfig = require(__dirname+'/../../getconfig.js')
, { trimSetting, numberSetting, booleanSetting, arraySetting } = require(__dirname+'/../../helpers/setting.js')
, { remove } = require('fs-extra');
@ -15,8 +15,10 @@ module.exports = async (req, res, next) => {
const oldSettings = getConfig();
const newSettings = {
secureCookies: booleanSetting(req.body.secure_cookies),
refererCheck: booleanSetting(req.body.referrer_check),
...oldSettings,
filters: arraySetting(req.body.filters, oldSettings.filters),
filterMode: numberSetting(req.body.filter_mode, oldSettings.filterMode),
filterBanDuration: numberSetting(req.body.ban_duration, oldSettings.filterBanDuration),
allowedHosts: arraySetting(req.body.allowed_hosts, oldSettings.allowedHosts),
countryCodeHeader: trimSetting(req.body.country_code_header, oldSettings.countryCodeHeader),
ipHeader: trimSetting(req.body.ip_header, oldSettings.ipHeader),
@ -27,14 +29,6 @@ module.exports = async (req, res, next) => {
captchaOptions: {
type: trimSetting(req.body.captcha_options_type, oldSettings.captchaOptions.type),
generateLimit: trimSetting(req.body.captcha_options_generate_limit, oldSettings.captchaOptions.generateLimit),
google: {
siteKey: trimSetting(req.body.captcha_options_google_site_key, oldSettings.captchaOptions.google.siteKey),
secretKey: trimSetting(req.body.captcha_options_google_secret_key, oldSettings.captchaOptions.google.secretKey),
},
hcaptcha: {
siteKey: trimSetting(req.body.captcha_options_hcaptcha_site_key, oldSettings.captchaOptions.hcaptcha.siteKey),
secretKey: trimSetting(req.body.captcha_option_hcaptcha_secret_key, oldSettings.captchaOptions.hcaptcha.secretKey),
},
grid: {
size: trimSetting(req.body.captcha_options_grid_size, oldSettings.captchaOptions.grid.size),
imageSize: trimSetting(req.body.captcha_options_grid_image_size, oldSettings.captchaOptions.grid.imageSize),
@ -46,6 +40,10 @@ module.exports = async (req, res, next) => {
},
distortion: numberSetting(req.body.captcha_options_distortion, oldSettings.captchaOptions.distortion),
},
/*
secureCookies: booleanSetting(req.body.secure_cookies),
refererCheck: booleanSetting(req.body.referrer_check),
dnsbl: {
enabled: booleanSetting(req.body.dnsbl_enabled),
blacklists: arraySetting(req.body.dnsbl_blacklists, oldSettings.dnsbl.blacklists),
@ -249,12 +247,11 @@ module.exports = async (req, res, next) => {
other: booleanSetting(req.body.board_defaults_allowed_file_types_other, oldSettings.boardDefaults.allowedFileTypes.other)
}
},
filters: arraySetting(req.body.filters, oldSettings.filters),
filterMode: numberSetting(req.body.filter_mode, oldSettings.filterMode),
filterBanDuration: numberSetting(req.body.ban_duration, oldSettings.filterBanDuration),
*/
};
cache.set('globalsettings', newSettings);
redis.set('globalsettings', newSettings);
/*
//todo: implement removing pages/rebuilding for all affected boards i.e. query for ones with settings.catchaMode < newSettings.captchaMode
@ -301,7 +298,7 @@ module.exports = async (req, res, next) => {
}
//publish to redis so running processes get updated config
redisPublisher.publish('config', JSON.stringify(newSettings));
redis.redisPublisher.publish('config', JSON.stringify(newSettings));
return dynamicResponse(req, res, 200, 'message', {
'title': 'Success',

@ -1,10 +1,10 @@
'use strict';
const Redis = require('ioredis')
, configs = require(__dirname+'/configs/main.js')
, sharedClient = new Redis(configs.redis)
, subscriber = new Redis(configs.redis)
, publisher = new Redis(configs.redis)
, secrets = require(__dirname+'/configs/secrets.js')
, sharedClient = new Redis(secrets.redis)
, subscriber = new Redis(secrets.redis)
, publisher = new Redis(secrets.redis)
, messageCallbacks = {
'config': [], //others in future?
}
@ -12,7 +12,7 @@ const Redis = require('ioredis')
module.exports = {
redisClient: sharedClient,
redisSubsriber: subscriber,
redisSubscriber: subscriber,
redisPublisher: publisher,
close: () => {

@ -10,7 +10,7 @@ const getConfig = require(__dirname+'/getconfig.js')
, app = express()
, server = require('http').createServer(app)
, cookieParser = require('cookie-parser')
, { port, cookieSecret, debugLogs } = getConfig()
, { port, cookieSecret, debugLogs, google, hcaptcha } = require(__dirname+'/configs/secrets.js')
, referrerCheck = require(__dirname+'/helpers/referrercheck.js')
, Mongo = require(__dirname+'/db/db.js')
, Socketio = require(__dirname+'/socketio.js')
@ -78,19 +78,9 @@ const getConfig = require(__dirname+'/getconfig.js')
app.locals.meta = meta;
app.locals.captchaType = captchaOptions.type;
app.locals.postFilesSize = formatSize(globalLimits.postFilesSize.max);
switch (captchaOptions.type) {
case 'google':
app.locals.googleRecaptchaSiteKey = captchaOptions.google.siteKey;
break;
case 'hcaptcha':
app.locals.hcaptchaSiteKey = captchaOptions.hcaptcha.siteKey;
break;
case 'grid':
app.locals.captchaGridSize = captchaOptions.grid.size;
break;
default:
break;
}
app.locals.googleRecaptchaSiteKey = google.siteKey;
app.locals.hcaptchaSiteKey = hcaptcha.siteKey;
app.locals.captchaGridSize = captchaOptions.grid.size;
}
loadAppLocals();
redis.addCallback('config', loadAppLocals);

@ -38,4 +38,49 @@ block content
.row
.label Filter Auto Ban Duration
input(type='text' name='ban_duration' placeholder='e.g. 1w' value=settings.filterBanDuration)
.row
.label Allowed Hosts
textarea(name='allowed_hosts' placeholder='Newline separated') #{settings.allowedHosts.join('\n')}
.row
.label Country Code Header
input(type='text' name='country_code_header' value=settings.countryCodeHeader)
.row
.label IP Header
input(type='text' name='ip_header' value=settings.ipHeader)
.row
.label Meta Site Name
input(type='text' name='meta_site_name' value=settings.meta.siteName)
.row
.label Meta URL
input(type='text' name='meta_url' value=settings.meta.url)
.row
h4.mv-5 Captcha
.row
.label Type
select(name='captcha_options_type')
option(value='text', selected=settings.captchaOptions.type === 'text') Text
option(value='grid', selected=settings.captchaOptions.type === 'grid') Grid
option(value='google', selected=settings.captchaOptions.type === 'google') Google
option(value='hcaptcha', selected=settings.captchaOptions.type === 'hcaptcha') Hcaptcha
.row
.label Generate Limit
input(type='number' name='captcha_options_generate_limit' value=settings.captchaOptions.generateLimit)
.row
.label Image Size
input(type='number' name='captcha_options_image_size' value=settings.captchaOptions.grid.imageSize)
.row
.label Grid Size
input(type='number' name='captcha_options_grid_size' value=settings.captchaOptions.grid.size)
.row
.label Grid Icon Offset
input(type='number' name='captcha_options_grid_icon_y_offset' value=settings.captchaOptions.grid.iconYOffset)
.row
.label Minimum Distortions
input(type='number' name='captcha_options_num_distorts_min' value=settings.captchaOptions.numDistorts.min)
.row
.label Maximum Distortions
input(type='number' name='captcha_options_num_distorts_max' value=settings.captchaOptions.numDistorts.max)
.row
.label Distortion Intensity
input(type='number' name='captcha_options_distortion' value=settings.captchaOptions.distortion)
input(type='submit', value='save settings')

Loading…
Cancel
Save