const WebSocket = require('ws');
const fs = require('fs');

const token = process.argv[2];
const agentId = process.argv[3];
const command = process.argv[4].startsWith('@') ? fs.readFileSync(process.argv[4].slice(1), 'utf8') : process.argv[4];

const ws = new WebSocket(`ws://localhost:5000/ws?type=shell&agentId=${agentId}`);

let buffer = '';
ws.on('open', () => {
    ws.send(JSON.stringify({ type: 'auth', token }));
    setTimeout(() => {
        ws.send(command + '\r\n');
    }, 1500);
});

ws.on('message', (data) => {
    buffer += data.toString();
});

const waitMs = parseInt(process.argv[5]) || 8000;
setTimeout(() => {
    console.log(buffer);
    process.exit(0);
}, waitMs);
