chomens-bot-js/plugins/proxy.js

125 lines
3.3 KiB
JavaScript
Raw Normal View History

2022-11-30 06:01:46 -05:00
const util = require('util')
const mc = require('minecraft-protocol')
const { loadPlugins } = require('../util/loadPlugins')
const minecraftVersionToNumber = require('../util/minecraftVersionToNumber')
2022-11-30 06:01:46 -05:00
function inject (bot, dcclient, config) {
2022-12-04 07:58:22 -05:00
if (!config.proxy.enabled) return
2022-11-30 06:01:46 -05:00
let index
config.servers.forEach((server, _index) => {
if (bot.options.host !== server.host) return
index = _index
})
bot.proxy = {}
2022-12-04 07:58:22 -05:00
const version = config.proxy.version
2022-11-30 06:01:46 -05:00
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 = []
2022-12-07 06:39:37 -05:00
const targetPacketBlacklist = []
2022-11-30 06:01:46 -05:00
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 })
}
2022-11-30 06:01:46 -05:00
}
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)
2022-11-30 06:01:46 -05:00
})
target.on('packet', (data, meta) => {
if (!clientEnded &&
meta.state === mc.states.PLAY &&
2022-12-07 06:39:37 -05:00
client.state === mc.states.PLAY &&
!targetPacketBlacklist.includes(meta.name)
2022-11-30 06:01:46 -05:00
) client.write(meta.name, data)
})
target.on('error', () => {})
target.on('end', (reason) => {
2022-11-30 06:01:46 -05:00
target.end()
client.end(`Target disconnected with reason: ${util.inspect(reason)}`)
2022-11-30 06:01:46 -05:00
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
2022-11-30 06:01:46 -05:00
target.write(meta.name, data)
})
2022-12-07 06:39:37 -05:00
bot.proxy[client.username] = {
target,
client
}
2022-11-30 06:01:46 -05:00
function endListener (reason) {
delete bot.proxy[client.username]
client.end(`Bot disconnected with reason: ${util.inspect(reason)}`)
bot.off('end', endListener)
2022-11-30 06:01:46 -05:00
}
bot.on('end', endListener)
})
bot.on('end', () => {
srv.close()
srv.removeAllListeners()
})
2022-11-30 06:01:46 -05:00
};
module.exports = { inject }