mcraft-fun-mineflayer
Version:
Mineflayer viewer (connector) for mcraft.fun project and vanilla Minecraft client! Both TCP and WebSockets servers are supported.
116 lines (115 loc) • 4.86 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.createProxyServer = void 0;
const minecraft_protocol_1 = require("minecraft-protocol");
const studioServer_1 = require("./studioServer");
const vanillaAuxClients_1 = require("./vanillaAuxClients");
const username = process.argv[2] || 'hiall2';
const connection = process.argv[3] || 'grim.mcraft.fun';
// const version = process.argv[4] || undefined
const version = '1.19.4';
const port = process.argv[5] || undefined;
const [host, serverPort] = connection.split(':');
const onlyPlayState = true;
const createProxyServer = () => {
(0, studioServer_1.createPacketsStudioServer)();
const currentState = {
leadingConnection: 0,
// packetsReceivedInSession: 0,
// packetsSentInSession: 0,
};
const server = (0, minecraft_protocol_1.createServer)({
'online-mode': false,
port: port ? parseInt(port) : 25565,
version,
});
const handlePacket = (name, data, isFromServer, connectionIndex, state, buffer) => {
(0, studioServer_1.handlePacket)(connectionIndex, isFromServer, name, data, state, buffer);
};
const auxClients = [];
let serverClient;
let auxHelpers;
let targetClientConnectionIndex;
const startTargetClient = () => {
serverClient = (0, minecraft_protocol_1.createClient)({
host,
port: serverPort ? parseInt(serverPort) : 25565,
version: version || undefined,
keepAlive: false,
username,
// auth: 'microsoft',
onMsaCode(data) {
console.log('msa code', data);
},
});
auxHelpers = (0, vanillaAuxClients_1.handleAuxClientsProxyVanilla)(serverClient, {
auxClients
});
};
const handleNewClient = (proxyClient) => {
if (!serverClient) {
startTargetClient();
targetClientConnectionIndex = proxyClient.id;
}
else {
auxClients.push(proxyClient);
auxHelpers.onNewAuxConnection(proxyClient);
}
const connectionIndex = proxyClient.id;
let writeCurrentBypass = false;
const oldWrite = proxyClient.write.bind(proxyClient);
proxyClient.write = (name, data) => {
if (proxyClient.state !== minecraft_protocol_1.states.PLAY && !writeCurrentBypass && !onlyPlayState) {
console.log('skipping', name, data);
return;
}
writeCurrentBypass = false;
oldWrite(name, data);
};
proxyClient.on('end', () => {
console.log('proxy client disconnected', connectionIndex === targetClientConnectionIndex, connectionIndex);
(0, studioServer_1.handleClientDisconnect)(connectionIndex);
});
serverClient.on('packet', (data, meta, buffer, fullBuffer) => {
handlePacket(meta.name, data, true, connectionIndex, meta.state, buffer);
if ((meta.state !== minecraft_protocol_1.states.PLAY || proxyClient.state !== minecraft_protocol_1.states.PLAY) && onlyPlayState) {
return;
}
if (!onlyPlayState && meta.state !== proxyClient.state) {
proxyClient.state = meta.state;
}
writeCurrentBypass = true;
proxyClient.write(meta.name, data);
if (meta.name === 'set_compression') {
serverClient.compressionThreshold = data.threshold;
}
});
proxyClient.on('packet', (data, meta, buffer, fullBuffer) => {
if (targetClientConnectionIndex === connectionIndex) {
auxHelpers.writeMainClientPackets(meta.name, data);
}
handlePacket(meta.name, data, false, connectionIndex, meta.state, buffer);
if ((meta.state !== minecraft_protocol_1.states.PLAY || serverClient.state !== minecraft_protocol_1.states.PLAY) && onlyPlayState) {
return;
}
// const packetData = target.deserializer.parsePacketBuffer(fullBuffer).data.params
// console.log('writing', meta.name, packetData)
// target.writeRaw(fullBuffer)
if (connectionIndex === currentState.leadingConnection) {
serverClient.write(meta.name, data);
}
});
};
server.on(onlyPlayState ? 'login' : 'connection', client => {
if (targetClientConnectionIndex === undefined) {
handleNewClient(client);
}
});
server.on('playerJoin', (client) => {
if (targetClientConnectionIndex !== undefined && targetClientConnectionIndex !== client.id) {
handleNewClient(client);
}
});
};
exports.createProxyServer = createProxyServer;
(0, exports.createProxyServer)();