Generate instance ID separate from rcon password

Create a random instance ID using Math.random instead of deriving it
from the RCON password with a hombrew hash algorithm.  Remove
stringUtils as it's no longer needed.
This commit is contained in:
Hornwitser committed 2019-10-14 15:51:45 +02:00
1 parent 33e9f9d983
commit 6b4ab6660c
5 files changed
+9 -64

No files matched your search

+2
View File
@@ -18,6 +18,8 @@ Version 2.0.0
privileges requirements of symlinks.
- Changed the per instance scenario folder to be linked instead of
copied on instance creation.
- Instance id is no longer derived from the rcon password, instead it's
generated upon instance creation and stored in the instance config.
### Breaking Changes
+7 -2
View File
@@ -17,7 +17,6 @@ const crypto = require("crypto");
// internal libraries
const objectOps = require("lib/objectOps.js");
const fileOps = require("lib/fileOps");
const stringUtils = require("lib/stringUtils.js");
const pluginManager = require("lib/manager/pluginManager");
const modManager = require("lib/manager/modManager");
const hashFile = require('lib/hash').hashFile;
@@ -402,6 +401,7 @@ write-data=${ instance.path() }\r\n
await symlinkMods(instance, "sharedMods", console);
let instconf = {
"id": Math.random() * 2 ** 31 | 0,
"factorioPort": args.port || process.env.FACTORIOPORT || randomDynamicPort(),
"clientPort": args["rcon-port"] || process.env.RCONPORT || randomDynamicPort(),
"__comment_clientPassword": "This is the rcon password. Its also used for making an instanceID. Make sure its unique and not blank.",
@@ -490,7 +490,12 @@ write-data=${ instance.path() }\r\n
async function startInstance(config, args, instance) {
var instanceconfig = JSON.parse(await fs.readFile(instance.path("config.json")));
instanceconfig.unique = stringUtils.hashCode(instanceconfig.clientPassword);
if (typeof instanceconfig.id !== "number" || isNaN(instanceconfig.id)) {
throw new Error(`${instance.path("config.json")} is missing id`);
}
// Temporary measure for backwards compatibility
instanceconfig.unique = instanceconfig.id;
if (process.env.FACTORIOPORT) {
instanceconfig.factorioPort = process.env.FACTORIOPORT;
}
-13
View File
@@ -1,13 +0,0 @@
function hashCode(string){
if(typeof string !== "string") throw "ERROR stringUtils: Not a string";
var hash = 0;
if (string.length == 0) return hash;
for (let i = 0; i < string.length; i++) {
hash = ((hash << 5) - hash) + string.charCodeAt(i);
hash = hash & hash; // Convert to 32bit integer
}
return hash;
}
module.exports = {
hashCode: hashCode,
}
-1
View File
@@ -3,7 +3,6 @@ const fs = require("fs-extra");
const objectOps = require("lib/objectOps.js");
const fileOps = require("lib/fileOps");
const stringUtils = require("lib/stringUtils.js");
const checkHotpatchInstallation = require("lib/clusterTools/checkHotpatchInstallation");
const getLua = require("lib/clusterTools/getLua");
-48
View File
@@ -1,48 +0,0 @@
const assert = require("assert");
const stringUtils = require("lib/stringUtils");
describe("stringUtils.js", function(){
describe("hashCode", function(){
it("Produces a 32bit signed int hash from a string", function(){
let x = stringUtils.hashCode("whatever");
let y = stringUtils.hashCode(" a much longer string to prove this works for long strings as well and also special characters &!¤(#)¤");
assert(typeof x == "number", "hashCode should be returning a 32 bit signed int");
assert(x.toString() == "1934383586");
assert(y.toString() == "1648886132");
});
let permutations = 1000;
it("Rarely produces same hash for different strings ("+permutations+" hashes with collision check)", function(){
let keyHashPairs = [];
for(let i = 0; i < permutations; i++){
let string = Math.random().toString();
keyHashPairs.push({
key:string,
hash:stringUtils.hashCode(string),
});
}
for(let i = 0; i < permutations; i++){
for(let o = 0; o < permutations; o++){
assert(i == o || keyHashPairs[o].key != keyHashPairs[i].key, "Woha, Math.random() generated 2 equal numbers???");
assert(i == o || keyHashPairs[o].hash != keyHashPairs[i].hash, "Hash collision, as long at it doesn't happen again its fine.");
}
}
});
it("Returns 0 for 0 length strings", function(){
let x = stringUtils.hashCode("");
assert.deepEqual(x, 0);
});
it("Throws when supplied with anything other than a string", function(){
assert.throws(function(){
stringUtils.hashCode([]);
}, "ERROR: Not a string")
assert.throws(function(){
stringUtils.hashCode({});
}, "ERROR: Not a string")
assert.throws(function(){
stringUtils.hashCode(0);
}, "ERROR: Not a string")
});
});
});