Skip to content

Commit

Permalink
Add insert target
Browse files Browse the repository at this point in the history
  • Loading branch information
kucingbasah737 committed Nov 25, 2023
1 parent c9e58c1 commit d5f6cb8
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 10 deletions.
61 changes: 51 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@ const { hideBin } = require('yargs/helpers');
// const logger = require('./lib/logger');
const sdNotify = require('./lib/sd-notify');

const runWebServer = async () => {
// eslint-disable-next-line global-require
const webserver = require('./lib/webserver');

webserver();

// logger.info(`${MODULE_NAME} 300582D4: Starting webserver`);
if (process.env.SYSTEMD_NOTIFY) {
sdNotify();
}
};

const updatePasswordCmd = async () => {
// eslint-disable-next-line global-require
const updatePassword = require('./lib/update-password');
Expand Down Expand Up @@ -40,33 +52,62 @@ const updatePasswordCmd = async () => {
process.exit(0);
}

// console.log(`Going to update "${promptResult.email}" password`);
try {
// eslint-disable-next-line no-unused-vars
const result = await updatePassword(null, promptResult.email, promptResult.newPassword);
await updatePassword(null, promptResult.email, promptResult.newPassword);
console.log(`Password for ${promptResult.email} updated`);
// console.log(JSON.stringify(result, null, 2));
} catch (e) {
console.warn('Exception');
}
process.exit(0);
};

const runWebServer = async () => {
const insertTargetCmd = async () => {
// eslint-disable-next-line global-require
const webserver = require('./lib/webserver');
const insertTarget = require('./lib/insert-target');

webserver();
prompt.start();
const promptResult = await prompt.get([
{
name: 'email',
message: 'User email',
required: true,
},
{
name: 'hostname',
message: 'Hostname',
required: true,
},
{
name: 'path',
message: 'Path',
required: true,
},
{
name: 'targetUrl',
message: 'Redirect to',
required: true,
},
]);

// logger.info(`${MODULE_NAME} 300582D4: Starting webserver`);
if (process.env.SYSTEMD_NOTIFY) {
sdNotify();
try {
const result = await insertTarget(
null,
promptResult.email,
promptResult.hostname,
promptResult.path,
promptResult.targetUrl,
);
console.log(JSON.stringify(result, null, 2));
} catch (e) {
console.warn('Exception');
}
process.exit(0);
};

// eslint-disable-next-line no-unused-vars
const { argv } = yargs(hideBin(process.argv))
.command('update-password', 'update user password', () => {}, updatePasswordCmd)
.command('insert-target', 'insert new target', () => {}, insertTargetCmd)
.command('serve', 'serve the world', () => {}, runWebServer)
.demandCommand()
.strict();
59 changes: 59 additions & 0 deletions lib/get-user-by-email.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
const MODULE_NAME = 'GET-USER-BY-EMAIL';

const TABLE_NAME = 'users';

const logger = require('./logger');
const mysql = require('./mysql');

/**
* @typedef User
* @type {object}
* @property {string} email
* @property {string} password
* @property {Date} created
* @property {Date} ts
* @property {number} disabled
* @property {number} super
*/

/**
*
* @param {string} xid
* @param {string} email
* @param {boolean} includeDisabled
* @returns {Promise<User>}
*/
module.exports = async (xid, email, includeDisabled) => {
const conditions = [];
const values = [TABLE_NAME];

conditions.push('email = ?');
values.push(email);

if (!includeDisabled) {
conditions.push('disabled = 0');
}

const query = `
SELECT * FROM ??
WHERE
${conditions.join(' AND ')}
LIMIT 1
`.trim();

try {
const [result] = await mysql.poolPromise.query(query, values);
return (result || [])[0];
} catch (e) {
const newE = new Error(`${MODULE_NAME} 2B93AED6: Exception`);

logger.error(newE.message, {
xid,
eCode: e.code,
eMessage: e.message,
query: mysql.formatSimplified(query, values),
});

throw newE;
}
};
54 changes: 54 additions & 0 deletions lib/insert-target.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const MODULE_NAME = 'INSERT-TARGET';

const TABLE_NAME = 'targets';

const logger = require('./logger');
const mysql = require('./mysql');

module.exports = async (xid, email, hostname, name, targetUrl) => {
const sanitizedName = (name || '')
.trim()
.replace(/^\/+/, '')
.replace(/\/$/g, '')
.trim();

if (!sanitizedName) {
return null;
}

let sanitizedTargetUrl = targetUrl.trim();
if (sanitizedTargetUrl.search(/^http(s)*:\/\//) < 0) {
sanitizedTargetUrl = `https://${sanitizedTargetUrl}`;
}

if (!sanitizedTargetUrl) {
return null;
}

const query = 'INSERT INTO ?? SET ?';
const values = [
TABLE_NAME,
{
name: sanitizedName,
hostname,
user_email: email,
target_url: sanitizedTargetUrl,
},
];

try {
const [result] = await mysql.poolPromise.query(query, values);
return result;
} catch (e) {
const newE = new Error(`${MODULE_NAME} 2340DA44: Exception`);

logger.warn(newE.message, {
xid,
eCode: e.code,
eMessage: e.message,
query: mysql.formatSimplified(query, values),
});

throw newE;
}
};

0 comments on commit d5f6cb8

Please sign in to comment.