Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
DJWoodZ committed Sep 17, 2024
0 parents commit 618ab34
Show file tree
Hide file tree
Showing 7 changed files with 2,902 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module.exports = {
env: {
commonjs: true,
es2021: true,
node: true,
},
extends: 'airbnb-base',
overrides: [
],
parserOptions: {
ecmaVersion: 'latest',
},
rules: {
},
};
130 changes: 130 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Snowpack dependency directory (https://snowpack.dev/)
web_modules/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional stylelint cache
.stylelintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variable files
.env
.env.development.local
.env.test.local
.env.production.local
.env.local

# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache

# Next.js build output
.next
out

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# vuepress v2.x temp and cache directory
.temp
.cache

# Docusaurus cache and generated files
.docusaurus

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port

# Stores VSCode versions used for testing VSCode extensions
.vscode-test

# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*
19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright 2024 DJ WoodZ

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
68 changes: 68 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
Satisfactory Dedicated Server Query Port Probe
==============================================

A zero dependency Node.js library for probing the Lightweight Query API of a [Satisfactory Dedicated Server](https://satisfactory.fandom.com/wiki/Dedicated_servers).

Installation
------------

```
npm i @djwoodz/satisfactory-dedicated-server-lightweight-query-probe
```

Example Usage
-------------

```js
// import module
const { probe } = require('@djwoodz/satisfactory-dedicated-server-lightweight-query-probe');
```

```js
// probe using await
const data = await probe('127.0.0.1', 7777, 10000);
console.log(data);
```

```js
// probe using .then()
probe('127.0.0.1', 7777, 10000)
.then((data) => {
console.log(data);
});
```

Example Response
----------------

```js
{
protocolVersion: 1, // the protocol version
clientData: 1726262591138n, // the unique data sent to the server (timestamp)
serverState: 'Game ongoing', // the state of the server
serverVersion: 366202, // the version of the server
serverFlags: 0n, // the server flags
subStates: [ // the sub states
{ id: 0, version: 17 },
{ id: 1, version: 1 },
{ id: 3, version: 5 },
{ id: 2, version: 1 }
],
serverName: 'Satisfactory Server' // the server name
}
```

API
---

## probe([address], [port], [timeout]) ⇒ <code>Promise</code>

Requests data from a Satisfactory Dedicated Server's Lightweight Query API

**Returns**: <code>Promise</code> - Promise object that represents Lightweight Query API data

| Param | Type | Default | Description |
| --- | --- | --- | --- |
| [address] | <code>string</code> | <code>&#x27;127.0.0.1&#x27;</code> | The address of the Satisfactory Dedicated Server (hostname or IP) |
| [port] | <code>number</code> \| <code>string</code> | <code>7777</code> | The port number of the Satisfactory Dedicated Server |
| [timeout] | <code>number</code> \| <code>string</code> | <code>10000</code> | The timeout for the request (milliseconds) |
111 changes: 111 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
const udp = require('node:dgram');

const gameState = ['Unknown', 'No game loaded', 'Creating game', 'Game ongoing'];
const protocolMagic = 0xF6D5;

/**
* Requests data from a Satisfactory Dedicated Server's Lightweight Query API
* @param {string} [address='127.0.0.1'] The address of the Satisfactory Dedicated Server (hostname
* or IP)
* @param {(number|string)} [port=7777] The port number of the Satisfactory Dedicated Server
* @param {(number|string)} [timeout=10000] The timeout for the request (milliseconds)
* @returns {Promise} Promise object that represents Lightweight Query API data
*/
const probe = (address = '127.0.0.1', port = 7777, timeout = 10000) => new Promise((resolve, reject) => {
const socket = udp.createSocket('udp4');
const now = BigInt(new Date().getTime());

const id = setTimeout(() => {
clearTimeout(id);
socket.close();
reject(new Error('Connection timed out'));
}, parseInt(timeout, 10));

socket.on('message', (msg) => {
const buf = Buffer.from(msg);

const magic = buf.readUint16LE(0);
const messageType = buf.readUInt8(2);
const protocolVersion = buf.readUInt8(3);

if (magic === protocolMagic && messageType === 1 && protocolVersion === 1) {
const payloadOffset = 4;

const clientData = BigInt(buf.readBigUInt64LE(payloadOffset + 0));
const serverState = gameState[buf.readUInt8(payloadOffset + 8)];
const serverVersion = buf.readUint32LE(payloadOffset + 9);
const serverFlags = buf.readBigUInt64LE(payloadOffset + 13);
const numSubStates = buf.readUInt8(payloadOffset + 21);
const subStates = [];

for (let i = 0; i < numSubStates; i += 1) {
subStates.push({
id: buf.readUInt8(payloadOffset + 22 + (i * 3)),
version: buf.readUint16LE(payloadOffset + 22 + 1 + (i * 3)),
});
}

const serverNameLength = buf.readUint16LE(payloadOffset + 22 + (numSubStates * 3));

let serverName = '';
for (let i = 0; i < serverNameLength; i += 1) {
serverName += String
.fromCharCode(buf.readUInt8(payloadOffset + 24 + (numSubStates * 3) + i));
}

const lastByte = buf.readUInt8(payloadOffset + 24 + (numSubStates * 3) + serverNameLength);

if (buf.length === (payloadOffset + 25 + (numSubStates * 3) + serverNameLength)
&& lastByte === 1 && clientData === now) {
const data = {
protocolVersion,
clientData,
serverState,
serverVersion,
serverFlags,
subStates,
serverName,
};

clearTimeout(id);
socket.close();
resolve(data);
return;
}
}

clearTimeout(id);
socket.close();
reject(new Error('Unexpected response'));
});

socket.on('error', (error) => {
clearTimeout(id);
socket.close();
reject(error);
});

const buf = Buffer.alloc(13);
buf.writeUInt16LE(protocolMagic, 0);
buf.writeUInt8(0, 2); // Message type
buf.writeUInt8(1, 3); // Protocol version
buf.writeBigUInt64LE(now, 4); // Client data
buf.writeUInt8(1, 12);

socket.send(
buf,
parseInt(port, 10),
address,
(error) => {
if (error) {
clearTimeout(id);
socket.close();
reject(error);
}
},
);
});

module.exports = {
probe,
};
Loading

0 comments on commit 618ab34

Please sign in to comment.