mirror of
https://github.com/ChomeNS/chomens-bot-mc.git
synced 2024-11-30 10:56:55 -05:00
124 lines
3.3 KiB
JavaScript
124 lines
3.3 KiB
JavaScript
|
|
const util = require('util')
|
|
const mc = require('minecraft-protocol')
|
|
const { loadPlugins } = require('../util/loadPlugins')
|
|
const minecraftVersionToNumber = require('../util/minecraftVersionToNumber')
|
|
|
|
function inject (bot, dcclient, config) {
|
|
if (!config.proxy.enabled) return
|
|
|
|
let index
|
|
config.servers.forEach((server, _index) => {
|
|
if (bot.options.host !== server.host) return
|
|
index = _index
|
|
})
|
|
|
|
bot.proxy = {}
|
|
|
|
const version = config.proxy.version
|
|
const srv = mc.createServer({
|
|
'online-mode': false,
|
|
port: 25566 + index,
|
|
keepAlive: false,
|
|
version
|
|
})
|
|
|
|
srv.on('login', function (client) {
|
|
bot.console.info(`[Proxy] ${client.username} connected to proxy`)
|
|
let clientEnded = false
|
|
let targetEnded = false
|
|
|
|
const target = mc.createClient({
|
|
username: client.username,
|
|
host: bot.options.host,
|
|
version
|
|
})
|
|
|
|
const clientPacketBlacklist = []
|
|
const targetPacketBlacklist = []
|
|
|
|
target.chat = function (message) {
|
|
if (minecraftVersionToNumber(target.version) >= 1.19) {
|
|
if (message.startsWith('/')) {
|
|
target.write('chat_command', {
|
|
command: message.substring(1), // removes / from the command
|
|
timestamp: BigInt(Date.now()),
|
|
salt: 0n,
|
|
argumentSignatures: [],
|
|
signedPreview: false
|
|
})
|
|
} else {
|
|
target.write('chat_message', {
|
|
message,
|
|
timestamp: BigInt(Date.now()),
|
|
salt: 0,
|
|
signature: Buffer.alloc(0) // the bot will never go online mode i guess so i will just use this
|
|
})
|
|
}
|
|
} else {
|
|
target.write('chat', { message })
|
|
}
|
|
}
|
|
|
|
target.on('login', (packet) => {
|
|
bot.console.info(`[Proxy] ${client.username} target logged in`)
|
|
target.entityId = packet.entityId
|
|
loadPlugins(bot, null, config, null, target, client, true, clientPacketBlacklist, targetPacketBlacklist)
|
|
})
|
|
|
|
target.on('packet', (data, meta) => {
|
|
if (!clientEnded &&
|
|
meta.state === mc.states.PLAY &&
|
|
client.state === mc.states.PLAY &&
|
|
!targetPacketBlacklist.includes(meta.name)
|
|
) client.write(meta.name, data)
|
|
})
|
|
|
|
target.on('error', () => {})
|
|
|
|
target.on('end', (reason) => {
|
|
target.end()
|
|
client.end(`Target disconnected with reason: ${util.inspect(reason)}`)
|
|
targetEnded = true
|
|
})
|
|
|
|
client.on('end', function () {
|
|
clientEnded = true
|
|
target.end()
|
|
target.removeAllListeners()
|
|
client.removeAllListeners()
|
|
bot.console.info(`[Proxy] ${client.username} ended`)
|
|
})
|
|
|
|
client.on('error', function () {
|
|
clientEnded = true
|
|
target.removeAllListeners()
|
|
client.removeAllListeners()
|
|
bot.console.info(`[Proxy] ${client.username} got error`)
|
|
})
|
|
|
|
client.on('packet', (data, meta) => {
|
|
if (clientPacketBlacklist.includes(meta.name)) return
|
|
target.write(meta.name, data)
|
|
})
|
|
|
|
bot.proxy[client.username] = {
|
|
target,
|
|
client
|
|
}
|
|
|
|
function endListener (reason) {
|
|
delete bot.proxy[client.username]
|
|
client.end(`Bot disconnected with reason: ${util.inspect(reason)}`)
|
|
bot.off('end', endListener)
|
|
}
|
|
bot.on('end', endListener)
|
|
})
|
|
|
|
bot.on('end', () => {
|
|
srv.close()
|
|
srv.removeAllListeners()
|
|
})
|
|
};
|
|
|
|
module.exports = { inject }
|