Skip to content

Commit

Permalink
feat: Added check version and fixed small code (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
FajarKim authored Jan 28, 2024
1 parent 138d32d commit e54c124
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 34 deletions.
53 changes: 27 additions & 26 deletions bin/cli.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env node
const fs = require("fs");
const path = require("path");
const { execSync } = require("child_process");
const { program } = require("commander");
const {
encrypt, decrypt,
Expand All @@ -15,11 +16,13 @@ const {
const packageJsonPath = path.join(__dirname, "../package.json");
const packageJson = require(packageJsonPath);

const programName = String(process.argv.slice(1,2)).replace(/.+\//g, "");
const __program = String(process.argv.slice(1,2)).replace(/.+\//g, "");

process.stdout.write(execSync(`node ${__dirname}/../lib/check-version.js`, { encoding: "utf-8" }));

program
.version(`Node OpenSSL Enc ${packageJson.version} (Library: OpenSSL ${process.versions.openssl})`)
.description(`Node OpenSSL Enc (${programName}) is encryption and decryption data stdin or file with OpenSSL Ciphers`)
.description(`Node OpenSSL Enc (${__program}) is encryption and decryption data stdin or file with OpenSSL Ciphers`)
.option("-c, --cipher <cipher>", "select cipher for encrypt or decrypt")
.option("-f, --file <file>", "input path file for encrypt or decrypt")
.option("-p, --passkey <pass>", "input passphrase key")
Expand Down Expand Up @@ -50,21 +53,21 @@ program
var specialMode = "";
if (! special) {
if (! passkey || ! cipher) {
console.error(`${programName}: missing required operand`);
console.log(`Try '${programName} -h' for more information.`)
console.error(`${__program}: missing required operand`);
console.log(`Try '${__program} -h' for more information.`)
process.exit(1);
}
if (algorithmUnsupported.includes(cipher.toLowerCase())) {
console.error(`${programName}: the cipher '${cipher}' is not supported for this tool`);
console.log(`Try '${programName} -l' for list all cipher supported.`);
console.error(`${__program}: the cipher '${cipher}' is not supported for this tool`);
console.log(`Try '${__program} -l' for list all cipher supported.`);
process.exit(1);
} else if (! algorithmSupported.includes(cipher.toLowerCase())) {
console.error(`${programName}: '${cipher}' is not cipher`);
console.log(`Try '${programName} -l' for list all cipher supported.`);
console.error(`${__program}: '${cipher}' is not cipher`);
console.log(`Try '${__program} -l' for list all cipher supported.`);
process.exit(1);
}
} else if (! specialEnc.includes(special.toLowerCase())) {
console.error(`${programName}: '${special}' is not mode for special enc`)
console.error(`${__program}: '${special}' is not mode for special enc`)
process.exit(1);
} else {
var specialMode = special.toLowerCase();
Expand All @@ -89,7 +92,7 @@ program

if (out) {
fs.writeFileSync(out, decrypted);
console.info(`${programName}: Decryption completed`);
console.info(`${__program}: Decryption completed`);
console.log(`The result saved to '${out}'.`);
} else {
process.stdout.write(decrypted);
Expand All @@ -100,7 +103,7 @@ program

if (out) {
fs.writeFileSync(out, decrypted);
console.info(`${programName}: Decryption completed`);
console.info(`${__program}: Decryption completed`);
console.log(`The result saved to '${out}'.`);
} else {
process.stdout.write(decrypted);
Expand All @@ -115,7 +118,7 @@ program

if (out) {
fs.writeFileSync(out, decrypted);
console.info(`${programName}: Decryption completed`);
console.info(`${__program}: Decryption completed`);
console.log(`The result saved to '${out}'.`);
} else {
process.stdout.write(decrypted);
Expand All @@ -128,7 +131,7 @@ program

if (out) {
fs.writeFileSync(out, encrypted);
console.info(`${programName}: Encryption completed`);
console.info(`${__program}: Encryption completed`);
console.log(`The result saved to '${out}'.`);
} else {
process.stdout.write(encrypted);
Expand All @@ -139,7 +142,7 @@ program

if (out) {
fs.writeFileSync(out, encrypted);
console.info(`${programName}: Encryption completed`);
console.info(`${__program}: Encryption completed`);
console.log(`The result saved to '${out}'.`);
} else {
process.stdout.write(encrypted);
Expand All @@ -150,7 +153,7 @@ program

if (out) {
fs.writeFileSync(out, encrypted);
console.info(`${programName}: Encryption completed`);
console.info(`${__program}: Encryption completed`);
console.log(`The result saved to '${out}'.`);
} else {
process.stdout.write(encrypted);
Expand Down Expand Up @@ -259,17 +262,15 @@ program
}
}
} catch(error) {
console.error(`${programName}: ${error.message}`);
console.error(`${__program}: ${error.message}`);
const errorCode = typeof error.code === "number" ? error.code : 1;
process.exit(errorCode);
}
});

program.on("--help", () => {
console.log("");
console.log(`This tool licensed under ${packageJson.license} License, see <${packageJson.homepage.replace("#readme", "")}/tree/master/LICENSE>`)
console.log(`Report any bugs to <${packageJson.bugs.url}>`);
console.log(`Full documentation <${packageJson.homepage}>`);
});

program.parse(process.argv);
})
.on("--help", () => {
console.log("");
console.log(`This tool licensed under ${packageJson.license} License, see <${packageJson.homepage.replace("#readme", "")}/tree/master/LICENSE>`)
console.log(`Report any bugs to <${packageJson.bugs.url}>`);
console.log(`Full documentation <${packageJson.homepage}>`);
})
.parse(process.argv);
80 changes: 80 additions & 0 deletions lib/check-version.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
const path = require("path");
const https = require("https");
const net = require("net");
const packageJsonPath = path.join(__dirname, "../package.json");
const packageJson = require(packageJsonPath);

/**
* Checks the availability of internet connection.
*
* @returns {Promise<boolean>} - A promise that resolves to true if internet is available, false otherwise.
*/
function isInternetAvailable() {
return new Promise((resolve) => {
const socket = net.createConnection(80, "www.google.com");

socket.on("connect", () => {
socket.end();
resolve(true);
});

socket.on("error", () => {
resolve(false);
});
});
}

/**
* Fetches the new version from the provided package.json URL.
*
* @param {string} url - The URL of the package.json file.
* @returns {Promise<string>} - A promise that resolves to the version string.
*/
function getPackageJsonVersion() {
const url = "https://raw.githubusercontent.com/FajarKim/node-openssl-enc/master/package.json";
return isInternetAvailable().then((internetAvailable) => {
if (!internetAvailable) {
return;
}

return new Promise((resolve, reject) => {
https.get(url, (response) => {
let data = "";

// A chunk of data has been received.
response.on("data", (chunk) => {
data += chunk;
});

// The whole response has been received.
response.on("end", () => {
try {
const packageJson = JSON.parse(data);
const version = packageJson.version;

resolve(version);
} catch (error) {
reject(new Error("Error parsing JSON"));
}
});
}).on("error", (error) => {
reject(new Error(error.message));
});
});
});
}

getPackageJsonVersion()
.then((version) => {
if (version !== undefined && version !== packageJson.version) {
console.info(`You have a missed version: ${packageJson.version} -> ${version}`);
console.log(`Try 'npm -g install ${packageJson.name}@${version}' for update this tool`);
}
})
.catch((error) => {
console.error(error.message);
const errorCode = typeof error.code === "number" ? error.code : 1;
process.exit(errorCode);
});

module.exports = { isInternetAvailable, getPackageJsonVersion };
6 changes: 2 additions & 4 deletions lib/decrypt.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,9 @@ const {
function decrypt(algorithm, data, password, extra = null) {
var algorithm = algorithm.toLowerCase();
if (algorithmUnsupported.includes(algorithm)) {
console.error(`Error: \`${algorithm}' is not supported in this tool`);
process.exit(1)
throw Error(`'${algorithm}' is not supported in this tool`);
} else if (! algorithmSupported.includes(algorithm)) {
console.error(`Error: \`${algorithm}' is not cipher`);
process.exit(1)
throw Error(`'${algorithm}' is not cipher`);
}

/*****************************************************
Expand Down
6 changes: 2 additions & 4 deletions lib/encrypt.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,9 @@ const {
function encrypt(algorithm, data, password, extra = null) {
var algorithm = algorithm.toLowerCase();
if (algorithmUnsupported.includes(algorithm)) {
console.error(`Error: \`${algorithm}' is not supported in this tool`);
process.exit(1)
throw Error(`'${algorithm}' is not supported in this tool`);
} else if (! algorithmSupported.includes(algorithm)) {
console.error(`Error: \`${algorithm}' is not cipher`);
process.exit(1)
throw Error(`'${algorithm}' is not cipher`);
}

/*****************************************************
Expand Down

0 comments on commit e54c124

Please sign in to comment.