feat: update client support mcsso

Signed-off-by: MiaoWoo <admin@yumc.pw>
MiaoWoo 2020-09-24 10:33:38 +08:00
parent f8046e38be
commit df266905f2
2 changed files with 49 additions and 24 deletions

View File

@ -3,12 +3,12 @@ import { $ } from './color'
export function attachEvents(client) { export function attachEvents(client) {
client.on('chat', (packet) => { client.on('chat', (packet) => {
// Listen for chat messages and echo them back. // Listen for chat messages and echo them back.
var jsonMsg = JSON.parse(packet.message); var jsonMsg = JSON.parse(packet.message)
console.log($(jsonMsg)) console.log($(jsonMsg))
}) })
client.on('state', (newState, oldState) => { client.on('state', (newState, oldState) => {
console.log('Client Change State', oldState, 'to', newState) console.log('Client Change State', oldState, 'to', newState)
let targetServer = process.argv[3] let targetServer = process.argv[6]
if (newState == "play" && targetServer) { if (newState == "play" && targetServer) {
setTimeout(() => { setTimeout(() => {
client.write('chat', { client.write('chat', {
@ -24,4 +24,10 @@ export function attachEvents(client) {
} else if (packet.health > 0) { } else if (packet.health > 0) {
} }
}) })
client.on('kick_disconnect', (packet) => {
console.log($(packet.reason))
})
client.on('disconnect', (packet) => {
console.log($(packet.reason))
})
} }

View File

@ -5,33 +5,52 @@ import { attachForge } from './forge'
import { attachEvents } from './event' import { attachEvents } from './event'
let username = process.argv[2] || 'Mr_jtb' let username = process.argv[2] || 'Mr_jtb'
let version = process.argv[3] || '1.12.2' let password = process.argv[3] || ''
let address = process.argv[4] || '192.168.2.5:25577' let version = process.argv[4] || '1.12.2'
let realAddress = address.split(":"); let readAddress = process.argv[5] || '192.168.2.5:25577'
let client = createConnection(realAddress[0], parseInt(realAddress[1] || "25565"), username) let realAddress = readAddress.split(":")
let address = realAddress[0]
let port = parseInt(realAddress[1] || "25565")
let client = commandLineCreateClient()
function createConnection(host: string, port: number, username: string) { function commandLineCreateClient() {
return createConnection(address, port, username, password)
}
function createConnection(host: string, port: number, username: string, password: string) {
let client = createClient({ let client = createClient({
version, version,
host, host,
port, port,
username, username,
skipValidation: true password,
clientToken: 'd02c7f39-2376-45da-a5a5-50e24fa8b185',
//@ts-ignore
authServer: 'https://mcsso.yumc.pw/api/yggdrasil/authserver',
sessionServer: 'https://mcsso.yumc.pw/api/yggdrasil/sessionserver'
}) })
attachCommon(client)
attachForge(client) attachForge(client)
attachEvents(client) attachEvents(client)
return client; return client
} }
client.on('error', (error) => { function attachCommon(client) {
console.log("Client Error", error) client.on('error', (error) => {
}) console.log("Client Error", error)
})
client.on('end', (resone) => { client.on('end', (resone) => {
console.log("Client End Resone:", resone) console.log("Client End Resone:", resone)
client = createConnection('192.168.2.5', 25577, username) if (`${resone}` != "SocketClosed") {
}) setTimeout(() => {
client = commandLineCreateClient()
}, 500)
} else {
process.exit(0)
}
})
}
const rl = createInterface({ const rl = createInterface({
input: process.stdin, input: process.stdin,
@ -54,22 +73,22 @@ const rl = createInterface({
rl.on('line', function (line) { rl.on('line', function (line) {
switch (line) { switch (line) {
case "": case "":
break; break
case "eval": case "eval":
break; break
case "write": case "write":
break; break
case "/respawn": case "/respawn":
client.write('client_command', { payload: 0 }) client.write('client_command', { payload: 0 })
break; break
case "//reco": case "//reco":
client.end("") client.end("")
client = createConnection('192.168.2.5', 25577, username) client = commandLineCreateClient()
break; break
case "//quit": case "//quit":
console.info('Disconnected') console.info('Disconnected')
client.end("") client.end("")
break; break
case "//end": case "//end":
console.info('Forcibly ended client') console.info('Forcibly ended client')
process.exit(0) process.exit(0)