-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
560 lines (490 loc) · 23.2 KB
/
app.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
const sgMail = require('@sendgrid/mail')
const express = require("express")
const bodyParser = require("body-parser")
const cors = require("cors")
const app = express()
const dotenv = require('dotenv')
const rateLimit = require("express-rate-limit")
const bearerToken = require('express-bearer-token')
const CryptoJS = require("crypto-js")
const port = process.env.PORT || 4000
const queries = require("./queries")
//enable env
dotenv.config()
sgMail.setApiKey(process.env.SGKEY)
//body parser
app.use(bodyParser.json())
//cors options
var whitelist = ['http://localhost:8000', 'https://arthuranteater.com']
var siteCors = {
allowedHeaders: ['Content-Type', 'Authorization'],
origin: function (origin, callback) {
if (whitelist.indexOf(origin) !== -1) {
callback(null, true)
} else {
console.log('origin', origin)
callback(new Error('Not allowed by CORS'))
}
},
optionsSuccessStatus: 200,
preflightContinue: true,
credentials: true
}
var adminCors = {
allowedHeaders: ['Content-Type', 'Authorization'],
optionsSuccessStatus: 200,
preflightContinue: true,
credentials: true
}
//enabling cors
app.options('/site/*', cors(siteCors))
app.use('/site/*', cors(siteCors))
app.options('/', cors(adminCors))
app.use('/', cors(adminCors))
//caching in stores
let passStore = []
let proxyStore = []
let subStore = []
let elist = []
delHash = (hash, store) => {
store = store.filter(e => e !== hash)
}
createHash = (word, store) => {
const hash = CryptoJS.HmacSHA256(word, process.env.SECRET)
const b64 = CryptoJS.enc.Base64.stringify(hash)
store.push(b64)
setTimeout(delHash, 600000, b64, store)
}
const encrypt = email => {
return new Promise(resolve => {
const cipher = CryptoJS.AES.encrypt(email, process.env.DB_ENCRYPT).toString()
resolve(cipher)
})
}
const addSubToStore = (sub, id) => {
return new Promise(resolve => {
encrypt(sub.Email).then(n64 => {
console.log('n64', n64)
sub.Email = n64
sub.Passcode = id
subStore.push(sub)
console.log('subStoreinadd', subStore)
setTimeout(delHash, 600000, sub, subStore)
resolve(sub)
})
})
}
const decrypt = cipher => {
const bytes = CryptoJS.AES.decrypt(cipher, process.env.DB_ENCRYPT)
return bytes.toString(CryptoJS.enc.Utf8)
}
const addToElist = (cipher, name) => {
return new Promise(resolve => {
const bytes = CryptoJS.AES.decrypt(cipher, process.env.DB_ENCRYPT)
const email = bytes.toString(CryptoJS.enc.Utf8)
//elist.push(`${name} <${email}>`)
resolve(email)
})
}
//test decrypt
// addToElist('U2FsdGVkX1+nDGEXZWjTulG8o2Q7fZJKqxXY6cur4SD7CP30lTM1livJu182iiGs').then(email => {
// console.log('email1', email)
// encrypt(email).then(n64 => {
// console.log('n64', n64)
// addToElist(n64).then(email => {
// console.log('email2', email)
// })
// })
// })
const createPass = new Promise(resolve => {
let pass = ''
var val = process.env.VALUES
for (let i = 0; i < 10; i++) {
pass += val.charAt(Math.floor(Math.random() * val.length))
}
resolve(pass)
})
const findSubInStore = id => {
return new Promise(resolve => {
const subArr = subStore.filter(e => e.Passcode == id)
const fsub = subArr[0]
resolve(fsub)
})
}
//auth tokens
app.use(bearerToken())
const proxyToken = function (req, res, nxt) {
if (req.token) {
createHash(req.body.Email, proxyStore)
if (proxyStore.includes(req.token)) {
delHash(req.token, proxyStore)
nxt()
} else {
res.status(200).json({ Response: "No matching ID, please try again" })
console.error('no match in proxyStore:', proxyStore)
}
} else {
res.status(200).json({ Response: "No access" })
console.error('no proxy token found')
}
}
const passToken = function (req, res, nxt) {
if (req.token) {
if (passStore.includes(req.token)) {
delHash(req.token, passStore)
nxt()
} else {
res.status(200).json({ Response: "No matching ID, please try again" })
console.error('no match in passStore:', passStore, 'req.token', req.token)
}
} else {
res.status(200).json({ Response: "No access" })
console.error('no pass token found')
}
}
const adminToken = function (req, res, nxt) {
if (req.token) {
if (req.token === process.env.ADMIN) {
nxt()
} else {
res.status(200).json({ Response: 'No access' })
console.error('no match with token:', req.token)
}
} else {
res.status(200).json({ Response: 'No access' })
console.error('no admin token found')
}
}
//rate limiter
app.enable("trust proxy")
const limiter = rateLimit({
windowMs: 10 * 60 * 1000,
max: 8,
handler: function (req, res, nxt) {
res.status(200).json({ Response: 'No access, too many requests, please try again later' })
console.log('hit req limit')
}
})
const plimiter = rateLimit({
windowMs: 10 * 60 * 1000,
max: 1,
handler: function (req, res, nxt) {
res.status(200).json({ Response: 'No access, too many requests, please try again later' })
console.log('hit req limit')
}
})
//local server
app.listen(port, (req, res) => {
console.log(`listening on ${port}`);
})
//base64 encoder
// var fs = require('fs')
// var imageAsBase64 = fs.readFileSync('./your-image.png', 'base64')
// fs.readFileSync(path[, options])
// Synchronous version of fs.readFile(). Returns the contents of the path.
// If the encoding option is specified then this function returns a string. Otherwise it returns a buffer.
//logo src
const logo = {
base64: "iVBORw0KGgoAAAANSUhEUgAAAVQAAAEqCAMAAAB9ZFg2AAACQFBMVEUAAAD///8pKSkjIyMbGxsUFBQDAwMpPmAqPFoMDAw0NDR8bmWmgWfJjWTjklvtmF/0mVvLil6sgGKCcWS5hGDEiF6pfV+cemPWk2T2nmLdkV2ifGLtnmfUjV2LdGSVeWXBhl7EjWbqll27hF4MBgV0a2Xkmmb4nl/+p2nvoGjyoGf7o2XyoWjtn2izhGOrfmH4n2GSdmK8imbZlmbMkWdtV1Tjm2jFjmhmZmZpZ2aAcmhqampSUlJKSko4SFJdVVRcXFx2dna3t7jb29ve4ufDw8OEhIS3uLiysrLV1dXLy8uSkpKkpKR7e3vEycuV1P9iwf9Uu/+b1/+srKxfuPPS4+5bvv9luO9Ur+xUt/jb5exitexUse5UtPOJyPKMjIyDw+1rxP+31+zC3e5zve16yv/O6//N4O2bm5tPo9xHbYai2v9OntNOms01OTy7u7tNkL02PUM4NTTN0tlSpdzH6f9CcpI7OztJh7DV7v5JjbpEb4u34v85QUY+WWvD5fym0Oyw3/5QntG85P+s0+5RqeJJgKXI2uZJibLS5/VTm8tjvfiOxuzA3/M8V2nR1dzJ3+1tosaCzP1DZ39Ggqpzv/FERERXU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Y2b/cQCJXS/8bNm+j+LgZ3isBIU0FsyZZqPKl6SfLy8uv1Pnyakm16f1vNkNL/YFJBauSUNWXqfXlTz/9q2ZqN/fTLPWDqJgKZSWhaqh+dmH5b3UtTu36+Viz+XlkSIWqDpBsn42r9drfLzSXl+pZkg6oqs1uyhcXLjQ/jJCpSNYB+qQ0yayW6p80m81/nKGkDbsdqo8uXAhhnoJ2lKlKJRv/bDaXX1bt41RRvmxeCL6XCq0ojTXF1vvE8l8kVP9NclZb08/JwS+iRiqQNZlmL0xpqJ8RuP+M2T/jR6apryJIKk5VSfIx4L8fdw3H95tBt/2g7QBYlWQylUp+TejSdvvQ58iB1yJKKpSVlrfSRK9zK+or8ug70SUVzcpUz1uD9V0ynjYDfMs3tBc864dUsHm+Fa0f/a8ZyBTaCgcrnZOaF5pjH7z35tevM+IAEz+0ExrWd9NNa0n7faUPtBEq1jfMrGm/T0xB+6Bj/eLrL7nouY8uLlKxrMEKtEv0XKFFIsgKrRE9V2iHCLJCG0TPFbr10XOFbnf0XKFbHD1X6LZGDha6jZGDhW5b5GCh24SjXPIUVS55iiuXNIWWvqX8P5XsvP5IhBBpAAAAJXRFWHRkYXRlOmNyZWF0ZQAyMDE5LTAyLTAxVDIwOjA1OjQzLTA1OjAwXayCzgAAACV0RVh0ZGF0ZTptb2RpZnkAMjAxOS0wMi0wMVQyMDowNTo0My0wNTowMCzxOnIAAAAASUVORK5CYII="
}
//today's date
let today
const getDate = () => {
today = new Date
let dd = today.getDate()
let mm = today.getMonth() + 1
const yyyy = today.getFullYear()
if (dd < 10) {
dd = '0' + dd;
}
if (mm < 10) {
mm = '0' + mm;
}
today = yyyy + '-' + mm + '-' + dd
}
//all subs
app.get(`/${process.env.ALLSUB}/`, adminToken, (req, res) => {
console.log('received all subs req')
queries.listSubs().then(data => {
res.json({ data })
console.log('sent all subs')
})
})
//subs by cat
app.get(`/${process.env.GETSUB}/:id`, adminToken, (req, res) => {
console.log('received subs by cat req')
queries.getByCat(req.params.id).then(data => {
res.json({ data })
console.log('sent subs by cat')
})
})
//all posts
app.get(`/${process.env.ALLPOST}/`, adminToken, (req, res) => {
console.log('received all posts req')
queries.listPosts().then(data => {
res.json({ data })
console.log('sent all posts')
})
})
//all errs
app.get(`/${process.env.ALLERR}/`, adminToken, (req, res) => {
console.log('received all errs req')
queries.listErrs().then(data => {
res.json({ data })
console.log('sent all errs')
})
})
//errs by email
app.get(`/${process.env.GETERRS}/:id`, adminToken, (req, res) => {
console.log('received errs by email req')
let email = req.params.id
queries.getErrsByEmail(email).then(data => {
res.json({ data })
console.log('sent errs by email')
})
})
//welcome email
app.post(`/site/${process.env.WELCOME}/`, proxyToken, limiter, (req, res) => {
console.log('received welcome email req')
getDate()
const welSub = req.body
createPass.then(pass => {
console.log('pass', pass)
console.log('req.body', req.body)
createHash(pass, passStore)
addSubToStore(welSub, pass)
console.log('subStore', subStore)
const type = 'welcome'
const name = welSub.Name
const email = welSub.Email
const welPkg = {
to: {
name: name,
email: email,
},
from: {
name: 'arthuranteater',
email: '[email protected]'
},
subject: `Welcome to arthuranteater, ${name}`,
text: 'Welcome to arthuranteater!',
html: `<img src="cid:logo" width="80" height="80"><h2>Welcome to arthuranteater, ${name}!</h2><h3><strong>Sharing projects, coding challenges, new tech, and best practices</strong></h3>
<p><strong>You have selected to receive alerts for the categories: ${welSub.Categories}. If our email went into to spam, please mark it as not spam and add us to your contacts.</strong></p>
<p><strong>Copy the Subscriber ID and paste into the <a href="https://arthuranteater.com/subscribe" target="_blank">subscribe page</a>.</strong></p>
<h2><strong>Subscriber ID: ${pass}</strong></h2>
<div><a href="https://huntcodes.co/#contact" target="_blank">Contact Us</a><span></div>`,
attachments: [
{
content: logo.base64,
filename: 'logo.png',
type: 'image/png',
disposition: 'inline',
content_id: 'logo'
},
]
}
sgMail.send(welPkg, (err, sgres) => {
if (err) {
const mes = err.toString()
res.json({ Response: `No email sent, ${mes}` })
console.error('Send Grid Error:', mes)
console.log(`Send Grid Response: ${sgres}`)
const errPkg = {
Email: email, Name: name, Message: mes, Type: type, EDate: today
}
queries.addErr(errPkg).then(() => {
console.log(`${errPkg} added to log`)
}).catch(err => {
console.error('addErr err:', err)
})
}
else {
res.json({ Response: email })
console.log(`sent welcome email to ${email}`)
}
})
}).catch(err => {
res.json({ Response: "No pass, please try again" })
console.error('createPass:', err)
})
})
//add sub
app.post(`/site/${process.env.ADDSUB}/`, passToken, limiter, (req, res, next) => {
console.log('received add subscriber req')
findSubInStore(req.body.id).then(addSub => {
queries.findSub(addSub.Email).then(data => {
if (data.length == 0) {
queries.addSub(addSub).then(() => {
res.json({ Response: 'success' })
console.log(`${addSub.Name} added`)
}).catch(err => {
res.json({ Response: 'Query error' })
console.error('Query Error:', err)
})
} else {
res.json({ Response: 'Email already added' })
console.log('email already added')
}
}).catch(err => {
res.json({ Response: 'Query error' })
console.error('Query Error:', err)
})
}).catch(err => {
res.json({ Response: 'No matches in store' })
console.error('findSubInStore:', err)
})
})
//bye email
const findDbSub = (dbSubs, email) => {
return new Promise((resolve, reject) => {
const dbSub = dbSubs.find(dbSub => {
let decrypted = decrypt(dbSub.Email)
return email === decrypted
})
if (dbSub) {
resolve(dbSub)
} else {
const reason = new Error(`No matches for ${email}`)
reject(reason)
}
})
}
app.post(`/site/${process.env.BYE}/`, proxyToken, limiter, (req, res) => {
console.log('received bye email req')
getDate()
const email = req.body.Email
queries.listSubs().then(dbSubs => {
findDbSub(dbSubs, email).then(data => {
const name = data.Name
const pass = data.Passcode
createHash(pass, passStore)
const type = 'bye'
const byeEm = {
to: {
email: email,
},
from: {
name: 'arthuranteater',
email: '[email protected]'
},
subject: `To unsubscribe from arthuranteater...`,
text: 'To unsubscribe from arthuranteater...',
html: `<img src="cid:logo" width="80" height="80"><h2>arthuranteater</h2><h3><strong>Sharing projects, coding challenges, new tech, and best practices</strong></h3>
<p><strong>${name}, we have received a request to remove ${email} from our mailing list. If this was sent in error or by accident, please ignore this notice.</p>
<p><strong>Copy the Unsubscribe ID and paste into <a href="https://arthuranteater.com/unsubscribe" target="_blank">unsubscribe page</a></strong></p>
<h2><strong>Unsubscribe ID: ${pass}</strong></h2>
<div><a href="https://huntcodes.co/#contact" target="_blank">Contact Us</a><span></div>`,
attachments: [
{
content: logo.base64,
filename: 'logo.png',
type: 'image/png',
disposition: 'inline',
content_id: 'logo'
},
]
}
sgMail.send(byeEm, (err, sgres) => {
if (err) {
const mes = err.toString()
res.json({ Response: `No email sent, ${mes}` })
console.error('Send Grid Error:', mes)
console.log(`Send Grid Response: ${sgres}`)
const errPkg = {
Email: email, Message: mes, Type: type, EDate: today
}
queries.addErr(errPkg).then(() => {
console.log(`${errPkg} error added to log`)
}).catch(err => {
console.error('addErr err:', err)
})
}
else {
res.json({ Response: email })
console.log(`sent bye email to ${email}`)
}
})
}).catch(err => {
res.json({ Response: `No matches for ${email}` })
console.error(err.message)
})
}).catch(err => {
res.json({ Response: "No access, query error, please report and try again later" })
console.error(`listSubs: ${err}`)
})
})
//delete sub
app.post(`/site/${process.env.DELSUB}/`, passToken, limiter, (req, res, next) => {
console.log('received delete sub req')
const id = req.body.id
queries.delSub(id).then(delSub => {
if (delSub.length == 0) {
res.json({ Response: 'No matches in db' })
console.error(`No matches for ${id}`)
} else {
res.json({ Response: 'success' })
console.log('subscriber removed')
}
})
})
//new post
app.post(`/${process.env.ADDPOST}/`, adminToken, plimiter, (req, res, nxt) => {
console.log('received new post req')
getDate()
const type = 'post'
const post = req.body
const title = post.Title
const cat = post.Category
queries.findPost(post.PDate).then(data => {
console.log('post received')
if (data.length == 0) {
console.log(`adding ${title}`)
queries.addPost(post).then(() => {
res.json({ Response: 'post received' })
queries.getSubsByCat(cat).then(posSubs => {
elist = []
posSubs.map(posSub => {
addToElist(posSub.Email, posSub.Name)
})
const postEm = {
to: elist,
from: 'arthuranteater <[email protected]>',
subject: `New ${cat} Post!`,
text: `new ${cat} post`,
html: `<div><h2><a href=http://localhost:8000/subscribe${post.Slug}>${title}</a></h2><h3>${post.SubTitle}</h3></div><div><a href="https://huntcodes.co/#contact" target="_blank">Contact Us</a>
<span> | </span><a href="https://arthuranteater.com/unsubscribe" target="_blank">Unsubscribe</a></div>`,
}
sgMail.sendMultiple(postEm, (err, sgres) => {
if (err) {
let mes = err.toString()
console.error('Send Grid:', mes)
console.log(`Send Grid Response: ${sgres}`)
let errPkg = {
Email: email, Name: name, Message: mes, Type: type, EDate: today
}
queries.addErr(errPkg).then(() => {
console.log(`${errPkg} added to db`)
}).catch(err => {
console.error('addErr:', err)
})
}
else {
console.log(`sent new post to ${postEm.to}`)
}
})
}).catch(err => {
res.json({ Response: 'Query error' })
console.error('getSubsByCat:', err)
})
}).catch(err => {
res.json({ Response: 'Query error' })
console.error('addPost:', err)
})
} else {
console.log(`already added ${title}`)
}
}).catch(err => {
res.json({ Response: 'Query error' })
console.error('findPost:', err)
})
})