7bd002f666f0b6ba5f253fb3433e2cdbbdc51640
[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 }
25
26 void VPNControl::initialise()
27 {
28 setStatus(VPNSTATUS_UNINITIALISED);
29 }
30
31 void VPNControl::setStatus(VPNSTATUS newStatus)
32 {
33 if (vpnStatus != newStatus) {
34 vpnStatus = newStatus;
35 emit statusChanged(newStatus);
36 printf ("Emitting status %d\n", newStatus);
37 }
38 }
39 int VPNControl::getTlsDirection() const
40 {
41 return tlsDirection;
42 }
43
44 void VPNControl::setTlsDirection(int value)
45 {
46 if (value != tlsDirection) {
47 printf ("TLS direction set to %d\n", value);
48 tlsDirection = value;
49 settingsSetValue("tlsDirection", value);
50 emit tlsDirectionChanged (value);
51 }
52 }
53
54 bool VPNControl::getUseTLS() const
55 {
56 return useTLS;
57 }
58
59 void VPNControl::setUseTLS(bool value)
60 {
61 if (value != useTLS) {
62 printf ("Use TLS set to %d\n", value);
63 useTLS = value;
64 settingsSetValue("useTLS", value);
65 emit useTLSChanged(useTLS);
66 }
67 }
68
69 bool VPNControl::getCompressed() const
70 {
71 return compressed;
72 }
73
74 void VPNControl::setCompressed(bool value)
75 {
76 if (value != compressed) {
77 printf ("Use compression set to %d\n", value);
78 compressed = value;
79 settingsSetValue("compressed", value);
80 emit compressedChanged(compressed);
81 }
82 }
83
84 unsigned int VPNControl::getPort() const
85 {
86 return port;
87 }
88
89 void VPNControl::setPort(unsigned int value)
90 {
91 if (value != port) {
92 printf ("Port set to %d\n", value);
93 port = value;
94 settingsSetValue("port", value);
95 emit portChanged(port);
96 }
97 }
98
99 QString VPNControl::getServer() const
100 {
101 return server;
102 }
103
104 void VPNControl::setServer(const QString &value)
105 {
106 if (value != server) {
107 printf ("Server set to %s\n", value.toUtf8().constData());
108 server = value;
109 settingsSetValue("server", value);
110 emit serverChanged(server);
111 }
112 }
113
114 void VPNControl::settingsSetValue (QString key, QString value) {
115 QSettings settings;
116
117 settings.setValue(key, value);
118 }
119
120 void VPNControl::settingsSetValue (QString key, int value) {
121 QSettings settings;
122
123 settings.setValue(key, value);
124 }
125
126 void VPNControl::vpnConnect() {
127 if (vpnProcess != NULL) {
128 printf ("Process already running.\n");
129 }
130 else {
131 printf ("Connect\n");
132
133 vpnProcess = new QProcess();
134 QString program = "openvpn";
135 collectArguments ();
136 vpnProcess->setReadChannel(QProcess::StandardOutput);
137 connect(vpnProcess, SIGNAL(error(QProcess::ProcessError)), this, SLOT(readError(QProcess::ProcessError)));
138 connect(vpnProcess, SIGNAL(readyRead()), this, SLOT(readData()));
139 connect(vpnProcess, SIGNAL(started()), this, SLOT(started()));
140 connect(vpnProcess, SIGNAL(finished(int)), this, SLOT(finished(int)));
141
142 vpnProcess->start(program, arguments);
143 vpnProcess->closeWriteChannel();
144 setStatus(VPNSTATUS_INITIALISING);
145 arguments.clear();
146 }
147 }
148
149 void VPNControl::collectArguments () {
150 arguments.clear();
151
152 addArgument("config", "/home/nemo/Documents/Configure/OpenVPN/config.ovpn");
153 addArgument("remote", server);
154 addArgument("port", QString::number(port));
155 addOption("comp-lzo", compressed);
156 if (useTLS) {
157 addArgument("tls-auth", "/home/nemo/Documents/Configure/OpenVPN/ta.key");
158 addValue(QString::number(tlsDirection));
159 }
160 addArgument("ca", "/home/nemo/Documents/Configure/OpenVPN/ca.crt");
161 addArgument("cert", "/home/nemo/Documents/Configure/OpenVPN/Jolla.crt");
162 addArgument("key", "/home/nemo/Documents/Configure/OpenVPN/Jolla.key");
163 }
164
165 void VPNControl::addArgument (QString key, QString value) {
166 QString argument;
167
168 argument = "--" + key;
169 arguments.append(argument);
170 if (value != "") {
171 arguments.append(value);
172 }
173 }
174
175 void VPNControl::addArgument (QString key) {
176 QString argument;
177
178 argument = "--" + key;
179 arguments.append(argument);
180 }
181
182 void VPNControl::addOption (QString key, bool add) {
183 if (add) {
184 addArgument (key);
185 }
186 }
187
188 void VPNControl::addValue (QString key) {
189 arguments.append(key);
190 }
191
192 void VPNControl::vpnDisconnect() {
193 if (vpnProcess != NULL) {
194 printf ("Disconnect\n");
195
196 vpnProcess->terminate();
197 setStatus(VPNSTATUS_DISCONNECTING);
198 }
199 }
200
201 void VPNControl::readData() {
202 while (vpnProcess->canReadLine()) {
203 QByteArray read = vpnProcess->readLine();
204 printf ("Output: %s", read.data());
205 if (read.endsWith("Initialization Sequence Completed\n")) {
206 printf ("We're connected!\n");
207 setStatus(VPNSTATUS_CONNECTED);
208 }
209 }
210 }
211
212 void VPNControl::started() {
213 printf ("Started\n");
214 setStatus(VPNSTATUS_CONNECTING);
215 }
216
217 void VPNControl::finished(int code) {
218 printf ("Finished with code %d\n", code);
219 if (vpnProcess != NULL) {
220 //delete vpnProcess;
221 vpnProcess = NULL;
222 }
223 setStatus(VPNSTATUS_UNINITIALISED);
224 }
225
226 void VPNControl::readError(QProcess::ProcessError error)
227 {
228 printf ("Error: %d\n", error);
229 if (vpnProcess != NULL) {
230 QByteArray dataOut = vpnProcess->readAllStandardOutput();
231 QByteArray errorOut = vpnProcess->readAllStandardError();
232
233 printf ("Output text: %s\n", dataOut.data());
234 printf ("Error text: %s\n", errorOut.data());
235 }
236
237 // Disconnect
238 vpnDisconnect();
239 }
240
241 void VPNControl::updateConfiguration()
242 {
243 printf ("Update configuration\n");
244 }