7bd002f666f0b6ba5f253fb3433e2cdbbdc51640
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();
26 void VPNControl::initialise()
28 setStatus(VPNSTATUS_UNINITIALISED
);
31 void VPNControl::setStatus(VPNSTATUS newStatus
)
33 if (vpnStatus
!= newStatus
) {
34 vpnStatus
= newStatus
;
35 emit
statusChanged(newStatus
);
36 printf ("Emitting status %d\n", newStatus
);
39 int VPNControl::getTlsDirection() const
44 void VPNControl::setTlsDirection(int value
)
46 if (value
!= tlsDirection
) {
47 printf ("TLS direction set to %d\n", value
);
49 settingsSetValue("tlsDirection", value
);
50 emit
tlsDirectionChanged (value
);
54 bool VPNControl::getUseTLS() const
59 void VPNControl::setUseTLS(bool value
)
61 if (value
!= useTLS
) {
62 printf ("Use TLS set to %d\n", value
);
64 settingsSetValue("useTLS", value
);
65 emit
useTLSChanged(useTLS
);
69 bool VPNControl::getCompressed() const
74 void VPNControl::setCompressed(bool value
)
76 if (value
!= compressed
) {
77 printf ("Use compression set to %d\n", value
);
79 settingsSetValue("compressed", value
);
80 emit
compressedChanged(compressed
);
84 unsigned int VPNControl::getPort() const
89 void VPNControl::setPort(unsigned int value
)
92 printf ("Port set to %d\n", value
);
94 settingsSetValue("port", value
);
95 emit
portChanged(port
);
99 QString
VPNControl::getServer() const
104 void VPNControl::setServer(const QString
&value
)
106 if (value
!= server
) {
107 printf ("Server set to %s\n", value
.toUtf8().constData());
109 settingsSetValue("server", value
);
110 emit
serverChanged(server
);
114 void VPNControl::settingsSetValue (QString key
, QString value
) {
117 settings
.setValue(key
, value
);
120 void VPNControl::settingsSetValue (QString key
, int value
) {
123 settings
.setValue(key
, value
);
126 void VPNControl::vpnConnect() {
127 if (vpnProcess
!= NULL
) {
128 printf ("Process already running.\n");
131 printf ("Connect\n");
133 vpnProcess
= new QProcess();
134 QString program
= "openvpn";
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)));
142 vpnProcess
->start(program
, arguments
);
143 vpnProcess
->closeWriteChannel();
144 setStatus(VPNSTATUS_INITIALISING
);
149 void VPNControl::collectArguments () {
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
);
157 addArgument("tls-auth", "/home/nemo/Documents/Configure/OpenVPN/ta.key");
158 addValue(QString::number(tlsDirection
));
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");
165 void VPNControl::addArgument (QString key
, QString value
) {
168 argument
= "--" + key
;
169 arguments
.append(argument
);
171 arguments
.append(value
);
175 void VPNControl::addArgument (QString key
) {
178 argument
= "--" + key
;
179 arguments
.append(argument
);
182 void VPNControl::addOption (QString key
, bool add
) {
188 void VPNControl::addValue (QString key
) {
189 arguments
.append(key
);
192 void VPNControl::vpnDisconnect() {
193 if (vpnProcess
!= NULL
) {
194 printf ("Disconnect\n");
196 vpnProcess
->terminate();
197 setStatus(VPNSTATUS_DISCONNECTING
);
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
);
212 void VPNControl::started() {
213 printf ("Started\n");
214 setStatus(VPNSTATUS_CONNECTING
);
217 void VPNControl::finished(int code
) {
218 printf ("Finished with code %d\n", code
);
219 if (vpnProcess
!= NULL
) {
223 setStatus(VPNSTATUS_UNINITIALISED
);
226 void VPNControl::readError(QProcess::ProcessError error
)
228 printf ("Error: %d\n", error
);
229 if (vpnProcess
!= NULL
) {
230 QByteArray dataOut
= vpnProcess
->readAllStandardOutput();
231 QByteArray errorOut
= vpnProcess
->readAllStandardError();
233 printf ("Output text: %s\n", dataOut
.data());
234 printf ("Error text: %s\n", errorOut
.data());
241 void VPNControl::updateConfiguration()
243 printf ("Update configuration\n");