Merge pull request #1461 from louislam/proxy-improvement

Proxy Improvements
pull/1415/head
Louis Lam 3 years ago committed by GitHub
commit 4fb2c69dd1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,7 +1,5 @@
console.log("== Uptime Kuma Reset Password Tool ==");
console.log("Loading the database");
const Database = require("../server/database");
const { R } = require("redbean-node");
const readline = require("readline");
@ -13,8 +11,9 @@ const rl = readline.createInterface({
});
const main = async () => {
console.log("Connecting the database");
Database.init(args);
await Database.connect();
await Database.connect(false, false, true);
try {
// No need to actually reset the password for testing, just make sure no connection problem. It is ok for now.

@ -83,7 +83,7 @@ class Database {
console.log(`Data Dir: ${Database.dataDir}`);
}
static async connect(testMode = false) {
static async connect(testMode = false, autoloadModels = true, noLog = false) {
const acquireConnectionTimeout = 120 * 1000;
const Dialect = require("knex/lib/dialects/sqlite3/index.js");
@ -113,7 +113,10 @@ class Database {
// Auto map the model to a bean object
R.freeze(true);
if (autoloadModels) {
await R.autoloadModels("./server/model");
}
await R.exec("PRAGMA foreign_keys = ON");
if (testMode) {
@ -126,11 +129,13 @@ class Database {
await R.exec("PRAGMA cache_size = -12000");
await R.exec("PRAGMA auto_vacuum = FULL");
if (!noLog) {
console.log("SQLite config:");
console.log(await R.getAll("PRAGMA journal_mode"));
console.log(await R.getAll("PRAGMA cache_size"));
console.log("SQLite Version: " + await R.getCell("SELECT sqlite_version()"));
}
}
static async patch() {
let version = parseInt(await setting("database_version"));

@ -3,6 +3,7 @@ const HttpProxyAgent = require("http-proxy-agent");
const HttpsProxyAgent = require("https-proxy-agent");
const SocksProxyAgent = require("socks-proxy-agent");
const { debug } = require("../src/util");
const server = require("./server");
class Proxy {
@ -144,6 +145,22 @@ class Proxy {
httpsAgent
};
}
/**
* Reload proxy settings for current monitors
* @returns {Promise<void>}
*/
static async reloadProxy() {
let updatedList = await R.getAssoc("SELECT id, proxy_id FROM monitor");
for (let monitorID in server.monitorList) {
let monitor = server.monitorList[monitorID];
if (updatedList[monitorID]) {
monitor.proxy_id = updatedList[monitorID].proxy_id;
}
}
}
}
/**

@ -48,6 +48,27 @@ debug("Importing 2FA Modules");
const notp = require("notp");
const base32 = require("thirty-two");
/**
* `module.exports` (alias: `server`) should be inside this class, in order to avoid circular dependency issue.
* @type {UptimeKumaServer}
*/
class UptimeKumaServer {
/**
* Main monitor list
* @type {{}}
*/
monitorList = {};
entryPage = "dashboard";
async sendMonitorList(socket) {
let list = await getMonitorJSONList(socket.userID);
io.to(socket.userID).emit("monitorList", list);
return list;
}
}
const server = module.exports = new UptimeKumaServer();
console.log("Importing this project modules");
debug("Importing Monitor");
const Monitor = require("./model/monitor");
@ -115,20 +136,20 @@ if (config.demoMode) {
console.log("Creating express and socket.io instance");
const app = express();
let server;
let httpServer;
if (sslKey && sslCert) {
console.log("Server Type: HTTPS");
server = https.createServer({
httpServer = https.createServer({
key: fs.readFileSync(sslKey),
cert: fs.readFileSync(sslCert)
}, app);
} else {
console.log("Server Type: HTTP");
server = http.createServer(app);
httpServer = http.createServer(app);
}
const io = new Server(server);
const io = new Server(httpServer);
module.exports.io = io;
// Must be after io instantiation
@ -138,6 +159,7 @@ const databaseSocketHandler = require("./socket-handlers/database-socket-handler
const TwoFA = require("./2fa");
const StatusPage = require("./model/status_page");
const { cloudflaredSocketHandler, autoStart: cloudflaredAutoStart, stop: cloudflaredStop } = require("./socket-handlers/cloudflared-socket-handler");
const { proxySocketHandler } = require("./socket-handlers/proxy-socket-handler");
app.use(express.json());
@ -162,12 +184,6 @@ let totalClient = 0;
*/
let jwtSecret = null;
/**
* Main monitor list
* @type {{}}
*/
let monitorList = {};
/**
* Show Setup Page
* @type {boolean}
@ -190,8 +206,6 @@ try {
}
}
exports.entryPage = "dashboard";
(async () => {
Database.init(args);
await initDatabase(testMode);
@ -600,7 +614,7 @@ exports.entryPage = "dashboard";
await updateMonitorNotification(bean.id, notificationIDList);
await sendMonitorList(socket);
await server.sendMonitorList(socket);
await startMonitor(socket.userID, bean.id);
callback({
@ -629,7 +643,7 @@ exports.entryPage = "dashboard";
}
// Reset Prometheus labels
monitorList[monitor.id]?.prometheus()?.remove();
server.monitorList[monitor.id]?.prometheus()?.remove();
bean.name = monitor.name;
bean.type = monitor.type;
@ -663,7 +677,7 @@ exports.entryPage = "dashboard";
await restartMonitor(socket.userID, bean.id);
}
await sendMonitorList(socket);
await server.sendMonitorList(socket);
callback({
ok: true,
@ -683,7 +697,7 @@ exports.entryPage = "dashboard";
socket.on("getMonitorList", async (callback) => {
try {
checkLogin(socket);
await sendMonitorList(socket);
await server.sendMonitorList(socket);
callback({
ok: true,
});
@ -757,7 +771,7 @@ exports.entryPage = "dashboard";
try {
checkLogin(socket);
await startMonitor(socket.userID, monitorID);
await sendMonitorList(socket);
await server.sendMonitorList(socket);
callback({
ok: true,
@ -776,7 +790,7 @@ exports.entryPage = "dashboard";
try {
checkLogin(socket);
await pauseMonitor(socket.userID, monitorID);
await sendMonitorList(socket);
await server.sendMonitorList(socket);
callback({
ok: true,
@ -797,9 +811,9 @@ exports.entryPage = "dashboard";
console.log(`Delete Monitor: ${monitorID} User ID: ${socket.userID}`);
if (monitorID in monitorList) {
monitorList[monitorID].stop();
delete monitorList[monitorID];
if (monitorID in server.monitorList) {
server.monitorList[monitorID].stop();
delete server.monitorList[monitorID];
}
await R.exec("DELETE FROM monitor WHERE id = ? AND user_id = ? ", [
@ -812,7 +826,7 @@ exports.entryPage = "dashboard";
msg: "Deleted Successfully.",
});
await sendMonitorList(socket);
await server.sendMonitorList(socket);
// Clear heartbeat list on client
await sendImportantHeartbeatList(socket, monitorID, true, true);
@ -1112,52 +1126,6 @@ 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);
@ -1184,8 +1152,8 @@ exports.entryPage = "dashboard";
// If the import option is "overwrite" it'll clear most of the tables, except "settings" and "user"
if (importHandle == "overwrite") {
// Stops every monitor first, so it doesn't execute any heartbeat while importing
for (let id in monitorList) {
let monitor = monitorList[id];
for (let id in server.monitorList) {
let monitor = server.monitorList[id];
await monitor.stop();
}
await R.exec("DELETE FROM heartbeat");
@ -1348,7 +1316,7 @@ exports.entryPage = "dashboard";
}
await sendNotificationList(socket);
await sendMonitorList(socket);
await server.sendMonitorList(socket);
}
callback({
@ -1438,6 +1406,7 @@ exports.entryPage = "dashboard";
statusPageSocketHandler(socket);
cloudflaredSocketHandler(socket);
databaseSocketHandler(socket);
proxySocketHandler(socket);
debug("added all socket handlers");
@ -1458,12 +1427,12 @@ exports.entryPage = "dashboard";
console.log("Init the server");
server.once("error", async (err) => {
httpServer.once("error", async (err) => {
console.error("Cannot listen: " + err.message);
await shutdownFunction();
});
server.listen(port, hostname, () => {
httpServer.listen(port, hostname, () => {
if (hostname) {
console.log(`Listening on ${hostname}:${port}`);
} else {
@ -1510,17 +1479,11 @@ async function checkOwner(userID, monitorID) {
}
}
async function sendMonitorList(socket) {
let list = await getMonitorJSONList(socket.userID);
io.to(socket.userID).emit("monitorList", list);
return list;
}
async function afterLogin(socket, user) {
socket.userID = user.id;
socket.join(user.id);
let monitorList = await sendMonitorList(socket);
let monitorList = await server.sendMonitorList(socket);
sendNotificationList(socket);
sendProxyList(socket);
@ -1603,11 +1566,11 @@ async function startMonitor(userID, monitorID) {
monitorID,
]);
if (monitor.id in monitorList) {
monitorList[monitor.id].stop();
if (monitor.id in server.monitorList) {
server.monitorList[monitor.id].stop();
}
monitorList[monitor.id] = monitor;
server.monitorList[monitor.id] = monitor;
monitor.start(io);
}
@ -1615,19 +1578,6 @@ 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);
@ -1638,8 +1588,8 @@ async function pauseMonitor(userID, monitorID) {
userID,
]);
if (monitorID in monitorList) {
monitorList[monitorID].stop();
if (monitorID in server.monitorList) {
server.monitorList[monitorID].stop();
}
}
@ -1650,7 +1600,7 @@ async function startMonitors() {
let list = await R.find("monitor", " active = 1 ");
for (let monitor of list) {
monitorList[monitor.id] = monitor;
server.monitorList[monitor.id] = monitor;
}
for (let monitor of list) {
@ -1665,8 +1615,8 @@ async function shutdownFunction(signal) {
console.log("Called signal: " + signal);
console.log("Stopping all monitors");
for (let id in monitorList) {
let monitor = monitorList[id];
for (let id in server.monitorList) {
let monitor = server.monitorList[id];
monitor.stop();
}
await sleep(2000);
@ -1680,7 +1630,7 @@ function finalFunction() {
console.log("Graceful shutdown successful!");
}
gracefulShutdown(server, {
gracefulShutdown(httpServer, {
signals: "SIGINT SIGTERM",
timeout: 30000, // timeout: 30 secs
development: false, // not in dev mode

@ -0,0 +1,53 @@
const { checkLogin } = require("../util-server");
const { Proxy } = require("../proxy");
const { sendProxyList } = require("../client");
const server = require("../server");
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) {
await Proxy.reloadProxy();
await server.sendMonitorList(socket);
}
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 Proxy.reloadProxy();
callback({
ok: true,
msg: "Deleted",
});
} catch (e) {
callback({
ok: false,
msg: e.message,
});
}
});
};

@ -232,17 +232,18 @@
</button>
<!-- Proxies -->
<h2 class="mt-5 mb-2">{{ $t("Proxies") }}</h2>
<div v-if="monitor.type === 'http' || monitor.type === 'keyword'">
<h2 class="mt-5 mb-2">{{ $t("Proxy") }}</h2>
<p v-if="$root.proxyList.length === 0">
{{ $t("Not available, please setup.") }}
</p>
<div v-if="$root.proxyList.length > 0" class="form-check form-switch my-3">
<div v-if="$root.proxyList.length > 0" class="form-check my-3">
<input id="proxy-disable" v-model="monitor.proxyId" :value="null" name="proxy" class="form-check-input" type="radio">
<label class="form-check-label" for="proxy-disable">{{ $t("No Proxy") }}</label>
</div>
<div v-for="proxy in $root.proxyList" :key="proxy.id" class="form-check form-switch my-3">
<div v-for="proxy in $root.proxyList" :key="proxy.id" class="form-check my-3">
<input :id="`proxy-${proxy.id}`" v-model="monitor.proxyId" :value="proxy.id" name="proxy" class="form-check-input" type="radio">
<label class="form-check-label" :for="`proxy-${proxy.id}`">
@ -256,6 +257,7 @@
<button class="btn btn-primary me-2" type="button" @click="$refs.proxyDialog.show()">
{{ $t("Setup Proxy") }}
</button>
</div>
<!-- HTTP Options -->
<template v-if="monitor.type === 'http' || monitor.type === 'keyword' ">

Loading…
Cancel
Save