cacictimer.cpp
10.7 KB
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
#include "cacictimer.h"
CacicTimer::CacicTimer()
{
OCacicComm = new CacicComm();
ccacic = new CCacic();
timer = new QTimer(this);
logManager = QLogger::QLoggerManager::getInstance();
OCacicComm = CacicComm("http://teste.cacic.cc",
"rG/HcIDVTZ3pPKCf[[MAIS]]I6aigUb7OMeij3FfC7qNaznk0rBRYb6q6kSK3eNfjgptS8BfwW5yJqCvD2ai7xlw9I6P21j6cvQUqlHmAJpCbfwR13urdRE9AhjfokMcPrH6R1/zXPGMHveLRRgKLcqWu2X96rmoQQdRq9EW1SXmYUAx1dCV[[MAIS]]3Ha61XBw5pq58q35zH8Gjt998rTi3ecV8ShXXevqyK[[MAIS]]W07xpgTjbbd6Fbs/35gPfdRRgMNFjq7Gq[[MAIS]]llFgYMJ6UcEhw8f0ZLQo2oL/eRW/CNyeBW6wG0hIo6EIdpi/Ht0/95Uwn2Og[[MAIS]]2UPXsmKKuTMeGwUvPdbEfexlinUO0[[MAIS]]j9qIa2dpjtl0Y5Fyk1Bvw2ZYRTXwgJpUHsBboWmtTFpgX3wSGOW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F[[MAIS]]rXxiOSk66M0Hq[[MAIS]]OeUBh4z3Ra6f9yPJn0K9hIQyT[[MAIS]]Dr/QW/QdJIOkLTW8ZyyAF4DW9ojiTlUdySvB5EB1jyZ5nNTpzyE38qIdg7uM1tdWdm9FZGYxMdHwi1tS2dBeIWkDfWEjih01xiVt2dmdRgo0zIZvmmYDJ9LhCU3XPDlwdeQ8[[MAIS]]F7fnxDfxGqZQjZJ[[MAIS]]Y/Q8bdLpgbhcd3gt3SzjMLE0M[[MAIS]]hTmHG1KP2CzCibZ7grFAN3Y3jSPjmEj5Fzs26tdhdjNzSIt1FCueea8P2c4NmxPYkJCg0D1mgNrkH3L5svsO[[MAIS]]6YYrl3df4s[[MAIS]]fcK/to9s/pnkKfBKrAPF6Z4s2WPsALUPBDS5ty7RC2vqXRvH6hPR4o7xhXzQOQ/1xytr/RoEWEVeZ2wgHVVbbtFsALul/IDc7B45wmwxBx9iZHVwpQJVlNZZUBtwKqKhnXz5pM[[MAIS]]7yUAkfhMcO09pPd5kB2MBdRJs[[MAIS]]2FIk0GRUw05IHkc1rZEX5Ow==__CRYPTED__",
"Yot8BeM9lOh431SB7dYQXw==__CRYPTED__",
"Dcr8b5IfZOJjt6qyH5dGyw==__CRYPTED__",
"2.5.1.1.256.32",
"2.8.1.7",
"2.8.1.6");
OCacicComm->setUrlSsl("https://10.1.0.137/cacic/web/app_dev.php");
OCacicComm->setUsuario("cacic");
OCacicComm->setPassword("cacic123");
connect(timer,SIGNAL(timeout()),this,SLOT(mslot()));
}
CacicTimer::~CacicTimer()
{
logManager->closeLogger();
delete logManager;
delete ccacic;
delete OCacicComm;
}
void CacicTimer::iniciarTimer(int x, QString applicationDirPath)
{
setApplicationDirPath(applicationDirPath);
logManager->addDestination(getApplicationDirPath().append("/cacicLog.txt"), QStringList("Cacic Daemon"), QLogger::InfoLevel);
logManager->addDestination(getApplicationDirPath().append("/cacicLog.txt"), QStringList("Cacic Daemon"), QLogger::ErrorLevel);
timer->start(x);
}
void CacicTimer::mslot(){
if(getTest()){
//manager->QLogger::QLog_Trace("ModuleName", "Message: ");
if(getConfig()){
qDebug() << "getConfig() success. - " + QDateTime::currentDateTime().toLocalTime().toString();
QLogger::QLog_Info("Cacic Daemon", "getConfig() executado com sucesso.");
// Compara o novo arquivo de configuração com um antigo e se forem diferentes
// mantem o mais recente; caso iguais simplesmente apaga o novo.
QFile *fileOld;
QFile *fileNew;
fileOld = new QFile(getApplicationDirPath().append("/getConfig.json"));
fileNew = new QFile(getApplicationDirPath().append("/getConfigNew.json"));
if( fileOld->exists() && fileNew->exists() ){
if( Md5IsEqual(QVariant::fromValue(fileOld), QVariant::fromValue(fileNew)) ) {
fileNew->remove();
} else {
// Renomeia getConfigNew.json para getConfig.json
fileOld->remove();
fileNew->rename("getConfigNew.json","getConfig.json");
}
jsonConfig = ccacic->getJsonFromFile(getApplicationDirPath().append("/getConfig.json"));
} else if( fileOld->exists() ){
jsonConfig = ccacic->getJsonFromFile(getApplicationDirPath().append("/getConfig.json"));
} else {
QLogger::QLog_Error("Cacic Daemon", "Arquivo de configuração não criado.");
}
delete fileOld;
delete fileNew;
/* lê json de configurações e armazena quais módulos executáveis.
* E faz o mesmo tipo de comparação de hashs, com o fim de:
* ou mantem o binário do módulo ou baixa um novo.
*/
/* Aqui estou assumindo um formato do .json em que:
* há a key modulos contem uma lista com o nome dos executaveis e os seus valores hash md5
* há a key metodo que explicita o método de download dos executaveis
*/
foreach( QJsonValue moduloValues, jsonConfig["modulos"].toArray() ) {
QString moduloKey, moduloValue;
moduloKey = moduloValues.toObject()["md5"].toString();
moduloValue = moduloValues.toObject()["nome"].toString();
moduleMap.insert(moduloKey, moduloValue);
}
metodoDownload = jsonConfig["metodo"].toString();
int countExecNotFound = 0;
QMap<QString, QString>::const_iterator mapIterator = moduleMap.constBegin();
while (mapIterator != moduleMap.constEnd()) {
QString nomeModulo = mapIterator.value();
QString hashModulo = mapIterator.key();
// Calcula hash do binario atual
#if defined(Q_OS_WIN)
fileOld = new QFile(getApplicationDirPath().append("/").append(modulo).append(".exe"));
#else
fileOld = new QFile(getApplicationDirPath().append("/").append(nomeModulo));
#endif
if(!fileOld->exists()) {
QLogger::QLog_Error("Cacic Daemon", QString("Módulo ").append(nomeModulo).append(" não encontrado."));
countExecNotFound++;
if( countExecNotFound == moduleMap.size() ) {
QLogger::QLog_Error("Cacic Daemon", "Não foi possível encontrar nenhum módulo executável!");
return;
}
continue;
}
QString oldMd5 = QString(QCryptographicHash::hash(fileOld->readAll(),QCryptographicHash::Md5).toHex());
if ( oldMd5 != hashModulo ) {
fileOld->rename(getApplicationDirPath().append("/").append(nomeModulo),
getApplicationDirPath().append("/").append("nomeModulo").append("Old") );
// Download nova versão do executável
if(metodoDownload == "ftp" || metodoDownload == "") {
// OCacicComm->ftpDownload( QString("/ws/get/").append(nomeModulo) );
}
fileOld->remove();
delete fileOld;
}
mapIterator++;
}
//aqui irá abrir o semaforo e iniciar a coleta.
}else{
qDebug() << "getConfig() failed. - " + QDateTime::currentDateTime().toLocalTime().toString();
QLogger::QLog_Error("Cacic Daemon", "Falha na obtenção do arquivo de configuração.");
}
}else{
qDebug() << "getTest() failed. - " + QDateTime::currentDateTime().toLocalTime().toString();
QLogger::QLog_Error("Cacic Daemon", "Falha na execução do getTest().");
}
}
bool CacicTimer::getTest(){
try{
OCacicComm->setUrlGerente("http://10.1.0.137/cacic/web/app_dev.php");
OCacicComm->setUsuario("cacic");
OCacicComm->setPassword("cacic123");
bool ok;
QJsonObject as;
as["computador"] = OCacic_Computer.toJsonObject();
QJsonObject jsonresult = OCacicComm->comm("/ws/neo/login", &ok, as);
if(jsonresult.contains("error")){
return false;
}
try{
ccacic->setJsonToFile(jsonresult, getApplicationDirPath() + "/getTest.json");
return true;
} catch (...) {
qDebug() << "Erro ao salvar o arquivo de configurações.";
return false;
}
} catch (...){
qDebug() << "Erro ao conectar com o servidor.";
return false;
}
}
bool CacicTimer::getConfig(){
try{
OCacicComm->setUrlGerente("http://10.1.0.137/cacic/web/app_dev.php");
OCacicComm->setUsuario("cacic");
OCacicComm->setPassword("cacic123");
bool ok;
QJsonObject as;
as["computador"] = OCacic_Computer.toJsonObject();
QJsonObject jsonresult = OCacicComm->comm("/ws/neo/login", &ok, as);
if(jsonresult.contains("error")){
return false;
}
try{
ccacic->setJsonToFile(jsonresult, getApplicationDirPath().append("/getConfigNew.json"));
return true;
} catch (...) {
qDebug() << "Erro ao salvar o arquivo de configurações.";
return false;
}
} catch (...){
qDebug() << "Erro ao conectar com o servidor.";
return false;
}
}
void CacicTimer::registraInicio()
{
}
QString CacicTimer::getApplicationDirPath() const
{
return applicationDirPath;
}
void CacicTimer::setApplicationDirPath(const QString &value)
{
applicationDirPath = value;
}
void CacicTimer::registraFim()
{
}
bool CacicTimer::Md5IsEqual(QVariant document01,QVariant document02){
QString getconfigMD5 = QString(QCryptographicHash::hash(
(document01.toByteArray()),QCryptographicHash::Md5).toHex());
QString getconfigMD52 = QString(QCryptographicHash::hash(
(document02.toByteArray()),QCryptographicHash::Md5).toHex());
if(getconfigMD5 == getconfigMD52){
return true;
}else{
return false;
}
}