added rcon.web for rust

This commit is contained in:
BrainFooLong committed 2017-01-19 14:23:57 +01:00
1 parent 01c48adaff
commit 7e2f2d706e
5 files changed
+162 -70

No files matched your search

+2
View File
@@ -96,6 +96,8 @@ lang.values.en = {
"servers.rcon_password.info": "Will be stored as cleartext in database. Required to run background " +
"cronjobs for scheduled tasks. Don't panic, it's not readable without server access." +
"The one that means 'rcon.password' in console",
"servers.webrcon.title" : "Web Rcon",
"servers.webrcon.info" : "Especially Rust provide a Web Rcon interface. This must be yes if server is started with rcon.web 1",
"servers.users.title": "Assigned users",
"servers.users.info": "Only the given user's will see this server",
"delete.confirm": "Are you sure? This cannot be undone!",
+11
View File
@@ -53,6 +53,17 @@
</select>
</div>
<div class="title spacer">
<strong data-translate="servers.webrcon.title"></strong>
<small data-translate="servers.webrcon.info"></small>
</div>
<div class="spacer">
<select name="web" class="selectpicker">
<option value="no" data-translate="no"></option>
<option value="yes" data-translate="yes"></option>
</select>
</div>
<span data-name="save" data-translate="save" class="btn btn-default btn-info submit-form save"></span>
<span data-name="delete" data-translate="delete" class="btn btn-danger btn-info submit-form hidden delete"></span>
<a href="#users" class="btn page-link cancel hidden" data-translate="cancel"></a>
+124 -58
View File
@@ -3,6 +3,7 @@
var net = require("net");
var events = require("events");
var base64 = require(__dirname + "/base64");
var WebSocket = require("ws");
/**
* RCON socket connection
@@ -115,36 +116,81 @@ Rcon.prototype.nextPacketId = function () {
*/
Rcon.prototype.connect = function (callback) {
if (this.socket) return false;
var self = this;
var serverName = this.server.serverData.host + ":" + this.server.serverData.rcon_port;
this.socket = new net.Socket();
// initial timeout is 10 seconds, if server is connected than unset this timeout
this.socket.setTimeout(10 * 1000);
this.socket.on("error", function (err) {
if (callback) callback(err);
this.disconnect();
}.bind(this));
this.socket.on("timeout", function () {
var serverName = self.server.serverData.host + ":" + self.server.serverData.rcon_port;
console.error(new Date(), "RconServer [" + serverName + "]: Connection timed out");
this.disconnect();
}.bind(this));
this.socket.on("end", function () {
this.disconnect();
}.bind(this));
this.socket.connect(this.port, this.host, function (err) {
this.socket.setTimeout(0);
this.socket.on("data", function (data) {
this.dataBuffer = Buffer.concat([this.dataBuffer, data]);
this._data();
if (this.server.serverData.web) {
// handling for web rcon
this.socket = new WebSocket("ws://" + this.server.serverData.host + ":" + (this.server.serverData.rcon_port) + "/" + this.server.serverData.rcon_password);
this.socket.on("error", function (err) {
if(!self.socket) return;
if (callback) callback(err);
this.disconnect();
}.bind(this));
if (callback) callback(null);
this.emit("connect");
}.bind(this));
this.socket.on("open", function () {
if(!self.socket) return;
if (callback) callback(null);
this.emit("connect");
}.bind(this));
this.socket.on("close", function () {
if(!self.socket) return;
this.emit("disconnect");
}.bind(this));
this.socket.on("message", function (message) {
if (message) {
var json = JSON.parse(message);
var cb = self.callbacks[json.Identifier] || {};
self.callbacks[message.Identifier] = null;
var response = {
"size": json.Message.length,
"id": json.Identifier,
"type": 0,
"body": json.Message,
"user": cb.user || null,
"timestamp": new Date(),
"log": cb.log || false
};
try {
if (cb.callback) cb.callback(json.Message);
} catch (e) {
console.error(new Date(), "RconServer [" + serverName + "]: send callback error", e, e.stack);
}
this.emit("message", response);
}
}.bind(this));
return true;
} else {
// handling for normal connections
this.socket = new net.Socket();
// initial timeout is 10 seconds, if server is connected than unset this timeout
this.socket.setTimeout(10 * 1000);
this.socket.on("error", function (err) {
if (callback) callback(err);
this.disconnect();
}.bind(this));
this.socket.on("timeout", function () {
var serverName = self.server.serverData.host + ":" + self.server.serverData.rcon_port;
console.error(new Date(), "RconServer [" + serverName + "]: Connection timed out");
this.disconnect();
}.bind(this));
this.socket.on("end", function () {
this.disconnect();
}.bind(this));
this.socket.connect(this.port, this.host, function (err) {
this.socket.setTimeout(0);
this.socket.on("data", function (data) {
this.dataBuffer = Buffer.concat([this.dataBuffer, data]);
this._data();
}.bind(this));
if (callback) callback(null);
this.emit("connect");
}.bind(this));
}
return true;
};
@@ -157,17 +203,6 @@ Rcon.prototype.connect = function (callback) {
* @param {number=} type Message type
*/
Rcon.prototype.send = function (cmd, user, log, callback, type) {
// it could happen that we send multiple requests at once without receiving data inbetween
// this will swallow the send request
// we fix this by queue send requests and processing them only after we received some data
if (this.sendBlocked) {
this.queue.push(Array.prototype.slice.call(arguments));
return;
}
this.sendBlocked = true;
if (typeof type !== 'number') {
type = Rcon.SERVERDATA_EXECCOMMAND;
}
if (!this.socket) {
process.nextTick(function () {
var err = new Error("Not connected");
@@ -176,30 +211,56 @@ Rcon.prototype.send = function (cmd, user, log, callback, type) {
});
return;
}
if (!Buffer.isBuffer(cmd)) {
cmd = new Buffer(cmd);
}
// for auth request we handle a special callback
if (type == Rcon.SERVERDATA_AUTH) {
this.authCallback = callback;
} else {
if (this.server.serverData.web) {
// handling for web rcon
this.socket.send(JSON.stringify({
"Identifier": this.callbacks.length,
"Message": cmd.toString(),
"Name": "Rcon Web Admin"
}));
this.callbacks.push({
"callback": callback,
"user": user,
"log": log
});
}
// write request
var self = this;
self.socket.write(self._pack(this.packetId, type, cmd), null, function () {
self.packetId = self.nextPacketId();
// write an extra empty request to be able to find multipart message boundings
// minecraft dont have multipart packages so we need no end
if (type != Rcon.SERVERDATA_AUTH && self.server.serverData.game != "minecraft") {
self.socket.write(self._pack(self.packetId, Rcon.SERVERDATA_RESPONSE_VALUE, new Buffer(0)));
self.packetId = self.nextPacketId();
} else {
// handling for normal sockets
// it could happen that we send multiple requests at once without receiving data inbetween
// this will swallow the send request
// we fix this by queue send requests and processing them only after we received some data
if (this.sendBlocked) {
this.queue.push(Array.prototype.slice.call(arguments));
return;
}
});
this.sendBlocked = true;
if (typeof type !== 'number') {
type = Rcon.SERVERDATA_EXECCOMMAND;
}
if (!Buffer.isBuffer(cmd)) {
cmd = new Buffer(cmd);
}
// for auth request we handle a special callback
if (type == Rcon.SERVERDATA_AUTH) {
this.authCallback = callback;
} else {
this.callbacks.push({
"callback": callback,
"user": user,
"log": log
});
}
// write request
var self = this;
self.socket.write(self._pack(this.packetId, type, cmd), null, function () {
self.packetId = self.nextPacketId();
// write an extra empty request to be able to find multipart message boundings
// minecraft dont have multipart packages so we need no end
if (type != Rcon.SERVERDATA_AUTH && self.server.serverData.game != "minecraft") {
self.socket.write(self._pack(self.packetId, Rcon.SERVERDATA_RESPONSE_VALUE, new Buffer(0)));
self.packetId = self.nextPacketId();
}
});
}
};
/**
@@ -208,16 +269,21 @@ Rcon.prototype.send = function (cmd, user, log, callback, type) {
*/
Rcon.prototype.disconnect = function () {
if (!this.socket) return false;
this.socket.removeAllListeners();
// handling for web rcon
if (!this.server.serverData.web) {
this.socket.removeAllListeners();
this.socket.end();
}
// blind error handler, we don't care about those error's anymore
// if we don't add this, it will result in uncatched error
this.socket.on("error", function () {
});
this.socket.end();
this.socket = null;
this.emit("disconnect");
return true;
};
/**
+24 -12
View File
@@ -137,18 +137,22 @@ function RconServer(id, serverData) {
console.error(new Date(), "RconServer [" + serverName + "]: Connection failed");
return;
}
// authenticate
self.injectServerMessage("Rcon authentication by rcon web admin...");
self.con.send(self.serverData.rcon_password, null, true, function (success) {
self.injectServerMessage("Rcon authentication " + (success ? "successfull" : "invalid"));
if (!success) {
console.error(new Date(), "Invalid rcon password for server " + self.serverData.name + ":" + self.serverData.rcon_port);
return;
}
if (self.serverData.web) {
self.connected = true;
Widget.callMethodForAllWidgetsIfActive("onServerConnected", self);
}, Rcon.SERVERDATA_AUTH);
} else {
// authenticate
self.injectServerMessage("Rcon authentication by rcon web admin...");
self.con.send(self.serverData.rcon_password, null, true, function (success) {
self.injectServerMessage("Rcon authentication " + (success ? "successfull" : "invalid"));
if (!success) {
console.error(new Date(), "Invalid rcon password for server " + self.serverData.name + ":" + self.serverData.rcon_port);
return;
}
self.connected = true;
Widget.callMethodForAllWidgetsIfActive("onServerConnected", self);
}, Rcon.SERVERDATA_AUTH);
}
// catch errors
self.con.on("error", function (err) {
console.trace("RconServer [" + serverName + "]", err);
@@ -160,6 +164,12 @@ function RconServer(id, serverData) {
self.onServerMessage(rconMessage);
});
});
// on disconnect remove server from instances
this.con.on("disconnect", function () {
self.removeInstance();
});
}
/**
@@ -175,7 +185,7 @@ RconServer.connectAll = function () {
var servers = db.get("servers").value();
if (servers) {
for (var i in servers) {
RconServer.get(servers[i].id);
RconServer.get(servers[i].id, true);
}
}
};
@@ -184,12 +194,14 @@ RconServer.connectAll = function () {
* Get the server instance for given id
* Connect to server if not yet connected
* @param {string} id
* @param {boolean} connect If false or not set than return null if no connection exist
* @return {RconServer|null}
*/
RconServer.get = function (id) {
RconServer.get = function (id, connect) {
if (RconServer.instances[id]) {
return RconServer.instances[id];
}
if (!connect) return null;
var serverData = db.get("servers").get(id).cloneDeep().value();
if (serverData) {
RconServer.instances[id] = new RconServer(id, serverData);
+1
View File
@@ -71,6 +71,7 @@ var View = function (user, messageData, callback) {
serverData.name = formData.name;
serverData.host = formData.host;
serverData.users = formData.users;
serverData.web = formData.web == "yes";
serverData.rcon_port = parseInt(formData.rcon_port);
serverData.rcon_password = formData.rcon_password;
db.get("servers").set(id, serverData).value();