"use strict";
const crypto = require("crypto");
const fs = require("fs");
const net = require("net");
const tls = require("tls");
const config = require("../config.js");
const limits = require("./limits.js");
// SmartFoxServer 1.x wire protocol as spoken by the client's SmartFoxClient:
// messages are UTF-8, terminated by a NUL byte, and come in three flavours
// told apart by their first character:
// .. system (XML)
// %xt%%%%%...% extension (raw)
// {"t":"xt","b":{...}} extension (JSON)
const POLICY = '';
const MAX_MESSAGE = 64 * 1024;
const RATE_WINDOW_MS = 10000;
const MAX_BACKLOG = 4 * 1024 * 1024;
const MAX_ERRORS_PER_WINDOW = 20;
// the Create Account screens are done in a few minutes; a guest connection
// does not get to stay for as long as it keeps sending something.
const SIGNUP_LIFETIME_MS = 5 * 60000;
// the login message carries the account's login token; keep it out of the log.
const forLog = message => message.replace(/()/, "$1(hidden)$2");
const connections = new Map(); // remote address -> open sockets
let nextUserId = 1;
class Session {
constructor(socket) {
this.socket = socket;
this.id = nextUserId++;
this.key = null;
this.account = null;
this.room = null;
this.pos = null;
this.address = socket.remoteAddress || "";
this.remote = `${this.address}:${socket.remotePort}`;
}
get name() {
return this.account ? this.account.username : `#${this.id}`;
}
write(message) {
if (this.socket.writable) {
if (config.logPackets) {
console.log(`[game] -> ${this.name} ${message}`);
}
this.socket.write(message + "\0");
// A client that stopped reading must not pile up messages here forever.
if (this.socket.writableLength > MAX_BACKLOG) {
this.socket.destroy();
}
}
}
sendSys(action, roomId, body = "") {
this.write(`${body}`);
}
// raw extension message. the client reads params starting at index 2 of
// [cmd, roomId, ...params].
// "%" separates fields and NUL ends the message, so neither may appear
// inside one: a player-chosen string could otherwise forge fields.
sendXt(cmd, roomId, ...params) {
this.write(`%xt%${[cmd, roomId, ...params].map(param => String(param).replace(/[%\0]/g, "")).join("%")}%`);
}
sendXtJson(roomId, obj) {
this.write(JSON.stringify({t: "xt", b: {r: roomId, o: obj}}));
}
close() {
this.socket.destroy();
}
}
const parseSys = message => {
const body = /([\s\S]*)<\/body>/.exec(message);
if (!body) {
return null;
}
const cdata = tag => {
const match = new RegExp(`<${tag}>${tag}>`).exec(body[3]);
return match ? match[1] : null;
};
return {action: body[1], roomId: Number(body[2]), raw: body[3], cdata};
};
// handlers: {sys(session, msg), xt(session, cmd, roomId, params), close(session)}
const start = handlers => new Promise((resolve, reject) => {
const dispatch = (session, message) => {
if (config.logPackets) {
console.log(`[game] <- ${session.name} ${forLog(message)}`);
}
if (message.startsWith("${session.key}`);
return;
default:
handlers.sys(session, msg);
return;
}
}
if (message[0] === "%") {
// %xt%o%rj%1%a%b% -> ["", "xt", "o", "rj", "1", "a", "b", ""]
const parts = message.split("%");
if (parts[1] === "xt" && parts.length >= 6) {
handlers.xt(session, parts[3], Number(parts[4]), parts.slice(5, -1));
}
return;
}
if (message[0] === "{") {
const body = JSON.parse(message).b || {};
const params = Array.isArray(body.p) ? body.p : Object.values(Object(body.p));
handlers.xt(session, String(body.c), Number(body.r), params.map(String));
}
};
const onConnection = socket => {
const address = limits.key(socket.remoteAddress);
const open = connections.get(address) || 0;
const total = [...connections.values()].reduce((sum, count) => sum + count, 0);
socket.on("error", () => {});
if (open >= config.maxConnectionsPerAddress || total >= config.maxConnections) {
socket.destroy();
return;
}
connections.set(address, open + 1);
const session = new Session(socket);
let buffer = Buffer.alloc(0);
let windowStart = Date.now();
let messagesInWindow = 0;
let bytesInWindow = 0;
let errorsInWindow = 0;
socket.setNoDelay(true);
console.log(`[game] ${session.remote} connected as #${session.id}`);
// whoever connects has to log in (or start signing up) promptly, and
// nobody may sit silent for long: the client sends keep-alives.
const loginTimer = setTimeout(() => {
if (!session.account && !session.signingUp) {
socket.destroy();
}
}, config.loginTimeoutMs);
const signupTimer = setTimeout(() => {
if (!session.account) {
socket.destroy();
}
}, SIGNUP_LIFETIME_MS);
socket.setTimeout(config.idleTimeoutMs, () => socket.destroy());
socket.on("data", chunk => {
if (Date.now() - windowStart > RATE_WINDOW_MS) {
windowStart = Date.now();
messagesInWindow = 0;
bytesInWindow = 0;
errorsInWindow = 0;
}
bytesInWindow += chunk.length;
if (bytesInWindow > config.maxBytesPerTenSeconds) {
console.log(`[game] ${session.name} disconnected for flooding`);
socket.destroy();
return;
}
buffer = Buffer.concat([buffer, chunk]);
let end;
while ((end = buffer.indexOf(0)) !== -1) {
if (end > MAX_MESSAGE) {
socket.destroy();
return;
}
if (++messagesInWindow > config.maxMessagesPerTenSeconds) {
console.log(`[game] ${session.name} disconnected for flooding`);
socket.destroy();
return;
}
const message = buffer.toString("utf8", 0, end);
buffer = buffer.subarray(end + 1);
try {
dispatch(session, message);
}
catch (err) {
// one line a failure, and an end to them: a client that sends
// nonsense in a loop is not to fill the log.
const where = String(err.stack || "").split("\n")[1] || "";
console.log(`[game] error handling message from ${session.name}: ${err.message}${where ? ` (${where.trim()})` : ""}`);
if (++errorsInWindow > MAX_ERRORS_PER_WINDOW) {
console.log(`[game] ${session.name} disconnected: too many messages that could not be handled`);
socket.destroy();
return;
}
}
if (socket.destroyed) {
return;
}
}
if (buffer.length > MAX_MESSAGE) {
socket.destroy();
}
});
socket.on("close", () => {
clearTimeout(loginTimer);
clearTimeout(signupTimer);
const left = connections.get(address) - 1;
if (left > 0) {
connections.set(address, left);
}
else {
connections.delete(address);
}
console.log(`[game] ${session.name} disconnected`);
// nothing thrown while a player is seen off may take the server down.
try {
handlers.close(session);
}
catch (err) {
console.log(`[game] error while ${session.name} left: ${err.stack}`);
}
});
};
const server = config.tls
? tls.createServer({cert: fs.readFileSync(config.tls.certFile), key: fs.readFileSync(config.tls.keyFile),
handshakeTimeout: config.tlsHandshakeTimeoutMs}, onConnection)
: net.createServer(onConnection);
server.on("tlsClientError", () => {});
server.maxConnections = config.maxConnections * 2;
if (config.tls) {
// connections that have not finished their TLS greeting are counted
// too: the limit above is only reached once they have.
const greeting = new Map(); // address -> connections not yet through it
server.on("connection", raw => {
const address = limits.key(raw.remoteAddress);
const count = (greeting.get(address) || 0) + 1;
if (count > config.maxConnectionsPerAddress * 2) {
raw.destroy();
return;
}
greeting.set(address, count);
const done = () => {
const left = greeting.get(address) - 1;
if (left > 0) {
greeting.set(address, left);
}
else {
greeting.delete(address);
}
};
raw.once("close", done);
});
}
server.once("error", reject);
server.listen(config.gamePort, config.gameBindAddress, () => {
console.log(`[game] listening on ${config.gameBindAddress}:${config.gamePort}${config.tls ? " (TLS)" : ""}`);
resolve(server);
});
});
module.exports = {start};