versione 1.2.14 :

- aggiornati i file di configurazione, ENV e script non funzionanti., package.
- corretto custom-service-worker.js con CORS
- ottimizzato il server, la chiamata Load iniziale (senza promise, con async/await).
This commit is contained in:
Surya Paolo
2025-03-12 21:03:02 +01:00
parent d106a59bb5
commit 7827e49760
19 changed files with 594 additions and 1549 deletions

View File

@@ -78,7 +78,7 @@ connectToDatabase(connectionUrl, options)
.then(() => {
console.log('------------------------------------------------------------------');
console.log('--------------- CONNESSIONE AL DB EFFETTUATA ! -----------------');
console.log('------------------------------------------------------------------');
console.log('------------------------------------------------------------------');
const { CfgServer } = require('./models/cfgserver');
const { ObjectId } = require('mongodb');
@@ -792,7 +792,6 @@ connectToDatabase(connectionUrl, options)
console.log('isProduction', isProduction);
const NOCORS = false;
const CORS_ENABLE_FOR_ALL_SITES = false;
const ISDEBUG = false;
@@ -806,33 +805,55 @@ connectToDatabase(connectionUrl, options)
} else {
console.log('WITH CORS')
let myorigin = '*';
let credentials = false;
let credentials = true;
const allowedOrigins = domains.flatMap(domain => [
`https://${domain.hostname}`,
`https://api.${domain.hostname}`,
`https://test.${domain.hostname}`,
`https://testapi.${domain.hostname}`,
`http://${domain.hostname}`,
`http://api.${domain.hostname}`,
`http://test.${domain.hostname}`,
`http://testapi.${domain.hostname}`
]);
let allowedOrigins = null;
if (!isProduction) {
allowedOrigins = 'http://localhost:3000';
} else {
allowedOrigins = domains.flatMap(domain => [
`https://${domain.hostname}`,
`https://api.${domain.hostname}`,
`https://test.${domain.hostname}`,
`https://testapi.${domain.hostname}`,
`https://freeplanet.app:3000`,
`http://${domain.hostname}`,
`http://api.${domain.hostname}`,
`http://test.${domain.hostname}`,
`http://testapi.${domain.hostname}`
]);
}
let myorigin = '*';
if (domains.length > 0) {
myorigin = function (origin, callback) {
// Array di domini consentiti
myorigin = (origin, callback) => {
try {
// Validazione dell'input
if (!origin || typeof origin !== 'string' || !/^https?:\/\/[^\s/$.?#].[^\s]*$/.test(origin)) {
console.error('❌ Origine non valida', origin);
}
// console.log('allowedOrigins', allowedOrigins);
// Logging per il debug
// console.log('Verifica origine:', origin);
// Permetti richieste senza origin (es. mobile apps)
if (!origin || allowedOrigins.includes(origin)) {
callback(null, true);
} else {
callback(new Error('❌ CORS non permesso per questa origine'));
// Controllo delle origini consentite
if (!origin || allowedOrigins.includes(origin)) {
// console.log('✅ Origine consentita:', origin);
return callback(null, true);
}
// Blocco delle origini non autorizzate
console.warn('❌ Origine bloccata:', origin);
callback(new Error('CORS non permesso per questa origine ' + origin));
} catch (error) {
console.error('Errore durante la verifica dell\'origine:', error.message);
callback(error);
}
}
};
}
// Configurazione CORS dettagliata
@@ -861,38 +882,13 @@ connectToDatabase(connectionUrl, options)
// HO AGGIUNTO QUESTA RIGA PER IL CORS !!!!!!!
app.use(express.json()); // Middleware per il parsing del corpo JSON
// Gestione specifica delle richieste OPTIONS
/* app.options('*', function (req, res) {
const origin = req.headers.origin;
if (corsOptions.origin === '*' ||
(typeof corsOptions.origin === 'function' &&
corsOptions.origin.toString().includes(origin))) {
res.setHeader('Access-Control-Allow-Origin', origin);
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
res.setHeader('Access-Control-Allow-Headers',
'Origin, X-Requested-With, Content-Type, Accept, Authorization, x-auth, x-refrtok');
res.setHeader('Access-Control-Allow-Credentials', 'true');
res.setHeader('Access-Control-Expose-Headers', 'x-auth, x-refrtok');
res.setHeader('Access-Control-Max-Age', '86400');
res.status(204).end();
} else {
res.status(403).end();
}
});*/
app.options('*', cors(corsOptions)); // Gestisce tutte le richieste OPTIONS
// Middleware per assicurarsi che gli headers CORS siano sempre presenti
// Middleware personalizzato per assicurare gli headers CORS
app.use((req, res, next) => {
let origin = req.headers.origin;
if (!origin) {
origin = '*';
}
if (corsOptions.origin === '*' ||
(typeof corsOptions.origin === 'function' &&
corsOptions.origin.toString().includes(origin))) {
//console.log('Access-Control-Allow-Origin')
const origin = req.headers.origin || '*';
if (allowedOrigins.includes(origin) || corsOptions.origin === '*') {
// console.log(' ... ORIGIN', origin);
res.setHeader('Access-Control-Allow-Origin', origin);
res.setHeader('Access-Control-Allow-Credentials', 'true');
res.setHeader('Access-Control-Expose-Headers', 'x-auth, x-refrtok');
@@ -938,20 +934,19 @@ connectToDatabase(connectionUrl, options)
}
if (isProduction) {
for (let i = 0; i < domains.length; i++) {
const credentials = getCredentials(domains[i].hostname);
const mycredentials = getCredentials(domains[i].hostname);
// console.log('credentials: ', credentials);
httpsServer = https.createServer(credentials, app);
httpsServer = https.createServer(mycredentials, app);
console.log('⭐️⭐️⭐️⭐️⭐️ HTTPS server: ' + domains[i].hostname + ' Port:', domains[i].port + (domains[i].website ? 'WebSite = ' + domains[i].website : ''));
httpsServer.listen(domains[i].port);
}
} else {
if (process.env.HTTPS_LOCALHOST === "true") {
let credentials = null;
let mycredentials = null;
try {
credentials = {
mycredentials = {
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',
@@ -963,8 +958,8 @@ connectToDatabase(connectionUrl, options)
throw error;
}
if (credentials) {
httpsServer = https.createServer(credentials, app);
if (mycredentials) {
httpsServer = https.createServer(mycredentials, app);
console.log('⭐️⭐️⭐️ HTTPS server IN LOCALE : port', port);
httpsServer.listen(port);
} else {