mirror of
https://github.com/Litlyx/litlyx
synced 2025-12-10 07:48:37 +01:00
update security + chart events è anomaly service
This commit is contained in:
@@ -4,10 +4,9 @@ import { AnomalyVisitModel } from '@schema/anomalies/AnomalyVisitSchema';
|
||||
import { AnomalyEventsModel } from '@schema/anomalies/AnomalyEventsSchema';
|
||||
import { EventModel } from "@schema/metrics/EventSchema";
|
||||
import { VisitModel } from '@schema/metrics/VisitSchema'
|
||||
import EmailService from "@services/EmailService";
|
||||
|
||||
import * as url from 'url';
|
||||
import { ProjectModel } from "@schema/ProjectSchema";
|
||||
import { UserModel } from "@schema/UserSchema";
|
||||
import { getAggregation } from "./Aggregations";
|
||||
|
||||
type TAvgInput = { _id: string, count: number }
|
||||
@@ -112,7 +111,7 @@ export async function findAnomalies(project_id: string, callback: AnomalyCallbac
|
||||
visits: [],
|
||||
events: [],
|
||||
dns: [],
|
||||
pid: project_id
|
||||
pid: project_id,
|
||||
}
|
||||
|
||||
for (const visit of visitAnomalies) {
|
||||
@@ -136,19 +135,6 @@ export async function findAnomalies(project_id: string, callback: AnomalyCallbac
|
||||
report.dns.push(website);
|
||||
}
|
||||
|
||||
// const project = await ProjectModel.findById(pid);
|
||||
// if (!project) return { ok: false, error: 'Cannot find project with id ' + pid.toString() }
|
||||
// const user = await UserModel.findById(project.owner);
|
||||
// if (!user) return { ok: false, error: 'Cannot find user with id ' + project.owner.toString() }
|
||||
|
||||
// if (shouldSendMail.visitsEvents === true) {
|
||||
// await EmailService.sendAnomalyVisitsEventsEmail(user.email, project.name);
|
||||
// }
|
||||
// if (shouldSendMail.domains === true) {
|
||||
// await EmailService.sendAnomalyDomainEmail(user.email, project.name);
|
||||
// }
|
||||
|
||||
|
||||
callback(report);
|
||||
return report;
|
||||
|
||||
|
||||
@@ -1,18 +1,46 @@
|
||||
import { anomalyCheckAll, AnomalyReport } from "./AnomalyService";
|
||||
import { anomalyCheckAll, AnomalyReport, findAnomalies } from "./AnomalyService";
|
||||
import { connectDatabase } from '@services/DatabaseService'
|
||||
import { requireEnv } from '@utils/requireEnv'
|
||||
|
||||
import EmailService from "@services/EmailService";
|
||||
|
||||
import { ProjectModel } from "@schema/ProjectSchema";
|
||||
import { UserModel } from "@schema/UserSchema";
|
||||
|
||||
EmailService.init(requireEnv('BREVO_API_KEY'));
|
||||
connectDatabase(requireEnv('MONGO_CONNECTION_STRING'));
|
||||
|
||||
anomalyCheckAll(async report => {
|
||||
|
||||
import fs from 'fs';
|
||||
|
||||
const reports: AnomalyReport[] = [];
|
||||
|
||||
anomalyCheckAll(report => {
|
||||
if (report.visits.length > 0 || report.events.length > 0 || report.dns.length > 0) {
|
||||
reports.push(report);
|
||||
|
||||
const project = await ProjectModel.findById(report.pid);
|
||||
if (!project) return { ok: false, error: 'Cannot find project with id ' + report.pid.toString() }
|
||||
const user = await UserModel.findById(project.owner);
|
||||
if (!user) return { ok: false, error: 'Cannot find user with id ' + project.owner.toString() }
|
||||
|
||||
if (report.visits.length > 0 || report.events.length > 0) {
|
||||
|
||||
await EmailService.sendAnomalyVisitsEventsEmail(
|
||||
user.email,
|
||||
project.name,
|
||||
{ visits: report.visits, events: report.events }
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
if (report.visits.length > 0) {
|
||||
|
||||
await EmailService.sendAnomalyDomainEmail(
|
||||
user.email,
|
||||
project.name,
|
||||
report.dns
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}).then(e => {
|
||||
fs.writeFileSync('security-report.json', JSON.stringify(reports));
|
||||
|
||||
|
||||
});
|
||||
Reference in New Issue
Block a user