auth.json & data.json creation now done in their respective managers

pull/22/head
tycrek 4 years ago
parent d335ab2f24
commit 2b54a2fb4c
No known key found for this signature in database
GPG Key ID: 25D74F3943625263

127
ass.js

@ -16,13 +16,14 @@ const helmet = require('helmet');
const rateLimit = require('express-rate-limit');
const uploadRouter = require('./routers/upload');
const resourceRouter = require('./routers/resource');
const { path, log, generateToken } = require('./utils');
const { path, log } = require('./utils');
const { CODE_NO_CONTENT, CODE_INTERNAL_SERVER_ERROR } = require('./MagicNumbers.json');
//#endregion
// Set up premium frontend
const FRONTEND_NAME = 'ass-x'; // <-- Change this to use a custom frontend
const ASS_PREMIUM = fs.existsSync(`./${FRONTEND_NAME}/package.json`) ? (require('submodule'), require(`./${FRONTEND_NAME}`)) : { enabled: false };
log(`Frontend: ${ASS_PREMIUM.enabled ? ASS_PREMIUM.brand : '<none>'}`);
//#region Variables, module setup
const app = express();
@ -31,84 +32,50 @@ const ROUTERS = {
resource: resourceRouter
};
// Configure filename and location settings
let users = {};
let data = {};
// Read users and data
const users = require('./auth');
const data = require('./data');
log('Users & data read from filesystem');
//#endregion
/**
* Operations to run to ensure ass can start properly
*/
function preStartup() {
// Make sure data.json exists
if (!fs.existsSync(path('data.json'))) {
fs.writeJsonSync(path('data.json'), data, { spaces: 4 });
log('File [data.json] created');
} else log('File [data.json] exists');
// Make sure auth.json exists and generate the first key
if (!fs.existsSync(path('auth.json'))) {
const token = generateToken();
users[token] = { username: 'ass', count: 0 };
fs.writeJsonSync(path('auth.json'), { users }, { spaces: 4 });
log(`File [auth.json] created\n!! Important: save this token in a secure spot: ${Object.keys(users)[0]}\n`);
} else log('File [auth.json] exists');
// Read users and data
users = require('./auth');
data = require('./data');
log('Users & data read from filesystem');
// Create thumbnails directory
fs.ensureDirSync(path(diskFilePath, 'thumbnails'));
// Print frontend operating mode
log(`Frontend: ${ASS_PREMIUM.enabled ? ASS_PREMIUM.brand : '<none>'}`);
}
/**
* Builds the router & starts the server
*/
function startup() {
// Enable/disable Express features
app.enable('case sensitive routing');
app.disable('x-powered-by');
// Set Express variables
app.set('trust proxy', isProxied);
app.set('view engine', 'pug');
// Helmet security middleware
app.use(helmet.noSniff());
app.use(helmet.ieNoOpen());
app.use(helmet.xssFilter());
app.use(helmet.referrerPolicy());
app.use(helmet.dnsPrefetchControl());
useSsl && app.use(helmet.hsts({ preload: true })); // skipcq: JS-0093
// Rate limit middleware
app.use(rateLimit({
windowMs: 1000 * 60, // 60 seconds // skipcq: JS-0074
max: 90 // Limit each IP to 30 requests per windowMs // skipcq: JS-0074
}));
// Don't process favicon requests (custom middleware)
app.use((req, res, next) => (req.url.includes('favicon.ico') ? res.sendStatus(CODE_NO_CONTENT) : next()));
// Assign routers ('/:resouceId' always needs to be LAST since it's a catch-all route)
app.use('/', ROUTERS.upload);
ASS_PREMIUM.enabled && app.use(ASS_PREMIUM.endpoint, ASS_PREMIUM.router); // skipcq: JS-0093
app.use('/:resourceId', (req, _, next) => (req.resourceId = req.params.resourceId, next()), ROUTERS.resource); // skipcq: JS-0086, JS-0090
// Error handler
app.use(([err, , res,]) => {
console.error(err);
res.sendStatus(CODE_INTERNAL_SERVER_ERROR);
});
// Host the server
app.listen(port, host, () => log(`Server started on [${host}:${port}]\nAuthorized users: ${Object.keys(users).length}\nAvailable files: ${Object.keys(data).length}`));
}
preStartup();
startup();
// Create thumbnails directory
fs.ensureDirSync(path(diskFilePath, 'thumbnails'));
// Enable/disable Express features
app.enable('case sensitive routing');
app.disable('x-powered-by');
// Set Express variables
app.set('trust proxy', isProxied);
app.set('view engine', 'pug');
// Helmet security middleware
app.use(helmet.noSniff());
app.use(helmet.ieNoOpen());
app.use(helmet.xssFilter());
app.use(helmet.referrerPolicy());
app.use(helmet.dnsPrefetchControl());
useSsl && app.use(helmet.hsts({ preload: true })); // skipcq: JS-0093
// Rate limit middleware
app.use(rateLimit({
windowMs: 1000 * 60, // 60 seconds // skipcq: JS-0074
max: 90 // Limit each IP to 30 requests per windowMs // skipcq: JS-0074
}));
// Don't process favicon requests (custom middleware)
app.use((req, res, next) => (req.url.includes('favicon.ico') ? res.sendStatus(CODE_NO_CONTENT) : next()));
// Assign routers ('/:resouceId' always needs to be LAST since it's a catch-all route)
app.use('/', ROUTERS.upload);
ASS_PREMIUM.enabled && app.use(ASS_PREMIUM.endpoint, ASS_PREMIUM.router); // skipcq: JS-0093
app.use('/:resourceId', (req, _, next) => (req.resourceId = req.params.resourceId, next()), ROUTERS.resource); // skipcq: JS-0086, JS-0090
// Error handler
app.use(([err, , res,]) => {
console.error(err);
res.sendStatus(CODE_INTERNAL_SERVER_ERROR);
});
// Host the server
app.listen(port, host, () => log(`Server started on [${host}:${port}]\nAuthorized users: ${Object.keys(users).length}\nAvailable files: ${Object.keys(data).length}`));

@ -3,25 +3,27 @@
*/
const fs = require('fs-extra');
const { log, path, arrayEquals } = require('./utils');
const { log, path, arrayEquals, generateToken } = require('./utils');
let users = {};
try {
users = require('./auth.json').users || {};
// Make sure auth.json exists and generate the first key
if (!fs.existsSync(path('auth.json'))) {
let users = {};
users[generateToken()] = { username: 'ass', count: 0 };
fs.writeJsonSync(path('auth.json'), { users }, { spaces: 4 });
log(`File [auth.json] created\n\n!! Important: save this token in a secure spot: ${Object.keys(users)[0]}\n`);
} else log('File [auth.json] exists');
// Monitor auth.json for changes (triggered by running 'npm run new-token')
fs.watch(path('auth.json'), { persistent: false },
(eventType) => eventType === 'change' && fs.readJson(path('auth.json'))
.then((json) => {
if (!(arrayEquals(Object.keys(users), Object.keys(json.users)))) {
Object.keys(json.users).forEach((token) => (!Object.prototype.hasOwnProperty.call(users, token)) && (users[token] = json.users[token]));
log(`New token added: ${Object.keys(users)[Object.keys(users).length - 1]}`);
}
})
.catch(console.error));
} catch (ex) {
console.error(ex);
users = {};
}
const users = require('./auth.json').users || {};
// Monitor auth.json for changes (triggered by running 'npm run new-token')
fs.watch(path('auth.json'), { persistent: false },
(eventType) => eventType === 'change' && fs.readJson(path('auth.json'))
.then((json) => {
if (!(arrayEquals(Object.keys(users), Object.keys(json.users)))) {
Object.keys(json.users).forEach((token) => (!Object.prototype.hasOwnProperty.call(users, token)) && (users[token] = json.users[token]));
log(`New token added: ${Object.keys(users)[Object.keys(users).length - 1]}`);
}
})
.catch(console.error));
module.exports = users;

@ -2,11 +2,14 @@
* Used for global data management
*/
let data = {};
try {
data = require('./data.json');
} catch (ex) {
data = {};
}
const fs = require('fs-extra');
const { log, path } = require('./utils');
// Make sure data.json exists
if (!fs.existsSync(path('data.json'))) {
fs.writeJsonSync(path('data.json'), {}, { spaces: 4 });
log('File [data.json] created');
} else log('File [data.json] exists');
const data = require('./data.json');
module.exports = data;

Loading…
Cancel
Save