-
Notifications
You must be signed in to change notification settings - Fork 0
/
LaClientMonitor.cpp
182 lines (143 loc) · 4.79 KB
/
LaClientMonitor.cpp
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
#include "LaClientMonitor.h"
#include <QUdpSocket>
#include <QHostAddress>
#include <QTimer>
#include <QApplication>
#include <QProcess>
#include <QFile>
#include <QDir>
#include <QDateTime>
#include <QSharedMemory>
const int SS5_LOG_LOCAL_PORT = 50666;
const int CHECK_CLIENT_PROCESS_TIMER_INTERVAL = 3000;
const int MAX_MONITOR_ATTEMPTS = 3;
LaClientMonitor::LaClientMonitor(QObject *parent)
: QObject(parent)
{
mFailtAttemptCount = 0;
mCheckProcessTimer = new QTimer();
mCheckProcessTimer->setInterval(CHECK_CLIENT_PROCESS_TIMER_INTERVAL);
mReadProcessIdsTimer = new QTimer();
mReadProcessIdsTimer->setInterval(1000);
mReadProcessIdsTimer->start();
mLogFile = NULL;
startNewLogFile();
connect(mCheckProcessTimer, SIGNAL(timeout()), this, SLOT(checkProcess()));
connect(mReadProcessIdsTimer, SIGNAL(timeout()), this, SLOT(readProcessIds()));
/*
QSharedMemory monitorsignature;
monitorsignature.setKey("1234");
if(monitorsignature.create(512,QSharedMemory::ReadWrite)==true) {
monitorsignature.lock();
writeLog("Monitor iniciado.");
} else {
writeLog("Não foi possivel iniciar o monitor.");
exit(0);
} */
mCheckProcessTimer->start();
}
void LaClientMonitor::checkProcess()
{
QSharedMemory shared("61BB200D-3579-453e-9044-");
if(shared.create(512,QSharedMemory::ReadWrite)==true) {
writeLog("Nao foi possivel encontrar o cliente. Finalizando.");
killAllProcess();
} else {
//writeLog("Cliente encontrado." + shared.error());
}
/*
QProcess process;
process.setReadChannel(QProcess::StandardOutput);
process.setReadChannelMode(QProcess::MergedChannels);
process.start("wmic.exe process get description");
process.waitForStarted(1000);
process.waitForFinished(1000);
QByteArray list = process.readAll();
QString processList = QString(list);
if(processList.contains("FasterTunnelClient.exe")) {
qDebug() << "Faster Tunnel is running";
mFailtAttemptCount=0;
}
else {
qDebug() << "Faster Tunnel is NOT running";
mFailtAttemptCount++;
writeLog("Failed to find client process ["
+ QString::number(mFailtAttemptCount) + " Attempts] - " + QString(list));
if(mFailtAttemptCount >= MAX_MONITOR_ATTEMPTS) {
writeLog("Client killed by FailAttempts");
killAllProcess();
}
} */
}
void LaClientMonitor::killAllProcess() {
foreach (int pid, mProcessIdList) {
QProcess *p = new QProcess(this);
QString cmd="taskkill";
QStringList params;
params << "/pid" << QString::number(pid) << "/f";
p->start(cmd,params);
if(!p->waitForFinished()) {
delete p;
return;
}
QString result = QString(p->readAllStandardOutput());
qDebug() << "KillProcess: " << result;
}
QProcess *p1 = new QProcess(this);
QString cmd1="taskkill";
QStringList params1;
params1 << "/im" << "fcca.exe" << "/f";
p1->start(cmd1,params1);
if(!p1->waitForFinished()) {
delete p1;
}
qApp->quit();
}
void LaClientMonitor::disconnectSS5() {
QStringList params;
params << "3" << "2";
QString path = qApp->applicationDirPath() + "/ss5/ss5capcmd.exe";
QProcess *p = new QProcess();
p->start(path, params);
}
void LaClientMonitor::terminateSS5() {
QStringList params;
params << "4" << "4";
QString path = qApp->applicationDirPath() + "/ss5/ss5capcmd.exe";
QProcess *p = new QProcess();
p->start(path, params);
}
void LaClientMonitor::startNewLogFile() {
QString fileName = "monitor_" + QDateTime::currentDateTime().toString("yyyy_MM_dd-hh_mm_ss") +
".log";
QString path = qApp->applicationDirPath() + "/logs/";
if(mLogFile && mLogFile->isOpen())
mLogFile->close();
QDir().mkdir(path);
mLogFile = new QFile(path+fileName);
mLogFile->open(QFile::WriteOnly | QIODevice::Text);
}
void LaClientMonitor::writeLog(QString log) {
if(!mLogFile)
startNewLogFile();
QString time = QTime::currentTime().toString("hh:mm:ss: ");
mLogFile->write(time.toUtf8() + log.toUtf8());
mLogFile->write("\n");
mLogFile->flush();
}
void LaClientMonitor::readProcessIds()
{
QString path = qApp->applicationDirPath() + QDir::separator();
QString fileName = "pids.bin";
QFile * pIdsFile = new QFile(path + fileName);
QDir().mkdir(path);
if(pIdsFile->open(QFile::ReadOnly | QIODevice::Text)) {
mProcessIdList.clear();
QString pIds = QString(pIdsFile->readLine());
QStringList pIdsList = pIds.split(";", QString::SkipEmptyParts);
foreach (QString pid, pIdsList) {
mProcessIdList.append(pid.toInt());
}
pIdsFile->close();
}
}