Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to IP hash-based client ID system. #186

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions backend/websockets/chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ module.exports = async function(ws, data, send, broadcast, server, ctx) {
blocks.no_reg = true;
break;
default:
id = san_nbr(id);
id = san_nbr(parseInt(id, 16));
if (id < 0) return;

if ((blocks.id.length + blocks.user.length) >= chatBlockLimit)
Expand All @@ -365,7 +365,7 @@ module.exports = async function(ws, data, send, broadcast, server, ctx) {
}
}

serverChatResponse("Blocked chats from ID: " + id, location);
serverChatResponse("Blocked chats from ID: " + id.toString(16), location);
},
blockuser: function(username) {
var blocks = ws.sdata.chat_blocks;
Expand Down Expand Up @@ -403,7 +403,7 @@ module.exports = async function(ws, data, send, broadcast, server, ctx) {
case "reg":
blocks.no_reg = false;
default:
id = san_nbr(id);
id = san_nbr(parseInt(id, 16));
if(id < 0) return;

var idx = blocks.id.indexOf(id);
Expand All @@ -419,7 +419,7 @@ module.exports = async function(ws, data, send, broadcast, server, ctx) {
}
}

serverChatResponse("Unblocked chats from ID: " + id, location);
serverChatResponse("Unblocked chats from ID: " + id.toString(16), location);
},
unblockuser: function(username) {
var blocks = ws.sdata.chat_blocks;
Expand Down Expand Up @@ -467,7 +467,7 @@ module.exports = async function(ws, data, send, broadcast, server, ctx) {
if(!message) {
return serverChatResponse("No message given", location);
}
id = parseInt(id, 10);
id = parseInt(id, 16);
if(isNaN(id)) {
return serverChatResponse("Invalid ID format", location);
}
Expand Down Expand Up @@ -596,7 +596,7 @@ module.exports = async function(ws, data, send, broadcast, server, ctx) {
},
mute: function(id, time, flag) {
if(!is_owner && !user.staff) return;
id = san_nbr(id);
id = san_nbr(parseInt(id, 16));
time = san_nbr(time); // in seconds

var timeSuffixMap = {
Expand Down Expand Up @@ -674,7 +674,7 @@ module.exports = async function(ws, data, send, broadcast, server, ctx) {
},
delete: async function(id, timestamp) {
if(!is_owner && !user.staff) return;
id = san_nbr(id);
id = san_nbr(parseInt(id, 16));
timestamp = san_nbr(timestamp);
var wid = world.id;
if(location == "global") {
Expand Down
11 changes: 6 additions & 5 deletions frontend/static/yw/javascript/chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,7 @@ function buildChatElement(field, id, type, nickname, message, realUsername, op,
if(date) dateStr = convertToDate(date);
var pm = dataObj.privateMessage;
var isGreen = false;
var hexId = id.toString(16);

if(chatGreentext && message[0] == ">" && !(":;_-".includes(message[1]))) { // exception to some emoticons
message = message.substr(1);
Expand Down Expand Up @@ -792,7 +793,7 @@ function buildChatElement(field, id, type, nickname, message, realUsername, op,
}

if(type == "user" || type == "user_nick") {
nickTitle.push("ID " + id);
nickTitle.push("ID " + hexId);
}

if(hasTagDom) {
Expand Down Expand Up @@ -832,13 +833,13 @@ function buildChatElement(field, id, type, nickname, message, realUsername, op,
nickDom.style.fontWeight = "bold";
}
nickDom.style.pointerEvents = "default";
if(state.userModel.is_operator) idTag = "[" + id + "]";
if(state.userModel.is_operator) idTag = "[" + hexId + "]";
}
if(type == "anon_nick") {
idTag = "[*" + id + "]"
idTag = "[*" + hexId + "]"
}
if(type == "anon") {
idTag = "[" + id + "]"
idTag = "[" + hexId + "]"
}
if(type == "user_nick") {
nickDom.style.color = color;
Expand All @@ -847,7 +848,7 @@ function buildChatElement(field, id, type, nickname, message, realUsername, op,
impersonationWarning = " (Special chars)";
}
nickTitle.push("Username \"" + realUsername + "\"" + impersonationWarning);
if(state.userModel.is_operator) idTag = "[*" + id + "]";
if(state.userModel.is_operator) idTag = "[*" + hexId + "]";
}

if(state.userModel.is_operator) {
Expand Down
26 changes: 5 additions & 21 deletions runserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -1462,33 +1462,17 @@ function getWorldData(worldId) {
if(worldData[worldId]) return worldData[worldId];

worldData[worldId] = {
id_overflow_int: 10000,
display_user_count: 0,
user_count: 0
};

return worldData[worldId];
}
function generateClientId(world_id) {
var worldObj = getWorldData(world_id);

var rand_ids = client_ips[world_id];
if(!rand_ids) rand_ids = {};

// attempt to get a random id
for(var i = 0; i < 64; i++) {
var inclusive_id = Math.floor(Math.random() * ((9999 - 1) + 1)) + 1;
if(!rand_ids[inclusive_id]) {
return inclusive_id;
}
}
// attempt to enumerate if it failed
for(var i = 1; i <= 9999; i++) {
if(!rand_ids[i]) {
return i;
}
}
return worldObj.id_overflow_int++;
function generateClientId(ip_addr) {
var id = crypto.createHash("sha256").update(ip_addr).update(settings.id_pepper).digest().readUIntBE(0, 3);
if (id == 0) id = 1; // better safe than sorry right
return id;
}

function getUserCountFromWorld(worldId) {
Expand Down Expand Up @@ -2104,7 +2088,7 @@ async function manageWebsocketConnection(ws, req) {
initial_user_count = worldObj.user_count;
}

clientId = generateClientId(world.id);
clientId = generateClientId(ws.sdata.ipAddress);

if(!client_ips[world.id]) {
client_ips[world.id] = {};
Expand Down
1 change: 1 addition & 0 deletions settings_example.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,6 @@
"display_email": "\"Our World of Text\" <[email protected]>"
},

"id_pepper": "REPLACE THIS WITH A SECRET HIGH-ENTROPY VALUE",
"activation_key_days_expire": 3
}