Added About page.
[openvpnui.git] / src / vpncontrol.cpp
1 #include "vpncontrol.h"
2 #include "stdio.h"
3 #include <QSettings>
4
5
6 VPNControl::VPNControl(QObject *parent) :
7 QObject(parent),
8 vpnProcess(NULL),
9 vpnStatus(VPNSTATUS_INVALID),
10 server(""),
11 port(1194),
12 compressed(true),
13 useTLS(true),
14 tlsDirection(1)
15 {
16 // Read in the settings
17 QSettings settings;
18 settings.setValue("showAll", false);
19
20 // Read configuration settings
21 server = settings.value("server", "127.0.0.1").toString();
22 port = settings.value("port", 1194).toInt();
23 compressed = settings.value("compressed", true).toBool();
24 useTLS = settings.value("useTLS", true).toBool();
25 tlsDirection = settings.value("tlsDirection", 1).toInt();
26 caCertFile = settings.value("caCertFile", "").toString();
27 clientCertFile = settings.value("clientCertFile", "").toString();
28 clientKeyFile = settings.value("clientKeyFile", "").toString();
29 tlsKeyFile = settings.value("tlsKeyFile", "").toString();
30 configFile = settings.value("configFile", "").toString();
31 }
32
33 void VPNControl::initialise()
34 {
35 setStatus(VPNSTATUS_UNINITIALISED);
36 }
37
38 void VPNControl::setStatus(VPNSTATUS newStatus)
39 {
40 if (vpnStatus != newStatus) {
41 vpnStatus = newStatus;
42 emit statusChanged(newStatus);
43 }
44 }
45 int VPNControl::getTlsDirection() const
46 {
47 return tlsDirection;
48 }
49
50 void VPNControl::setTlsDirection(int value)
51 {
52 if (value != tlsDirection) {
53 tlsDirection = value;
54 settingsSetValue("tlsDirection", value);
55 emit tlsDirectionChanged (value);
56 }
57 }
58
59 bool VPNControl::getUseTLS() const
60 {
61 return useTLS;
62 }
63
64 void VPNControl::setUseTLS(bool value)
65 {
66 if (value != useTLS) {
67 useTLS = value;
68 settingsSetValue("useTLS", value);
69 emit useTLSChanged(useTLS);
70 }
71 }
72
73 bool VPNControl::getCompressed() const
74 {
75 return compressed;
76 }
77
78 void VPNControl::setCompressed(bool value)
79 {
80 if (value != compressed) {
81 compressed = value;
82 settingsSetValue("compressed", value);
83 emit compressedChanged(compressed);
84 }
85 }
86
87 unsigned int VPNControl::getPort() const
88 {
89 return port;
90 }
91
92 void VPNControl::setPort(unsigned int value)
93 {
94 if (value != port) {
95 port = value;
96 settingsSetValue("port", value);
97 emit portChanged(port);
98 }
99 }
100
101 QString VPNControl::getServer() const
102 {
103 return server;
104 }
105
106 void VPNControl::setServer(const QString &value)
107 {
108 if (value != server) {
109 server = value;
110 settingsSetValue("server", value);
111 emit serverChanged(server);
112 }
113 }
114
115 QString VPNControl::getCaCertFile () const
116 {
117 return caCertFile;
118 }
119
120 void VPNControl::setCaCertFile(const QString &value)
121 {
122 if (value != caCertFile) {
123 caCertFile = value;
124 settingsSetValue("caCertFile", value);
125 emit caCertFileChanged(caCertFile);
126 }
127 }
128
129 void VPNControl::setClientCertFile(const QString &value)
130 {
131 if (value != clientCertFile) {
132 clientCertFile = value;
133 settingsSetValue("clientCertFile", value);
134 emit clientCertFileChanged(clientCertFile);
135 }
136 }
137
138 void VPNControl::setClientKeyFile(const QString &value)
139 {
140 if (value != clientKeyFile) {
141 clientKeyFile = value;
142 settingsSetValue("clientKeyFile", value);
143 emit clientKeyFileChanged(clientKeyFile);
144 }
145 }
146
147 void VPNControl::setTlsKeyFile(const QString &value)
148 {
149 if (value != tlsKeyFile) {
150 tlsKeyFile = value;
151 settingsSetValue("tlsKeyFile", value);
152 emit tlsKeyFileChanged(tlsKeyFile);
153 }
154 }
155
156 void VPNControl::setConfigFile(const QString &value)
157 {
158 if (value != configFile) {
159 configFile = value;
160 settingsSetValue("configFile", value);
161 emit tlsKeyFileChanged(configFile);
162 }
163 }
164
165 QString VPNControl::getClientCertFile () const
166 {
167 return clientCertFile;
168 }
169
170 QString VPNControl::getClientKeyFile () const
171 {
172 return clientKeyFile;
173 }
174
175 QString VPNControl::getTlsKeyFile () const
176 {
177 return tlsKeyFile;
178 }
179
180 QString VPNControl::getConfigFile () const
181 {
182 return configFile;
183 }
184
185 QString VPNControl::getLogText() const
186 {
187 return logText;
188 }
189
190 void VPNControl::setLogText(const QString &value)
191 {
192 logText = value;
193 emit logTextChanged(logText);
194 }
195
196 void VPNControl::settingsSetValue (QString key, QString value) {
197 QSettings settings;
198
199 settings.setValue(key, value);
200 }
201
202 void VPNControl::settingsSetValue (QString key, int value) {
203 QSettings settings;
204
205 settings.setValue(key, value);
206 }
207
208 void VPNControl::vpnConnect() {
209 if (vpnProcess != NULL) {
210 printf ("Process already running.\n");
211 }
212 else {
213 vpnProcess = new QProcess();
214 QString program = "/usr/share/harbour-openvpn-rig/bin/ovpnpermit";
215 collectArguments ();
216 vpnProcess->setReadChannel(QProcess::StandardOutput);
217 connect(vpnProcess, SIGNAL(error(QProcess::ProcessError)), this, SLOT(readError(QProcess::ProcessError)));
218 connect(vpnProcess, SIGNAL(readyRead()), this, SLOT(readData()));
219 connect(vpnProcess, SIGNAL(started()), this, SLOT(started()));
220 connect(vpnProcess, SIGNAL(finished(int)), this, SLOT(finished(int)));
221
222 vpnProcess->start(program, arguments);
223 vpnProcess->closeWriteChannel();
224 setStatus(VPNSTATUS_INITIALISING);
225 arguments.clear();
226 }
227 }
228
229 void VPNControl::collectArguments () {
230 arguments.clear();
231
232 addArgumentNonempty("config", configFile);
233 addOption("client", true);
234 addOption("persist-key", true);
235 addOption("persist-tun", true);
236 addOption("nobind", true);
237 addArgument("resolv-retry", "infinite");
238 addArgument("dev", "tun");
239 addArgument("verb", "3");
240 addArgument("proto", "udp");
241 addArgument("user", "nemo");
242 addArgument("group", "nemo");
243 addArgumentNonempty("remote", server);
244 addArgumentNonempty("port", QString::number(port));
245 addOption("comp-lzo", compressed);
246 if ((useTLS) && (!tlsKeyFile.isEmpty())) {
247 addArgument("tls-auth", tlsKeyFile);
248 addValue(QString::number(tlsDirection));
249 }
250 addArgumentNonempty("ca", caCertFile);
251 addArgumentNonempty("cert", clientCertFile);
252 addArgumentNonempty("key", clientKeyFile);
253 }
254
255 void VPNControl::addArgument (QString key, QString value) {
256 QString argument;
257
258 argument = "--" + key;
259 arguments.append(argument);
260 if (!value.isEmpty()) {
261 arguments.append(value);
262 }
263 }
264
265 void VPNControl::addArgumentNonempty (QString key, QString value) {
266 QString argument;
267
268 if (!value.isEmpty()) {
269 argument = "--" + key;
270 arguments.append(argument);
271 arguments.append(value);
272 }
273 }
274
275 void VPNControl::addArgument (QString key) {
276 QString argument;
277
278 argument = "--" + key;
279 arguments.append(argument);
280 }
281
282 void VPNControl::addOption (QString key, bool add) {
283 if (add) {
284 addArgument (key);
285 }
286 }
287
288 void VPNControl::addValue (QString key) {
289 arguments.append(key);
290 }
291
292 void VPNControl::vpnDisconnect() {
293 if (vpnProcess != NULL) {
294
295 vpnProcess->terminate();
296 setStatus(VPNSTATUS_DISCONNECTING);
297 }
298 }
299
300 void VPNControl::readData() {
301 while (vpnProcess->canReadLine()) {
302 QByteArray read = vpnProcess->readLine();
303 //printf ("Output: %s", read.data());
304
305 logAppend(read);
306
307 if (read.endsWith("Initialization Sequence Completed\n")) {
308 setStatus(VPNSTATUS_CONNECTED);
309 }
310 }
311 }
312
313 void VPNControl::started() {
314 setStatus(VPNSTATUS_CONNECTING);
315 }
316
317 void VPNControl::finished(int code) {
318 if (vpnProcess != NULL) {
319 //delete vpnProcess;
320 vpnProcess = NULL;
321 }
322 logAppend("Finished with code " + QString::number(code));
323 setStatus(VPNSTATUS_UNINITIALISED);
324 }
325
326 void VPNControl::readError(QProcess::ProcessError error)
327 {
328 printf ("Error: %d\n", error);
329 if (vpnProcess != NULL) {
330 QByteArray dataOut = vpnProcess->readAllStandardOutput();
331 QByteArray errorOut = vpnProcess->readAllStandardError();
332
333 printf ("Output text: %s\n", dataOut.data());
334 printf ("Error text: %s\n", errorOut.data());
335 }
336
337 // Disconnect
338 vpnDisconnect();
339 }
340
341 void VPNControl::updateConfiguration()
342 {
343 printf ("Update configuration\n");
344 }
345
346 void VPNControl::logAppend(const QString &text)
347 {
348 if (!text.isEmpty()) {
349 QString append = text;
350 // Ensure we end with a newline
351 if (!append.endsWith('\n')) {
352 append += '\n';
353 }
354 // How many lines to add
355 int newLines = append.count('\n');
356 int currentLines = logText.count('\n');
357 int removeLines = currentLines + newLines - 24;
358
359 // Remove excess lines from the top
360 while (removeLines > 0) {
361 int nextLine = logText.indexOf('\n');
362 if (nextLine > 0) {
363 logText = logText.mid(nextLine + 1);
364 }
365 removeLines--;
366 }
367
368 // Add new lines
369 logText.append(append);
370 emit logTextChanged(logText);
371 }
372 }
373