update raknet-native, remove aes-js

This commit is contained in:
extremeheat 2021-04-07 06:56:24 -04:00
commit 2aade94033
4 changed files with 55 additions and 26 deletions

View file

@ -5,8 +5,9 @@ const Reliability = require('jsp-raknet/protocol/reliability')
const RakClient = require('jsp-raknet/client')
const ConnWorker = require('./rakWorker')
const { waitFor } = require('./datatypes/util')
const ServerName = require('./server/advertisement')
try {
var { Client, Server, PacketPriority, PacketReliability, McPingMessage } = require('raknet-native') // eslint-disable-line
var { Client, Server, PacketPriority, PacketReliability } = require('raknet-native') // eslint-disable-line
} catch (e) {
console.debug('[raknet] native not found, using js', e)
}
@ -19,7 +20,7 @@ class RakNativeClient extends EventEmitter {
this.onCloseConnection = () => { }
this.onEncapsulated = () => { }
this.raknet = new Client(options.hostname, options.port, 'minecraft')
this.raknet = new Client(options.hostname, options.port, { protocolVersion: 10 })
this.raknet.on('encapsulated', ({ buffer, address }) => {
this.onEncapsulated(buffer, address)
})
@ -65,16 +66,17 @@ class RakNativeClient extends EventEmitter {
}
class RakNativeServer extends EventEmitter {
constructor (options = {}) {
constructor (options = {}, server) {
super()
this.onOpenConnection = () => { }
this.onCloseConnection = () => { }
this.onEncapsulated = () => { }
this.raknet = new Server(options.hostname, options.port, {
maxConnections: options.maxConnections || 3,
minecraft: {},
message: new McPingMessage().toBuffer()
protocolVersion: 10,
message: ServerName.getServerName(server)
})
// TODO: periodically update the server name until we're closed
this.raknet.on('openConnection', (client) => {
client.sendReliable = function (buffer, immediate) {
@ -91,7 +93,6 @@ class RakNativeServer extends EventEmitter {
})
this.raknet.on('encapsulated', ({ buffer, address }) => {
// console.log('ENCAP',thingy)
this.onEncapsulated(buffer, address)
})
}

View file

@ -0,0 +1,39 @@
class ServerName {
motd = 'Bedrock Protocol Server'
name = 'bedrock-protocol'
protocol = 408
version = '1.16.20'
players = {
online: 0,
max: 5
}
gamemode = 'Creative'
serverId = '0'
toString (version) {
return [
'MCPE',
this.motd,
this.protocol,
this.version,
this.players.online,
this.players.max,
this.serverId,
this.name,
this.gamemode
].join(';') + ';'
}
toBuffer (version) {
const str = this.toString(version)
return Buffer.concat([Buffer.from([0, str.length]), Buffer.from(str)])
}
}
module.exports = {
ServerName,
getServerName (client) {
return new ServerName().toBuffer()
}
}

View file

@ -1,8 +1,7 @@
const { Transform } = require('readable-stream')
const crypto = globalThis.isElectron ? require('browserify-cipher/browser') : require('crypto')
const { createHash } = require('crypto')
const aesjs = require('aes-js')
const crypto = require('crypto')
const Zlib = require('zlib')
if (globalThis.isElectron) var { CipherCFB8 } = require('raknet-native') // eslint-ignore-line
const CIPHER_ALG = 'aes-256-cfb8'
@ -23,37 +22,28 @@ function createDecipher (secret, initialValue) {
class Cipher extends Transform {
constructor (secret, iv) {
super()
this.aes = new aesjs.ModeOfOperation.cfb(secret, iv, 1) // eslint-disable-line new-cap
this.aes = new CipherCFB8(secret, iv)
}
_transform (chunk, enc, cb) {
try {
const res = this.aes.encrypt(chunk)
cb(null, res)
} catch (e) {
cb(e)
}
const ciphered = this.aes.cipher(chunk)
cb(null, ciphered)
}
}
class Decipher extends Transform {
constructor (secret, iv) {
super()
this.aes = new aesjs.ModeOfOperation.cfb(secret, iv, 1) // eslint-disable-line new-cap
this.aes = new CipherCFB8(secret, iv)
}
_transform (chunk, enc, cb) {
try {
const res = this.aes.decrypt(chunk)
cb(null, res)
} catch (e) {
cb(e)
}
cb(null, this.aes.decipher(chunk))
}
}
function computeCheckSum (packetPlaintext, sendCounter, secretKeyBytes) {
const digest = createHash('sha256')
const digest = crypto.createHash('sha256')
const counter = Buffer.alloc(8)
counter.writeBigInt64LE(sendCounter, 0)
digest.update(counter)