- Cloudflare e configurazione dominio DNS (host, host_test, host_ip, ecc...)
This commit is contained in:
@@ -57,12 +57,6 @@ const catalogo = new Schema(
|
||||
);
|
||||
|
||||
const MyElemSchema = new Schema({
|
||||
_id: {
|
||||
type: ObjectId,
|
||||
default: function () {
|
||||
return new ObjectId();
|
||||
},
|
||||
},
|
||||
idapp: {
|
||||
type: String,
|
||||
},
|
||||
|
||||
@@ -37,9 +37,33 @@ const SiteSchema = new Schema({
|
||||
host: {
|
||||
type: String,
|
||||
},
|
||||
host_ip: {
|
||||
type: String,
|
||||
},
|
||||
host_test: {
|
||||
type: String,
|
||||
},
|
||||
host_test_ip: {
|
||||
type: String,
|
||||
},
|
||||
host_api: {
|
||||
type: String,
|
||||
},
|
||||
host_api_ip: {
|
||||
type: String,
|
||||
},
|
||||
host_testapi: {
|
||||
type: String,
|
||||
},
|
||||
host_testapi_ip: {
|
||||
type: String,
|
||||
},
|
||||
cf_token: {
|
||||
type: String,
|
||||
},
|
||||
cf_zoneId: {
|
||||
type: String,
|
||||
},
|
||||
portapp: {
|
||||
type: String,
|
||||
},
|
||||
@@ -135,6 +159,11 @@ const SiteSchema = new Schema({
|
||||
enableAI: { type: Boolean, default: false },
|
||||
enableGroups: { type: Boolean, default: false },
|
||||
enableCircuits: { type: Boolean, default: false },
|
||||
enableGoods: { type: Boolean, default: false },
|
||||
enableServices: { type: Boolean, default: false },
|
||||
enableActivities: { type: Boolean, default: false },
|
||||
enableHosps: { type: Boolean, default: false },
|
||||
enableEvents: { type: Boolean, default: false },
|
||||
enableProj: { type: Boolean, default: false },
|
||||
enableTodos: { type: Boolean, default: false },
|
||||
enableRegByBot: { type: Boolean, default: false },
|
||||
@@ -226,6 +255,17 @@ module.exports.executeQueryTable = async function (idapp, params, userreq) {
|
||||
if (User.isAdmin(userreq.perm)) {
|
||||
const myarr = await Site.find({});
|
||||
|
||||
if (myarr.length === 0) {
|
||||
/* {
|
||||
"_id" : ObjectId("620a71e194438ecd1acfdbca"),
|
||||
"idapp" : "14",
|
||||
"chiave" : "vers",
|
||||
"userId" : "ALL",
|
||||
"valore" : "0.3.21"
|
||||
}*/
|
||||
|
||||
}
|
||||
|
||||
// return await Site.find({}).lean();
|
||||
return ({ count: myarr.length, rows: myarr })
|
||||
|
||||
@@ -351,7 +391,7 @@ module.exports.createFirstUserAdmin = async function () {
|
||||
const numusers = await User.countDocuments({ idapp: mysite.idapp });
|
||||
if (numusers === 0) {
|
||||
// Non esistono utenti, quindi creo quello di Admin
|
||||
|
||||
|
||||
const utenteadmin = { idapp: '13', username: telegrambot.ADMIN_USER_SERVER };
|
||||
|
||||
const newuser = new User(utenteadmin);
|
||||
|
||||
@@ -33,6 +33,16 @@ class CloudFlare {
|
||||
// Estrai i dati dalla risposta
|
||||
this.zones = response.data.result;
|
||||
|
||||
if (this.zones && this.zones.length > 0) {
|
||||
for (const zone of this.zones) {
|
||||
let domain = zone.name;
|
||||
|
||||
await this.findAndUpdateOnSite(domain, zone, apiToken);
|
||||
|
||||
// Cerca su Site
|
||||
}
|
||||
}
|
||||
|
||||
return this.zones;
|
||||
|
||||
// Stampa le zone
|
||||
@@ -42,6 +52,40 @@ class CloudFlare {
|
||||
}
|
||||
}
|
||||
|
||||
async findAndUpdateOnSite(domain, zone, apiToken) {
|
||||
try {
|
||||
const Site = require('../models/site');
|
||||
const recsite = await Site.findOne({ host: domain });
|
||||
|
||||
if (recsite) {
|
||||
// Aggiorna i parametri token e zoneId
|
||||
|
||||
let modif = (recsite.cf_token !== apiToken) || (recsite.cf_zoneId !== zone.id);
|
||||
|
||||
if (modif) {
|
||||
// update the Site
|
||||
|
||||
await Site.findOneAndUpdate({ _id: recsite._id }, {
|
||||
$set: {
|
||||
cf_token: apiToken,
|
||||
cf_zoneId: zone.id
|
||||
}
|
||||
}, { new: true })
|
||||
.then((updatedSite) => {
|
||||
console.log('Site aggiornato:', updatedSite);
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error('Errore durante l\'aggiornamento del Site:', error);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.error('Errore durante il recupero dei record DNS di Cloudflare:', error.message);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Funzione per estrarre i record DNS
|
||||
async fetchDNSRecords(apiToken, zoneId) {
|
||||
const apiUrlDNS = apiUrl + `/zones/${zoneId}/dns_records`;
|
||||
@@ -85,6 +129,48 @@ class CloudFlare {
|
||||
console.error('Errore durante l\'aggiornamento del record DNS:', error.message);
|
||||
}
|
||||
}
|
||||
|
||||
async setCorrectIpsOnDNS(domainrec) {
|
||||
|
||||
const arrparams = [{ urladd: '', paramsite: 'host_ip' }, { urladd: 'test.', paramsite: 'host_test_ip' }, { urladd: 'api.', paramsite: 'host_api_ip' }, { urladd: 'testapi.', paramsite: 'host_testapi_ip' }]
|
||||
|
||||
for (let i = 0; i < arrparams.length; i++) {
|
||||
this.setSingleIpsOnDNS(domainrec.name, arrparams[i])
|
||||
}
|
||||
|
||||
// pause for 3 second
|
||||
await new Promise(resolve => setTimeout(resolve, 3000));
|
||||
}
|
||||
|
||||
async setSingleIpsOnDNS(domain, paramobj) {
|
||||
try {
|
||||
// get the parameters (Token and zoneId on Database)
|
||||
const Site = require('../models/site');
|
||||
|
||||
await Site.findOne({ host: domain }).lean()
|
||||
.then(async (site) => {
|
||||
if (site) {
|
||||
|
||||
if (site.cf_token && site.cf_zoneId) {
|
||||
const myarrrecdns = await this.fetchDNSRecords(site.cf_token, site.cf_zoneId);
|
||||
|
||||
// find
|
||||
let recTypeA = await myarrrecdns.find((rec) => rec.type === 'A' && (rec.name === (paramobj.urladd + domain)));
|
||||
|
||||
if (recTypeA && site[paramobj.paramsite]) {
|
||||
if (recTypeA.content !== site[paramobj.paramsite]) {
|
||||
recTypeA.content = site[paramobj.paramsite];
|
||||
return this.updateDNSRecord(site.cf_token, site.cf_zoneId, recTypeA.id, recTypeA);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
} catch (e) {
|
||||
console.error('e', e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = CloudFlare
|
||||
@@ -1224,6 +1224,8 @@ router.post('/cloudflare', authenticate, async (req, res) => {
|
||||
result = await cf.fetchDNSRecords(tok, zoneId);
|
||||
} else if (cmd === "setRecordDNS") {
|
||||
result = await cf.updateDNSRecord(tok, zoneId, dnsRecordId, record);
|
||||
} else if (cmd === "setCorrectIpsOnDNS") {
|
||||
result = await cf.setCorrectIpsOnDNS(record);
|
||||
} else if (cmd === "gettok") {
|
||||
result = JSON.parse(process.env.CLOUDFLARE_TOKENS);
|
||||
}
|
||||
|
||||
@@ -1708,8 +1708,24 @@ router.get(process.env.LINK_CHECK_UPDATES, authenticate_noerror, async (req, res
|
||||
|
||||
await CfgServer.find({ idapp }).then(async (arrcfgrec) => {
|
||||
|
||||
if (!arrcfgrec)
|
||||
return res.status(404).send();
|
||||
if (arrcfgrec.length === 0) {
|
||||
if (User.isAdmin(req.user.perm)) {
|
||||
// crea un nuovo record
|
||||
const mycfgServer = new CfgServer();
|
||||
mycfgServer.idapp = idapp;
|
||||
mycfgServer.chiave = 'vers';
|
||||
mycfgServer.userId = 'ALL';
|
||||
mycfgServer.valore = await tools.getVersServer();
|
||||
|
||||
mycfgServer.save();
|
||||
|
||||
arrcfgrec = await CfgServer.find({ idapp })
|
||||
} else {
|
||||
return res.status(404).send();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
// ++Add to Log Stat ....
|
||||
|
||||
@@ -1887,8 +1903,7 @@ function uploadFile(req, res, version) {
|
||||
tofile = mydir + '/' + file.originalFilename;
|
||||
let newname = tofile;
|
||||
file.path = newname;
|
||||
// console.log('fromfile', fromfile)
|
||||
// console.log('tofile', tofile);
|
||||
console.log('fromfile', fromfile, 'tofile', tofile)
|
||||
|
||||
|
||||
if (!tools.sulServer()) {
|
||||
|
||||
@@ -866,12 +866,13 @@ function startServer(app, port) {
|
||||
|
||||
let scriptProcess = null;
|
||||
|
||||
try {
|
||||
const pty = require('node-pty');
|
||||
|
||||
const pty = require('node-pty');
|
||||
ws.on('message', (message) => {
|
||||
|
||||
ws.on('message', (message) => {
|
||||
const parsedMessage = JSON.parse(message);
|
||||
const parsedMessage = JSON.parse(message);
|
||||
|
||||
try {
|
||||
|
||||
if ((parsedMessage.type === 'start_script') && (User.isAdminById(parsedMessage.user_id))) {
|
||||
if (scriptProcess) {
|
||||
@@ -929,17 +930,19 @@ function startServer(app, port) {
|
||||
scriptProcess.write(parsedMessage.data + '\n');
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
ws.on('close', () => {
|
||||
console.log('*** Client socket disconnected');
|
||||
if (scriptProcess) {
|
||||
scriptProcess.kill();
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('connection: Errore durante l\'inizializzazione del WebSocket, error:', error.message);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Errore durante l\'elaborazione del messaggio:', error.message);
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
ws.on('close', () => {
|
||||
console.log('*** Client socket disconnected');
|
||||
if (scriptProcess) {
|
||||
scriptProcess.kill();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
} else {
|
||||
|
||||
@@ -1 +1 @@
|
||||
1.0.56
|
||||
1.0.57
|
||||
Reference in New Issue
Block a user