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 settings
.setValue("showAll", false);
27 void VPNControl::initialise()
29 setStatus(VPNSTATUS_UNINITIALISED
);
32 void VPNControl::setStatus(VPNSTATUS newStatus
)
34 if (vpnStatus
!= newStatus
) {
35 vpnStatus
= newStatus
;
36 emit
statusChanged(newStatus
);
39 int VPNControl::getTlsDirection() const
44 void VPNControl::setTlsDirection(int value
)
46 if (value
!= tlsDirection
) {
48 settingsSetValue("tlsDirection", value
);
49 emit
tlsDirectionChanged (value
);
53 bool VPNControl::getUseTLS() const
58 void VPNControl::setUseTLS(bool value
)
60 if (value
!= useTLS
) {
62 settingsSetValue("useTLS", value
);
63 emit
useTLSChanged(useTLS
);
67 bool VPNControl::getCompressed() const
72 void VPNControl::setCompressed(bool value
)
74 if (value
!= compressed
) {
76 settingsSetValue("compressed", value
);
77 emit
compressedChanged(compressed
);
81 unsigned int VPNControl::getPort() const
86 void VPNControl::setPort(unsigned int value
)
90 settingsSetValue("port", value
);
91 emit
portChanged(port
);
95 QString
VPNControl::getServer() const
100 void VPNControl::setServer(const QString
&value
)
102 if (value
!= server
) {
104 settingsSetValue("server", value
);
105 emit
serverChanged(server
);
109 QString
VPNControl::getLogText() const
114 void VPNControl::setLogText(const QString
&value
)
117 emit
logTextChanged(value
);
120 void VPNControl::settingsSetValue (QString key
, QString value
) {
123 settings
.setValue(key
, value
);
126 void VPNControl::settingsSetValue (QString key
, int value
) {
129 settings
.setValue(key
, value
);
132 void VPNControl::vpnConnect() {
133 if (vpnProcess
!= NULL
) {
134 printf ("Process already running.\n");
137 vpnProcess
= new QProcess();
138 QString program
= "openvpn";
140 vpnProcess
->setReadChannel(QProcess::StandardOutput
);
141 connect(vpnProcess
, SIGNAL(error(QProcess::ProcessError
)), this, SLOT(readError(QProcess::ProcessError
)));
142 connect(vpnProcess
, SIGNAL(readyRead()), this, SLOT(readData()));
143 connect(vpnProcess
, SIGNAL(started()), this, SLOT(started()));
144 connect(vpnProcess
, SIGNAL(finished(int)), this, SLOT(finished(int)));
146 vpnProcess
->start(program
, arguments
);
147 vpnProcess
->closeWriteChannel();
148 setStatus(VPNSTATUS_INITIALISING
);
153 void VPNControl::collectArguments () {
156 addArgument("config", "/home/nemo/Documents/Configure/OpenVPN/config.ovpn");
157 addArgument("remote", server
);
158 addArgument("port", QString::number(port
));
159 addOption("comp-lzo", compressed
);
161 addArgument("tls-auth", "/home/nemo/Documents/Configure/OpenVPN/ta.key");
162 addValue(QString::number(tlsDirection
));
164 addArgument("ca", "/home/nemo/Documents/Configure/OpenVPN/ca.crt");
165 addArgument("cert", "/home/nemo/Documents/Configure/OpenVPN/Jolla.crt");
166 addArgument("key", "/home/nemo/Documents/Configure/OpenVPN/Jolla.key");
169 void VPNControl::addArgument (QString key
, QString value
) {
172 argument
= "--" + key
;
173 arguments
.append(argument
);
175 arguments
.append(value
);
179 void VPNControl::addArgument (QString key
) {
182 argument
= "--" + key
;
183 arguments
.append(argument
);
186 void VPNControl::addOption (QString key
, bool add
) {
192 void VPNControl::addValue (QString key
) {
193 arguments
.append(key
);
196 void VPNControl::vpnDisconnect() {
197 if (vpnProcess
!= NULL
) {
199 vpnProcess
->terminate();
200 setStatus(VPNSTATUS_DISCONNECTING
);
204 void VPNControl::readData() {
205 while (vpnProcess
->canReadLine()) {
206 QByteArray read
= vpnProcess
->readLine();
207 //printf ("Output: %s", read.data());
211 if (read
.endsWith("Initialization Sequence Completed\n")) {
212 setStatus(VPNSTATUS_CONNECTED
);
217 void VPNControl::started() {
218 setStatus(VPNSTATUS_CONNECTING
);
221 void VPNControl::finished(int code
) {
222 if (vpnProcess
!= NULL
) {
226 setStatus(VPNSTATUS_UNINITIALISED
);
229 void VPNControl::readError(QProcess::ProcessError error
)
231 printf ("Error: %d\n", error
);
232 if (vpnProcess
!= NULL
) {
233 QByteArray dataOut
= vpnProcess
->readAllStandardOutput();
234 QByteArray errorOut
= vpnProcess
->readAllStandardError();
236 printf ("Output text: %s\n", dataOut
.data());
237 printf ("Error text: %s\n", errorOut
.data());
244 void VPNControl::updateConfiguration()
246 printf ("Update configuration\n");
249 void VPNControl::logAppend(const QString
&text
)
251 if (!text
.isEmpty()) {
252 // How many lines to add
253 int newLines
= text
.count('\n');
254 int currentLines
= logText
.count('\n');
255 int removeLines
= currentLines
+ newLines
- 18;
257 // Remove excess lines
258 while (removeLines
> 0) {
259 int nextLine
= logText
.lastIndexOf('\n');
261 logText
= logText
.left(nextLine
);
267 logText
.prepend(text
);
268 emit
logTextChanged(logText
);