Use offline mode as default authentication, fallback to offline mode if invalid option. (#998)

* Default n-m-p to use offline unless specified.

* Remove expression

* actually make offline default

* Revert examples

* Revert all example changes

* Add deprecation warning that mojang servers are no longer accepting mojang auth tokens.
This commit is contained in:
Jordan Jones 2022-07-29 09:03:20 -07:00 committed by GitHub
parent e4c797d4d8
commit e079e9b0f6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 29 additions and 15 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` or `mojang`. default to 'mojang'
* auth : the type of account to use, either `microsoft`, `mojang`, or `offline`. default 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

@ -78,7 +78,7 @@ var client = mc.createClient({
port: 25565, // optional
username: "email@example.com",
password: "12345678",
auth: 'mojang' // optional; by default uses mojang, if using a microsoft account, set to 'microsoft'
auth: 'microsoft' // optional; by default uses offline mode, if using a microsoft account, set to 'microsoft'
});
client.on('chat', function(packet) {
// Listen for chat messages and echo them back.
@ -92,7 +92,7 @@ client.on('chat', function(packet) {
});
```
If the server is in offline mode, you may leave out the `password` option.
If the server is in offline mode, you may leave out the `password` option and switch auth to `offline`.
You can also leave out `password` when using a Microsoft account. If provided, password based auth will be attempted first which may fail. *Note:* if using a Microsoft account, your account age must be >= 18 years old.
### Hello World server example

View file

@ -82,7 +82,7 @@ module.exports = async function (client, options) {
remoteId: oldProfileObj?.remoteId ?? '',
username: options.username,
localId: profile,
type: (options.auth?.toLowerCase() === 'microsoft' ? 'Xbox' : 'Mojang'),
type: (options.auth?.toLowerCase() === 'mojang' ? 'Mojang' : 'Xbox'),
persistent: true
}
auths.accounts[profile] = newProfileObj

View file

@ -6,7 +6,7 @@ const assert = require('assert')
const encrypt = require('./client/encrypt')
const keepalive = require('./client/keepalive')
const compress = require('./client/compress')
const auth = require('./client/auth')
const auth = require('./client/mojangAuth')
const microsoftAuth = require('./client/microsoftAuth')
const setProtocol = require('./client/setProtocol')
const play = require('./client/play')
@ -34,10 +34,19 @@ function createClient (options) {
const client = new Client(false, version.minecraftVersion, options.customPackets, hideErrors)
tcpDns(client, options)
if (options.auth === 'microsoft') {
microsoftAuth.authenticate(client, options)
} else {
auth(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.version === false) autoVersion(client, options)
setProtocol(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'
auth?: 'mojang' | 'microsoft' | 'offline'
password?: string
host?: string
clientToken?: string

View file

@ -100,7 +100,8 @@ for (const supportedVersion of mc.supportedVersions) {
const client = mc.createClient({
username: 'Player',
version: version.minecraftVersion,
port: PORT
port: PORT,
auth: 'offline'
})
client.on('error', err => done(err))
const lineListener = function (line) {
@ -149,7 +150,8 @@ for (const supportedVersion of mc.supportedVersions) {
const client = mc.createClient({
username: 'Player',
version: version.minecraftVersion,
port: PORT
port: PORT,
auth: 'offline'
})
client.on('error', err => done(err))
client.on('login', function () {
@ -167,7 +169,8 @@ for (const supportedVersion of mc.supportedVersions) {
const client = mc.createClient({
username: 'Player',
version: version.minecraftVersion === '1.8.8' ? '1.11.2' : '1.8.8',
port: PORT
port: PORT,
auth: 'offline'
})
client.once('error', function (err) {
if (err.message.startsWith('This server is version')) {
@ -215,7 +218,8 @@ for (const supportedVersion of mc.supportedVersions) {
username: process.env.MC_USERNAME,
password: process.env.MC_PASSWORD,
version: version.minecraftVersion,
port: PORT
port: PORT,
auth: 'offline'
})
client.on('error', err => done(err))
const lineListener = function (line) {
@ -252,7 +256,8 @@ for (const supportedVersion of mc.supportedVersions) {
const client = mc.createClient({
username: 'Player',
version: version.minecraftVersion,
port: PORT
port: PORT,
auth: 'offline'
})
client.on('error', err => done(err))
let gotKicked = false