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 addArgumentNonempty("remote", server
);
234 addArgumentNonempty("port", QString::number(port
));
235 addOption("comp-lzo", compressed
);
236 if ((useTLS
) && (!tlsKeyFile
.isEmpty())) {
237 addArgument("tls-auth", tlsKeyFile
);
238 addValue(QString::number(tlsDirection
));
240 addArgumentNonempty("ca", caCertFile
);
241 addArgumentNonempty("cert", clientCertFile
);
242 addArgumentNonempty("key", clientKeyFile
);
245 void VPNControl::addArgument (QString key
, QString value
) {
248 argument
= "--" + key
;
249 arguments
.append(argument
);
250 if (!value
.isEmpty()) {
251 arguments
.append(value
);
255 void VPNControl::addArgumentNonempty (QString key
, QString value
) {
258 if (!value
.isEmpty()) {
259 argument
= "--" + key
;
260 arguments
.append(argument
);
261 arguments
.append(value
);
265 void VPNControl::addArgument (QString key
) {
268 argument
= "--" + key
;
269 arguments
.append(argument
);
272 void VPNControl::addOption (QString key
, bool add
) {
278 void VPNControl::addValue (QString key
) {
279 arguments
.append(key
);
282 void VPNControl::vpnDisconnect() {
283 if (vpnProcess
!= NULL
) {
285 vpnProcess
->terminate();
286 setStatus(VPNSTATUS_DISCONNECTING
);
290 void VPNControl::readData() {
291 while (vpnProcess
->canReadLine()) {
292 QByteArray read
= vpnProcess
->readLine();
293 //printf ("Output: %s", read.data());
297 if (read
.endsWith("Initialization Sequence Completed\n")) {
298 setStatus(VPNSTATUS_CONNECTED
);
303 void VPNControl::started() {
304 setStatus(VPNSTATUS_CONNECTING
);
307 void VPNControl::finished(int code
) {
308 if (vpnProcess
!= NULL
) {
312 setStatus(VPNSTATUS_UNINITIALISED
);
315 void VPNControl::readError(QProcess::ProcessError error
)
317 printf ("Error: %d\n", error
);
318 if (vpnProcess
!= NULL
) {
319 QByteArray dataOut
= vpnProcess
->readAllStandardOutput();
320 QByteArray errorOut
= vpnProcess
->readAllStandardError();
322 printf ("Output text: %s\n", dataOut
.data());
323 printf ("Error text: %s\n", errorOut
.data());
330 void VPNControl::updateConfiguration()
332 printf ("Update configuration\n");
335 void VPNControl::logAppend(const QString
&text
)
337 if (!text
.isEmpty()) {
338 // How many lines to add
339 int newLines
= text
.count('\n');
340 int currentLines
= logText
.count('\n');
341 int removeLines
= currentLines
+ newLines
- 18;
343 // Remove excess lines
344 // while (removeLines > 0) {
345 // int nextLine = logText.indexOf('\n');
346 // if (nextLine > 0) {
347 // logText = logText.right(nextLine);
353 logText
.append(text
);
354 emit
logTextChanged(logText
);