added logging feature
This commit is contained in:
@@ -21,7 +21,7 @@ export interface genReturn{
|
||||
|
||||
export function generateToken(config: any,dataSafe: SecureVault): Promise<genReturn>{
|
||||
return new Promise<genReturn>((resolve,error) => {
|
||||
parseMails(config).then(res => {
|
||||
parseMails(config, dataSafe).then(res => {
|
||||
generateCodes(resolve,error,res,config,dataSafe);
|
||||
})
|
||||
});
|
||||
@@ -91,6 +91,7 @@ async function sendMails(resolve: (value?: genReturn) => void,error: (reason?: a
|
||||
if (config.force){dataSafe.clearVault();}
|
||||
for(let i = 0; i < mailArray.length; i++){
|
||||
// send mail
|
||||
dataSafe.writeTransaction(`process: ${mailArray[i].mail}`);
|
||||
if (!config.dryrun){
|
||||
dataSafe.pushData({
|
||||
name: mailArray[i].name,
|
||||
@@ -98,7 +99,7 @@ async function sendMails(resolve: (value?: genReturn) => void,error: (reason?: a
|
||||
code: codeArray[i]
|
||||
})
|
||||
}
|
||||
await send(mailArray[i].name, mailArray[i].mail, codeArray[i],template,mailserver,config);
|
||||
await send(mailArray[i].name, mailArray[i].mail, codeArray[i],template,mailserver,config,dataSafe);
|
||||
position ++;
|
||||
pbar.update(position);
|
||||
}
|
||||
@@ -114,7 +115,7 @@ async function sendMails(resolve: (value?: genReturn) => void,error: (reason?: a
|
||||
|
||||
}
|
||||
|
||||
async function send(name: string, mail: string, code: string,template: HandlebarsTemplateDelegate<any>,mailserver: Mail,config: any){
|
||||
async function send(name: string, mail: string, code: string,template: HandlebarsTemplateDelegate<any>,mailserver: Mail,config: any,dataSafe: SecureVault){
|
||||
if (config.dryrun){
|
||||
await delay(100);
|
||||
console.log(`\n\x1b[36m -> dryrun: would send to ${mail}\x1b[0m`);
|
||||
@@ -133,8 +134,10 @@ async function send(name: string, mail: string, code: string,template: Handlebar
|
||||
};
|
||||
try {
|
||||
await mailserver.sendMail(mailOptions);
|
||||
dataSafe.writeTransaction(` -> mail sent`);
|
||||
} catch (error) {
|
||||
console.log(`Error sendign mail to ${mail} : ${error}`)
|
||||
dataSafe.writeTransaction(` -> mail failed : ${error}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,13 +1,12 @@
|
||||
import { rejects } from "assert";
|
||||
import * as fs from 'fs'
|
||||
import { Console } from "console";
|
||||
import { SecureVault } from "./vault";
|
||||
|
||||
export interface MLItem{
|
||||
mail: string;
|
||||
name: string;
|
||||
}
|
||||
|
||||
export function parseMails(config: any) {
|
||||
export function parseMails(config: any, dataSafe: SecureVault) {
|
||||
return new Promise<MLItem[]>((resolve,reject) => {
|
||||
let mailArray: MLItem[] = [];
|
||||
let currSection: string = "global";
|
||||
@@ -37,6 +36,7 @@ export function parseMails(config: any) {
|
||||
const ix = line.indexOf(";")
|
||||
if (ix !== -1){
|
||||
// check if already exist
|
||||
dataSafe.writeTransaction(`reading mail ${line.substr(0,ix)} from category ${currSection}`);
|
||||
if (config.force || config.usedMails.filter((el: MLItem) => el.mail == line.substr(0,ix)).length == 0){
|
||||
mailArray.push({
|
||||
mail: line.substr(0,ix),
|
||||
@@ -44,6 +44,7 @@ export function parseMails(config: any) {
|
||||
})
|
||||
curCounter ++;
|
||||
}else{
|
||||
dataSafe.writeTransaction(` -> already exists. Skipping`);
|
||||
console.error(`Skipping ${line.substr(0,ix)}: Already sent`)
|
||||
}
|
||||
}else{
|
||||
|
||||
11
src/vault.ts
11
src/vault.ts
@@ -59,15 +59,22 @@ export class SecureVault {
|
||||
}
|
||||
var asym_encrypted = crypto.publicEncrypt(this.safe.publicKey, buffer);
|
||||
const u = uuid.v4()
|
||||
this.safe.items.push({
|
||||
|
||||
const item = {
|
||||
u,
|
||||
d: encrypted.toString('hex'),
|
||||
k: asym_encrypted.toString("base64"),
|
||||
iv: iv.toString('hex')
|
||||
})
|
||||
}
|
||||
this.writeTransaction("push: " + JSON.stringify(item))
|
||||
this.safe.items.push(item)
|
||||
return u;
|
||||
}
|
||||
|
||||
writeTransaction(payload: string){
|
||||
fs.appendFileSync('vault.log', `${payload}\n`);
|
||||
}
|
||||
|
||||
async saveData(path: string): Promise<void>{
|
||||
fs.writeFileSync(path, JSON.stringify({
|
||||
version: vaultVersion,
|
||||
|
||||
Reference in New Issue
Block a user