add offline as an auth mode

This commit is contained in:
Romain Beaumont 2022-02-06 17:41:02 +00:00
parent 5a653f68fe
commit 0d8fea2445
9 changed files with 25 additions and 19 deletions

View file

@ -81,7 +81,7 @@ Returns a `Client` instance and perform login.
`options` is an object containing the properties : `options` is an object containing the properties :
* username * username
* port : default to 25565 * port : default to 25565
* auth : the type of account to use, either `microsoft` or `mojang`. default to 'microsoft' * auth : the type of account to use, either `microsoft`, `mojang` or `offline`. default to 'microsoft'
* password : can be omitted * password : can be omitted
* (microsoft account) leave this blank to use device code auth. If you provide * (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. 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 port: 25565, // optional
username: "email@example.com", username: "email@example.com",
password: "12345678", password: "12345678",
auth: 'microsoft' // optional; by default uses microsoft auth: 'microsoft' // optional; by default uses microsoft ; mojang or offline are also valid values
}); });
client.on('chat', function(packet) { client.on('chat', function(packet) {
// Listen for chat messages and echo them back. // 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. In this case set 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. 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 ### Hello World server example

View file

@ -18,7 +18,7 @@ document.getElementById('connect').addEventListener('click', () => {
username: document.getElementById('username').value, username: document.getElementById('username').value,
password: document.getElementById('password').value === '' ? undefined : document.getElementById('password').value password: document.getElementById('password').value === '' ? undefined : document.getElementById('password').value
} }
if (authType.value === 'Microsoft') { if (authType.value === '' || authType.value === 'Microsoft') {
data.auth = 'microsoft' data.auth = 'microsoft'
delete data.password delete data.password
} }

View file

@ -12,8 +12,7 @@ const client = mc.createClient({
host, host,
port: parseInt(port), port: parseInt(port),
username: userOrEmail, // your microsoft account email username: userOrEmail, // your microsoft account email
password: password, // your microsoft account password password: password // your microsoft account password
auth: 'microsoft' // This option must be present and set to 'microsoft' to use Microsoft Account Authentication. Failure to do so will result in yggdrasil throwing invalid account information.
}) })
client.on('connect', function () { client.on('connect', function () {

View file

@ -10,9 +10,8 @@ if (process.argv.length < 4 || process.argv.length > 6) {
const client = mc.createClient({ const client = mc.createClient({
host: process.argv[2], host: process.argv[2],
port: parseInt(process.argv[3]), port: parseInt(process.argv[3]),
username: process.argv[4], // your microsoft account email username: process.argv[4] // your microsoft account email
// password: process.argv[5], // your microsoft account password // password: process.argv[5], // your microsoft account password
auth: 'microsoft' // This option must be present and set to 'microsoft' to use Microsoft Account Authentication. Failure to do so will result in yggdrasil throwing invalid account information.
}) })
client.on('connect', function () { client.on('connect', function () {

View file

@ -96,7 +96,8 @@ srv.on('login', function (client) {
port: port, port: port,
username: client.username, username: client.username,
keepAlive: false, keepAlive: false,
version: version version: version,
auth: 'offline'
}) })
client.on('packet', function (data, meta) { client.on('packet', function (data, meta) {
if (targetClient.state === states.PLAY && meta.state === states.PLAY) { if (targetClient.state === states.PLAY && meta.state === states.PLAY) {

View file

@ -34,11 +34,13 @@ function createClient (options) {
const client = new Client(false, version.minecraftVersion, options.customPackets, hideErrors) const client = new Client(false, version.minecraftVersion, options.customPackets, hideErrors)
tcpDns(client, options) tcpDns(client, options)
if (options.auth === 'microsoft') { if (options.auth !== 'offline') {
if (options.auth === 'microsoft' || options.auth === undefined) {
microsoftAuth.authenticate(client, options) microsoftAuth.authenticate(client, options)
} else { } else if (options.auth === 'mojang') {
auth(client, options) auth(client, options)
} }
}
if (options.version === false) autoVersion(client, options) if (options.version === false) autoVersion(client, options)
setProtocol(client, options) setProtocol(client, options)
keepalive(client, options) keepalive(client, options)

2
src/index.d.ts vendored
View file

@ -80,7 +80,7 @@ declare module 'minecraft-protocol' {
export interface ClientOptions { export interface ClientOptions {
username: string username: string
port?: number port?: number
auth?: 'mojang' | 'microsoft' auth?: 'mojang' | 'microsoft' | 'offline'
password?: string password?: string
host?: string host?: string
clientToken?: string clientToken?: string

View file

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