chomens-bot-js/plugins/proxy.js

130 lines
3.6 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.server.host !== server.host) return
2022-11-30 06:01:46 -05:00
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', (client) => {
2022-11-30 06:01:46 -05:00
bot.console.info(`[Proxy] ${client.username} connected to proxy`)
let clientEnded = false
2022-12-18 07:53:53 -05:00
// eslint-disable-next-line no-unused-vars
2022-11-30 06:01:46 -05:00
let targetEnded = false
const target = mc.createClient({
username: client.username,
host: bot.server.host,
port: bot.server.port,
2022-11-30 06:01:46 -05:00
version
})
const clientPacketBlacklist = []
2022-12-07 06:39:37 -05:00
const targetPacketBlacklist = []
// should this be here or in the chat plugin?
target.sendMessage = function (message) {
if (message.startsWith('/') && minecraftVersionToNumber(target.version) >= 1.19) {
// totallynotskidded™ from mineflayer
const command = message.slice(1)
const timestamp = BigInt(Date.now())
target.write('chat_command', {
command,
timestamp,
salt: 0n,
argumentSignatures: [],
signedPreview: false,
messageCount: 0,
acknowledged: Buffer.alloc(3),
// 1.19.2 Chat Command packet also includes an array of last seen messages
previousMessages: []
})
} else {
target.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', targetEndListener)
target.on('kick_disconnect', ({ reason }) => targetEndListener(JSON.parse(reason)))
target.on('disconnect', ({ reason }) => targetEndListener(JSON.parse(reason)))
function targetEndListener (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
}
2022-11-30 06:01:46 -05:00
client.on('end', () => {
2022-11-30 06:01:46 -05:00
clientEnded = true
target.end()
target.removeAllListeners()
client.removeAllListeners()
bot.console.info(`[Proxy] ${client.username} ended`)
})
client.on('error', () => {
2022-11-30 06:01:46 -05:00
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
}
function botEndListener (reason) {
delete bot.proxy[client.username]
client.end(`Bot disconnected with reason: ${util.inspect(reason)}`)
bot.off('end', botEndListener)
2022-11-30 06:01:46 -05:00
}
bot.on('end', botEndListener)
2022-11-30 06:01:46 -05:00
})
bot.on('end', () => {
srv.close()
srv.removeAllListeners()
})
2022-11-30 06:01:46 -05:00
};
module.exports = { inject }