diff --git a/README.md b/README.md index 789e21a..ef6522a 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,28 @@ cp .env.development .env.test And modifying the file with your configuration +### Certificates + +Once you have gone through the process of getting your Let’s Encrypt certificates you will have 4 certificates: + +```bash +cert.pem +chain.pem +fullchain.pem +privkey.pem +``` + +You will need to download the root certificate (root.pem) and an intermediate certificate (chain.pem) from Let’s Encrypt – https://letsencrypt.org/certificates/ + +See this article: +https://www.psclistens.com/insight/blog/enabling-a-nodejs-ssl-webserver-using-let-s-encrypt-pem-certificates/ + + +To see if all the certificates are OK, see this page check and enter the correct host and port: +https://decoder.link/sslchecker + +For example: +https://decoder.link/sslchecker/test.freeplanet.app/3001 ### Build the ambient test diff --git a/src/server/server.js b/src/server/server.js index d421aae..194e01a 100755 --- a/src/server/server.js +++ b/src/server/server.js @@ -6,7 +6,6 @@ const _ = require('lodash'); // console.log(" cors"); const cors = require('cors'); - // console.log(" 2) fs"); const fs = require('fs'); @@ -25,22 +24,32 @@ const cron = require('node-cron'); require('./db/mongoose'); -const { Settings } = require('./models/settings'); +const {Settings} = require('./models/settings'); -const i18n = require("i18n"); +const i18n = require('i18n'); + +// OBTAIN +// https://www.psclistens.com/insight/blog/enabling-a-nodejs-ssl-webserver-using-let-s-encrypt-pem-certificates/ -if ((process.env.NODE_ENV === 'production') || (process.env.NODE_ENV === 'test')) { +if ((process.env.NODE_ENV === 'production') || + (process.env.NODE_ENV === 'test')) { var privateKey = fs.readFileSync(process.env.PATH_CERT_KEY, 'utf8'); var certificate = fs.readFileSync(process.env.PATH_SERVER_CRT, 'utf8'); - var credentials = { key: privateKey, cert: certificate }; + var credentials = { + key: privateKey, + cert: certificate, + ca: [ + fs.readFileSync(process.env.PATH_SSL_ROOT_PEM, 'utf8'), + fs.readFileSync(process.env.PATH_SSL_CHAIN_PEM, 'utf8') + ] + }; var https = require('https'); } else { var http = require('http'); } - -console.log("DB: " + process.env.DATABASE); +console.log('DB: ' + process.env.DATABASE); // console.log("PORT: " + port); // console.log("MONGODB_URI: " + process.env.MONGODB_URI); @@ -54,12 +63,12 @@ require('./models/cfgserver'); const shared_consts = require('./tools/shared_nodejs'); -var mongoose = require('mongoose').set('debug', false) +var mongoose = require('mongoose').set('debug', false); mongoose.set('debug', process.env.DEBUG); const cfgserver = mongoose.model('cfgserver'); -const { ObjectID } = require('mongodb'); +const {ObjectID} = require('mongodb'); const populate = require('./populate/populate'); @@ -67,7 +76,7 @@ const printf = require('util').format; myLoad().then(ris => { - const { User } = require('./models/user'); + const {User} = require('./models/user'); require('./models/todo'); require('./models/project'); @@ -81,7 +90,6 @@ myLoad().then(ris => { require('./models/calzoom'); const mysql_func = require('./mysql/mysql_func'); - const index_router = require('./router/index_router'); const push_router = require('./router/push_router'); const newsletter_router = require('./router/newsletter_router'); @@ -107,7 +115,7 @@ myLoad().then(ris => { const mygoods_router = require('./router/mygoods_router'); const mygen_router = require('./router/mygen_router'); - const { MyEvent } = require('./models/myevent'); + const {MyEvent} = require('./models/myevent'); app.use(express.static('views')); @@ -120,11 +128,10 @@ myLoad().then(ris => { // Set static folder // app.use(express.static(path.join(__dirname, 'public'))); - i18n.configure({ locales: ['it', 'enUs', 'es', 'fr', 'pt', 'si'], // cookie: 'cook', - directory: __dirname + '/locales' + directory: __dirname + '/locales', }); app.use(cors({ @@ -136,7 +143,6 @@ myLoad().then(ris => { // app.use(express.cookieParser()); app.use(i18n.init); - // Use Routes app.use('/', index_router); app.use('/subscribe', subscribe_router); @@ -177,10 +183,10 @@ myLoad().then(ris => { // will print stacktrace if (app.get('env') === 'development') { - app.use(function (err, req, res, next) { + app.use(function(err, req, res, next) { console.log('Error: ', err.message); // console.trace(); - res.status(err.status || 500).send({ error: err.message }); + res.status(err.status || 500).send({error: err.message}); // res.render('error', { // message: err.message, // error: err @@ -191,7 +197,6 @@ myLoad().then(ris => { // require('./telegram/telegrambot'); - // *** DB CONNECTIONS *** // mysql_func.mySqlConn_Shen.connect((err) => { // if (!err) @@ -200,24 +205,23 @@ myLoad().then(ris => { // console.log('DB connection to Shen Database FAILED \n Error: ' + JSON.stringify(err, undefined, 2)); // }); - - if (process.env.NODE_ENV === 'production'){ - console.log("*** PRODUCTION! "); + if (process.env.NODE_ENV === 'production') { + console.log('*** PRODUCTION! '); } - if ((process.env.NODE_ENV === 'production') || (process.env.NODE_ENV === 'test')) { + if ((process.env.NODE_ENV === 'production') || + (process.env.NODE_ENV === 'test')) { var httpsServer = https.createServer(credentials, app); - console.log("httpsServer: port ", port); + console.log('httpsServer: port ', port); httpsServer.listen(port); } else { - console.log("httpServer: port ", port); + console.log('httpServer: port ', port); var httpServer = http.createServer(app); httpServer.listen(port); } mystart(); - }); // app.use(throttle(1024 * 128)); // throttling bandwidth @@ -239,10 +243,8 @@ async function mystart() { testmsgwebpush(); - // tools.sendNotifToAdmin('Riparti', 'Riparti'); - let miapass = ''; if (miapass !== '') { @@ -268,35 +270,32 @@ async function mystart() { faitest(); - // ----------------- MAILCHIMP ----- const querystring = require('querystring'); const mailchimpClientId = 'xxxxxxxxxxxxxxxx'; - app.get('/mailchimp/auth/authorize', function (req, res) { + app.get('/mailchimp/auth/authorize', function(req, res) { res.redirect('https://login.mailchimp.com/oauth2/authorize?' + - querystring.stringify({ - 'response_type': 'code', - 'client_id': mailchimpClientId, - 'redirect_uri': 'http://127.0.0.1:3000/mailchimp/auth/callback' - })); + querystring.stringify({ + 'response_type': 'code', + 'client_id': mailchimpClientId, + 'redirect_uri': 'http://127.0.0.1:3000/mailchimp/auth/callback', + })); }); - - } // ----------------- - function populateDBadmin() { - const cfgserv = [{ - _id: new ObjectID(), - idapp: '9', - chiave: 'vers', - userId: 'ALL', - valore: '0.1.2' - }]; + const cfgserv = [ + { + _id: new ObjectID(), + idapp: '9', + chiave: 'vers', + userId: 'ALL', + valore: '0.1.2', + }]; let cfg = new cfgserver(cfgserv[0]).save(); } @@ -305,7 +304,6 @@ function mycron() { const sendemail = require('./sendemail'); - for (const app of tools.getApps()) { sendemail.checkifPendingNewsletter(app.idapp); sendemail.checkifSentNewsletter(app.idapp); @@ -317,7 +315,8 @@ async function mycron_30min() { for (const app of tools.getApps()) { let enablecrontab = false; - enablecrontab = await Settings.getValDbSettings(app.idapp, tools.ENABLE_CRONTAB, false); + enablecrontab = await Settings.getValDbSettings(app.idapp, + tools.ENABLE_CRONTAB, false); if (enablecrontab) { // ... @@ -326,29 +325,28 @@ async function mycron_30min() { } function testmsgwebpush() { - const { User } = require('./models/user'); + const {User} = require('./models/user'); // console.log('nomeapp 1: ' , tools.getNomeAppByIdApp(1)); // console.log('nomeapp 2: ' , tools.getNomeAppByIdApp(2)); - User.find({ username: 'paoloar77', idapp: '1' }).then((arrusers) => { + User.find({username: 'paoloar77', idapp: '1'}).then((arrusers) => { if (arrusers !== null) { for (const user of arrusers) { - tools.sendNotificationToUser(user._id, 'Server', 'Il Server è Ripartito', '/', '', 'server', []) - .then(ris => { - if (ris) { + tools.sendNotificationToUser(user._id, 'Server', + 'Il Server è Ripartito', '/', '', 'server', []).then(ris => { + if (ris) { - } else { - // already sent the error on calling sendNotificationToUser - } - }) + } else { + // already sent the error on calling sendNotificationToUser + } + }); } } }); } - // Cron every X minutes cron.schedule('*/2 * * * *', () => { // console.log('Running Cron Job'); @@ -357,7 +355,6 @@ cron.schedule('*/2 * * * *', () => { } }); - // Cron every X minutes cron.schedule('*/60 * * * *', async () => { if (!process.env.DEBUG) { @@ -369,18 +366,18 @@ cron.schedule('*/60 * * * *', async () => { // tools.writelogfile('test', 'prova.txt'); - async function resetProcessingJob() { - const { Newstosent } = require('./models/newstosent'); + const {Newstosent} = require('./models/newstosent'); arrrec = await Newstosent.find({}); for (const rec of arrrec) { rec.processing_job = false; - await Newstosent.findOneAndUpdate({ _id: rec.id }, { $set: rec }, { new: false }).then((item) => { + await Newstosent.findOneAndUpdate({_id: rec.id}, {$set: rec}, {new: false}). + then((item) => { - }); + }); } } @@ -390,24 +387,29 @@ async function resetProcessingJob() { async function inizia() { - if (process.env.NODE_ENV === 'development') { - await telegrambot.sendMsgTelegram(tools.FREEPLANET, telegrambot.ADMIN_USER_SERVER, `Ciao ${telegrambot.ADMIN_USER_NAME_SERVER}!`); + await telegrambot.sendMsgTelegram(tools.FREEPLANET, + telegrambot.ADMIN_USER_SERVER, + `Ciao ${telegrambot.ADMIN_USER_NAME_SERVER}!`); - await telegrambot.sendMsgTelegramByIdTelegram(tools.FREEPLANET, telegrambot.ADMIN_IDTELEGRAM_SERVER, `Ciao ${telegrambot.ADMIN_USER_NAME_SERVER}\n` + `🔅 Il Server ${process.env.DATABASE} è appena ripartito!`); + await telegrambot.sendMsgTelegramByIdTelegram(tools.FREEPLANET, + telegrambot.ADMIN_IDTELEGRAM_SERVER, + `Ciao ${telegrambot.ADMIN_USER_NAME_SERVER}\n` + + `🔅 Il Server ${process.env.DATABASE} è appena ripartito!`); } else { // await telegrambot.sendMsgTelegram(tools.FREEPLANET, telegrambot.ADMIN_USER_SERVER, `Ciao ${telegrambot.ADMIN_USER_NAME_SERVER}!`); - await telegrambot.sendMsgTelegramByIdTelegram(tools.FREEPLANET, telegrambot.ADMIN_IDTELEGRAM_SERVER, `Ciao ${telegrambot.ADMIN_USER_NAME_SERVER}\n` + `🔅 Il Server ${process.env.DATABASE} è appena ripartito!`); + await telegrambot.sendMsgTelegramByIdTelegram(tools.FREEPLANET, + telegrambot.ADMIN_IDTELEGRAM_SERVER, + `Ciao ${telegrambot.ADMIN_USER_NAME_SERVER}\n` + + `🔅 Il Server ${process.env.DATABASE} è appena ripartito!`); } } - // - // telegrambot.sendMsgTelegramToTheManagers('7', 'PROVAAA!'); // if (process.env.PROD !== 1) { @@ -446,14 +448,13 @@ async function faitest() { const langdest = 'it'; - telegrambot.askConfirmationUserRegistration(myuser.idapp, shared_consts.CallFunz.REGISTRATION, myuser, 'perseo77', langdest); + telegrambot.askConfirmationUserRegistration(myuser.idapp, + shared_consts.CallFunz.REGISTRATION, myuser, 'perseo77', langdest); } - if (false) { - const user = await User.findOne({ idapp: 12, username: 'paolotest1', @@ -462,13 +463,11 @@ async function faitest() { await sendemail.sendEmail_Registration('it', 'paolo@arcodiluce.it', user, '12', ''); - - } if (false) { - const { User } = require('./models/user'); + const {User} = require('./models/user'); const idapp = tools.FREEPLANET; const idreg = 0; @@ -505,5 +504,5 @@ async function faitest() { } } -module.exports = { app }; +module.exports = {app};