diff --git a/docs/API.md b/docs/API.md index 29abce3..ff3c78c 100644 --- a/docs/API.md +++ b/docs/API.md @@ -93,7 +93,7 @@ Returns a `Client` instance and perform login. `options` is an object containing the properties : * username * port : default to 25565 - * auth : the type of account to use, either `microsoft`, `mojang`, or `offline`. default to 'offline' + * auth : the type of account to use, either `microsoft`, `mojang`, `offline` or `function (client, options) => void`. defaults to 'offline'. * password : can be omitted * (microsoft account) leave this blank to use device code auth. If you provide a password, we try to do username and password auth, but this does not always work. diff --git a/docs/FAQ.md b/docs/FAQ.md index eac88a4..7fa9e71 100644 --- a/docs/FAQ.md +++ b/docs/FAQ.md @@ -1,20 +1,29 @@ -## FAQ +# FAQ This Frequently Asked Question document is meant to help people for the most common things. -### How to hide errors ? +## How to hide errors ? Use `hideErrors: true` in createClient options You may also choose to add these listeners : + ```js client.on('error', () => {}) client.on('end', () => {}) ``` -### How can I make a proxy with this ? +## How can I make a proxy with this ? -* Check out our WIP proxy lib https://github.com/PrismarineJS/prismarine-proxy -* See this example https://github.com/PrismarineJS/node-minecraft-protocol/tree/master/examples/proxy -* Read this issue https://github.com/PrismarineJS/node-minecraft-protocol/issues/712 -* check out https://github.com/Heath123/pakkit -* Check out this app https://github.com/wvffle/minecraft-packet-debugger +* Check out our WIP proxy lib +* See this example +* Read this issue +* check out +* Check out this app + +## Can you support alternative auth methods? + +Supporting alternative authentcation methods has been a long standing issue with Prismarine for awhile. We do add support for using your own custom authentication method by providing a function to the `options.auth` property. In order to keep the legitimacy of the project, and to prevent bad attention from Mojang, we will not be supporting any custom authentication methods in the official repositories. + +It is up to the end user to support and maintain the authentication protocol if this is used as support in many of the official channels will be limited. + +If you still wish to proceed, please make sure to throughly read and attempt to understand all implementations of the authentcation you wish to implement. Using an non-official authentication server can make you vulnerable to all different kinds of attacks which are not limited to insecure and/or malicious code! We will not be held responsible for anything you mess up. diff --git a/examples/client_custom_auth/client_custom_auth.js b/examples/client_custom_auth/client_custom_auth.js new file mode 100644 index 0000000..0544497 --- /dev/null +++ b/examples/client_custom_auth/client_custom_auth.js @@ -0,0 +1,41 @@ +'use strict' + +const mc = require('minecraft-protocol') + +const [, , host, port, username, password] = process.argv +if (!username || !password) { + console.log('Usage : node client_custom_auth.js []') + process.exit(1) +} + +const client = mc.createClient({ + host, + port: parseInt(port), + username: username, + password: password, + sessionServer: '', // URL to your session server proxy that changes the expected result of mojang's seession server to mcleaks expected. + // For more information: https://github.com/PrismarineJS/node-yggdrasil/blob/master/src/Server.js#L19 + auth: async (client, options) => { + // handle custom authentication your way. + + // client.username = options.username + // options.accessToken = + return options.connect(client) + } +}) + +client.on('connect', function () { + console.info('connected') +}) +client.on('disconnect', function (packet) { + console.log('disconnected: ' + packet.reason) +}) +client.on('chat', function (packet) { + const jsonMsg = JSON.parse(packet.message) + if (jsonMsg.translate === 'chat.type.announcement' || jsonMsg.translate === 'chat.type.text') { + const username = jsonMsg.with[0].text + const msg = jsonMsg.with[1] + if (username === client.username) return + client.write('chat', { message: msg }) + } +}) diff --git a/examples/client_custom_auth/package.json b/examples/client_custom_auth/package.json new file mode 100644 index 0000000..3feec66 --- /dev/null +++ b/examples/client_custom_auth/package.json @@ -0,0 +1,10 @@ +{ + "name": "node-minecraft-protocol-example-client-custom-auth", + "version": "0.0.0", + "description": "A node-minecraft-protocol example", + "main": "client_custom_auth.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "" +} diff --git a/src/createClient.js b/src/createClient.js index 8a3ac2e..bee9ae1 100644 --- a/src/createClient.js +++ b/src/createClient.js @@ -34,19 +34,23 @@ function createClient (options) { const client = new Client(false, version.minecraftVersion, options.customPackets, hideErrors) tcpDns(client, options) - switch (options.auth) { - case 'mojang': - console.warn('[deprecated] mojang auth servers no longer accept mojang accounts to login. convert your account.\nhttps://help.minecraft.net/hc/en-us/articles/4403181904525-How-to-Migrate-Your-Mojang-Account-to-a-Microsoft-Account') - auth(client, options) - break - case 'microsoft': - microsoftAuth.authenticate(client, options) - break - case 'offline': - default: - client.username = options.username - options.connect(client) - break + if (options.auth instanceof Function) { + options.auth(client, options) + } else { + switch (options.auth) { + case 'mojang': + console.warn('[deprecated] mojang auth servers no longer accept mojang accounts to login. convert your account.\nhttps://help.minecraft.net/hc/en-us/articles/4403181904525-How-to-Migrate-Your-Mojang-Account-to-a-Microsoft-Account') + auth(client, options) + break + case 'microsoft': + microsoftAuth.authenticate(client, options) + break + case 'offline': + default: + client.username = options.username + options.connect(client) + break + } } if (options.version === false) autoVersion(client, options) setProtocol(client, options) diff --git a/src/index.d.ts b/src/index.d.ts index 17bb00b..df91550 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -101,7 +101,7 @@ declare module 'minecraft-protocol' { export interface ClientOptions { username: string port?: number - auth?: 'mojang' | 'microsoft' | 'offline' + auth?: 'mojang' | 'microsoft' | 'offline' | ((client: Client, options: ClientOptions) => void) password?: string host?: string clientToken?: string