2023-11-30 08:11:00 +00:00
|
|
|
import { Database } from "../backend/database";
|
|
|
|
import { R } from "redbean-node";
|
|
|
|
import readline from "readline";
|
|
|
|
import { User } from "../backend/models/user";
|
|
|
|
import { DockgeServer } from "../backend/dockge-server";
|
|
|
|
import { log } from "../backend/log";
|
2023-12-10 13:16:23 +00:00
|
|
|
import { io } from "socket.io-client";
|
2023-12-25 20:12:44 +00:00
|
|
|
import { BaseRes } from "../common/util-common";
|
2024-03-10 22:18:50 +00:00
|
|
|
import { generatePasswordHash } from "../backend/password-hash";
|
2023-11-30 08:11:00 +00:00
|
|
|
|
|
|
|
console.log("== Dockge Reset Password Tool ==");
|
|
|
|
|
|
|
|
const rl = readline.createInterface({
|
|
|
|
input: process.stdin,
|
|
|
|
output: process.stdout
|
|
|
|
});
|
|
|
|
|
2023-12-10 13:16:23 +00:00
|
|
|
const server = new DockgeServer();
|
2023-11-30 08:11:00 +00:00
|
|
|
|
2023-12-10 13:16:23 +00:00
|
|
|
export const main = async () => {
|
2023-11-30 08:11:00 +00:00
|
|
|
// Check if
|
|
|
|
console.log("Connecting the database");
|
|
|
|
try {
|
|
|
|
await Database.init(server);
|
|
|
|
} catch (e) {
|
|
|
|
if (e instanceof Error) {
|
|
|
|
log.error("server", "Failed to connect to your database: " + e.message);
|
|
|
|
}
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2024-03-10 22:34:03 +00:00
|
|
|
let user ;
|
2023-11-30 08:11:00 +00:00
|
|
|
// No need to actually reset the password for testing, just make sure no connection problem. It is ok for now.
|
2024-03-10 22:41:19 +00:00
|
|
|
|
2023-11-30 08:11:00 +00:00
|
|
|
if (!process.env.TEST_BACKEND) {
|
2024-03-10 22:18:50 +00:00
|
|
|
user = await R.findOne("user");
|
2024-03-10 22:41:19 +00:00
|
|
|
|
|
|
|
if (! user ) {
|
2024-03-10 22:34:03 +00:00
|
|
|
if ( !process.env.USER ) {
|
|
|
|
throw new Error("user not found or provided, have you installed? Try to set USER and PASSWORD variables ...");
|
|
|
|
} else {
|
|
|
|
console.log("Trying to initialise user : " + process.env.USER);
|
|
|
|
user = R.dispense("user");
|
2024-03-10 22:41:19 +00:00
|
|
|
user.username = process.env.USER;
|
2024-03-10 22:34:03 +00:00
|
|
|
user.password = generatePasswordHash(process.env.PASSWORD);
|
2024-03-10 22:41:19 +00:00
|
|
|
await R.store(user);
|
2024-03-10 22:34:03 +00:00
|
|
|
console.log("User/Password set successfully");
|
2024-03-10 22:41:19 +00:00
|
|
|
|
2024-03-10 22:34:03 +00:00
|
|
|
// Reset all sessions by reset jwt secret
|
|
|
|
await server.initJWTSecret();
|
|
|
|
console.log("JWT reset successfully.");
|
|
|
|
|
|
|
|
// Disconnect all other socket clients of the user
|
|
|
|
await disconnectAllSocketClients(user.username, user.password);
|
|
|
|
console.log("You may have to restart");
|
|
|
|
exit;
|
|
|
|
}
|
2023-11-30 08:11:00 +00:00
|
|
|
}
|
2024-03-10 22:41:19 +00:00
|
|
|
}
|
2023-11-30 08:11:00 +00:00
|
|
|
|
2024-03-10 22:41:19 +00:00
|
|
|
let password = "";
|
|
|
|
let confirmPassword = " ";
|
2024-02-11 22:11:39 +00:00
|
|
|
|
2024-03-10 22:41:19 +00:00
|
|
|
while (true) {
|
2023-11-30 08:11:00 +00:00
|
|
|
|
2024-03-10 22:41:19 +00:00
|
|
|
if (process.env.PASSWORD) {
|
|
|
|
console.log("Found password : " + process.env.PASSWORD) ;
|
|
|
|
password = process.env.PASSWORD ;
|
|
|
|
confirmPassword = process.env.PASSWORD ;
|
2024-03-10 22:49:19 +00:00
|
|
|
} else {
|
2024-03-10 22:41:19 +00:00
|
|
|
console.log("No found password: " ) ;
|
|
|
|
password = await question("New Password: ");
|
|
|
|
confirmPassword = await question("Confirm New Password: ");
|
|
|
|
}
|
2024-03-10 22:34:03 +00:00
|
|
|
|
2024-03-10 22:41:19 +00:00
|
|
|
if (password === confirmPassword) {
|
|
|
|
await User.resetPassword(user.id, password);
|
|
|
|
console.log("Password reset successfully.");
|
2024-03-10 22:34:03 +00:00
|
|
|
|
2024-03-10 22:41:19 +00:00
|
|
|
// Reset all sessions by reset jwt secret
|
|
|
|
await server.initJWTSecret();
|
2024-03-10 22:34:03 +00:00
|
|
|
|
2024-03-10 22:41:19 +00:00
|
|
|
console.log("JWT reset successfully.");
|
2024-03-10 22:34:03 +00:00
|
|
|
|
2024-03-10 22:41:19 +00:00
|
|
|
// Disconnect all other socket clients of the user
|
|
|
|
await disconnectAllSocketClients(user.username, password);
|
2024-03-10 22:34:03 +00:00
|
|
|
|
2024-03-10 22:41:19 +00:00
|
|
|
} else {
|
|
|
|
console.log("Passwords do not match, please try again.");
|
|
|
|
break;
|
2024-03-10 22:34:03 +00:00
|
|
|
}
|
2024-03-10 22:41:19 +00:00
|
|
|
break;
|
2024-03-10 22:49:19 +00:00
|
|
|
}
|
2024-03-10 22:41:19 +00:00
|
|
|
} catch (e) {
|
|
|
|
if (e instanceof Error) {
|
|
|
|
console.error("Error: " + e.message);
|
2024-03-10 22:34:03 +00:00
|
|
|
}
|
2024-03-10 22:41:19 +00:00
|
|
|
}
|
2024-03-10 22:49:19 +00:00
|
|
|
|
2023-11-30 08:11:00 +00:00
|
|
|
await Database.close();
|
|
|
|
rl.close();
|
|
|
|
|
|
|
|
console.log("Finished.");
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Ask question of user
|
|
|
|
* @param question Question to ask
|
|
|
|
* @returns Users response
|
|
|
|
*/
|
|
|
|
function question(question : string) : Promise<string> {
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
rl.question(question, (answer) => {
|
|
|
|
resolve(answer);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-12-10 13:16:23 +00:00
|
|
|
function disconnectAllSocketClients(username : string, password : string) : Promise<void> {
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
const url = server.getLocalWebSocketURL();
|
|
|
|
|
|
|
|
console.log("Connecting to " + url + " to disconnect all other socket clients");
|
|
|
|
|
|
|
|
// Disconnect all socket connections
|
|
|
|
const socket = io(url, {
|
|
|
|
reconnection: false,
|
|
|
|
timeout: 5000,
|
|
|
|
});
|
|
|
|
socket.on("connect", () => {
|
|
|
|
socket.emit("login", {
|
2024-03-10 22:49:19 +00:00
|
|
|
username,
|
|
|
|
password,
|
2023-12-10 13:16:23 +00:00
|
|
|
}, (res : BaseRes) => {
|
2024-03-10 22:49:19 +00:00
|
|
|
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();
|
2023-12-10 13:16:23 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
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 " + url);
|
|
|
|
console.warn("Please restart the server to disconnect all sessions manually.");
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
socket.on("disconnect", () => {
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-11-30 08:11:00 +00:00
|
|
|
if (!process.env.TEST_BACKEND) {
|
|
|
|
main();
|
|
|
|
}
|
2024-03-10 22:18:50 +00:00
|
|
|
|