-
Notifications
You must be signed in to change notification settings - Fork 3
/
servicecontroller.cpp
237 lines (192 loc) · 7.75 KB
/
servicecontroller.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
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
233
234
235
236
237
#include "servicecontroller.h"
#include <tchar.h>
ServiceController::ServiceController(QWidget *parent) :
QDialog(parent) {
// Timer für den Dienst
servTimer = new QTimer(this);
connect(servTimer, SIGNAL(timeout()), this, SLOT(checkService()));
serviceRunning = false;
}
void ServiceController::startService() {
// Need WinAPI to controll the service
if (!this->isServiceRunning()) {
SC_HANDLE schSCManager;
SC_HANDLE schService;
SERVICE_STATUS ssStatus;
DWORD dwOldCheckPoint;
DWORD dwStartTickCount;
DWORD dwWaitTime;
// Paras for Start Service
DWORD dwArgc = 2;
// Pointer to arguments array
// Field 0 = service name
// Field 1 = config to load
LPCTSTR *lpszArgv = new LPCTSTR[2];
lpszArgv[0] = (LPTSTR) (LPCSTR) QString ("OpenVPNService").utf16();
lpszArgv[1] = (LPTSTR) (LPCSTR) QString ("Simple").utf16();
// Auf Parent zugreifen
Preferences *prefDialog = dynamic_cast<Preferences*> (this->parent());
prefDialog->setIcon(Connecting);
prefDialog->showTrayMessage("Starting service ...", QSystemTrayIcon::Information);
// Connect to the SC Manager
schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT);
if (NULL == schSCManager) {
// Connetion faild
prefDialog->setIcon(Error);
prefDialog->showTrayMessage("Create handle to SC Manager failed!", QSystemTrayIcon::Critical);
return;
}
// Try to open OpenVPN service
schService = OpenService(schSCManager, (WCHAR*) QString ("OpenVPNService").utf16(), SERVICE_START | SERVICE_QUERY_STATUS);
if (schService == NULL) {
// Open service failed
prefDialog->setIcon(Error);
prefDialog->showTrayMessage("Open Service SC Manager failed!", QSystemTrayIcon::Critical);
return;
}
if (!StartService(schService, dwArgc, lpszArgv)) {
// Service cant be started
prefDialog->setIcon(Error);
prefDialog->showTrayMessage("Starting service failed!", QSystemTrayIcon::Critical);
return;
}
if (!QueryServiceStatus(schService, &ssStatus)) {
// Service cant be query
prefDialog->setIcon(Error);
prefDialog->showTrayMessage("Service query failed!", QSystemTrayIcon::Critical);
return;
}
dwStartTickCount = GetTickCount();
dwOldCheckPoint = ssStatus.dwCheckPoint;
while (ssStatus.dwCurrentState == SERVICE_START_PENDING) {
dwWaitTime = ssStatus.dwWaitHint / 10;
if( dwWaitTime < 1000 )
dwWaitTime = 1000;
else if ( dwWaitTime > 5000 )
dwWaitTime = 5000;
Sleep( dwWaitTime );
// Check the status again.
if (!QueryServiceStatus(schService, &ssStatus))
break;
if ( ssStatus.dwCheckPoint > dwOldCheckPoint )
{
// The service is making progress.
dwStartTickCount = GetTickCount();
dwOldCheckPoint = ssStatus.dwCheckPoint;
}
else
{
if(GetTickCount()-dwStartTickCount > ssStatus.dwWaitHint)
{
break;
}
}
} // end while
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
if (ssStatus.dwCurrentState != SERVICE_RUNNING) {
// Service didnt started
prefDialog->setIcon(Error);
prefDialog->showTrayMessage("Service didnt started!", QSystemTrayIcon::Critical);
return;
}
// Dienst gestartet und connected
prefDialog->setIcon(Connected);
prefDialog->showTrayMessage("OpenVPN started successfully.", QSystemTrayIcon::Information);
serviceRunning = true;
// Timer starten
servTimer->start(3000);
}
} // end startService
void ServiceController::stopService() {
if (this->isServiceRunning()) {
SC_HANDLE schSCManager;
SC_HANDLE schService;
SERVICE_STATUS ssStatus;
Preferences *prefDialog = dynamic_cast<Preferences*> (this->parent());
prefDialog->showTrayMessage("Stopping service ...", QSystemTrayIcon::Information);
schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT);
if (NULL == schSCManager) {
// Connetion faild
prefDialog->setIcon(Error);
prefDialog->showTrayMessage("Create handle to SC Manager failed!", QSystemTrayIcon::Critical);
return;
}
schService = OpenService(schSCManager, (WCHAR*) QString ("OpenVPNService").utf16(), SERVICE_STOP);
if (schService == NULL) {
// Open service failed
prefDialog->setIcon(Error);
prefDialog->showTrayMessage("Open Service SC Manager failed!", QSystemTrayIcon::Critical);
return;
}
if (!ControlService(schService, SERVICE_CONTROL_STOP, &ssStatus) ) {
// Stop service failed
prefDialog->setIcon(Error);
prefDialog->showTrayMessage("Stop Service SC Manager failed!", QSystemTrayIcon::Critical);
return;
}
prefDialog->setIcon(Inaktiv);
prefDialog->showTrayMessage("Service stopped successfully.", QSystemTrayIcon::Information);
// TImer stoppen
servTimer->stop();
serviceRunning = false;
}
}
void ServiceController::restartService() {
this->stopService();
QTimer::singleShot(1500, this, SLOT(shotStart()));
}
void ServiceController::checkService() {
SC_HANDLE schSCManager;
SC_HANDLE schService;
SERVICE_STATUS ssStatus;
Preferences *prefDialog = dynamic_cast<Preferences*> (this->parent());
schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT);
if (NULL == schSCManager) {
// not running
prefDialog->setIcon(Inaktiv);
prefDialog->setStatusMessage(QString("OpenVPN service is stopped."));
servTimer->stop();
serviceRunning = false;
return;
}
schService = OpenService(schSCManager, (WCHAR*) QString ("OpenVPNService").utf16(), SERVICE_QUERY_STATUS);
if (schService == NULL) {
// not running
prefDialog->setIcon(Inaktiv);
prefDialog->setStatusMessage(QString("OpenVPN service is stopped."));
servTimer->stop();
serviceRunning = false;
return;
}
if (!QueryServiceStatus(schService, &ssStatus)) {
// not running
prefDialog->setIcon(Inaktiv);
prefDialog->setStatusMessage(QString("OpenVPN service is stopped."));
servTimer->stop();
serviceRunning = false;
return;
}
if (ssStatus.dwCurrentState == SERVICE_RUNNING) {
// running
prefDialog->setIcon(Connected);
prefDialog->setStatusMessage(QString("OpenVPN service is running."));
serviceRunning = true;
if (!servTimer->isActive()) {
servTimer->start(3000);
}
} else {
// not running
prefDialog->setIcon(Inaktiv);
prefDialog->setStatusMessage(QString("OpenVPN service is stopped."));
serviceRunning = false;
servTimer->stop();
}
}
void ServiceController::shotStart() {
this->startService();
}
bool ServiceController::isServiceRunning() {
this->checkService();
return this->serviceRunning;
}