Provide interface for using not at all supported alternative accounts. (#1026)

* Handle auth potentially being an function

* "Simple" example of how in theroy it would work

* Add package.json

* Fix lint

* add type declarations

* Remove mcleaks example

* Add doc changes

* adjust warning

* Fix lint from rebase manual edit.
This commit is contained in:
Jordan Jones 2022-07-29 09:41:43 -07:00 committed by GitHub
parent e079e9b0f6
commit 60379eb7d2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 87 additions and 23 deletions

View file

@ -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.

View file

@ -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 <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>
## 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.

View file

@ -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 <host> <port> <username/email> [<password>]')
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 })
}
})

View file

@ -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": ""
}

View file

@ -34,6 +34,9 @@ function createClient (options) {
const client = new Client(false, version.minecraftVersion, options.customPackets, hideErrors)
tcpDns(client, options)
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')
@ -48,6 +51,7 @@ function createClient (options) {
options.connect(client)
break
}
}
if (options.version === false) autoVersion(client, options)
setProtocol(client, options)
keepalive(client, options)

2
src/index.d.ts vendored
View file

@ -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