diff --git a/README.md b/README.md index 94dcb7a..799df4b 100644 --- a/README.md +++ b/README.md @@ -1,16 +1,23 @@ # node-red-contrib-gamedig -Query for the status of most game/voice servers using Node-RED. +Query for server information of most game/voice servers using Node-RED. This package adds the node "Query Game Server" that uses the NPM package [GameDig](https://www.npmjs.com/package/gamedig) to query if a server is online or not and if so returns the data of the server. You can pass the server type, host, and port on the input message or define them on the node (settings defined on the node will override msg values). +You can also specify manual GameDig options using `msg.options` as an input. This will override any other options. For example: you can set `msg.options.guildId` that is required for querying Discord servers. + +Visit the [GameDig GitLab page](https://github.com/gamedig/node-gamedig#return-value) if you want more information about what this library parses and standardizes from the server response. + ### Usage Examples -#### Inserting query data into InfluxDB and using Grafana to view results -![Flow Preview](https://skylar.tech/content/images/2019/12/image-2.png) -I created a post on my website about how to use this node to query gameservers and store the results in InfluxDB. I then give a dashboard in Grafana that can be used to display the data. Check it out here: -https://skylar.tech/tracking-game-server-statistics-using-node-red-influxdb-and-grafana/ +- #### Inserting query data into InfluxDB and using Grafana to view results + ![Flow Preview](https://skylar.tech/content/images/2019/12/image-2.png) + I created a post on my website about how to use this node to query gameservers and store the results in InfluxDB. I then give a dashboard in Grafana that can be used to display the data. Check it out here: + https://skylar.tech/tracking-game-server-statistics-using-node-red-influxdb-and-grafana/ + +- #### Automatically restarting servers when unavailable + Ever host a server and have it stop responding but the process doesn't crash so it doesn't auto restart? If you pair this with something like [node-red-contrib-dockerode](https://flows.nodered.org/node/node-red-contrib-dockerode) you can automatically restart the container/process if the query fails X times to respond. ### Other Packages diff --git a/query-game-server.html b/query-game-server.html index bd9f010..cce5d32 100644 --- a/query-game-server.html +++ b/query-game-server.html @@ -11,6 +11,11 @@ max_attempts: { value: '' }, socket_timeout: { value: '' }, attempt_timeout: { value: '' }, + given_port_only: { value: '' }, + ip_family: { value: '0' }, + debug: { value: '' }, + request_rules: { value: '' }, + output_options: { value: '' } }, inputs:1, outputs:1, @@ -28,20 +33,38 @@ return 'Query Game Server'; }, oneditprepare: function() { + let server_types = null; + $.getJSON('/gamedig/types', function(data) { - let html = '' + - '' + - ''; - for(let game of data) { - html += "" + - "" + - "" + - "" + - ""; + if(data.result !== 'ok' || !data.hasOwnProperty("server_types")) + { + console.error("server_types failed to load"); + return; + } + + server_types = data.server_types; + }); + + $("#node-input-server_type").autoComplete({ + search: function(val) { + if(!server_types) return false; // ignore until we have the types loaded + + let matches = []; + server_types.forEach(v => { + if ( + v.name.toLowerCase().indexOf(val.toLowerCase()) > -1 || + v.type.toLowerCase().indexOf(val.toLowerCase()) > -1 || + v.protocol.toLowerCase().indexOf(val.toLowerCase()) > -1 + ) { + matches.push({ + value: v.type, + label: `${v.name} (${v.type})`, + protocol: v.protocol + }); + } + }); + return matches; } - html += '' + - '
TypeNameProtocol
"+game['type']+""+game['name']+""+game['protocol']+"
'; - $("#query-game-server-types").html(html); }); } }); @@ -64,10 +87,10 @@
- +
- View server types below. + Recommend visiting the GameDig GitHub page for more information about the server type you are trying to query. Some types require extra setup.
@@ -79,8 +102,8 @@
- - + +
Query port for the server (join and query port may differ). @@ -102,7 +125,7 @@
- +
Number of attempts to query server in case of failure. @@ -110,52 +133,85 @@
- +
Milliseconds to wait for a single packet. Beware that increasing this will cause many queries to take longer even if the server is online.
- - + +
Milliseconds allowed for an entire query attempt. This timeout is not commonly hit, as the socketTimeout typically fires first.
-

Server Types

-

- Search available types below.
- You can also view the list here. -

- -
- +
+ + + + Only attempt to query server on given port (default: false). +
-
- +
+ + + + Enables massive amounts of debug logging to stdout. + +
+ +
+ +
+ + For many valve games, additional 'rules' may be fetched into the unstable raw field by setting this to true. Beware that this may increase query time and this is for Valve games only. +
+
+ +
+ +
+ + Outputs msg.options as an object that contains all the options used to query the server using GameDig. Note: If you pass msg.options as an input it will override all set options so make sure you unset it if chaining multiple server query nodes together unless that is what you want. +
+
+ +
+ + +
+
+ IP family/version returned when looking up hostnames via DNS, can be IPv4 and IPv6, IPv4 only or IPv6 only. +
\ No newline at end of file diff --git a/query-game-server.js b/query-game-server.js index 6a9fabd..cdc2a65 100644 --- a/query-game-server.js +++ b/query-game-server.js @@ -4,7 +4,7 @@ module.exports = function(RED) { function QueryGameServer(config) { RED.nodes.createNode(this, config); - var node = this; + let node = this; this.server_type = config.server_type; this.host = config.host; this.port = config.port; @@ -12,47 +12,50 @@ module.exports = function(RED) { this.max_attempts = config.max_attempts || 1; this.socket_timeout = config.socket_timeout || 2000; this.attempt_timeout = config.attempt_timeout || 10000; + this.given_port_only = config.given_port_only || false; + this.ip_family = config.ip_family || 0; + this.debug = config.debug || false; + this.request_rules = config.request_rules || false; + this.output_options = config.output_options || false; node.on('input', function(msg) { - if(node.server_type) { - msg.server_type = node.server_type; + let options = { + 'type': node.server_type || msg.server_type || undefined, + 'host': node.host || msg.host || undefined, + 'port': node.port || msg.port || undefined, + 'maxAttempts': node.max_attempts || msg.max_attempts || undefined, + 'socketTimeout': node.socket_timeout || msg.socket_timeout || undefined, + 'attemptTimeout': node.attempt_timeout || msg.attempt_timeout || undefined, + 'givenPortOnly': node.given_port_only || msg.given_port_only || undefined, + 'ipFamily': node.ip_family || msg.ip_family || undefined, + 'debug': node.debug || msg.config || undefined, + 'requestRules': node.request_rules || msg.request_rules || undefined + }; + + if(typeof msg.options === 'object' && msg.options) + { + options = {...options, ...msg.options}; } - if(node.host) { - msg.host = node.host; + // set the things we want to return + msg.server_type = options.type; + msg.host = options.host; + msg.port = options.port; + if(node.output_options) + { + msg.options = options; } - if(!msg.host) { - node.error("msg.host missing from input."); + + if(!options.host) { + node.error("host missing from input."); return; } - if(node.port) { - msg.port = node.port; - } - - if(node.halt_if) { - msg.halt_if = node.halt_if; + if(!options.type) { + node.error("server_type missing from input."); + return; } - if(node.max_attempts) { - msg.max_attempts = node.max_attempts; - } - - if(node.socket_timeout) { - msg.socket_timeout = node.socket_timeout; - } - - if(node.attempt_timeout) { - msg.attempt_timeout = node.attempt_timeout; - } - - gamedig.query({ - 'type': msg.server_type, - 'host': msg.host, - 'port': msg.port, - 'maxAttempts': msg.max_attempts, - 'socketTimeout': msg.socket_timeout, - 'attemptTimeout': msg.attempt_timeout - }) + gamedig.query(options) .then(function(state) { msg.payload = 'online'; msg.data = state; @@ -84,7 +87,7 @@ module.exports = function(RED) { // so we just use regex to parse the info from the README // this could break so we also reference the gamedig repo let availableTypesContent = fs.readFileSync(require.resolve("gamedig/games.txt"), 'utf-8') - results = []; + server_types = []; availableTypesContent .split(/\r?\n/) @@ -102,12 +105,15 @@ module.exports = function(RED) { // avp2010|Aliens vs. Predator (2010)|valve|port=27015 let [game_type, game_name, game_protocol] = line.split('|'); - results.push({ - 'name': game_type, + server_types.push({ + 'name': game_name, 'type': game_type, 'protocol': game_protocol }); }); - res.json(results); + res.json({ + 'result': 'ok', + 'server_types': server_types + }); }); }; \ No newline at end of file