19351948f66fec55258b3d447bd547ddb96ed2ba
[openvpnui.git] / src / vpncontrol.cpp
1 #include "vpncontrol.h"
2 #include "stdio.h"
3
4 VPNControl::VPNControl(QObject *parent) :
5 QObject(parent),
6 vpnProcess(NULL),
7 vpnStatus(VPNSTATUS_INVALID),
8 server(""),
9 port(1174),
10 compressed(true),
11 useTLS(true),
12 tlsDirection(1)
13 {
14 }
15
16 void VPNControl::initialise()
17 {
18 setStatus(VPNSTATUS_UNINITIALISED);
19 }
20
21 void VPNControl::setStatus(VPNSTATUS newStatus)
22 {
23 if (vpnStatus != newStatus) {
24 vpnStatus = newStatus;
25 emit statusChanged(newStatus);
26 printf ("Emitting status %d\n", newStatus);
27 }
28 }
29 int VPNControl::getTlsDirection() const
30 {
31 return tlsDirection;
32 }
33
34 void VPNControl::setTlsDirection(int value)
35 {
36 printf ("TLS direction set to %d\n", value);
37 tlsDirection = value;
38 emit tlsDirectionChanged (value);
39 }
40
41 bool VPNControl::getUseTLS() const
42 {
43 return useTLS;
44 }
45
46 void VPNControl::setUseTLS(bool value)
47 {
48 printf ("Use TLS set to %d\n", value);
49 useTLS = value;
50 emit useTLSChanged(useTLS);
51 }
52
53 bool VPNControl::getCompressed() const
54 {
55 return compressed;
56 }
57
58 void VPNControl::setCompressed(bool value)
59 {
60 printf ("Use compression set to %d\n", value);
61 compressed = value;
62 emit compressedChanged(compressed);
63 }
64
65 unsigned int VPNControl::getPort() const
66 {
67 return port;
68 }
69
70 void VPNControl::setPort(unsigned int value)
71 {
72 printf ("Port set to %d\n", value);
73 port = value;
74 emit portChanged(port);
75 }
76
77 QString VPNControl::getServer() const
78 {
79 return server;
80 }
81
82 void VPNControl::setServer(const QString &value)
83 {
84 printf ("Server set to %s\n", value.toUtf8().constData());
85 server = value;
86 emit serverChanged(server);
87 }
88
89 void VPNControl::vpnConnect() {
90 if (vpnProcess != NULL) {
91 printf ("Process already running.\n");
92 }
93 else {
94 printf ("Connect\n");
95
96 vpnProcess = new QProcess();
97 QString program = "openvpn";
98 collectArguments ();
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)));
104
105 vpnProcess->start(program, arguments);
106 vpnProcess->closeWriteChannel();
107 setStatus(VPNSTATUS_INITIALISING);
108 arguments.clear();
109 }
110 }
111
112 void VPNControl::collectArguments () {
113 arguments.clear();
114
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);
119 if (useTLS) {
120 addArgument("tls-auth", "/home/nemo/Documents/Configure/OpenVPN/ta.key");
121 addValue(QString::number(tlsDirection));
122 }
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");
126 }
127
128 void VPNControl::addArgument (QString key, QString value) {
129 QString argument;
130
131 argument = "--" + key;
132 arguments.append(argument);
133 if (value != "") {
134 arguments.append(value);
135 }
136 }
137
138 void VPNControl::addArgument (QString key) {
139 QString argument;
140
141 argument = "--" + key;
142 arguments.append(argument);
143 }
144
145 void VPNControl::addOption (QString key, bool add) {
146 if (add) {
147 addArgument (key);
148 }
149 }
150
151 void VPNControl::addValue (QString key) {
152 arguments.append(key);
153 }
154
155 void VPNControl::vpnDisconnect() {
156 if (vpnProcess != NULL) {
157 printf ("Disconnect\n");
158
159 vpnProcess->terminate();
160 setStatus(VPNSTATUS_DISCONNECTING);
161 }
162 }
163
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);
171 }
172 }
173 }
174
175 void VPNControl::started() {
176 printf ("Started\n");
177 setStatus(VPNSTATUS_CONNECTING);
178 }
179
180 void VPNControl::finished(int code) {
181 printf ("Finished with code %d\n", code);
182 if (vpnProcess != NULL) {
183 //delete vpnProcess;
184 vpnProcess = NULL;
185 }
186 setStatus(VPNSTATUS_UNINITIALISED);
187 }
188
189 void VPNControl::readError(QProcess::ProcessError error)
190 {
191 printf ("Error: %d\n", error);
192 if (vpnProcess != NULL) {
193 QByteArray dataOut = vpnProcess->readAllStandardOutput();
194 QByteArray errorOut = vpnProcess->readAllStandardError();
195
196 printf ("Output text: %s\n", dataOut.data());
197 printf ("Error text: %s\n", errorOut.data());
198 }
199
200 // Disconnect
201 vpnDisconnect();
202 }
203
204 void VPNControl::updateConfiguration()
205 {
206 printf ("Update configuration\n");
207 }