2022-04-07 08:45:37 +02:00
|
|
|
const { checkLogin } = require("../util-server");
|
|
|
|
const { Proxy } = require("../proxy");
|
|
|
|
const { sendProxyList } = require("../client");
|
2022-04-19 09:38:59 +02:00
|
|
|
const { UptimeKumaServer } = require("../uptime-kuma-server");
|
|
|
|
const server = UptimeKumaServer.getInstance();
|
2022-04-07 08:45:37 +02:00
|
|
|
|
2022-04-21 18:10:23 +02:00
|
|
|
/**
|
|
|
|
* Handlers for proxy
|
|
|
|
* @param {Socket} socket Socket.io instance
|
2023-08-11 09:46:41 +02:00
|
|
|
* @returns {void}
|
2022-04-21 18:10:23 +02:00
|
|
|
*/
|
2022-04-07 08:45:37 +02:00
|
|
|
module.exports.proxySocketHandler = (socket) => {
|
|
|
|
socket.on("addProxy", async (proxy, proxyID, callback) => {
|
|
|
|
try {
|
|
|
|
checkLogin(socket);
|
|
|
|
|
|
|
|
const proxyBean = await Proxy.save(proxy, proxyID, socket.userID);
|
|
|
|
await sendProxyList(socket);
|
|
|
|
|
|
|
|
if (proxy.applyExisting) {
|
2022-04-07 17:03:45 +02:00
|
|
|
await Proxy.reloadProxy();
|
|
|
|
await server.sendMonitorList(socket);
|
2022-04-07 08:45:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
callback({
|
|
|
|
ok: true,
|
2023-09-01 14:51:28 +02:00
|
|
|
msg: "Saved.",
|
|
|
|
msgi18n: true,
|
2022-04-07 08:45:37 +02:00
|
|
|
id: proxyBean.id,
|
|
|
|
});
|
|
|
|
|
|
|
|
} catch (e) {
|
|
|
|
callback({
|
|
|
|
ok: false,
|
|
|
|
msg: e.message,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
socket.on("deleteProxy", async (proxyID, callback) => {
|
|
|
|
try {
|
|
|
|
checkLogin(socket);
|
|
|
|
|
|
|
|
await Proxy.delete(proxyID, socket.userID);
|
|
|
|
await sendProxyList(socket);
|
2022-04-07 17:03:45 +02:00
|
|
|
await Proxy.reloadProxy();
|
2022-04-07 08:45:37 +02:00
|
|
|
|
|
|
|
callback({
|
|
|
|
ok: true,
|
|
|
|
msg: "Deleted",
|
|
|
|
});
|
|
|
|
|
|
|
|
} catch (e) {
|
|
|
|
callback({
|
|
|
|
ok: false,
|
|
|
|
msg: e.message,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|