2021-08-09 07:34:44 +02:00
|
|
|
console.log("== Uptime Kuma Reset Password Tool ==");
|
|
|
|
|
|
|
|
const Database = require("../server/database");
|
|
|
|
const { R } = require("redbean-node");
|
|
|
|
const readline = require("readline");
|
|
|
|
const { initJWTSecret } = require("../server/util-server");
|
2022-04-18 09:21:58 +02:00
|
|
|
const User = require("../server/model/user");
|
2023-12-10 13:40:40 +01:00
|
|
|
const { io } = require("socket.io-client");
|
|
|
|
const { localWebSocketURL } = require("../server/config");
|
2021-09-20 10:29:18 +02:00
|
|
|
const args = require("args-parser")(process.argv);
|
2024-02-23 14:10:09 +01:00
|
|
|
|
2021-08-09 07:34:44 +02:00
|
|
|
const rl = readline.createInterface({
|
|
|
|
input: process.stdin,
|
|
|
|
output: process.stdout
|
|
|
|
});
|
|
|
|
|
2021-10-15 19:33:44 +02:00
|
|
|
const main = async () => {
|
2023-12-04 21:23:42 +01:00
|
|
|
if ("dry-run" in args) {
|
|
|
|
console.log("Dry run mode, no changes will be made.");
|
|
|
|
}
|
|
|
|
|
2022-04-07 18:56:56 +02:00
|
|
|
console.log("Connecting the database");
|
2021-08-09 07:34:44 +02:00
|
|
|
|
|
|
|
try {
|
2024-02-23 14:10:09 +01:00
|
|
|
Database.initDataDir(args);
|
|
|
|
await Database.connect(false, false, true);
|
2021-10-15 19:33:44 +02:00
|
|
|
// No need to actually reset the password for testing, just make sure no connection problem. It is ok for now.
|
|
|
|
if (!process.env.TEST_BACKEND) {
|
2021-10-16 07:02:04 +02:00
|
|
|
const user = await R.findOne("user");
|
2021-10-15 19:33:44 +02:00
|
|
|
if (! user) {
|
|
|
|
throw new Error("user not found, have you installed?");
|
|
|
|
}
|
2021-08-09 07:34:44 +02:00
|
|
|
|
2021-10-15 19:33:44 +02:00
|
|
|
console.log("Found user: " + user.username);
|
2021-08-09 07:34:44 +02:00
|
|
|
|
2021-10-15 19:33:44 +02:00
|
|
|
while (true) {
|
2023-12-04 21:23:42 +01:00
|
|
|
let password;
|
|
|
|
let confirmPassword;
|
2021-08-09 07:34:44 +02:00
|
|
|
|
2023-12-04 21:23:42 +01:00
|
|
|
// When called with "--new-password" argument for unattended modification (e.g. npm run reset-password -- --new_password=secret)
|
|
|
|
if ("new-password" in args) {
|
|
|
|
console.log("Using password from argument");
|
|
|
|
console.warn("\x1b[31m%s\x1b[0m", "Warning: the password might be stored, in plain text, in your shell's history");
|
|
|
|
password = confirmPassword = args["new-password"] + "";
|
|
|
|
} else {
|
|
|
|
password = await question("New Password: ");
|
|
|
|
confirmPassword = await question("Confirm New Password: ");
|
|
|
|
}
|
2021-08-09 07:34:44 +02:00
|
|
|
|
2023-12-04 21:23:42 +01:00
|
|
|
if (password === confirmPassword) {
|
|
|
|
if (!("dry-run" in args)) {
|
|
|
|
await User.resetPassword(user.id, password);
|
2021-08-09 07:34:44 +02:00
|
|
|
|
2023-12-04 21:23:42 +01:00
|
|
|
// Reset all sessions by reset jwt secret
|
|
|
|
await initJWTSecret();
|
2023-12-10 13:40:40 +01:00
|
|
|
|
2023-12-10 19:13:47 +01:00
|
|
|
// Disconnect all other socket clients of the user
|
|
|
|
await disconnectAllSocketClients(user.username, password);
|
2023-12-04 21:23:42 +01:00
|
|
|
}
|
2021-10-15 19:33:44 +02:00
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
console.log("Passwords do not match, please try again.");
|
|
|
|
}
|
2021-08-09 07:34:44 +02:00
|
|
|
}
|
2021-10-15 19:33:44 +02:00
|
|
|
console.log("Password reset successfully.");
|
2023-12-10 13:40:40 +01:00
|
|
|
|
2021-08-09 07:34:44 +02:00
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
console.error("Error: " + e.message);
|
|
|
|
}
|
|
|
|
|
|
|
|
await Database.close();
|
2021-10-15 19:33:44 +02:00
|
|
|
rl.close();
|
2021-08-09 07:34:44 +02:00
|
|
|
|
2021-10-15 19:33:44 +02:00
|
|
|
console.log("Finished.");
|
|
|
|
};
|
2021-08-09 07:34:44 +02:00
|
|
|
|
2023-01-05 20:57:28 +01:00
|
|
|
/**
|
|
|
|
* Ask question of user
|
|
|
|
* @param {string} question Question to ask
|
|
|
|
* @returns {Promise<string>} Users response
|
|
|
|
*/
|
2021-08-09 07:34:44 +02:00
|
|
|
function question(question) {
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
rl.question(question, (answer) => {
|
|
|
|
resolve(answer);
|
2021-10-15 19:33:44 +02:00
|
|
|
});
|
2021-08-09 07:34:44 +02:00
|
|
|
});
|
|
|
|
}
|
2021-10-15 19:33:44 +02:00
|
|
|
|
2023-12-10 19:36:08 +01:00
|
|
|
/**
|
|
|
|
* Disconnect all socket clients of the user
|
|
|
|
* @param {string} username Username
|
|
|
|
* @param {string} password Password
|
|
|
|
* @returns {Promise<void>} Promise
|
|
|
|
*/
|
2023-12-10 13:40:40 +01:00
|
|
|
function disconnectAllSocketClients(username, password) {
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
console.log("Connecting to " + localWebSocketURL + " to disconnect all other socket clients");
|
|
|
|
|
|
|
|
// Disconnect all socket connections
|
|
|
|
const socket = io(localWebSocketURL, {
|
|
|
|
reconnection: false,
|
|
|
|
timeout: 5000,
|
|
|
|
});
|
|
|
|
socket.on("connect", () => {
|
|
|
|
socket.emit("login", {
|
|
|
|
username,
|
|
|
|
password,
|
|
|
|
}, (res) => {
|
|
|
|
if (res.ok) {
|
|
|
|
console.log("Logged in.");
|
|
|
|
socket.emit("disconnectOtherSocketClients");
|
|
|
|
} else {
|
|
|
|
console.warn("Login failed.");
|
|
|
|
console.warn("Please restart the server to disconnect all sessions.");
|
|
|
|
}
|
|
|
|
socket.close();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
socket.on("connect_error", function () {
|
|
|
|
// The localWebSocketURL is not guaranteed to be working for some complicated Uptime Kuma setup
|
|
|
|
// Ask the user to restart the server manually
|
|
|
|
console.warn("Failed to connect to " + localWebSocketURL);
|
|
|
|
console.warn("Please restart the server to disconnect all sessions manually.");
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
socket.on("disconnect", () => {
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-10-15 19:33:44 +02:00
|
|
|
if (!process.env.TEST_BACKEND) {
|
|
|
|
main();
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
main,
|
|
|
|
};
|