Added basic "user" implementation for usernames and upload counts

Use `npm run new-token EpicUsername` to add a token with a specified name. If omitted, a random one will be generated. Existing tokens can still be used; a random username will be generated on upload.
pull/19/head
tycrek 4 years ago
parent e374b3bfe9
commit 4e8777629f
No known key found for this signature in database
GPG Key ID: 25D74F3943625263

@ -43,6 +43,7 @@ const storage = multer.diskStorage({
var upload = multer({ storage });
var tokens = [];
var users = {};
var data = {};
//#endregion
@ -59,18 +60,19 @@ function preStartup() {
// Make sure auth.json exists and generate the first key
if (!fs.existsSync(path('auth.json'))) {
tokens.push(generateToken());
fs.writeJsonSync(path('auth.json'), { tokens }, { spaces: 4 });
fs.writeJsonSync(path('auth.json'), { tokens, users }, { spaces: 4 });
log(`File [auth.json] created\n!! Important: save this token in a secure spot: ${tokens[0]}\n`);
} else log('File [auth.json] exists');
// Read tokens and data
tokens = fs.readJsonSync(path('auth.json')).tokens;
users = fs.readJsonSync(path('auth.json')).users || {};
data = fs.readJsonSync(path('data.json'));
log('Tokens & data read from filesystem');
// Monitor auth.json for changes (triggered by running 'npm run new-token')
fs.watch(path('auth.json'), { persistent: false }, (eventType, _filename) => eventType === 'change' && fs.readJson(path('auth.json'))
.then((json) => (tokens.toString() != json.tokens.toString()) && (tokens = json.tokens) && log(`New token added: ${tokens[tokens.length - 1]}`))
.then((json) => (tokens.toString() != json.tokens.toString()) && (tokens = json.tokens) && (users = json.users) && log(`New token added: ${tokens[tokens.length - 1]}`))
.catch(console.error));
}
@ -89,6 +91,10 @@ function startup() {
// Get the uploaded time in milliseconds
req.file.timestamp = DateTime.now().toMillis();
// Keep track of the token that uploaded the resource
let uploadToken = req.headers.authorization;
req.file.token = uploadToken;
// Attach any embed overrides, if necessary
req.file.opengraph = {
title: req.headers['x-ass-og-title'],
@ -104,7 +110,7 @@ function startup() {
// Log the upload
let logInfo = `${req.file.originalname} (${req.file.mimetype})`;
log(`Uploaded: ${logInfo}`);
log(`Uploaded: ${logInfo} (user: ${users[uploadToken] ? users[uploadToken].username : '<token-only>'})`);
// Build the URLs
let resourceUrl = `${getTrueHttp()}${trueDomain}/${resourceId}`;
@ -112,9 +118,9 @@ function startup() {
// Send the response
res.type('json').send({ resource: resourceUrl, delete: deleteUrl })
// After we have sent the user the response, also send a Webhook to Discord (if headers are present)
.on('finish', () => {
// After we have sent the user the response, also send a Webhook to Discord (if headers are present)
if (req.headers['x-ass-webhook-client'] && req.headers['x-ass-webhook-token']) {
// Build the webhook client & embed
@ -133,6 +139,17 @@ function startup() {
embeds: [embed]
}).then((_msg) => whc.destroy());
}
// Also update the users upload count
if (!users[uploadToken]) {
let generator = () => generateId('random', 20, null);
let username = generator();
while (Object.values(users).findIndex((user) => user.username == username) != -1)
username = generator();
users[uploadToken] = { username, count: 0 };
}
users[uploadToken].count += 1;
fs.writeJsonSync(path('auth.json'), { tokens, users }, { spaces: 4 })
});
});

@ -1,6 +1,9 @@
const uuid = require('uuid').v4;
const fs = require('fs-extra');
const path = require('path');
const randomGen = require('./random');
const MAX_USERNAME = 20;
module.exports = () => uuid().replace(/-/g, '');
@ -12,6 +15,16 @@ if (require.main === module) {
fs.readJson(authPath)
.then((auth) => {
auth.tokens.push(token);
// Generate the user
let username = process.argv[2] ? process.argv[2].replace(/[^\da-z]/gi, '').substring(0, MAX_USERNAME) : randomGen(20);
if (!auth.users) auth.users = {};
if (Object.values(auth.users).findIndex((user) => user.username == username) != -1) {
console.log('Username already exists!');
process.exit(1);
}
auth.users[token] = { username, count: 0 };
fs.writeJsonSync(authPath, auth, { spaces: 4 });
})
.then(() => console.log(`A new token has been generated and automatically applied. You do not need to restart 'ass'.\n\nYour token: ${token}`))

4
package-lock.json generated

@ -1,12 +1,12 @@
{
"name": "ass",
"version": "0.2.2",
"version": "0.2.3",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "ass",
"version": "0.2.2",
"version": "0.2.3",
"license": "ISC",
"dependencies": {
"crypto-random-string": "3.3.1",

@ -1,6 +1,6 @@
{
"name": "ass",
"version": "0.2.2",
"version": "0.2.3",
"description": "A Not Shitty ShareX Upload Server That Actually Works As Intended",
"main": "ass.js",
"scripts": {

Loading…
Cancel
Save