Commit 5975cbdd authored by Admin's avatar Admin

ddd

parent 9a564b05
...@@ -2,8 +2,11 @@ ...@@ -2,8 +2,11 @@
const { exec } = require("child_process"); const { exec } = require("child_process");
const otplib = require("otplib"); const otplib = require("otplib");
// Expected length of the secret key // List of valid secret lengths
const expectedSecretLength = "ZGQ7DIXAENR5HPXONMYQJQPF2XVYM3OEUM4OBMBMDKRZ5DAVNYNPAISZTD76WML2".length; const validSecretLengths = [
"ZGQ7DIXAENR5HPXONMYQJQPF2XVYM3OEUM4OBMBMDKRZ5DAVNYNPAISZTD76WML2".length,
"QG5Z22ORKZC46SJRCCOPPEE5K475NHO7".length
];
// Store the last generated token and clipboard content // Store the last generated token and clipboard content
let lastToken = ""; let lastToken = "";
...@@ -29,27 +32,29 @@ function checkForNewCode(secret) { ...@@ -29,27 +32,29 @@ function checkForNewCode(secret) {
previousSecret = secret; previousSecret = secret;
} }
// Generate TOTP code using the current secret try {
const currentToken = otplib.authenticator.generate(secret); // Generate TOTP code using the current secret
const currentToken = otplib.authenticator.generate(secret);
// If the current token is different from the last token, log it // If the current token is different from the last token, log it
if (currentToken !== lastToken) { if (currentToken !== lastToken) {
console.log(`New 2FA code: ${currentToken}`); console.log(`New 2FA code: ${currentToken}`);
lastToken = currentToken; lastToken = currentToken;
}
} catch (error) {
console.error("Error generating TOTP code:", error.message);
} }
} }
// Run the clipboard read and TOTP generation every second, only on Windows // Run the clipboard read and TOTP generation every second, only on Windows
if (process.platform === "win32") { if (process.platform === "win32") {
setInterval(() => { setInterval(() => {
// Read the secret from clipboard and generate TOTP code if length matches
readClipboard((clipboardContent) => { readClipboard((clipboardContent) => {
if (clipboardContent.length === expectedSecretLength) { if (validSecretLengths.includes(clipboardContent.length)) {
checkForNewCode(clipboardContent); checkForNewCode(clipboardContent);
} else {
checkForNewCode(previousSecret);
} }
else
checkForNewCode(previousSecret)
}); });
}, 1000); }, 1000);
} else { } else {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment