1 #include "vpncontrol.h"
6 VPNControl::VPNControl(QObject
*parent
) :
9 vpnStatus(VPNSTATUS_INVALID
),
16 // Read in the settings
18 settings
.setValue("showAll", false);
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();
33 void VPNControl::initialise()
35 setStatus(VPNSTATUS_UNINITIALISED
);
38 void VPNControl::setStatus(VPNSTATUS newStatus
)
40 if (vpnStatus
!= newStatus
) {
41 vpnStatus
= newStatus
;
42 emit
statusChanged(newStatus
);
45 int VPNControl::getTlsDirection() const
50 void VPNControl::setTlsDirection(int value
)
52 if (value
!= tlsDirection
) {
54 settingsSetValue("tlsDirection", value
);
55 emit
tlsDirectionChanged (value
);
59 bool VPNControl::getUseTLS() const
64 void VPNControl::setUseTLS(bool value
)
66 if (value
!= useTLS
) {
68 settingsSetValue("useTLS", value
);
69 emit
useTLSChanged(useTLS
);
73 bool VPNControl::getCompressed() const
78 void VPNControl::setCompressed(bool value
)
80 if (value
!= compressed
) {
82 settingsSetValue("compressed", value
);
83 emit
compressedChanged(compressed
);
87 unsigned int VPNControl::getPort() const
92 void VPNControl::setPort(unsigned int value
)
96 settingsSetValue("port", value
);
97 emit
portChanged(port
);
101 QString
VPNControl::getServer() const
106 void VPNControl::setServer(const QString
&value
)
108 if (value
!= server
) {
110 settingsSetValue("server", value
);
111 emit
serverChanged(server
);
115 QString
VPNControl::getCaCertFile () const
120 void VPNControl::setCaCertFile(const QString
&value
)
122 if (value
!= caCertFile
) {
124 settingsSetValue("caCertFile", value
);
125 emit
caCertFileChanged(caCertFile
);
129 void VPNControl::setClientCertFile(const QString
&value
)
131 if (value
!= clientCertFile
) {
132 clientCertFile
= value
;
133 settingsSetValue("clientCertFile", value
);
134 emit
clientCertFileChanged(clientCertFile
);
138 void VPNControl::setClientKeyFile(const QString
&value
)
140 if (value
!= clientKeyFile
) {
141 clientKeyFile
= value
;
142 settingsSetValue("clientKeyFile", value
);
143 emit
clientKeyFileChanged(clientKeyFile
);
147 void VPNControl::setTlsKeyFile(const QString
&value
)
149 if (value
!= tlsKeyFile
) {
151 settingsSetValue("tlsKeyFile", value
);
152 emit
tlsKeyFileChanged(tlsKeyFile
);
156 void VPNControl::setConfigFile(const QString
&value
)
158 if (value
!= configFile
) {
160 settingsSetValue("configFile", value
);
161 emit
tlsKeyFileChanged(configFile
);
165 QString
VPNControl::getClientCertFile () const
167 return clientCertFile
;
170 QString
VPNControl::getClientKeyFile () const
172 return clientKeyFile
;
175 QString
VPNControl::getTlsKeyFile () const
180 QString
VPNControl::getConfigFile () const
185 QString
VPNControl::getLogText() const
190 void VPNControl::setLogText(const QString
&value
)
193 emit
logTextChanged(logText
);
196 void VPNControl::settingsSetValue (QString key
, QString value
) {
199 settings
.setValue(key
, value
);
202 void VPNControl::settingsSetValue (QString key
, int value
) {
205 settings
.setValue(key
, value
);
208 void VPNControl::vpnConnect() {
209 if (vpnProcess
!= NULL
) {
210 printf ("Process already running.\n");
213 vpnProcess
= new QProcess();
214 QString program
= "openvpn";
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)));
222 vpnProcess
->start(program
, arguments
);
223 vpnProcess
->closeWriteChannel();
224 setStatus(VPNSTATUS_INITIALISING
);
229 void VPNControl::collectArguments () {
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
));
250 addArgumentNonempty("ca", caCertFile
);
251 addArgumentNonempty("cert", clientCertFile
);
252 addArgumentNonempty("key", clientKeyFile
);
255 void VPNControl::addArgument (QString key
, QString value
) {
258 argument
= "--" + key
;
259 arguments
.append(argument
);
260 if (!value
.isEmpty()) {
261 arguments
.append(value
);
265 void VPNControl::addArgumentNonempty (QString key
, QString value
) {
268 if (!value
.isEmpty()) {
269 argument
= "--" + key
;
270 arguments
.append(argument
);
271 arguments
.append(value
);
275 void VPNControl::addArgument (QString key
) {
278 argument
= "--" + key
;
279 arguments
.append(argument
);
282 void VPNControl::addOption (QString key
, bool add
) {
288 void VPNControl::addValue (QString key
) {
289 arguments
.append(key
);
292 void VPNControl::vpnDisconnect() {
293 if (vpnProcess
!= NULL
) {
295 vpnProcess
->terminate();
296 setStatus(VPNSTATUS_DISCONNECTING
);
300 void VPNControl::readData() {
301 while (vpnProcess
->canReadLine()) {
302 QByteArray read
= vpnProcess
->readLine();
303 //printf ("Output: %s", read.data());
307 if (read
.endsWith("Initialization Sequence Completed\n")) {
308 setStatus(VPNSTATUS_CONNECTED
);
313 void VPNControl::started() {
314 setStatus(VPNSTATUS_CONNECTING
);
317 void VPNControl::finished(int code
) {
318 if (vpnProcess
!= NULL
) {
322 setStatus(VPNSTATUS_UNINITIALISED
);
325 void VPNControl::readError(QProcess::ProcessError error
)
327 printf ("Error: %d\n", error
);
328 if (vpnProcess
!= NULL
) {
329 QByteArray dataOut
= vpnProcess
->readAllStandardOutput();
330 QByteArray errorOut
= vpnProcess
->readAllStandardError();
332 printf ("Output text: %s\n", dataOut
.data());
333 printf ("Error text: %s\n", errorOut
.data());
340 void VPNControl::updateConfiguration()
342 printf ("Update configuration\n");
345 void VPNControl::logAppend(const QString
&text
)
347 if (!text
.isEmpty()) {
348 QString append
= text
;
349 // Ensure we end with a newline
350 if (!append
.endsWith('\n')) {
353 // How many lines to add
354 int newLines
= append
.count('\n');
355 int currentLines
= logText
.count('\n');
356 int removeLines
= currentLines
+ newLines
- 24;
358 // Remove excess lines from the top
359 while (removeLines
> 0) {
360 int nextLine
= logText
.indexOf('\n');
362 logText
= logText
.mid(nextLine
+ 1);
368 logText
.append(append
);
369 emit
logTextChanged(logText
);