1 #include "vpncontrol.h"
6 VPNControl::VPNControl(QObject
*parent
) :
9 vpnStatus(VPNSTATUS_INVALID
),
16 // Read in the settings
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();;
29 settings
.setValue("showAll", false);
32 void VPNControl::initialise()
34 setStatus(VPNSTATUS_UNINITIALISED
);
37 void VPNControl::setStatus(VPNSTATUS newStatus
)
39 if (vpnStatus
!= newStatus
) {
40 vpnStatus
= newStatus
;
41 emit
statusChanged(newStatus
);
44 int VPNControl::getTlsDirection() const
49 void VPNControl::setTlsDirection(int value
)
51 if (value
!= tlsDirection
) {
53 settingsSetValue("tlsDirection", value
);
54 emit
tlsDirectionChanged (value
);
58 bool VPNControl::getUseTLS() const
63 void VPNControl::setUseTLS(bool value
)
65 if (value
!= useTLS
) {
67 settingsSetValue("useTLS", value
);
68 emit
useTLSChanged(useTLS
);
72 bool VPNControl::getCompressed() const
77 void VPNControl::setCompressed(bool value
)
79 if (value
!= compressed
) {
81 settingsSetValue("compressed", value
);
82 emit
compressedChanged(compressed
);
86 unsigned int VPNControl::getPort() const
91 void VPNControl::setPort(unsigned int value
)
95 settingsSetValue("port", value
);
96 emit
portChanged(port
);
100 QString
VPNControl::getServer() const
105 void VPNControl::setServer(const QString
&value
)
107 if (value
!= server
) {
109 settingsSetValue("server", value
);
110 emit
serverChanged(server
);
114 QString
VPNControl::getCaCertFile () const
119 void VPNControl::setCaCertFile(const QString
&value
)
121 if (value
!= caCertFile
) {
123 settingsSetValue("caCertFile", value
);
124 emit
caCertFileChanged(caCertFile
);
128 void VPNControl::setClientCertFile(const QString
&value
)
130 if (value
!= clientCertFile
) {
131 clientCertFile
= value
;
132 settingsSetValue("clientCertFile", value
);
133 emit
clientCertFileChanged(clientCertFile
);
137 void VPNControl::setClientKeyFile(const QString
&value
)
139 if (value
!= clientKeyFile
) {
140 clientKeyFile
= value
;
141 settingsSetValue("clientKeyFile", value
);
142 emit
clientKeyFileChanged(clientKeyFile
);
146 void VPNControl::setTlsKeyFile(const QString
&value
)
148 if (value
!= tlsKeyFile
) {
150 settingsSetValue("tlsKeyFile", value
);
151 emit
tlsKeyFileChanged(tlsKeyFile
);
155 QString
VPNControl::getClientCertFile () const
157 return clientCertFile
;
160 QString
VPNControl::getClientKeyFile () const
162 return clientKeyFile
;
165 QString
VPNControl::getTlsKeyFile () const
170 QString
VPNControl::getLogText() const
175 void VPNControl::setLogText(const QString
&value
)
178 emit
logTextChanged(logText
);
181 void VPNControl::settingsSetValue (QString key
, QString value
) {
184 settings
.setValue(key
, value
);
187 void VPNControl::settingsSetValue (QString key
, int value
) {
190 settings
.setValue(key
, value
);
193 void VPNControl::vpnConnect() {
194 if (vpnProcess
!= NULL
) {
195 printf ("Process already running.\n");
198 vpnProcess
= new QProcess();
199 QString program
= "openvpn";
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)));
207 vpnProcess
->start(program
, arguments
);
208 vpnProcess
->closeWriteChannel();
209 setStatus(VPNSTATUS_INITIALISING
);
214 void VPNControl::collectArguments () {
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
));
225 addArgumentNonempty("ca", caCertFile
);
226 addArgumentNonempty("cert", clientCertFile
);
227 addArgumentNonempty("key", clientKeyFile
);
230 void VPNControl::addArgument (QString key
, QString value
) {
233 argument
= "--" + key
;
234 arguments
.append(argument
);
235 if (!value
.isEmpty()) {
236 arguments
.append(value
);
240 void VPNControl::addArgumentNonempty (QString key
, QString value
) {
243 if (!value
.isEmpty()) {
244 argument
= "--" + key
;
245 arguments
.append(argument
);
246 arguments
.append(value
);
250 void VPNControl::addArgument (QString key
) {
253 argument
= "--" + key
;
254 arguments
.append(argument
);
257 void VPNControl::addOption (QString key
, bool add
) {
263 void VPNControl::addValue (QString key
) {
264 arguments
.append(key
);
267 void VPNControl::vpnDisconnect() {
268 if (vpnProcess
!= NULL
) {
270 vpnProcess
->terminate();
271 setStatus(VPNSTATUS_DISCONNECTING
);
275 void VPNControl::readData() {
276 while (vpnProcess
->canReadLine()) {
277 QByteArray read
= vpnProcess
->readLine();
278 //printf ("Output: %s", read.data());
282 if (read
.endsWith("Initialization Sequence Completed\n")) {
283 setStatus(VPNSTATUS_CONNECTED
);
288 void VPNControl::started() {
289 setStatus(VPNSTATUS_CONNECTING
);
292 void VPNControl::finished(int code
) {
293 if (vpnProcess
!= NULL
) {
297 setStatus(VPNSTATUS_UNINITIALISED
);
300 void VPNControl::readError(QProcess::ProcessError error
)
302 printf ("Error: %d\n", error
);
303 if (vpnProcess
!= NULL
) {
304 QByteArray dataOut
= vpnProcess
->readAllStandardOutput();
305 QByteArray errorOut
= vpnProcess
->readAllStandardError();
307 printf ("Output text: %s\n", dataOut
.data());
308 printf ("Error text: %s\n", errorOut
.data());
315 void VPNControl::updateConfiguration()
317 printf ("Update configuration\n");
320 void VPNControl::logAppend(const QString
&text
)
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;
328 // Remove excess lines
329 // while (removeLines > 0) {
330 // int nextLine = logText.indexOf('\n');
331 // if (nextLine > 0) {
332 // logText = logText.right(nextLine);
338 logText
.append(text
);
339 emit
logTextChanged(logText
);