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