19351948f66fec55258b3d447bd547ddb96ed2ba
1 #include "vpncontrol.h"
4 VPNControl::VPNControl(QObject
*parent
) :
7 vpnStatus(VPNSTATUS_INVALID
),
16 void VPNControl::initialise()
18 setStatus(VPNSTATUS_UNINITIALISED
);
21 void VPNControl::setStatus(VPNSTATUS newStatus
)
23 if (vpnStatus
!= newStatus
) {
24 vpnStatus
= newStatus
;
25 emit
statusChanged(newStatus
);
26 printf ("Emitting status %d\n", newStatus
);
29 int VPNControl::getTlsDirection() const
34 void VPNControl::setTlsDirection(int value
)
36 printf ("TLS direction set to %d\n", value
);
38 emit
tlsDirectionChanged (value
);
41 bool VPNControl::getUseTLS() const
46 void VPNControl::setUseTLS(bool value
)
48 printf ("Use TLS set to %d\n", value
);
50 emit
useTLSChanged(useTLS
);
53 bool VPNControl::getCompressed() const
58 void VPNControl::setCompressed(bool value
)
60 printf ("Use compression set to %d\n", value
);
62 emit
compressedChanged(compressed
);
65 unsigned int VPNControl::getPort() const
70 void VPNControl::setPort(unsigned int value
)
72 printf ("Port set to %d\n", value
);
74 emit
portChanged(port
);
77 QString
VPNControl::getServer() const
82 void VPNControl::setServer(const QString
&value
)
84 printf ("Server set to %s\n", value
.toUtf8().constData());
86 emit
serverChanged(server
);
89 void VPNControl::vpnConnect() {
90 if (vpnProcess
!= NULL
) {
91 printf ("Process already running.\n");
96 vpnProcess
= new QProcess();
97 QString program
= "openvpn";
99 vpnProcess
->setReadChannel(QProcess::StandardOutput
);
100 connect(vpnProcess
, SIGNAL(error(QProcess::ProcessError
)), this, SLOT(readError(QProcess::ProcessError
)));
101 connect(vpnProcess
, SIGNAL(readyRead()), this, SLOT(readData()));
102 connect(vpnProcess
, SIGNAL(started()), this, SLOT(started()));
103 connect(vpnProcess
, SIGNAL(finished(int)), this, SLOT(finished(int)));
105 vpnProcess
->start(program
, arguments
);
106 vpnProcess
->closeWriteChannel();
107 setStatus(VPNSTATUS_INITIALISING
);
112 void VPNControl::collectArguments () {
115 addArgument("config", "/home/nemo/Documents/Configure/OpenVPN/config.ovpn");
116 addArgument("remote", server
);
117 addArgument("port", QString::number(port
));
118 addOption("comp-lzo", compressed
);
120 addArgument("tls-auth", "/home/nemo/Documents/Configure/OpenVPN/ta.key");
121 addValue(QString::number(tlsDirection
));
123 addArgument("ca", "/home/nemo/Documents/Configure/OpenVPN/ca.crt");
124 addArgument("cert", "/home/nemo/Documents/Configure/OpenVPN/Jolla.crt");
125 addArgument("key", "/home/nemo/Documents/Configure/OpenVPN/Jolla.key");
128 void VPNControl::addArgument (QString key
, QString value
) {
131 argument
= "--" + key
;
132 arguments
.append(argument
);
134 arguments
.append(value
);
138 void VPNControl::addArgument (QString key
) {
141 argument
= "--" + key
;
142 arguments
.append(argument
);
145 void VPNControl::addOption (QString key
, bool add
) {
151 void VPNControl::addValue (QString key
) {
152 arguments
.append(key
);
155 void VPNControl::vpnDisconnect() {
156 if (vpnProcess
!= NULL
) {
157 printf ("Disconnect\n");
159 vpnProcess
->terminate();
160 setStatus(VPNSTATUS_DISCONNECTING
);
164 void VPNControl::readData() {
165 while (vpnProcess
->canReadLine()) {
166 QByteArray read
= vpnProcess
->readLine();
167 printf ("Output: %s", read
.data());
168 if (read
.endsWith("Initialization Sequence Completed\n")) {
169 printf ("We're connected!\n");
170 setStatus(VPNSTATUS_CONNECTED
);
175 void VPNControl::started() {
176 printf ("Started\n");
177 setStatus(VPNSTATUS_CONNECTING
);
180 void VPNControl::finished(int code
) {
181 printf ("Finished with code %d\n", code
);
182 if (vpnProcess
!= NULL
) {
186 setStatus(VPNSTATUS_UNINITIALISED
);
189 void VPNControl::readError(QProcess::ProcessError error
)
191 printf ("Error: %d\n", error
);
192 if (vpnProcess
!= NULL
) {
193 QByteArray dataOut
= vpnProcess
->readAllStandardOutput();
194 QByteArray errorOut
= vpnProcess
->readAllStandardError();
196 printf ("Output text: %s\n", dataOut
.data());
197 printf ("Error text: %s\n", errorOut
.data());
204 void VPNControl::updateConfiguration()
206 printf ("Update configuration\n");