Logging updates

This commit is contained in:
extremeheat 2021-03-05 02:13:40 -05:00
commit f00357eca6
8 changed files with 57 additions and 28 deletions

View file

@ -1,6 +1,7 @@
const RakClient = require('jsp-raknet/client')
const { Worker, isMainThread, parentPort } = require('worker_threads')
const EncapsulatedPacket = require('jsp-raknet/protocol/encapsulated_packet')
const Reliability = require('jsp-raknet/protocol/reliability')
function connect(hostname, port) {
if (isMainThread) {
@ -49,7 +50,7 @@ function main() {
console.log('SEND' , globalThis.raknetConnection, evt.packet)
const sendPacket = new EncapsulatedPacket()
sendPacket.reliability = 0
sendPacket.reliability = Reliability.ReliableOrdered
sendPacket.buffer = evt.packet
globalThis.raknetConnection?.addEncapsulatedToQueue(sendPacket)

View file

@ -80,6 +80,7 @@ function Encrypt(client, server, options) {
// It works! First encrypted packet :)
client.write('client_to_server_handshake', {})
this.emit('join')
}
client.on('server.client_handshake', startClientboundEncryption)

View file

@ -8,13 +8,16 @@ const Options = require('./options')
const debug = require('debug')('minecraft-protocol')
const fs = require('fs')
const log = console.log
const useWorkers = true
class Client extends Connection {
/**
*
* @param {{ version: number, hostname: string, port: number }} options
*/
constructor(options) {
super()
this.options = { ...Options.defaultOptions, options }
this.options = { ...Options.defaultOptions, ...options }
this.serializer = createSerializer()
this.deserializer = createDeserializer()
this.validateOptions()
@ -29,10 +32,14 @@ class Client extends Connection {
this.on('session', this.connect)
this.startQueue()
this.inLog = (...args) => console.info('C ->', ...args)
this.outLog = (...args) => console.info('C <-', ...args)
// this.on('decrypted', this.onDecryptedPacket)
}
validateOptions() {
// console.log('Options', this.options)
if (!this.options.hostname || this.options.port == null) throw Error('Invalid hostname/port')
if (this.options.version < Options.MIN_VERSION) {
throw new Error(`Unsupported protocol version < ${Options.MIN_VERSION} : ${this.options.version}`)
}
@ -45,8 +52,10 @@ class Client extends Connection {
}
connect = async (sessionData) => {
const hostname = this.options.hostname || '127.0.0.1'
const port = this.options.port || 19132
if (useWorkers) {
this.worker = ConnWorker.connect('127.0.0.1', 19132)
this.worker = ConnWorker.connect(hostname, port)
this.worker.on('message', (evt) => {
switch (evt.type) {
case 'connected':
@ -103,6 +112,7 @@ class Client extends Connection {
chain: encodedChain,
client_data: this.clientUserChain
})
this.emit('loggingIn')
}
onDisconnectRequest(packet) {
@ -112,11 +122,11 @@ class Client extends Connection {
process.exit(1)
}
close() {
console.warn('Close not implemented!!')
}
tryRencode(name, params, actual) {
if (name == 'level_chunk') {
console.log("Skipping chunk validation, it's broken right now")
return
}
const packet = this.serializer.createPacketBuffer({ name, params })
console.assert(packet.toString('hex') == actual.toString('hex'))
@ -138,7 +148,7 @@ class Client extends Connection {
// console.log('packet', packet)
const des = this.deserializer.parsePacketBuffer(packet)
const pakData = { name: des.data.name, params: des.data.params }
console.log('->', pakData.name, serialize(pakData.params).slice(0, 100))
this.inLog('-> C', pakData.name, serialize(pakData.params).slice(0, 100))
// No idea what this exotic 0xA0 packet is, it's not implemented anywhere
// and seems empty. Possible gibberish from the raknet impl

View file

@ -1,4 +1,4 @@
// process.env.DEBUG = 'minecraft-protocol raknet'
process.env.DEBUG = 'minecraft-protocol raknet'
const { Client } = require('./client')
const fs = require('fs')
// console.log = () =>
@ -6,7 +6,7 @@ const fs = require('fs')
async function test() {
const client = new Client({
hostname: '127.0.0.1',
port: 19132
port: 19130
})
client.once('resource_packs_info', (packet) => {

View file

@ -3,19 +3,24 @@ const BatchPacket = require('./datatypes/BatchPacket')
const cipher = require('./transforms/encryption')
const { EventEmitter } = require('events')
const EncapsulatedPacket = require('jsp-raknet/protocol/encapsulated_packet')
const Reliability = require('jsp-raknet/protocol/reliability')
const debug = require('debug')('minecraft-protocol')
class Connection extends EventEmitter {
startEncryption(iv) {
this.encryptionEnabled = true
console.log('Started encryption', this.sharedSecret, iv)
this.inLog('Started encryption', this.sharedSecret, iv)
this.decrypt = cipher.createDecryptor(this, iv)
this.encrypt = cipher.createEncryptor(this, iv)
this.q2 = []
}
write(name, params) { // TODO: Batch
console.log('Need to encode', name, params)
// console.log('<-', name)
// console.log('Need to encode', name, params)
var s = this.connect ? 'C' : 'S'
if (this.downQ) s += 'P'
this.outLog('<- ' + s, name)
const batch = new BatchPacket()
const packet = this.serializer.createPacketBuffer({ name, params })
// console.log('Sending buf', packet.toString('hex').)
@ -29,9 +34,10 @@ class Connection extends EventEmitter {
}
queue(name, params) {
console.log('<- ', name)
this.outLog('Q <- ', name)
const packet = this.serializer.createPacketBuffer({ name, params })
this.q.push(packet)
this.q2.push(name)
}
startQueue() {
@ -40,9 +46,10 @@ class Connection extends EventEmitter {
if (this.q.length) {
//TODO: can we just build Batch before the queue loop?
const batch = new BatchPacket()
this.outLog('<- BATCH', this.q2)
// For now, we're over conservative so send max 3 packets
// per batch and hold the rest for the next tick
for (let i = 0; i < 3 && i < this.q.length; i++) {
for (let i = 0; /*i < 10 &&*/ i < this.q.length; i++) {
const packet = this.q.shift()
batch.addEncodedPacket(packet)
}
@ -51,6 +58,7 @@ class Connection extends EventEmitter {
} else {
this.sendDecryptedBatch(batch)
}
this.q2 = []
}
}, 100)
}
@ -105,11 +113,11 @@ class Connection extends EventEmitter {
// TODO: Rename this to sendEncapsulated
sendMCPE(buffer, immediate) {
if (this.worker) {
console.log('-> buf', buffer)
this.outLog('-> buf', buffer)
this.worker.postMessage({ type: 'queueEncapsulated', packet: buffer, immediate })
} else {
const sendPacket = new EncapsulatedPacket()
sendPacket.reliability = 0
sendPacket.reliability = Reliability.ReliableOrdered
sendPacket.buffer = buffer
this.connection.addEncapsulatedToQueue(sendPacket)
if (immediate) this.connection.sendQueue()
@ -118,10 +126,10 @@ class Connection extends EventEmitter {
// These are callbacks called from encryption.js
onEncryptedPacket = (buf) => {
console.log('ENC BUF', buf)
this.outLog('ENC BUF', buf)
const packet = Buffer.concat([Buffer.from([0xfe]), buf]) // add header
console.log('Sending wrapped encrypted batch', packet)
this.outLog('Sending wrapped encrypted batch', packet)
this.sendMCPE(packet)
}
@ -145,7 +153,7 @@ class Connection extends EventEmitter {
const batch = new BatchPacket(stream)
batch.decode()
const packets = batch.getPackets()
console.log('Reading ', packets.length, 'packets')
this.inLog('Reading ', packets.length, 'packets')
for (var packet of packets) {
this.readPacket(packet)
}

View file

@ -9,11 +9,14 @@ const debug = require('debug')('minecraft-protocol')
class Server extends EventEmitter {
constructor(options) {
super()
this.options = { ...Options.defaultOptions, options }
this.options = { ...Options.defaultOptions, ...options }
this.serializer = createSerializer()
this.deserializer = createDeserializer()
this.clients = {}
this.clientCount = 0
this.validateOptions()
this.inLog = (...args) => console.debug('S', ...args)
this.outLog = (...args) => console.debug('S', ...args)
}
validateOptions() {
@ -26,13 +29,14 @@ class Server extends EventEmitter {
debug('new connection', conn)
const player = new Player(this, conn)
this.clients[hash(conn.address)] = player
this.clientCount++
this.emit('connect', { client: player })
}
onCloseConnection = (inetAddr, reason) => {
debug('close connection', inetAddr, reason)
delete this.clients[hash(inetAddr)]
this.clientCount--
}
onEncapsulated = (encapsulated, inetAddr) => {
@ -45,7 +49,7 @@ class Server extends EventEmitter {
client.handle(buffer)
}
async create(serverIp, port) {
async create(serverIp = this.options.hostname, port = this.options.port) {
this.listener = new Listener(this)
this.raknet = await this.listener.listen(serverIp, port)
console.debug('Listening on', serverIp, port)

View file

@ -11,15 +11,20 @@ const ClientStatus = {
}
class Player extends Connection {
constructor(server, connection, options) {
constructor(server, connection) {
super()
this.server = server
this.serializer = server.serializer
this.deserializer = server.deserializer
// console.log('serializer/des',this.serializer,this.deserializer)
this.connection = connection
Encrypt(this, server, options)
this.options = server.options
Encrypt(this, server, this.options)
this.startQueue()
this.status = ClientStatus.Authenticating
this.inLog = (...args) => console.info('S ->', ...args)
this.outLog = (...args) => console.info('S <-', ...args)
}
getData() {
@ -103,7 +108,7 @@ class Player extends Connection {
throw e
}
console.log('->', des)
console.log('-> S', des)
switch (des.data.name) {
case 'login':
console.log(des)

View file

@ -1,4 +1,4 @@
// process.env.DEBUG = 'minecraft-protocol raknet'
process.env.DEBUG = 'minecraft-protocol raknet'
const { Server } = require('./server')
const CreativeItems = require('../data/creativeitems.json')
const NBT = require('prismarine-nbt')