/**
* 远程控制视图 — C2 机器列表与命令下发
*/
export async function loadView(container) {
container.innerHTML = '
⏳ 加载中...
';
try {
const uid = Date.now().toString(36);
const res = await fetch('/api/c2/machines');
const data = res.ok ? await res.json() : { machines: [] };
const machines = Array.isArray(data) ? data : (data.machines || []);
container.innerHTML = `
🖥️ 远程控制
${machines.length} 台机器
🟢在线 0
🔴离线 0
`;
const gridEl = document.getElementById('machineGrid_' + uid);
if (!gridEl) { container.innerHTML = '❌ 渲染错误: 容器丢失
'; return; }
function renderMachines(list) {
const online = list.filter(m => m.online !== false);
const onlineEl = document.getElementById('onlineCount_' + uid);
const offlineEl = document.getElementById('offlineCount_' + uid);
if (onlineEl) onlineEl.textContent = online.length;
if (offlineEl) offlineEl.textContent = list.length - online.length;
if (!list.length) {
gridEl.innerHTML = '暂无机器数据
';
return;
}
gridEl.innerHTML = list.map(m => {
const isOnline = m.online !== false;
return `
${m.hostname || m.name || m.id}
${m.ip || m.address || ''}
${m.os ? `${m.os}` : ''}
${m.platform ? `${m.platform}` : ''}
${m.version ? `v${m.version}` : ''}
`;
}).join('');
}
renderMachines(machines);
// 通用 C2 发送函数
window._sendC2 = async (target, type, params) => {
const resultId = 'c2Result_' + target.replace(/[^a-zA-Z0-9]/g, '_');
const el = document.getElementById(resultId);
if (el) el.innerHTML = '⏳ 发送中...';
try {
const r = await fetch('/api/c2/send', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ target, type, params }),
});
const d = await r.json();
if (el) {
const output = d.output || d.result || d.message || JSON.stringify(d);
el.innerHTML = `${r.ok ? '✅' : '❌'} ${output}`;
}
} catch (e) {
if (el) el.innerHTML = `❌ ${e.message}`;
}
};
// 自定义命令弹窗
window._showCustomC2 = (target) => {
const overlay = document.createElement('div');
overlay.style.cssText = 'position:fixed;top:0;left:0;right:0;bottom:0;background:rgba(0,0,0,0.4);z-index:99999;display:flex;align-items:center;justify-content:center';
overlay.innerHTML = `
✏️ 发送命令 → ${target}
`;
document.body.appendChild(overlay);
};
window._doCustomC2 = (target) => {
const type = document.getElementById('customC2Type')?.value || 'shell';
const paramsRaw = document.getElementById('customC2Params')?.value || '';
let params = paramsRaw;
// 尝试解析 JSON
try { params = JSON.parse(paramsRaw); } catch {}
window._sendC2(target, type, params);
document.querySelector('div[style*="position:fixed"]')?.remove();
};
} catch (e) {
container.innerHTML = `❌ ${e.message || e}
`;
}
}