Skip to content

Commit

Permalink
fix(core): added nonsecure rand fallback for nanoid
Browse files Browse the repository at this point in the history
  • Loading branch information
tabarra committed Feb 22, 2022
1 parent ed53153 commit b8687e3
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/components/fxRunner/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ const { dir, log, logOk, logWarn, logError } = require('../../extras/console')(m
const helpers = require('../../extras/helpers');
const OutputHandler = require('./outputHandler');

const { customAlphabet } = require('nanoid');
const { customAlphabet } = require('nanoid/non-secure');
const dict51 = require('nanoid-dictionary/nolookalikes');
const genMutex = customAlphabet(dict51, 5);
const genMutex = customAlphabet(dict51, 8);


//Helpers
Expand Down
12 changes: 11 additions & 1 deletion src/components/playerController/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,20 @@ class Database {
* @returns {object} lodash database
*/
async migrateDB(dbo, currVersion) {
if (currVersion === DATABASE_VERSION) {
return dbo;
}
if (typeof currVersion !== 'number') {
logError('Your players database version is not a number!');
process.exit();
}
if (currVersion > DATABASE_VERSION) {
logError(`Your players database is on v${currVersion}, and this txAdmin supports up to v${DATABASE_VERSION}.`);
logError('This means you likely downgraded your txAdmin version. Please update txAdmin.');
process.exit(1);
}

//Migrate database
if (currVersion < 1) {
logWarn(`Migrating your players database from v${currVersion} to v1. Wiping all the data.`);
await dbo.set('version', 1)
Expand Down Expand Up @@ -160,7 +170,7 @@ class Database {
}

if (currVersion !== DATABASE_VERSION) {
logError(`Your players database is on v${currVersion}, which is different from this version of txAdmin.`);
logError(`Your players database is on v${currVersion}, which is different from this version of txAdmin (v${DATABASE_VERSION}).`);
logError('Since there is currently no migration method ready for the migration, txAdmin will attempt to use it anyways.');
logError('Please make sure your txAdmin is on the most updated version!');
process.exit(1);
Expand Down
10 changes: 7 additions & 3 deletions src/components/playerController/idGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ module.exports.genWhitelistID = async (storage) => {
let attempts = 0;
while (attempts < maxAttempts) {
attempts++;
const id = 'R' + nanoidSecure.customAlphabet(GlobalData.noLookAlikesAlphabet, 4)();
if (attempts > 5) globals.databus.txStatsData.randIDFailures++;
const randFunc = (attempts <= 5) ? nanoidSecure : nanoidNonSecure;
const id = 'R' + randFunc.customAlphabet(GlobalData.noLookAlikesAlphabet, 4)();
if (await checkUniqueness(storage, id, 'pendingWL')) {
return id;
}
Expand All @@ -96,10 +98,12 @@ module.exports.genActionID = async (storage, actionType) => {
let attempts = 0;
while (attempts < maxAttempts) {
attempts++;
if (attempts > 5) globals.databus.txStatsData.randIDFailures++;
const randFunc = (attempts <= 5) ? nanoidSecure : nanoidNonSecure;
const id = actionPrefix
+ nanoidSecure.customAlphabet(GlobalData.noLookAlikesAlphabet, 3)()
+ randFunc.customAlphabet(GlobalData.noLookAlikesAlphabet, 3)()
+ '-'
+ nanoidSecure.customAlphabet(GlobalData.noLookAlikesAlphabet, 4)();
+ randFunc.customAlphabet(GlobalData.noLookAlikesAlphabet, 4)();
if (await checkUniqueness(storage, id, 'actions')) {
return id;
}
Expand Down
1 change: 1 addition & 0 deletions src/txAdmin.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ globals = {
bootSeconds: [],
freezeSeconds: [],
},
randIDFailures: 0,
pageViews: {},
httpCounter: {
current: 0,
Expand Down
4 changes: 3 additions & 1 deletion src/webroutes/intercom.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ module.exports = async function Intercom(ctx) {
try {
globals.monitor.handleHeartBeat('http', postData);
const extractData = {
'$statsVersion': 5,
//Changelog:
// 6: added txStatsData.randIDFailures
'$statsVersion': 6,
isZapHosting: GlobalData.isZapHosting,
txAdminVersion: GlobalData.txAdminVersion,
txAdminIsDefaultPort: (GlobalData.txAdminPort == 40120),
Expand Down

0 comments on commit b8687e3

Please sign in to comment.