Skip to content

Commit

Permalink
Merge pull request Barbosik#194 from uWebSockets/master
Browse files Browse the repository at this point in the history
Disable permessage-deflate, limit payloads to 1kb
  • Loading branch information
Barbosik authored Jul 15, 2016
2 parents 43ce872 + ffd5f07 commit 4433f63
Showing 1 changed file with 84 additions and 83 deletions.
167 changes: 84 additions & 83 deletions src/GameServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ function GameServer() {
serverScrambleLevel: 1, // Toggles scrambling of coordinates. 0 = No scrambling, 1 = lightweight scrambling. 2 = full scrambling (also known as scramble minimap); 3 - high scrambling (no border)
serverMaxLB: 10, // Controls the maximum players displayed on the leaderboard.
serverChat: 1, // Set to 1 to allow chat; 0 to disable chat.
serverName: 'MultiOgar #1', // Server name
serverName: 'MultiOgar #1', // Server name
serverWelcome1: 'Welcome to MultiOgar server!', // First server welcome message
serverWelcome2: '', // Second server welcome message (for info, etc)

Expand All @@ -102,7 +102,7 @@ function GameServer() {
virusMaxAmount: 100, // Maximum number of viruses on the map. If this number is reached, then ejected cells will pass through viruses.

ejectSize: 38, // Size of ejected cells (vanilla 38)
ejectSizeLoss: 43, // Eject size which will be substracted from player cell (vanilla 43?)
ejectSizeLoss: 43, // Eject size which will be substracted from player cell (vanilla 43?)
ejectCooldown: 3, // min ticks between ejects
ejectSpawnPlayer: 1, // if 1 then player may be spawned from ejected mass

Expand Down Expand Up @@ -132,13 +132,13 @@ function GameServer() {

// Parse config
this.loadConfig();
this.loadIpBanList();
this.loadUserList();
this.loadBadWords();

this.loadIpBanList();
this.loadUserList();
this.loadBadWords();

this.setBorder(this.config.borderWidth, this.config.borderHeight);
this.quadTree = new QuadNode(this.border, 16, 100);


// Gamemodes
this.gameMode = Gamemode.get(this.config.serverGamemode);
}
Expand Down Expand Up @@ -171,7 +171,8 @@ GameServer.prototype.start = function() {
}
var wsOptions = {
server: this.httpServer,
perMessageDeflate: true
perMessageDeflate: false,
maxPayload: 1024
};
this.wsServer = new WebSocket.Server(wsOptions);
this.wsServer.on('error', this.onServerSocketError.bind(this));
Expand Down Expand Up @@ -678,11 +679,11 @@ GameServer.prototype.updateVirus = function () {
GameServer.prototype.spawnFood = function() {
var cell = new Entity.Food(this, null, this.getRandomPosition(), this.config.foodMinSize);
if (this.config.foodMassGrow) {
var size = cell.getSize();
var maxGrow = this.config.foodMaxSize - size;
size += maxGrow * Math.random();
cell.setSize(size);
}
var size = cell.getSize();
var maxGrow = this.config.foodMaxSize - size;
size += maxGrow * Math.random();
cell.setSize(size);
}
cell.setColor(this.getRandomColor());
this.addNode(cell);
};
Expand Down Expand Up @@ -1098,82 +1099,82 @@ GameServer.prototype.splitMass = function (mass, count) {
if (splitMass < minMass) {
return [mass];
}
var masses = [];
var masses = [];
if (maxCount < 3 || maxCount < count || curMass / throwMass <= 30) {
// Monotone blow up
// Monotone blow up
for (var i = 0; i < maxCount; i++) {
masses.push(splitMass);
masses.push(splitMass);
}
} else {
// Diverse blow up
// Barbosik: draft version
var restCount = maxCount;
while (restCount > 2) {
var splitMass = curMass / 2;
var splitMass = curMass / 2;
if (splitMass <= throwMass) {
break;
}
var max = curMass - throwMass * (restCount - 1);
break;
}
var max = curMass - throwMass * (restCount - 1);
if (max <= throwMass || splitMass >= max) {
break;
}
masses.push(splitMass);
curMass -= splitMass;
restCount--;
}
}
var splitMass = curMass / 4;
if (splitMass > throwMass) {
while (restCount > 2) {
var max = curMass - throwMass * (restCount - 1);
var max = curMass - throwMass * (restCount - 1);
if (max <= throwMass || splitMass >= max) {
break;
}
masses.push(splitMass);
curMass -= splitMass;
restCount--;
}
}
}
}
var splitMass = curMass / 8;
if (splitMass > throwMass) {
while (restCount > 2) {
var max = curMass - throwMass * (restCount - 1);
var max = curMass - throwMass * (restCount - 1);
if (max <= throwMass || splitMass >= max) {
break;
}
masses.push(splitMass);
curMass -= splitMass;
restCount--;
}
}
}
}
if (restCount > 1) {
splitMass = curMass - throwMass * (restCount - 1);
splitMass = curMass - throwMass * (restCount - 1);
if (splitMass > throwMass) {
masses.push(splitMass);
masses.push(splitMass);
curMass -= splitMass;
restCount--;
}
}
}
if (restCount > 0) {
splitMass = curMass / restCount;
splitMass = curMass / restCount;
if (splitMass < throwMass-0.001) {
Logger.warn("GameServer.splitMass: throwMass-splitMass = "+(throwMass-splitMass).toFixed(3)+" (" + mass.toFixed(4) + ", " + count + ")");
Logger.warn("GameServer.splitMass: throwMass-splitMass = "+(throwMass-splitMass).toFixed(3)+" (" + mass.toFixed(4) + ", " + count + ")");
}
while (restCount > 0) {
masses.push(splitMass);
restCount--;
masses.push(splitMass);
restCount--;
}
}
}
}
//Logger.debug("===GameServer.splitMass===");
//Logger.debug("mass = " + mass.toFixed(3) + " | " + Math.sqrt(mass * 100).toFixed(3));
//var sum = 0;
//Logger.debug("===GameServer.splitMass===");
//Logger.debug("mass = " + mass.toFixed(3) + " | " + Math.sqrt(mass * 100).toFixed(3));
//var sum = 0;
//for (var i = 0; i < masses.length; i++) {
// Logger.debug("mass[" + i + "] = " + masses[i].toFixed(3) + " | " + Math.sqrt(masses[i] * 100).toFixed(3));
// sum += masses[i]
//}
//Logger.debug("sum = " + sum.toFixed(3) + " | " + Math.sqrt(sum * 100).toFixed(3));
// Logger.debug("mass[" + i + "] = " + masses[i].toFixed(3) + " | " + Math.sqrt(masses[i] * 100).toFixed(3));
// sum += masses[i]
//}
//Logger.debug("sum = " + sum.toFixed(3) + " | " + Math.sqrt(sum * 100).toFixed(3));
return masses;
};
};

GameServer.prototype.splitCells = function(client) {
// it seems that vanilla uses order by cell age
Expand Down Expand Up @@ -1351,7 +1352,7 @@ GameServer.prototype.updateMassDecay = function() {
var decay = 1 - this.config.playerDecayRate * this.gameMode.decayMod;
// Loop through all player cells
for (var i = 0; i < this.clients.length; i++) {
var playerTracker = this.clients[i].playerTracker;
var playerTracker = this.clients[i].playerTracker;
for (var j = 0; j < playerTracker.cells.length; j++) {
var cell = playerTracker.cells[j];
var size = cell.getSize();
Expand Down Expand Up @@ -1431,21 +1432,21 @@ GameServer.prototype.checkBadWord = function (value) {

GameServer.prototype.changeConfig = function (name, value) {
if (value == null || isNaN(value)) {
Logger.warn("Invalid value: " + value);
return;
}
Logger.warn("Invalid value: " + value);
return;
}
if (!this.config.hasOwnProperty(name)) {
Logger.warn("Unknown config value: " + name);
return;
}
this.config[name] = value;
Logger.warn("Unknown config value: " + name);
return;
}
this.config[name] = value;

// update/validate
this.config.playerMinSize = Math.max(32, this.config.playerMinSize);
Logger.setVerbosity(this.config.logVerbosity);
Logger.setFileVerbosity(this.config.logFileVerbosity);

Logger.print("Set " + name + " = " + this.config[name]);
Logger.print("Set " + name + " = " + this.config[name]);
};

GameServer.prototype.loadUserList = function () {
Expand Down Expand Up @@ -1526,17 +1527,17 @@ GameServer.prototype.loadIpBanList = function () {

GameServer.prototype.saveIpBanList = function () {
try {
var blFile = fs.createWriteStream(fileNameIpBan);
// Sort the blacklist and write.
var blFile = fs.createWriteStream(fileNameIpBan);
// Sort the blacklist and write.
this.ipBanList.sort().forEach(function (v) {
blFile.write(v + '\n');
});
blFile.end();
blFile.write(v + '\n');
});
blFile.end();
Logger.info(this.ipBanList.length + " IP ban records saved.");
} catch (err) {
Logger.error(err.stack);
Logger.error("Failed to save " + fileNameIpBan + ": " + err.message);
}
}
};

GameServer.prototype.checkIpBan = function (ipAddress) {
Expand Down Expand Up @@ -1565,18 +1566,18 @@ GameServer.prototype.checkIpBan = function (ipAddress) {
GameServer.prototype.banIp = function (ip) {
var ipBin = ip.split('.');
if (ipBin.length != 4) {
Logger.warn("Invalid IP format: " + ip);
return;
Logger.warn("Invalid IP format: " + ip);
return;
}
if (ipBin[0] == "127") {
Logger.warn("Cannot ban localhost");
return;
Logger.warn("Cannot ban localhost");
return;
}
if (this.ipBanList.indexOf(ip) >= 0) {
Logger.warn(ip + " is already in the ban list!");
return;
}
this.ipBanList.push(ip);
Logger.warn(ip + " is already in the ban list!");
return;
}
this.ipBanList.push(ip);
if (ipBin[2]=="*" || ipBin[3] == "*") {
Logger.print("The IP sub-net " + ip + " has been banned");
} else {
Expand All @@ -1589,13 +1590,13 @@ GameServer.prototype.banIp = function (ip) {

// remove player cells
socket.playerTracker.cells.forEach(function (cell) {
this.removeNode(cell);
this.removeNode(cell);
}, this);

// disconnect
socket.close(1000, "Banned from server");
var name = socket.playerTracker.getFriendlyName();
Logger.print("Banned: \"" + name + "\" with Player ID " + socket.playerTracker.pID);
socket.close(1000, "Banned from server");
var name = socket.playerTracker.getFriendlyName();
Logger.print("Banned: \"" + name + "\" with Player ID " + socket.playerTracker.pID);
this.sendChatMessage(null, null, "Banned \"" + name + "\""); // notify to don't confuse with server bug
}, this);
this.saveIpBanList();
Expand All @@ -1604,11 +1605,11 @@ GameServer.prototype.banIp = function (ip) {
GameServer.prototype.unbanIp = function (ip) {
var index = this.ipBanList.indexOf(ip);
if (index < 0) {
Logger.warn("IP " + ip + " is not in the ban list!");
return;
Logger.warn("IP " + ip + " is not in the ban list!");
return;
}
this.ipBanList.splice(index, 1);
Logger.print("Unbanned IP: " + ip);
this.ipBanList.splice(index, 1);
Logger.print("Unbanned IP: " + ip);
this.saveIpBanList();
};

Expand All @@ -1622,21 +1623,21 @@ GameServer.prototype.kickId = function (id) {
return;
// remove player cells
socket.playerTracker.cells.forEach(function (cell) {
this.removeNode(cell);
this.removeNode(cell);
}, this);
// disconnect
socket.close(1000, "Kicked from server");
var name = socket.playerTracker.getFriendlyName();
Logger.print("Kicked \"" + name + "\"");
socket.close(1000, "Kicked from server");
var name = socket.playerTracker.getFriendlyName();
Logger.print("Kicked \"" + name + "\"");
this.sendChatMessage(null, null, "Kicked \"" + name + "\""); // notify to don't confuse with server bug
count++;
count++;
}, this);
if (count > 0)
return;
return;
if (id == 0)
Logger.warn("No players to kick!");
else
Logger.warn("Player with ID "+id+" not found!");
Logger.warn("No players to kick!");
else
Logger.warn("Player with ID "+id+" not found!");
};

// Stats server
Expand Down

0 comments on commit 4433f63

Please sign in to comment.