diff --git a/db/patch-proxy.sql b/db/patch-proxy.sql new file mode 100644 index 00000000..41897b1e --- /dev/null +++ b/db/patch-proxy.sql @@ -0,0 +1,23 @@ +-- You should not modify if this have pushed to Github, unless it does serious wrong with the db. +BEGIN TRANSACTION; + +CREATE TABLE proxy ( + id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, + user_id INT NOT NULL, + protocol VARCHAR(10) NOT NULL, + host VARCHAR(255) NOT NULL, + port SMALLINT NOT NULL, + auth BOOLEAN NOT NULL, + username VARCHAR(255) NULL, + password VARCHAR(255) NULL, + active BOOLEAN NOT NULL DEFAULT 1, + 'default' BOOLEAN NOT NULL DEFAULT 0, + created_date DATETIME DEFAULT (DATETIME('now')) NOT NULL +); + +ALTER TABLE monitor ADD COLUMN proxy_id INTEGER REFERENCES proxy(id); + +CREATE INDEX proxy_id ON monitor (proxy_id); +CREATE INDEX proxy_user_id ON proxy (user_id); + +COMMIT; diff --git a/server/client.js b/server/client.js index c7b3bc16..2c07448b 100644 --- a/server/client.js +++ b/server/client.js @@ -83,6 +83,23 @@ async function sendImportantHeartbeatList(socket, monitorID, toUser = false, ove } +/** + * Delivers proxy list + * + * @param socket + * @return {Promise} + */ +async function sendProxyList(socket) { + const timeLogger = new TimeLogger(); + + const list = await R.find("proxy", " user_id = ? ", [socket.userID]); + io.to(socket.userID).emit("proxyList", list.map(bean => bean.export())); + + timeLogger.print("Send Proxy List"); + + return list; +} + async function sendInfo(socket) { socket.emit("info", { version: checkVersion.version, @@ -95,6 +112,6 @@ module.exports = { sendNotificationList, sendImportantHeartbeatList, sendHeartbeatList, - sendInfo + sendProxyList, + sendInfo, }; - diff --git a/server/database.js b/server/database.js index afcace70..a7f7ae7d 100644 --- a/server/database.js +++ b/server/database.js @@ -53,6 +53,7 @@ class Database { "patch-2fa-invalidate-used-token.sql": true, "patch-notification_sent_history.sql": true, "patch-monitor-basic-auth.sql": true, + "patch-proxy.sql": true, } /** diff --git a/server/model/monitor.js b/server/model/monitor.js index b4a80598..f938ca80 100644 --- a/server/model/monitor.js +++ b/server/model/monitor.js @@ -1,4 +1,6 @@ const https = require("https"); +const HttpProxyAgent = require("http-proxy-agent"); +const HttpsProxyAgent = require("https-proxy-agent"); const dayjs = require("dayjs"); const utc = require("dayjs/plugin/utc"); let timezone = require("dayjs/plugin/timezone"); @@ -77,6 +79,7 @@ class Monitor extends BeanModel { dns_resolve_server: this.dns_resolve_server, dns_last_result: this.dns_last_result, pushToken: this.pushToken, + proxyId: this.proxy_id, notificationIDList, tags: tags, }; @@ -173,6 +176,11 @@ class Monitor extends BeanModel { }; } + const httpsAgentOptions = { + maxCachedSessions: 0, // Use Custom agent to disable session reuse (https://github.com/nodejs/node/issues/3940) + rejectUnauthorized: !this.getIgnoreTls(), + }; + debug(`[${this.name}] Prepare Options for axios`); const options = { @@ -186,17 +194,51 @@ class Monitor extends BeanModel { ...(this.headers ? JSON.parse(this.headers) : {}), ...(basicAuthHeader), }, - httpsAgent: new https.Agent({ - maxCachedSessions: 0, // Use Custom agent to disable session reuse (https://github.com/nodejs/node/issues/3940) - rejectUnauthorized: ! this.getIgnoreTls(), - }), maxRedirects: this.maxredirects, validateStatus: (status) => { return checkStatusCode(status, this.getAcceptedStatuscodes()); }, }; + if (this.proxy_id) { + const proxy = await R.load("proxy", this.proxy_id); + + if (proxy && proxy.active) { + const httpProxyAgentOptions = { + protocol: proxy.protocol, + host: proxy.host, + port: proxy.port, + }; + const httpsProxyAgentOptions = { + ...httpsAgentOptions, + protocol: proxy.protocol, + hostname: proxy.host, + port: proxy.port, + }; + + if (proxy.auth) { + httpProxyAgentOptions.auth = `${proxy.username}:${proxy.password}`; + httpsProxyAgentOptions.auth = `${proxy.username}:${proxy.password}`; + } + + debug(`[${this.name}] HTTP options: ${JSON.stringify({ + "http": httpProxyAgentOptions, + "https": httpsProxyAgentOptions, + })}`); + + options.proxy = false; + options.httpAgent = new HttpProxyAgent(httpProxyAgentOptions); + options.httpsAgent = new HttpsProxyAgent(httpsProxyAgentOptions); + } + } + + if (!options.httpsAgent) { + options.httpsAgent = new https.Agent(httpsAgentOptions); + } + + debug(`[${this.name}] Axios Options: ${JSON.stringify(options)}`); debug(`[${this.name}] Axios Request`); + let res = await axios.request(options); bean.msg = `${res.status} - ${res.statusText}`; bean.ping = dayjs().valueOf() - startTime; diff --git a/server/model/proxy.js b/server/model/proxy.js new file mode 100644 index 00000000..7ddec434 --- /dev/null +++ b/server/model/proxy.js @@ -0,0 +1,21 @@ +const { BeanModel } = require("redbean-node/dist/bean-model"); + +class Proxy extends BeanModel { + toJSON() { + return { + id: this._id, + userId: this._user_id, + protocol: this._protocol, + host: this._host, + port: this._port, + auth: !!this._auth, + username: this._username, + password: this._password, + active: !!this._active, + default: !!this._default, + createdDate: this._created_date, + }; + } +} + +module.exports = Proxy; diff --git a/server/proxy.js b/server/proxy.js new file mode 100644 index 00000000..df383153 --- /dev/null +++ b/server/proxy.js @@ -0,0 +1,99 @@ +const { R } = require("redbean-node"); + +class Proxy { + + /** + * Saves and updates given proxy entity + * + * @param proxy + * @param proxyID + * @param userID + * @return {Promise} + */ + static async save(proxy, proxyID, userID) { + let bean; + + if (proxyID) { + bean = await R.findOne("proxy", " id = ? AND user_id = ? ", [proxyID, userID]); + + if (!bean) { + throw new Error("proxy not found"); + } + + } else { + bean = R.dispense("proxy"); + } + + // Make sure given proxy protocol is supported + if (!["http", "https"].includes(proxy.protocol)) { + throw new Error(`Unsupported proxy protocol "${proxy.protocol}. Supported protocols are http and https."`); + } + + // When proxy is default update deactivate old default proxy + if (proxy.default) { + await R.exec("UPDATE proxy SET `default` = 0 WHERE `default` = 1"); + } + + bean.user_id = userID; + bean.protocol = proxy.protocol; + bean.host = proxy.host; + bean.port = proxy.port; + bean.auth = proxy.auth; + bean.username = proxy.username; + bean.password = proxy.password; + bean.active = proxy.active || true; + bean.default = proxy.default || false; + + await R.store(bean); + + if (proxy.applyExisting) { + await applyProxyEveryMonitor(bean.id, userID); + } + + return bean; + } + + /** + * Deletes proxy with given id and removes it from monitors + * + * @param proxyID + * @param userID + * @return {Promise} + */ + static async delete(proxyID, userID) { + const bean = await R.findOne("proxy", " id = ? AND user_id = ? ", [proxyID, userID]); + + if (!bean) { + throw new Error("proxy not found"); + } + + // Delete removed proxy from monitors if exists + await R.exec("UPDATE monitor SET proxy_id = null WHERE proxy_id = ?", [proxyID]); + + // Delete proxy from list + await R.trash(bean); + } +} + +/** + * Applies given proxy id to monitors + * + * @param proxyID + * @param userID + * @return {Promise} + */ +async function applyProxyEveryMonitor(proxyID, userID) { + // Find all monitors with id and proxy id + const monitors = await R.getAll("SELECT id, proxy_id FROM monitor WHERE user_id = ?", [userID]); + + // Update proxy id not match with given proxy id + for (const monitor of monitors) { + if (monitor.proxy_id !== proxyID) { + await R.exec("UPDATE monitor SET proxy_id = ? WHERE id = ?", [proxyID, monitor.id]); + } + } +} + +module.exports = { + Proxy, +}; diff --git a/server/server.js b/server/server.js index 153cac4f..b713e4f7 100644 --- a/server/server.js +++ b/server/server.js @@ -58,6 +58,9 @@ debug("Importing Notification"); const { Notification } = require("./notification"); Notification.init(); +debug("Importing Proxy"); +const { Proxy } = require("./proxy"); + debug("Importing Database"); const Database = require("./database"); @@ -128,7 +131,7 @@ const io = new Server(server); module.exports.io = io; // Must be after io instantiation -const { sendNotificationList, sendHeartbeatList, sendImportantHeartbeatList, sendInfo } = require("./client"); +const { sendNotificationList, sendHeartbeatList, sendImportantHeartbeatList, sendInfo, sendProxyList } = require("./client"); const { statusPageSocketHandler } = require("./socket-handlers/status-page-socket-handler"); const databaseSocketHandler = require("./socket-handlers/database-socket-handler"); const TwoFA = require("./2fa"); @@ -599,6 +602,7 @@ exports.entryPage = "dashboard"; bean.dns_resolve_type = monitor.dns_resolve_type; bean.dns_resolve_server = monitor.dns_resolve_server; bean.pushToken = monitor.pushToken; + bean.proxyId = Number.isInteger(monitor.proxyId) ? monitor.proxyId : null; await R.store(bean); @@ -1061,6 +1065,52 @@ exports.entryPage = "dashboard"; } }); + 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) { + await restartMonitors(socket.userID); + } + + callback({ + ok: true, + msg: "Saved", + 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); + await restartMonitors(socket.userID); + + callback({ + ok: true, + msg: "Deleted", + }); + + } catch (e) { + callback({ + ok: false, + msg: e.message, + }); + } + }); + socket.on("checkApprise", async (callback) => { try { checkLogin(socket); @@ -1079,6 +1129,7 @@ exports.entryPage = "dashboard"; console.log(`Importing Backup, User ID: ${socket.userID}, Version: ${backupData.version}`); let notificationListData = backupData.notificationList; + let proxyListData = backupData.proxyList; let monitorListData = backupData.monitorList; let version17x = compareVersions.compare(backupData.version, "1.7.0", ">="); @@ -1097,6 +1148,7 @@ exports.entryPage = "dashboard"; await R.exec("DELETE FROM monitor_tag"); await R.exec("DELETE FROM tag"); await R.exec("DELETE FROM monitor"); + await R.exec("DELETE FROM proxy"); } // Only starts importing if the backup file contains at least one notification @@ -1116,6 +1168,24 @@ exports.entryPage = "dashboard"; } } + // Only starts importing if the backup file contains at least one proxy + if (proxyListData.length >= 1) { + const proxies = await R.findAll("proxy"); + + // Loop over proxy list and save proxies + for (const proxy of proxyListData) { + const exists = proxies.find(item => item.id === proxy.id); + + // Do not process when proxy already exists in import handle is skip and keep + if (["skip", "keep"].includes(importHandle) && !exists) { + return; + } + + // Save proxy as new entry if exists update exists one + await Proxy.save(proxy, exists ? proxy.id : undefined, proxy.userId); + } + } + // Only starts importing if the backup file contains at least one monitor if (monitorListData.length >= 1) { // Get every existing monitor name and puts them in one simple string @@ -1165,6 +1235,7 @@ exports.entryPage = "dashboard"; dns_resolve_type: monitorListData[i].dns_resolve_type, dns_resolve_server: monitorListData[i].dns_resolve_server, notificationIDList: {}, + proxy_id: monitorListData[i].proxy_id || null, }; if (monitorListData[i].pushToken) { @@ -1400,6 +1471,7 @@ async function afterLogin(socket, user) { let monitorList = await sendMonitorList(socket); sendNotificationList(socket); + sendProxyList(socket); await sleep(500); @@ -1490,6 +1562,19 @@ async function restartMonitor(userID, monitorID) { return await startMonitor(userID, monitorID); } +async function restartMonitors(userID) { + // Fetch all active monitors for user + const monitors = await R.getAll("SELECT id FROM monitor WHERE active = 1 AND user_id = ?", [userID]); + + for (const monitor of monitors) { + // Start updated monitor + await startMonitor(userID, monitor.id); + + // Give some delays, so all monitors won't make request at the same moment when just start the server. + await sleep(getRandomInt(300, 1000)); + } +} + async function pauseMonitor(userID, monitorID) { await checkOwner(userID, monitorID); diff --git a/src/components/ProxyDialog.vue b/src/components/ProxyDialog.vue new file mode 100644 index 00000000..372cc64b --- /dev/null +++ b/src/components/ProxyDialog.vue @@ -0,0 +1,203 @@ + + + + + diff --git a/src/components/settings/Proxies.vue b/src/components/settings/Proxies.vue new file mode 100644 index 00000000..344cbb6e --- /dev/null +++ b/src/components/settings/Proxies.vue @@ -0,0 +1,47 @@ + + + + + diff --git a/src/languages/en.js b/src/languages/en.js index 40c9c89f..cd62de17 100644 --- a/src/languages/en.js +++ b/src/languages/en.js @@ -369,4 +369,12 @@ export default { alertaApiKey: 'API Key', alertaAlertState: 'Alert State', alertaRecoverState: 'Recover State', + Proxies: "Proxies", + default: "Default", + enabled: "Enabled", + setAsDefault: "Set As Default", + deleteProxyMsg: "Are you sure want to delete this proxy for all monitors?", + proxyDescription: "Proxies must be assigned to a monitor to function.", + enableProxyDescription: "This proxy will not effect on monitor requests until it is activated. You can control temporarily disable the proxy from all monitors by activation status.", + setAsDefaultProxyDescription: "This proxy will be enabled by default for new monitors. You can still disable the proxy separately for each monitor.", }; diff --git a/src/mixins/socket.js b/src/mixins/socket.js index affac4f8..2f127bcf 100644 --- a/src/mixins/socket.js +++ b/src/mixins/socket.js @@ -33,6 +33,7 @@ export default { uptimeList: { }, tlsInfoList: {}, notificationList: [], + proxyList: [], connectionErrorMsg: "Cannot connect to the socket server. Reconnecting...", }; }, @@ -103,6 +104,16 @@ export default { this.notificationList = data; }); + socket.on("proxyList", (data) => { + this.proxyList = data.map(item => { + item.auth = !!item.auth; + item.active = !!item.active; + item.default = !!item.default; + + return item; + }); + }); + socket.on("heartbeat", (data) => { if (! (data.monitorID in this.heartbeatList)) { this.heartbeatList[data.monitorID] = []; diff --git a/src/pages/EditMonitor.vue b/src/pages/EditMonitor.vue index f89e63bf..47e68528 100644 --- a/src/pages/EditMonitor.vue +++ b/src/pages/EditMonitor.vue @@ -222,6 +222,32 @@ {{ $t("Setup Notification") }} + +

{{ $t("Proxies") }}

+

+ {{ $t("Not available, please setup.") }} +

+ +
+ + +
+ +
+ + + + + {{ $t("default") }} +
+ + + diff --git a/src/pages/Settings.vue b/src/pages/Settings.vue index 1717dd52..cb54ae49 100644 --- a/src/pages/Settings.vue +++ b/src/pages/Settings.vue @@ -81,6 +81,9 @@ export default { security: { title: this.$t("Security"), }, + proxies: { + title: this.$t("Proxies"), + }, backup: { title: this.$t("Backup"), }, diff --git a/src/router.js b/src/router.js index c881dc97..1b79dfcb 100644 --- a/src/router.js +++ b/src/router.js @@ -16,6 +16,7 @@ import General from "./components/settings/General.vue"; import Notifications from "./components/settings/Notifications.vue"; import MonitorHistory from "./components/settings/MonitorHistory.vue"; import Security from "./components/settings/Security.vue"; +import Proxies from "./components/settings/Proxies.vue"; import Backup from "./components/settings/Backup.vue"; import About from "./components/settings/About.vue"; @@ -88,6 +89,10 @@ const routes = [ path: "security", component: Security, }, + { + path: "proxies", + component: Proxies, + }, { path: "backup", component: Backup,