-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
200 lines (156 loc) · 6.74 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
(async()=>{
"use strict";
require("dotenv").config()
// Dependencies
const simpleAES256 = require("simple-aes-256")
const { MongoClient } = require("mongodb")
const request = require("request-async")
const bottleNeck = require("bottleneck")
const express = require("express")
const moment = require("moment")
const groom = require("groom")
const _ = require("lodash")
const hqc = require("hqc")
// Variables
var emot = {
emails: [],
masterKey: process.env.MASTER_KEY || "aW:FLAW{L{F:51290512wkOWQRJy5892wofqrji9`841ji",
adminKey: process.env.ADMIN_KEY || "FAfafwaklfjaf905215151;[5;51515211-1-521"
}
const web = express()
const port = process.env.PORT || 8080
const threads = new bottleNeck.default({ maxConcurrent: 20 })
const keyPair = await hqc.keyPair()
const client = new MongoClient(process.env.MONGODB_URL)
const database = client.db("core")
const emails = database.collection("emot.emails")
// Functions
async function check(email){
const emailIndex = _.findIndex(emot.emails, { email: email })
var response = await request(`https://cspi-pa1.vercel.app/check/email?e=${email}`)
response = JSON.parse(response.body)
if(response.status === "success"){
const emailSources = _.find(emot.emails, { email: email }).breaches
for( const source of response.data ) if(!emailSources.includes(source.name)) emot.emails[emailIndex].breaches.push(source.name)
}
email = emot.emails[emailIndex]
await emails.updateOne({ email: email.encryptedEmail }, { $set: { breaches: simpleAES256.encrypt(emot.masterKey, JSON.stringify(email.breaches)).toString("hex") } })
}
function syncEmails(){
return new Promise(async(resolve)=>{
var list = await emails.find({}, { projection: { _id: 0 } }).toArray()
if(!list.length) return resolve()
for( const email in list ){
list[email].encryptedEmail = list[email].email
list[email].email = simpleAES256.decrypt(emot.masterKey, Buffer.from(list[email].email, "hex")).toString()
list[email].breaches = JSON.parse(simpleAES256.decrypt(emot.masterKey, Buffer.from(list[email].breaches, "hex")).toString())
}
emot.emails = list
resolve()
})
}
const checkEmails = ()=>{if(emot.emails.length) for( const email of emot.emails ) threads.schedule(check, email.email)}
function getSecret(cyphertext){
return new Promise(async(resolve)=>{
resolve(await hqc.decrypt(Uint8Array.from(cyphertext.split(",").map(x=>parseInt(x,10))), keyPair.privateKey))
})
}
/// Configurations
//* Express
web.use(express.json())
// Main
console.log("Connecting to the database, please wait...")
await client.connect()
console.log("Successfully connected to the database.")
web.get("/pk", (req, res)=>{ res.json({ data: keyPair.publicKey.toString() })})
web.use((err, req, res, next)=>{
if(err.message === "Bad request") return res.json({
status: "failed",
message: "Bad request."
})
next()
})
web.use(async(req, res, next)=>{
try{
if(!req.body.hasOwnProperty("cyphertext") && !req.body.hasOwnProperty("data")) return res.json({
status: "failed",
message: "Invalid admin key."
})
const cyphertext = req.body.cyphertext
const clientSecret = await getSecret(req.body.cyphertext)
req.body = JSON.parse(simpleAES256.decrypt(clientSecret, Buffer.from(req.body.data, "hex")).toString())
req.body.cyphertext = cyphertext
if(!req.body.adminKey || req.body.adminKey !== emot.adminKey) return res.json({
status: "failed",
message: "Invalid admin key."
})
next()
}catch{
res.json({
status: "failed",
message: "Invalid admin key."
})
}
})
web.post("/list", async(req, res)=>{
const clientSecret = await getSecret(req.body.cyphertext)
const cleanList = []
for( const email of emot.emails ) cleanList.push({ email: email.email, breaches: email.breaches, createdDate: email.createdDate })
res.json({
status: "success",
data: simpleAES256.encrypt(clientSecret, JSON.stringify(cleanList)).toString("hex")
})
})
web.post("/add", async(req, res)=>{
const body = req.body
if(!body.email || !body.email.match(/([\w\.\-_]+)?\w+@[\w-_]+(\.\w+){1,}/)) return res.json({
status: "failed",
message: "Invalid email."
})
const email = _.find(emot.emails, { email: body.email })
if(email) return res.json({
status: "failed",
message: "The specified email already exists."
})
var data = { email: simpleAES256.encrypt(emot.masterKey, body.email).toString("hex"), breaches: simpleAES256.encrypt(emot.masterKey, JSON.stringify([])).toString("hex"), createdDate: moment().format("MMMM Do YYYY, h:mm:ss a") }
//* Adding process
await emails.insertOne(data)
data.encryptedEmail = data.email
data.email = body.email
data.breaches = JSON.parse(simpleAES256.decrypt(emot.masterKey, Buffer.from(data.breaches, "hex")).toString())
emot.emails.push(data)
res.json({
status: "success",
data: true
})
})
web.delete("/delete", async(req, res)=>{
const body = req.body
if(!body.email || !body.email.match(/([\w\.\-_]+)?\w+@[\w-_]+(\.\w+){1,}/)) return res.json({
status: "failed",
message: "Invalid email."
})
const email = _.find(emot.emails, { email: body.email })
if(!email) return res.json({
status: "failed",
message: "The specified email does not exists."
})
//* Delete process
await emails.deleteOne({ email: _.find(emot.emails, { email: body.email }).encryptedEmail })
delete emot.emails[_.findIndex(emot.emails, { email: body.email })]
emot.emails = groom(emot.emails)
res.json({
status: "success",
data: true
})
})
web.listen(port, async()=>{
console.log(`Server is running. Port: ${port}`)
await syncEmails()
checkEmails()
setInterval(async()=>{
await syncEmails()
checkEmails()
}, 1800000) // Sync & Check for breaches every 30 minutes
})
})()