From d9e37e3d76eea47b3e9a8583135a293da47b05ce Mon Sep 17 00:00:00 2001 From: IceTank <61137113+IceTank@users.noreply.github.com> Date: Sat, 20 May 2023 15:03:55 +0200 Subject: [PATCH] Add ipc connection option for servers (#1113) * Add ipc connection option for servers * Fix linting error --- docs/API.md | 3 ++- examples/ipc/ipc_server.js | 51 ++++++++++++++++++++++++++++++++++++++ examples/ipc/package.json | 8 ++++++ src/createServer.js | 9 +++++-- src/index.d.ts | 1 + 5 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 examples/ipc/ipc_server.js create mode 100644 examples/ipc/package.json diff --git a/docs/API.md b/docs/API.md index 0b2cb5e..c18a042 100644 --- a/docs/API.md +++ b/docs/API.md @@ -33,7 +33,8 @@ automatically logged in and validated against mojang's auth. * validateChannelProtocol (optional) : whether or not to enable protocol validation for custom protocols using plugin channels for the connected clients. Defaults to true * enforceSecureProfile (optional) : Kick clients that do not have chat signing keys from Mojang (1.19+) * generatePreview (optional) : Function to generate chat previews. Takes the raw message string and should return the message preview as a string. (1.19-1.19.2) - + * socketType (optional) : either `tcp` or `ipc`. Switches from a tcp connection to a ipc socket connection (or named pipes on windows). With the `ipc` option `host` becomes the path off the ipc connection on the local filesystem. Example: `\\.\pipe\minecraft-ipc` (Windows) `/tmp/minecraft-ipc.sock` (unix based systems). See the ipcConnection example for an example. + ## mc.Server(version,[customPackets]) Create a server instance for `version` of minecraft. diff --git a/examples/ipc/ipc_server.js b/examples/ipc/ipc_server.js new file mode 100644 index 0000000..1eb9175 --- /dev/null +++ b/examples/ipc/ipc_server.js @@ -0,0 +1,51 @@ +/** IPC Connection example + * + * This example shows how to use a IPC connection to communicate with a server or client. + * + * See the node.js documentation about IPC connections here: https://nodejs.org/api/net.html#identifying-paths-for-ipc-connections + */ + +const nmp = require('minecraft-protocol') +const net = require('net') + +const ipcName = 'minecraft-ipc' + +// IPC with node.js works differently on windows and unix systems +let ipcPath +if (process.platform === 'win32') { + ipcPath = `\\\\.\\pipe\\${ipcName}` +} else { + ipcPath = `/tmp/${ipcName}.sock` +} + +const server = nmp.createServer({ + version: '1.18.2', + socketType: 'ipc', + host: ipcPath, // When the optional option socketType is 'ipc' the host becomes the socket path + 'online-mode': false +}) + +server.on('listening', () => { + console.info('Server listening on', server.socketServer.address()) + connectAClient() +}) + +server.on('login', (client) => { + console.info(`New user '${client.username}' logged into the server`) +}) + +function connectAClient () { + const client = nmp.createClient({ + version: '1.18.2', + username: 'ipc_client', + connect: (client) => { + const socket = net.connect(ipcPath, () => { + client.setSocket(socket) + client.emit('connect') + }) + }, + auth: 'offline' + }) + client.on('connect', () => console.info('Client connected to server')) + client.on('end', () => console.info('Client disconnected from server')) +} diff --git a/examples/ipc/package.json b/examples/ipc/package.json new file mode 100644 index 0000000..cb8cd49 --- /dev/null +++ b/examples/ipc/package.json @@ -0,0 +1,8 @@ +{ + "name": "node-minecraft-protocol-example", + "version": "0.0.0", + "private": true, + "dependencies": { + }, + "description": "A node-minecraft-protocol example" +} \ No newline at end of file diff --git a/src/createServer.js b/src/createServer.js index a7e7372..4fa3477 100644 --- a/src/createServer.js +++ b/src/createServer.js @@ -23,7 +23,8 @@ function createServer (options = {}) { version, favicon, customPackets, - motdMsg // This is when you want to send formated motd's from ChatMessage instances + motdMsg, // This is when you want to send formated motd's from ChatMessage instances + socketType = 'tcp' } = options const maxPlayers = options['max-players'] !== undefined ? maxPlayersOld : maxPlayersNew @@ -63,6 +64,10 @@ function createServer (options = {}) { server.on('connection', function (client) { plugins.forEach(plugin => plugin(client, server, options)) }) - server.listen(port, host) + if (socketType === 'ipc') { + server.listen(host) + } else { + server.listen(port, host) + } return server } diff --git a/src/index.d.ts b/src/index.d.ts index c7607bc..7135cbb 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -192,6 +192,7 @@ declare module 'minecraft-protocol' { enforceSecureProfile?: boolean // 1.19.1 & 1.19.2 only: If client should send previews of messages they are typing to the server enableChatPreview?: boolean + socketType?: 'tcp' | 'ipc' } export interface SerializerOptions {