2018-12-24 20:31:02 +01:00
require ( './config/config' ) ;
2021-06-04 10:07:57 +02:00
// console.log(" lodash");
2023-02-20 02:19:53 +01:00
console . log ( process . versions ) ;
2018-12-24 20:31:02 +01:00
const _ = require ( 'lodash' ) ;
2021-06-04 10:07:57 +02:00
// console.log(" cors");
2018-12-24 20:31:02 +01:00
const cors = require ( 'cors' ) ;
2018-12-27 20:09:40 +01:00
2021-06-04 10:07:57 +02:00
// console.log(" 2) fs");
2019-12-07 00:20:06 +01:00
const fs = require ( 'fs' ) ;
2024-07-23 12:25:10 +02:00
var https = require ( 'https' ) ;
var http = require ( 'http' ) ;
2024-08-29 23:30:58 +02:00
const WebSocket = require ( 'ws' ) ;
const { spawn } = require ( 'child_process' ) ;
const pty = require ( 'node-pty' ) ;
2023-02-20 02:19:53 +01:00
const NUOVO _METODO _TEST = true ;
2023-02-16 20:13:38 +01:00
2024-07-23 12:25:10 +02:00
const METODO _MULTI _CORS = false ;
2022-02-27 16:56:02 +01:00
const server _constants = require ( './tools/server_constants' ) ;
2019-12-07 00:20:06 +01:00
//const throttle = require('express-throttle-bandwidth');
2022-01-23 23:25:34 +01:00
// app.use(throttle(1024 * 128)) // throttling bandwidth
2018-12-27 20:09:40 +01:00
2021-12-21 18:12:28 +01:00
// var cookieParser = require('cookie-parser')
// var csrf = require('csurf')
2019-02-05 03:40:22 +01:00
const express = require ( 'express' ) ;
2024-07-23 12:25:10 +02:00
const vhost = require ( 'vhost' ) ;
2019-02-05 03:40:22 +01:00
const bodyParser = require ( 'body-parser' ) ;
const path = require ( 'path' ) ;
2019-11-21 00:18:40 +01:00
const cron = require ( 'node-cron' ) ;
2023-02-15 21:40:19 +01:00
console . log ( 'Starting mongoose...' ) ;
2019-02-14 19:01:41 +01:00
2021-06-04 10:07:57 +02:00
require ( './db/mongoose' ) ;
2023-02-15 21:40:19 +01:00
// console.log('Starting pem...');
// const pem = require('pem')
const { Settings } = require ( './models/settings' ) ;
2022-02-22 21:14:49 +01:00
2023-12-07 08:34:24 +01:00
const Site = require ( './models/site' ) ;
2022-11-10 19:33:23 +01:00
// test
2022-03-01 23:50:58 +01:00
2022-02-22 21:14:49 +01:00
const i18n = require ( 'i18n' ) ;
2020-05-19 00:18:13 +02:00
2024-08-29 23:30:58 +02:00
const readline = require ( 'readline' ) ;
2023-02-20 02:19:53 +01:00
let credentials = null ;
2022-02-22 21:14:49 +01:00
// OBTAIN
// https://www.psclistens.com/insight/blog/enabling-a-nodejs-ssl-webserver-using-let-s-encrypt-pem-certificates/
2018-12-27 21:03:42 +01:00
2023-02-15 21:40:19 +01:00
2023-02-20 02:19:53 +01:00
if ( ( process . env . NODE _ENV === 'production' ) ) {
2024-04-23 22:47:20 +02:00
console . log ( '*** AMBIENTE DI PRODUZIONE (Aprile 2024) !!!!' )
2023-02-20 02:19:53 +01:00
} else if ( process . env . NODE _ENV === 'test' ) {
console . log ( '*** ### AMBIENTE DI TEST ' )
2018-12-27 20:09:40 +01:00
}
2023-02-20 02:19:53 +01:00
2022-02-22 21:14:49 +01:00
console . log ( 'DB: ' + process . env . DATABASE ) ;
2021-06-04 10:07:57 +02:00
// console.log("PORT: " + port);
// console.log("MONGODB_URI: " + process.env.MONGODB_URI);
2018-12-24 20:31:02 +01:00
var app = express ( ) ;
2023-12-09 11:55:58 +01:00
2021-12-29 18:26:08 +01:00
let telegrambot = null ;
2019-02-14 19:01:41 +01:00
const tools = require ( './tools/general' ) ;
2022-01-16 23:21:03 +01:00
const shared _consts = require ( './tools/shared_nodejs' ) ;
2022-09-14 11:32:04 +02:00
var mongoose = require ( 'mongoose' ) . set ( 'debug' , false ) ;
2021-04-30 01:31:12 +02:00
2022-09-14 11:32:04 +02:00
mongoose . set ( 'debug' , false ) ;
2021-04-30 01:31:12 +02:00
2023-02-15 21:40:19 +01:00
const { CfgServer } = require ( './models/cfgserver' ) ;
const { ObjectID } = require ( 'mongodb' ) ;
2019-02-22 10:23:39 +01:00
2022-01-23 23:25:34 +01:00
const populate = require ( './populate/populate' ) ;
2023-02-15 21:40:19 +01:00
const { Circuit } = require ( './models/circuit' ) ;
2022-01-23 23:25:34 +01:00
2022-01-16 23:21:03 +01:00
const printf = require ( 'util' ) . format ;
2021-10-08 00:38:35 +02:00
2021-04-30 01:31:12 +02:00
myLoad ( ) . then ( ris => {
2023-02-15 21:40:19 +01:00
const { User } = require ( './models/user' ) ;
2021-04-30 01:31:12 +02:00
require ( './models/todo' ) ;
require ( './models/project' ) ;
require ( './models/subscribers' ) ;
require ( './models/booking' ) ;
require ( './models/sendmsg' ) ;
2022-07-10 01:25:19 +02:00
require ( './models/sendnotif' ) ;
2021-04-30 01:31:12 +02:00
require ( './models/mailinglist' ) ;
require ( './models/newstosent' ) ;
require ( './models/mypage' ) ;
2022-10-27 11:22:58 +02:00
require ( './models/myelem' ) ;
2021-12-29 18:26:08 +01:00
require ( './models/bot' ) ;
2021-04-30 01:31:12 +02:00
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' ) ;
const booking _router = require ( './router/booking_router' ) ;
const dashboard _router = require ( './router/dashboard_router' ) ;
const myevent _router = require ( './router/myevent_router' ) ;
const subscribe _router = require ( './router/subscribe_router' ) ;
const sendmsg _router = require ( './router/sendmsg_router' ) ;
2022-07-10 01:25:19 +02:00
const sendnotif _router = require ( './router/sendnotif_router' ) ;
2021-04-30 01:31:12 +02:00
const email _router = require ( './router/email_router' ) ;
const todos _router = require ( './router/todos_router' ) ;
const test _router = require ( './router/test_router' ) ;
const projects _router = require ( './router/projects_router' ) ;
const report _router = require ( './router/report_router' ) ;
const users _router = require ( './router/users_router' ) ;
2023-09-27 18:38:57 +02:00
const reactions _router = require ( './router/reactions_router' ) ;
2022-02-03 00:33:15 +01:00
const mygroups _router = require ( './router/mygroups_router' ) ;
2022-08-30 17:00:48 +02:00
const circuits _router = require ( './router/circuits_router' ) ;
2023-02-06 22:58:10 +01:00
const accounts _router = require ( './router/accounts_router' ) ;
2021-04-30 01:31:12 +02:00
const iscrittiConacreis _router = require ( './router/iscrittiConacreis_router' ) ;
2022-10-29 12:37:50 +02:00
const iscrittiArcadei _router = require ( './router/iscrittiArcadei_router' ) ;
2021-04-30 01:31:12 +02:00
const site _router = require ( './router/site_router' ) ;
const admin _router = require ( './router/admin_router' ) ;
const products _router = require ( './router/products_router' ) ;
const cart _router = require ( './router/cart_router' ) ;
const orders _router = require ( './router/orders_router' ) ;
2024-03-28 20:25:48 +01:00
const city _router = require ( './router/city_router' ) ;
2022-01-26 01:31:22 +01:00
const myskills _router = require ( './router/myskills_router' ) ;
2022-02-21 13:12:27 +01:00
const mygoods _router = require ( './router/mygoods_router' ) ;
2022-02-16 19:21:12 +01:00
const mygen _router = require ( './router/mygen_router' ) ;
2024-01-30 14:00:37 +01:00
const aitools _router = require ( './router/aitools_router' ) ;
2021-04-30 01:31:12 +02:00
2023-02-15 21:40:19 +01:00
const { MyEvent } = require ( './models/myevent' ) ;
2021-04-30 01:31:12 +02:00
2024-04-29 14:58:45 +02:00
app . use ( bodyParser . json ( { limit : '50mb' } ) ) ;
app . use ( bodyParser . urlencoded ( { limit : '50mb' , extended : true } ) ) ;
2021-04-30 01:31:12 +02:00
app . use ( express . static ( 'views' ) ) ;
2019-10-05 20:01:56 +02:00
2023-02-15 21:40:19 +01:00
// app.use(express.static(path.join(__dirname, 'client')));
2019-02-05 03:40:22 +01:00
2021-04-30 01:31:12 +02:00
app . use ( bodyParser . json ( ) ) ;
2019-02-05 03:40:22 +01:00
2023-02-15 21:40:19 +01:00
// app.set('view engine', 'pug');
2019-02-05 03:40:22 +01:00
2023-02-15 21:40:19 +01:00
// Set static folder
// app.use(express.static(path.join(__dirname, 'public')));
2019-02-05 03:40:22 +01:00
2021-04-30 01:31:12 +02:00
i18n . configure ( {
locales : [ 'it' , 'enUs' , 'es' , 'fr' , 'pt' , 'si' ] ,
2022-07-23 17:48:33 +02:00
defaultLocale : 'it' ,
2021-04-30 01:31:12 +02:00
// cookie: 'cook',
2022-02-22 21:14:49 +01:00
directory : _ _dirname + '/locales' ,
2022-07-23 17:48:33 +02:00
api : {
'__' : 'translate' ,
'__n' : 'translateN'
} ,
2021-04-30 01:31:12 +02:00
} ) ;
2018-12-24 20:31:02 +01:00
2021-04-30 01:31:12 +02:00
app . use ( cors ( {
2024-04-11 11:43:19 +02:00
exposedHeaders : [ 'x-auth' , 'x-refrtok' ] ,
2021-04-30 01:31:12 +02:00
} ) ) ;
2018-12-24 20:31:02 +01:00
2021-04-30 01:31:12 +02:00
app . use ( bodyParser . json ( ) ) ;
2018-12-24 20:31:02 +01:00
2023-02-15 21:40:19 +01:00
// app.use(express.cookieParser());
2021-04-30 01:31:12 +02:00
app . use ( i18n . init ) ;
2018-12-24 20:31:02 +01:00
2023-02-15 21:40:19 +01:00
console . log ( 'Use Routes \...' ) ;
// Use Routes
2021-04-30 01:31:12 +02:00
app . use ( '/' , index _router ) ;
app . use ( '/subscribe' , subscribe _router ) ;
app . use ( '/sendmsg' , sendmsg _router ) ;
2022-07-10 01:25:19 +02:00
app . use ( '/sendnotif' , sendnotif _router ) ;
2021-04-30 01:31:12 +02:00
app . use ( '/push' , push _router ) ;
app . use ( '/news' , newsletter _router ) ;
app . use ( '/booking' , booking _router ) ;
app . use ( '/dashboard' , dashboard _router ) ;
app . use ( '/event' , myevent _router ) ;
app . use ( '/email' , email _router ) ;
app . use ( '/todos' , todos _router ) ;
app . use ( '/test' , test _router ) ;
app . use ( '/projects' , projects _router ) ;
app . use ( '/users' , users _router ) ;
2023-09-27 18:38:57 +02:00
app . use ( '/reactions' , reactions _router ) ;
2022-02-03 00:33:15 +01:00
app . use ( '/mygroup' , mygroups _router ) ;
2022-08-30 17:00:48 +02:00
app . use ( '/circuit' , circuits _router ) ;
2023-02-06 22:58:10 +01:00
app . use ( '/account' , accounts _router ) ;
2021-04-30 01:31:12 +02:00
app . use ( '/iscritti_conacreis' , iscrittiConacreis _router ) ;
2022-10-29 12:37:50 +02:00
app . use ( '/iscritti_arcadei' , iscrittiArcadei _router ) ;
2021-04-30 01:31:12 +02:00
app . use ( '/report' , report _router ) ;
app . use ( '/site' , site _router ) ;
app . use ( '/admin' , admin _router ) ;
app . use ( '/products' , products _router ) ;
app . use ( '/cart' , cart _router ) ;
app . use ( '/orders' , orders _router ) ;
2024-03-28 20:25:48 +01:00
app . use ( '/city' , city _router ) ;
2022-01-26 01:31:22 +01:00
app . use ( '/myskills' , myskills _router ) ;
2022-02-21 13:12:27 +01:00
app . use ( '/mygoods' , mygoods _router ) ;
2022-02-16 19:21:12 +01:00
app . use ( '/mygen' , mygen _router ) ;
2024-01-30 14:00:37 +01:00
app . use ( '/aitools' , aitools _router ) ;
2018-12-24 20:31:02 +01:00
2023-02-15 21:40:19 +01:00
// catch 404 and forward to error handler
// app.use(function (req, res, next) {
// var err = new Error('Not Found');
// err.status = 404;
// next(err);
// });
2018-12-24 20:31:02 +01:00
2023-02-15 21:40:19 +01:00
// app.set('views', path.join(__dirname, 'views'));
// app.set('view engine', 'pug');
2019-02-05 03:40:22 +01:00
2023-02-15 21:40:19 +01:00
// development error handler
// will print stacktrace
2021-04-30 01:31:12 +02:00
if ( app . get ( 'env' ) === 'development' ) {
2023-02-15 21:40:19 +01:00
app . use ( function ( err , req , res , next ) {
2022-08-10 17:07:02 +02:00
console . log ( 'Server Error: ' , err . message ) ;
2021-04-30 01:31:12 +02:00
// console.trace();
2023-02-15 21:40:19 +01:00
res . status ( err . status || 500 ) . send ( { error : err . message } ) ;
2021-04-30 01:31:12 +02:00
// res.render('error', {
// message: err.message,
// error: err
// });
} ) ;
2020-01-03 22:02:18 +01:00
2021-04-30 01:31:12 +02:00
}
2018-12-24 20:31:02 +01:00
2023-02-15 21:40:19 +01:00
// require('./telegram/telegrambot');
2020-01-03 01:52:49 +01:00
2023-02-15 21:40:19 +01:00
// *** DB CONNECTIONS ***
// mysql_func.mySqlConn_Shen.connect((err) => {
// if (!err)
// console.log('DB connection to Shen Database succeded.');
// else
// console.log('DB connection to Shen Database FAILED \n Error: ' + JSON.stringify(err, undefined, 2));
// });
2019-10-05 20:01:56 +02:00
2022-02-22 21:14:49 +01:00
if ( process . env . NODE _ENV === 'production' ) {
console . log ( '*** PRODUCTION! ' ) ;
2021-06-04 10:07:57 +02:00
}
2023-02-20 02:19:53 +01:00
if ( ( process . env . NODE _ENV === 'production' ) ||
( process . env . NODE _ENV === 'test' ) ) {
2021-04-30 01:31:12 +02:00
}
2024-07-23 12:25:10 +02:00
startServer ( app , process . env . PORT ) ;
2021-04-30 01:31:12 +02:00
mystart ( ) ;
} ) ;
2018-12-27 20:09:40 +01:00
2024-07-23 12:25:10 +02:00
2019-12-07 00:20:06 +01:00
// app.use(throttle(1024 * 128)); // throttling bandwidth
// app.use((req, res, next) => {
// res.header('Access-Control-Allow-Origin', '*')
// res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept')
// next()
// });
2021-04-30 01:31:12 +02:00
async function myLoad ( ) {
2021-09-22 01:13:41 +02:00
return tools . loadApps ( ) ;
2021-04-30 01:31:12 +02:00
}
async function mystart ( ) {
2022-03-02 20:18:29 +01:00
// await estraiTutteLeImmagini();
2022-03-01 23:50:58 +01:00
2022-07-23 17:48:33 +02:00
await tools . getApps ( ) ;
2021-04-30 01:31:12 +02:00
if ( process . env . PROD !== 1 ) {
2021-09-22 01:13:41 +02:00
2021-04-30 01:31:12 +02:00
testmsgwebpush ( ) ;
2021-03-17 02:24:11 +01:00
2021-04-30 01:31:12 +02:00
// tools.sendNotifToAdmin('Riparti', 'Riparti');
2022-01-07 01:18:01 +01:00
2021-12-29 18:26:08 +01:00
let miapass = '' ;
2020-04-24 10:29:25 +02:00
2021-04-30 01:31:12 +02:00
if ( miapass !== '' ) {
let crypt = tools . cryptdata ( miapass ) ;
let decrypt = tools . decryptdata ( crypt ) ;
2020-01-03 01:52:49 +01:00
2021-04-30 01:31:12 +02:00
console . log ( 'crypted:' , crypt ) ;
console . log ( 'decrypted:' , decrypt ) ;
}
2024-02-08 01:34:30 +01:00
mycron ( ) ;
2021-09-22 01:13:41 +02:00
if ( ! process . env . DEBUG ) {
mycron ( ) ;
}
2020-04-24 10:29:25 +02:00
}
2020-01-03 01:52:49 +01:00
2022-07-23 17:48:33 +02:00
2021-12-29 18:26:08 +01:00
telegrambot = require ( './telegram/telegrambot' ) ;
2021-04-30 01:31:12 +02:00
await inizia ( ) ;
await resetProcessingJob ( ) ;
2022-01-23 23:25:34 +01:00
populate . popolaTabelleNuove ( ) ;
2021-12-29 18:26:08 +01:00
faitest ( ) ;
2023-02-15 21:40:19 +01:00
// ----------------- MAILCHIMP -----
2021-04-30 01:31:12 +02:00
const querystring = require ( 'querystring' ) ;
const mailchimpClientId = 'xxxxxxxxxxxxxxxx' ;
2023-02-15 21:40:19 +01:00
app . get ( '/mailchimp/auth/authorize' , function ( req , res ) {
2021-04-30 01:31:12 +02:00
res . redirect ( 'https://login.mailchimp.com/oauth2/authorize?' +
2023-02-15 21:40:19 +01:00
querystring . stringify ( {
'response_type' : 'code' ,
'client_id' : mailchimpClientId ,
'redirect_uri' : 'http://127.0.0.1:3000/mailchimp/auth/callback' ,
} ) ) ;
2021-04-30 01:31:12 +02:00
} ) ;
2019-10-05 20:01:56 +02:00
2021-04-30 01:31:12 +02:00
}
// -----------------
2019-02-22 10:23:39 +01:00
function populateDBadmin ( ) {
2022-02-22 21:14:49 +01:00
const cfgserv = [
{
_id : new ObjectID ( ) ,
idapp : '9' ,
chiave : 'vers' ,
userId : 'ALL' ,
valore : '0.1.2' ,
} ] ;
2019-02-22 10:23:39 +01:00
2022-05-14 00:31:53 +02:00
let cfg = new CfgServer ( cfgserv [ 0 ] ) . save ( ) ;
2019-02-22 10:23:39 +01:00
}
2024-02-08 01:34:30 +01:00
async function mycron ( ) {
2019-02-22 10:23:39 +01:00
2024-02-08 01:34:30 +01:00
try {
const sendemail = require ( './sendemail' ) ;
2019-02-22 10:23:39 +01:00
2024-02-08 01:34:30 +01:00
const arr = await tools . getApps ( ) ;
for ( const app of arr ) {
sendemail . checkifPendingNewsletter ( app . idapp ) ;
sendemail . checkifSentNewsletter ( app . idapp ) ;
2020-05-19 00:18:13 +02:00
2024-02-08 01:34:30 +01:00
}
} catch ( e ) {
console . error ( 'Err mycron' , e ) ;
2020-05-19 00:18:13 +02:00
}
}
async function mycron _30min ( ) {
2024-03-21 21:22:57 +01:00
for ( const app of await tools . getApps ( ) ) {
2020-06-08 13:31:05 +02:00
let enablecrontab = false ;
2022-02-22 21:14:49 +01:00
enablecrontab = await Settings . getValDbSettings ( app . idapp ,
2023-02-15 21:40:19 +01:00
tools . ENABLE _CRONTAB , false ) ;
2020-05-19 00:18:13 +02:00
if ( enablecrontab ) {
2021-04-30 01:31:12 +02:00
// ...
2020-05-19 00:18:13 +02:00
}
2019-11-21 00:18:40 +01:00
}
2019-10-20 01:21:54 +02:00
}
2024-03-21 21:22:57 +01:00
async function mycron _everyday ( ) {
try {
const { User } = require ( './models/user' ) ;
const arrapps = await tools . getApps ( ) ;
for ( const app of arrapps ) {
// Azzera le richieste di password:
const usersblocked = await User . find ( { idapp : app . idapp , retry _pwd : { $exists : true , $gte : 29 } } ) . lean ( ) ;
for ( const user of usersblocked ) {
await User . findOneAndUpdate ( { _id : user . _id } , { $set : { retry _pwd : 20 } } ) ;
let text = ` ⚠️⚠️⚠️ L \' utente ${ user . username } ( ${ user . name } ${ user . surname } ) viene sbloccato dal numero massimo di tentativi di richiesta password! \n Telerlo d \' occhio ! \n @ ${ user . profile . username _telegram } ` ;
await telegrambot . sendMsgTelegramToTheAdminAllSites ( text , false ) ;
}
}
} catch ( e ) {
console . error ( 'mycron_everyday: ' , e ) ;
}
}
2019-02-14 19:01:41 +01:00
function testmsgwebpush ( ) {
2023-02-15 21:40:19 +01:00
const { User } = require ( './models/user' ) ;
2019-10-05 20:01:56 +02:00
// console.log('nomeapp 1: ' , tools.getNomeAppByIdApp(1));
// console.log('nomeapp 2: ' , tools.getNomeAppByIdApp(2));
2019-02-14 19:01:41 +01:00
2023-02-15 21:40:19 +01:00
User . find ( { username : 'paoloar77' , idapp : '1' } ) . then ( async ( arrusers ) => {
2020-04-10 13:02:33 +02:00
if ( arrusers !== null ) {
for ( const user of arrusers ) {
2022-09-11 11:45:33 +02:00
await tools . sendNotificationToUser ( user . _id , 'Server' ,
2023-02-15 21:40:19 +01:00
'Il Server è Ripartito' , '/' , '' , 'server' , [ ] ) . then ( ris => {
if ( ris ) {
2022-02-22 21:14:49 +01:00
2023-02-15 21:40:19 +01:00
} else {
// already sent the error on calling sendNotificationToUser
}
} ) ;
2020-04-10 13:02:33 +02:00
}
2019-02-14 19:01:41 +01:00
}
2019-10-05 20:01:56 +02:00
} ) ;
2019-02-14 19:01:41 +01:00
}
2019-12-04 18:20:50 +01:00
// Cron every X minutes
2024-02-08 01:34:30 +01:00
cron . schedule ( '*/1 * * * *' , ( ) => {
2020-04-10 13:02:33 +02:00
// console.log('Running Cron Job');
2024-02-08 01:34:30 +01:00
// if (!process.env.DEBUG) {
2024-03-21 21:22:57 +01:00
mycron ( ) ;
2024-02-08 01:34:30 +01:00
// }
2019-11-21 00:18:40 +01:00
} ) ;
2020-05-19 00:18:13 +02:00
// Cron every X minutes
2020-06-08 13:31:05 +02:00
cron . schedule ( '*/60 * * * *' , async ( ) => {
2021-09-22 01:13:41 +02:00
if ( ! process . env . DEBUG ) {
mycron _30min ( ) ;
}
2020-05-19 00:18:13 +02:00
} ) ;
2024-03-21 21:22:57 +01:00
// Cron every 21:00 (1 volta al giorno)
cron . schedule ( '0 21 * * *' , async ( ) => {
mycron _everyday ( ) ;
} ) ;
2020-06-08 13:31:05 +02:00
// mycron_30min();
// tools.writelogfile('test', 'prova.txt');
2020-02-07 22:08:46 +01:00
async function resetProcessingJob ( ) {
2023-02-15 21:40:19 +01:00
const { Newstosent } = require ( './models/newstosent' ) ;
2021-04-30 01:31:12 +02:00
2020-02-07 22:08:46 +01:00
arrrec = await Newstosent . find ( { } ) ;
for ( const rec of arrrec ) {
rec . processing _job = false ;
2023-02-15 21:40:19 +01:00
await Newstosent . findOneAndUpdate ( { _id : rec . id } , { $set : rec } , { new : false } ) .
then ( ( item ) => {
2020-02-07 22:08:46 +01:00
2023-02-15 21:40:19 +01:00
} ) ;
2020-02-07 22:08:46 +01:00
}
}
2018-12-27 20:09:40 +01:00
//app.listen(port, () => {
// console.log(`Server started at port ${port}`);
//});
2020-02-19 16:09:16 +01:00
async function inizia ( ) {
2021-04-30 01:31:12 +02:00
2022-03-12 21:38:19 +01:00
try {
2024-03-23 00:21:32 +01:00
if ( true ) {
2024-03-26 15:36:49 +01:00
const url = 'https://raw.githubusercontent.com/matteocontrini/comuni-json/master/comuni.json' ;
2024-03-23 00:21:32 +01:00
const outputPath = './comuni_italia_geojson.json' ;
2024-03-26 15:36:49 +01:00
downloadGeoJSON ( url , outputPath ) ;
2024-03-23 00:21:32 +01:00
}
2024-03-21 21:22:57 +01:00
mycron _everyday ( ) ;
2021-12-23 14:13:40 +01:00
2022-03-12 21:38:19 +01:00
if ( process . env . NODE _ENV === 'development' ) {
await telegrambot . sendMsgTelegram ( tools . FREEPLANET ,
2023-02-15 21:40:19 +01:00
telegrambot . ADMIN _USER _SERVER ,
` Ciao ${ telegrambot . ADMIN _USER _NAME _SERVER } ! ` ) ;
2020-01-03 22:02:18 +01:00
2022-03-12 21:38:19 +01:00
await telegrambot . sendMsgTelegramByIdTelegram ( tools . FREEPLANET ,
2023-02-15 21:40:19 +01:00
telegrambot . ADMIN _IDTELEGRAM _SERVER ,
` Ciao ${ telegrambot . ADMIN _USER _NAME _SERVER } \n ` +
` 🔅 Il Server ${ process . env . DATABASE } è appena ripartito! ` ) ;
2022-03-12 21:38:19 +01:00
} else {
await telegrambot . sendMsgTelegramToTheAdminAllSites ( ` Ciao Admin \n ` + ` 🔅🔅🔅 Il Server col BOT di {appname} è appena ripartito! ` , false ) ;
}
2022-11-06 13:39:01 +01:00
2023-12-07 08:34:24 +01:00
await Site . createFirstUserAdmin ( ) ;
2022-11-10 19:33:23 +01:00
/ * c o n s t { C i r c u i t } = r e q u i r e ( ' . / m o d e l s / c i r c u i t ' ) ;
2024-07-03 14:21:02 +02:00
2022-11-10 19:33:23 +01:00
await Circuit . setDeperimentoOff ( ) ;
2024-07-03 14:21:02 +02:00
2022-11-10 19:33:23 +01:00
* /
2023-02-20 02:19:53 +01:00
2022-11-06 13:39:01 +01:00
2022-03-12 21:38:19 +01:00
} catch ( e ) {
2020-02-19 16:09:16 +01:00
2021-12-23 14:13:40 +01:00
}
2020-02-19 16:09:16 +01:00
}
2020-02-07 22:08:46 +01:00
//
2020-01-03 22:02:18 +01:00
// telegrambot.sendMsgTelegramToTheManagers('7', 'PROVAAA!');
2020-01-20 01:48:25 +01:00
// if (process.env.PROD !== 1) {
// const reg = require('./reg/registration');
// const link = reg.getlinkregByEmail('7', 'tomasihelen@dasdasgmail.comAAAA' , 'HelenTomasidasdasd');
// const link2 = reg.getlinkregByEmail('7', 'tomasihelen@gmail.com' , 'HelenTomasi');
// //const link2 = reg.getlinkregByEmail('7', 'elenaliubicich@gmail.com' , 'Elenaliu');
//
// console.log(link);
// console.log(link2);
// }
2022-03-01 23:50:58 +01:00
async function estraiImmagini ( table ) {
2023-02-15 21:40:19 +01:00
const { User } = require ( './models/user' ) ;
2022-03-01 23:50:58 +01:00
let idapp = '13' ;
let arrlist ;
2022-07-23 17:48:33 +02:00
const globalTables = require ( './tools/globalTables' ) ;
2022-03-01 23:50:58 +01:00
const mytable = globalTables . getTableByTableName ( table ) ;
if ( ! mytable )
return ;
console . log ( 'INIZIO - estraiImmagini' , table ) ;
2023-02-15 21:40:19 +01:00
arrlist = await mytable . find ( { idapp } ) . lean ( ) ;
2022-03-01 23:50:58 +01:00
let file = '' ;
let filetocheck = '' ;
let dirmain = '' ;
let filefrom = '' ;
let filefrom2 = '' ;
let dir = tools . getdirByIdApp ( idapp ) + dirmain + '/upload/' ;
try {
if ( ! tools . sulServer ( ) ) {
dirmain = '/public' ;
}
for ( const rec of arrlist ) {
2023-02-15 21:40:19 +01:00
const myuser = await User . findOne ( { idapp , _id : rec . userId } ) . lean ( ) ;
2022-03-01 23:50:58 +01:00
if ( myuser ) {
const myphotos = rec . photos ;
if ( myphotos . length > 0 ) {
let folderprof = dir + 'profile/' + myuser . username ;
try {
// console.log('checkdir', folderprof);
if ( ! fs . existsSync ( folderprof ) ) {
2022-03-06 00:48:33 +01:00
console . log ( '*** Creadir' , folderprof ) ;
2022-03-01 23:50:58 +01:00
fs . mkdirSync ( folderprof ) ;
}
folderprof = dir + 'profile/' + myuser . username + '/' + table ;
// console.log('checkdir', folderprof);
if ( ! fs . existsSync ( folderprof ) ) {
2022-03-06 00:48:33 +01:00
console . log ( 'creadir' , folderprof ) ;
2022-03-01 23:50:58 +01:00
fs . mkdirSync ( folderprof ) ;
}
2022-03-06 00:48:33 +01:00
} catch ( e ) {
2022-03-01 23:50:58 +01:00
}
}
for ( const photo of myphotos ) {
if ( photo . imagefile ) {
file = dir + 'profile/' + myuser . username + '/' + table + '/' +
2023-02-15 21:40:19 +01:00
photo . imagefile ;
2022-03-01 23:50:58 +01:00
filefrom = dir + 'profile/undefined/' + table + '/' + photo . imagefile ;
filefrom2 = dir + 'profile/' + myuser . username + '/' + photo . imagefile ;
// console.log('file', file);
// console.log('filefrom', filefrom);
2022-03-06 00:48:33 +01:00
if ( ! tools . isFileExists ( file ) ) {
2022-03-01 23:50:58 +01:00
// non esiste
console . log ( 'non esiste' , file ) ;
console . log ( ' filefrom' , filefrom ) ;
console . log ( ' filefrom2' , filefrom2 ) ;
}
if ( ! tools . isFileExists ( file ) && tools . isFileExists ( filefrom ) ) {
console . log ( '@@@@@@ copia file:' , filefrom , 'a' , file ) ;
tools . copy ( filefrom , file ) ;
}
if ( ! tools . isFileExists ( file ) && tools . isFileExists ( filefrom2 ) ) {
console . log ( '@@@@@@ copia file 2:' , filefrom2 , 'a' , file ) ;
tools . copy ( filefrom2 , file ) ;
}
}
}
}
}
console . log ( 'FINE - estraiImmagini' , table ) ;
2022-03-06 00:48:33 +01:00
} catch ( e ) {
2022-03-01 23:50:58 +01:00
console . error ( 'e' , e ) ;
}
}
async function estraiTutteLeImmagini ( ) {
await estraiImmagini ( 'myskills' ) ;
await estraiImmagini ( 'mygoods' ) ;
await estraiImmagini ( 'mybachecas' ) ;
}
2021-10-08 00:38:35 +02:00
async function faitest ( ) {
2022-01-07 01:18:01 +01:00
// console.log('Fai Test:')
2021-10-08 00:38:35 +02:00
const testfind = false ;
2021-10-28 00:38:10 +02:00
// const $vers = tools.getVersionint('1.92.45');
2022-03-12 21:38:19 +01:00
if ( true ) {
// tools.execScript("ls -la");
}
2022-07-10 01:25:19 +02:00
if ( false ) {
prova = tools . removeAtChar ( '@prova' ) ;
}
2022-03-06 00:48:33 +01:00
if ( false ) {
const prova = tools . getConfSiteOptionEnabledByIdApp ( '13' , shared _consts . ConfSite . Notif _Reg _Push _Admin ) ;
2022-02-27 16:56:02 +01:00
console . log ( 'prova' , prova ) ;
}
2021-10-08 00:38:35 +02:00
if ( testfind ) {
2023-02-15 21:40:19 +01:00
const { City } = require ( './models/city' ) ;
2021-10-08 00:38:35 +02:00
let miacity = 'roma' ;
const ris = await City . findByCity ( miacity ) ;
console . log ( 'ris' , ris ) ;
}
2023-02-15 21:40:19 +01:00
const { User } = require ( './models/user' ) ;
2022-01-16 23:21:03 +01:00
2022-01-07 01:18:01 +01:00
if ( false ) {
2022-01-16 23:21:03 +01:00
let myuser = await User . findOne ( {
2022-02-09 19:48:39 +01:00
idapp : '1' ,
2022-01-07 01:18:01 +01:00
username : 'paoloar77' ,
} ) ;
2022-01-16 23:21:03 +01:00
const langdest = 'it' ;
2022-08-04 17:30:57 +02:00
telegrambot . askConfirmationUser ( myuser . idapp , shared _consts . CallFunz . REGISTRATION , myuser ) ;
2022-01-16 23:21:03 +01:00
}
if ( false ) {
const user = await User . findOne ( {
idapp : 12 ,
username : 'paolotest1' ,
} ) ;
await sendemail . sendEmail _Registration ( 'it' , 'paolo@arcodiluce.it' , user ,
2023-02-15 21:40:19 +01:00
'12' , '' ) ;
2022-01-07 01:18:01 +01:00
}
2021-12-29 18:26:08 +01:00
if ( false ) {
2023-02-15 21:40:19 +01:00
const { User } = require ( './models/user' ) ;
2021-12-29 18:26:08 +01:00
const idapp = tools . FREEPLANET ;
const idreg = 0 ;
try {
const user = await User . findOne ( {
idapp ,
username : 'paoloar773' ,
} ) ;
user . aportador _solidario = 'paoloar77' ;
let mylocalsconf = {
idapp ,
dataemail : null ,
locale : user . lang ,
nomeapp : tools . getNomeAppByIdApp ( idapp ) ,
strlinksito : tools . getHostByIdApp ( idapp ) ,
strlinkreg : '' ,
username : user . username ,
name : user . name ,
surname : user . surname ,
forgetpwd : tools . getHostByIdApp ( idapp ) + '/requestresetpwd' ,
emailto : '' ,
user ,
} ;
await telegrambot . notifyToTelegram ( telegrambot . phase . REGISTRATION ,
2023-02-15 21:40:19 +01:00
mylocalsconf ) ;
2021-12-29 18:26:08 +01:00
} catch ( e ) {
console . log ( 'error ' + e ) ;
}
}
2021-10-08 00:38:35 +02:00
}
2024-07-23 12:25:10 +02:00
/ * c o n s t d o m a i n s = [
{ hostname : 'piuchebuono.app' , port : 3000 } ,
{ hostname : 'gruppomacro.app' , port : 3010 } ,
] ; * /
function getCredentials ( hostname ) {
if ( NUOVO _METODO _TEST ) {
if ( METODO _MULTI _CORS ) {
const fileprivkey = ` /etc/letsencrypt/live/ ${ hostname } /privkey.pem ` ;
const filecert = ` /etc/letsencrypt/live/ ${ hostname } /cert.pem ` ;
console . log ( 'fileprivkey: ' , fileprivkey , ' filecert: ' , filecert ) ;
/ * r e t u r n {
SNICallback : function ( hostname , callback ) {
console . log ( 'hostname: ' , hostname ) ;
if ( domains . includes ( hostname ) ) {
const fileprivkey = ` /etc/letsencrypt/live/ ${ hostname } /privkey.pem ` ;
const filecert = ` /etc/letsencrypt/live/ ${ hostname } /fullchain.pem ` ;
// console.log('fileprivkey: ', fileprivkey, ' filecert: ', filecert);
const domainCert = {
key : fs . readFileSync ( fileprivkey , "utf8" ) ,
cert : fs . readFileSync ( filecert , "utf8" ) ,
} ;
callback ( null , domainCert ) ;
} else {
callback ( null , { key : privateKey , cert : certificate } ) ;
}
}
} ; * /
return {
key : fs . readFileSync ( fileprivkey , "utf8" ) ,
cert : fs . readFileSync ( filecert , "utf8" )
} ;
} else {
const keyStream = path . resolve ( ` ./ ${ process . env . PATH _CERT _KEY } ` ) ;
const certificateStream = path . resolve ( ` ./ ${ process . env . PATH _SERVER _CRT } ` ) ;
const privateKey = fs . readFileSync ( keyStream , "utf8" ) ;
const certificate = fs . readFileSync ( certificateStream , "utf8" ) ;
return { key : privateKey , cert : certificate } ;
}
} else if ( process . env . HTTPS _LOCALHOST === "true" ) {
try {
return {
key : fs . readFileSync ( process . env . PATH _CERT _KEY , 'utf8' ) ,
cert : fs . readFileSync ( process . env . PATH _SERVER _CRT , 'utf8' ) ,
ciphers : 'ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA:ECDHE-RSA-AES256-SHA384' ,
honorCipherOrder : true ,
secureProtocol : 'TLSv1_2_method'
} ;
} catch ( error ) {
console . error ( 'Errore durante la lettura dei file di certificazione, error:' , error . message ) ;
throw error ;
}
}
// Caso di default non specificato, potrebbe essere necessario aggiungere una gestione degli errori qui
}
function startServer ( app , port ) {
try {
const isProduction = [ 'production' , 'test' ] . includes ( process . env . NODE _ENV ) ;
let domains = [ ] ;
try {
2024-07-31 15:02:40 +02:00
if ( process . env . DOMAINS )
domains = JSON . parse ( process . env . DOMAINS ) ;
2024-07-23 12:25:10 +02:00
} catch ( error ) {
console . error ( "Errore durante la conversione della stringa DOMAINS:" , error ) ;
}
console . log ( 'domains' , domains ) ;
2024-08-29 23:30:58 +02:00
let httpsServer = null ;
let httpServer = null ;
2024-07-23 12:25:10 +02:00
if ( isProduction ) {
for ( let i = 0 ; i < domains . length ; i ++ ) {
const credentials = getCredentials ( domains [ i ] . hostname ) ;
// console.log('credentials: ', credentials);
2024-08-29 23:30:58 +02:00
httpsServer = https . createServer ( credentials , app ) ;
2024-07-23 12:25:10 +02:00
console . log ( '⭐️⭐️⭐️⭐️⭐️ HTTPS server: ' + domains [ i ] . hostname + ' Port:' , domains [ i ] . port ) ;
httpsServer . listen ( domains [ i ] . port ) ;
}
} else {
2024-07-31 15:02:40 +02:00
if ( process . env . HTTPS _LOCALHOST === "true" ) {
let credentials = null ;
try {
credentials = {
key : fs . readFileSync ( process . env . PATH _CERT _KEY , 'utf8' ) ,
cert : fs . readFileSync ( process . env . PATH _SERVER _CRT , 'utf8' ) ,
ciphers : 'ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA:ECDHE-RSA-AES256-SHA384' ,
honorCipherOrder : true ,
secureProtocol : 'TLSv1_2_method'
} ;
} catch ( error ) {
console . error ( 'Errore durante la lettura dei file di certificazione, error:' , error . message ) ;
throw error ;
}
if ( credentials ) {
2024-08-29 23:30:58 +02:00
httpsServer = https . createServer ( credentials , app ) ;
2024-07-31 15:02:40 +02:00
console . log ( '⭐️⭐️⭐️ HTTPS server IN LOCALE : port' , port ) ;
httpsServer . listen ( port ) ;
} else {
httpServer = http . createServer ( app ) ;
if ( httpServer ) {
console . log ( '⭐️⭐️⭐️ HTTP server IN LOCALE : port' , port ) ;
httpServer . listen ( port ) ;
}
}
// console.log('credentials', credentials);
} else {
httpServer = http . createServer ( app ) ;
if ( httpServer ) {
console . log ( '⭐️⭐️⭐️ HTTP server IN LOCALE : port' , port ) ;
httpServer . listen ( port ) ;
}
}
2024-07-23 12:25:10 +02:00
}
2024-08-29 23:30:58 +02:00
let wss = null ;
if ( httpsServer ) {
wss = new WebSocket . Server ( { server : httpsServer } ) ;
} else if ( httpServer ) {
wss = new WebSocket . Server ( { server : httpServer } ) ;
} else {
// console.error('Nessun server HTTP o HTTPS disponibile per WebSocket');
// process.exit(1);
}
wss . on ( 'connection' , ( ws ) => {
// console.log('Client connected');
let scriptProcess = null ;
ws . on ( 'message' , ( message ) => {
const parsedMessage = JSON . parse ( message ) ;
if ( parsedMessage . type === 'start_script' ) {
if ( scriptProcess ) {
scriptProcess . kill ( ) ;
}
const scriptPath = path . join ( _ _dirname , '..' , '..' , '' , parsedMessage . scriptName ) ;
// Verifica che lo script esista e sia all'interno della directory consentita
if ( fs . existsSync ( scriptPath ) ) {
scriptProcess = pty . spawn ( 'bash' , [ scriptPath ] , {
name : 'xterm-color' ,
cols : 80 ,
rows : 40 ,
cwd : process . cwd ( ) ,
env : process . env
} ) ;
let buffer = '' ;
scriptProcess . on ( 'data' , ( data ) => {
buffer += data ;
// Invia l'output al client
ws . send ( JSON . stringify ( { type : 'output' , data : data } ) ) ;
// Controlla se c'è una richiesta di input
if ( buffer . endsWith ( ': ' ) || buffer . includes ( '? ' ) ||
buffer . toLowerCase ( ) . includes ( 'password' )
|| buffer . includes ( 'Inserisci' )
|| buffer . includes ( 'Inserted' )
|| buffer . includes ( '(Y' )
) {
ws . send ( JSON . stringify ( { type : 'input_required' , prompt : data . trim ( ) } ) ) ;
buffer = '' ;
}
// Pulisci il buffer se diventa troppo grande
if ( buffer . length > 5024 ) {
buffer = buffer . slice ( - 500 ) ;
}
} ) ;
scriptProcess . on ( 'exit' , ( code ) => {
if ( code === 0 ) {
ws . send ( JSON . stringify ( { type : 'close' , data : ` *** FINE SCRIPT *** ` } ) ) ;
} else {
ws . send ( JSON . stringify ( { type : 'close' , data : ` Script terminato con codice ${ code } ` } ) ) ;
}
} ) ;
} else {
ws . send ( JSON . stringify ( { type : 'error' , data : 'Script non trovato o non autorizzato' } ) ) ;
}
} else if ( parsedMessage . type === 'input' ) {
if ( scriptProcess ) {
scriptProcess . write ( parsedMessage . data + '\n' ) ;
}
}
} ) ;
ws . on ( 'close' , ( ) => {
console . log ( 'Client disconnected' ) ;
if ( scriptProcess ) {
scriptProcess . kill ( ) ;
}
} ) ;
} ) ;
2024-07-23 12:25:10 +02:00
} catch ( e ) {
console . log ( 'error ' + e ) ;
}
}
2023-02-15 21:40:19 +01:00
module . exports = { app } ;
2021-10-08 00:38:35 +02:00