2023-12-20 15:29:24 +00:00
|
|
|
import { SocketHandler } from "../socket-handler.js";
|
|
|
|
import { DockgeServer } from "../dockge-server";
|
|
|
|
import { log } from "../log";
|
2023-12-24 14:01:17 +00:00
|
|
|
import { callbackError, checkLogin, DockgeSocket } from "../util-server";
|
2023-12-20 15:29:24 +00:00
|
|
|
|
|
|
|
export class ManageAgentSocketHandler extends SocketHandler {
|
|
|
|
|
|
|
|
create(socket : DockgeSocket, server : DockgeServer) {
|
2023-12-24 14:01:17 +00:00
|
|
|
// addAgent
|
|
|
|
socket.on("addAgent", async (data : unknown, callback : unknown) => {
|
|
|
|
try {
|
|
|
|
log.debug("manage-agent-socket-handler", "addAgent");
|
|
|
|
checkLogin(socket);
|
|
|
|
let manager = socket.instanceManager;
|
|
|
|
await manager.test(data.url, data.username, data.password);
|
|
|
|
await manager.add(data.url, data.username, data.password);
|
|
|
|
|
|
|
|
// connect to the agent
|
|
|
|
manager.connect(data.url, data.username, data.password);
|
|
|
|
|
2023-12-24 17:52:00 +00:00
|
|
|
// Refresh another sockets
|
|
|
|
// It is a bit difficult to control another browser sessions to connect/disconnect agents, so force them to refresh the page will be easier.
|
|
|
|
server.disconnectAllSocketClients(undefined, socket.id);
|
2023-12-24 14:01:17 +00:00
|
|
|
manager.sendAgentList();
|
|
|
|
|
|
|
|
callback({
|
|
|
|
ok: true,
|
|
|
|
msg: "agentAddedSuccessfully",
|
|
|
|
msgi18n: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
} catch (e) {
|
|
|
|
callbackError(e, callback);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// removeAgent
|
2023-12-24 17:52:00 +00:00
|
|
|
socket.on("removeAgent", async (url : unknown, callback : unknown) => {
|
2023-12-24 14:01:17 +00:00
|
|
|
try {
|
|
|
|
log.debug("manage-agent-socket-handler", "removeAgent");
|
|
|
|
checkLogin(socket);
|
2023-12-24 17:52:00 +00:00
|
|
|
let manager = socket.instanceManager;
|
|
|
|
await manager.remove(url);
|
|
|
|
|
|
|
|
server.disconnectAllSocketClients(undefined, socket.id);
|
|
|
|
manager.sendAgentList();
|
|
|
|
|
|
|
|
callback({
|
|
|
|
ok: true,
|
|
|
|
msg: "agentRemovedSuccessfully",
|
|
|
|
msgi18n: true,
|
|
|
|
});
|
2023-12-24 14:01:17 +00:00
|
|
|
} catch (e) {
|
|
|
|
callbackError(e, callback);
|
|
|
|
}
|
|
|
|
});
|
2023-12-20 15:29:24 +00:00
|
|
|
}
|
|
|
|
}
|