From 4e8777629faeb3b156037a57a525beb2ce7cb71a Mon Sep 17 00:00:00 2001 From: tycrek Date: Mon, 31 May 2021 14:38:03 -0600 Subject: [PATCH] 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. --- ass.js | 27 ++++++++++++++++++++++----- generators/token.js | 13 +++++++++++++ package-lock.json | 4 ++-- package.json | 2 +- 4 files changed, 38 insertions(+), 8 deletions(-) diff --git a/ass.js b/ass.js index ebe531d..6878ee5 100755 --- a/ass.js +++ b/ass.js @@ -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 : ''})`); // 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 }) }); }); diff --git a/generators/token.js b/generators/token.js index ecca38a..255c9b5 100644 --- a/generators/token.js +++ b/generators/token.js @@ -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}`)) diff --git a/package-lock.json b/package-lock.json index 0186837..a44b0b9 100755 --- a/package-lock.json +++ b/package-lock.json @@ -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", diff --git a/package.json b/package.json index cfa059b..7051386 100755 --- a/package.json +++ b/package.json @@ -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": {