X-Git-Url: https://www.flypig.org.uk/git/?a=blobdiff_plain;f=src%2Fvpncontrol.cpp;h=d3ec771304ce4c8333929f61cb0eac6392d7e521;hb=93e2321f381ed34de36c088a4f50a7550e148652;hp=a519dce1f29d799ab4e16ac995d447a440d967b9;hpb=2e0816fb79e6c696ada4fe098ba006fca2741a1f;p=openvpnui.git diff --git a/src/vpncontrol.cpp b/src/vpncontrol.cpp index a519dce..d3ec771 100644 --- a/src/vpncontrol.cpp +++ b/src/vpncontrol.cpp @@ -1,16 +1,33 @@ #include "vpncontrol.h" #include "stdio.h" +#include + VPNControl::VPNControl(QObject *parent) : QObject(parent), vpnProcess(NULL), vpnStatus(VPNSTATUS_INVALID), server(""), - port(1174), + port(1194), compressed(true), useTLS(true), tlsDirection(1) { + // Read in the settings + QSettings settings; + settings.setValue("showAll", false); + + // Read configuration settings + server = settings.value("server", "127.0.0.1").toString(); + port = settings.value("port", 1194).toInt(); + compressed = settings.value("compressed", true).toBool(); + useTLS = settings.value("useTLS", true).toBool(); + tlsDirection = settings.value("tlsDirection", 1).toInt(); + caCertFile = settings.value("caCertFile", "").toString(); + clientCertFile = settings.value("clientCertFile", "").toString(); + clientKeyFile = settings.value("clientKeyFile", "").toString(); + tlsKeyFile = settings.value("tlsKeyFile", "").toString(); + configFile = settings.value("configFile", "").toString(); } void VPNControl::initialise() @@ -23,7 +40,6 @@ void VPNControl::setStatus(VPNSTATUS newStatus) if (vpnStatus != newStatus) { vpnStatus = newStatus; emit statusChanged(newStatus); - printf ("Emitting status %d\n", newStatus); } } int VPNControl::getTlsDirection() const @@ -33,9 +49,11 @@ int VPNControl::getTlsDirection() const void VPNControl::setTlsDirection(int value) { - printf ("TLS direction set to %d\n", value); - tlsDirection = value; - emit tlsDirectionChanged (value); + if (value != tlsDirection) { + tlsDirection = value; + settingsSetValue("tlsDirection", value); + emit tlsDirectionChanged (value); + } } bool VPNControl::getUseTLS() const @@ -45,9 +63,11 @@ bool VPNControl::getUseTLS() const void VPNControl::setUseTLS(bool value) { - printf ("Use TLS set to %d\n", value); - useTLS = value; - emit useTLSChanged(useTLS); + if (value != useTLS) { + useTLS = value; + settingsSetValue("useTLS", value); + emit useTLSChanged(useTLS); + } } bool VPNControl::getCompressed() const @@ -57,9 +77,11 @@ bool VPNControl::getCompressed() const void VPNControl::setCompressed(bool value) { - printf ("Use compression set to %d\n", value); - compressed = value; - emit compressedChanged(compressed); + if (value != compressed) { + compressed = value; + settingsSetValue("compressed", value); + emit compressedChanged(compressed); + } } unsigned int VPNControl::getPort() const @@ -69,9 +91,11 @@ unsigned int VPNControl::getPort() const void VPNControl::setPort(unsigned int value) { - printf ("Port set to %d\n", value); - port = value; - emit portChanged(port); + if (value != port) { + port = value; + settingsSetValue("port", value); + emit portChanged(port); + } } QString VPNControl::getServer() const @@ -81,25 +105,114 @@ QString VPNControl::getServer() const void VPNControl::setServer(const QString &value) { - printf ("Server set to %s\n", value.toUtf8().constData()); - server = value; - emit serverChanged(server); + if (value != server) { + server = value; + settingsSetValue("server", value); + emit serverChanged(server); + } +} + +QString VPNControl::getCaCertFile () const +{ + return caCertFile; +} + +void VPNControl::setCaCertFile(const QString &value) +{ + if (value != caCertFile) { + caCertFile = value; + settingsSetValue("caCertFile", value); + emit caCertFileChanged(caCertFile); + } +} + +void VPNControl::setClientCertFile(const QString &value) +{ + if (value != clientCertFile) { + clientCertFile = value; + settingsSetValue("clientCertFile", value); + emit clientCertFileChanged(clientCertFile); + } +} + +void VPNControl::setClientKeyFile(const QString &value) +{ + if (value != clientKeyFile) { + clientKeyFile = value; + settingsSetValue("clientKeyFile", value); + emit clientKeyFileChanged(clientKeyFile); + } +} + +void VPNControl::setTlsKeyFile(const QString &value) +{ + if (value != tlsKeyFile) { + tlsKeyFile = value; + settingsSetValue("tlsKeyFile", value); + emit tlsKeyFileChanged(tlsKeyFile); + } +} + +void VPNControl::setConfigFile(const QString &value) +{ + if (value != configFile) { + configFile = value; + settingsSetValue("configFile", value); + emit tlsKeyFileChanged(configFile); + } +} + +QString VPNControl::getClientCertFile () const +{ + return clientCertFile; +} + +QString VPNControl::getClientKeyFile () const +{ + return clientKeyFile; +} + +QString VPNControl::getTlsKeyFile () const +{ + return tlsKeyFile; +} + +QString VPNControl::getConfigFile () const +{ + return configFile; +} + +QString VPNControl::getLogText() const +{ + return logText; +} + +void VPNControl::setLogText(const QString &value) +{ + logText = value; + emit logTextChanged(logText); } +void VPNControl::settingsSetValue (QString key, QString value) { + QSettings settings; + + settings.setValue(key, value); +} + +void VPNControl::settingsSetValue (QString key, int value) { + QSettings settings; + + settings.setValue(key, value); +} void VPNControl::vpnConnect() { if (vpnProcess != NULL) { printf ("Process already running.\n"); } else { - printf ("Connect\n"); - vpnProcess = new QProcess(); - //QString program = "/home/nemo/Documents/Development/Projects/Stooge/stooge"; - QString program = "openvpn"; - QStringList arguments; - arguments << "/home/nemo/Documents/Configure/OpenVPN/client.ovpn"; - + QString program = "/usr/share/harbour-openvpn-rig/bin/ovpnpermit"; + collectArguments (); vpnProcess->setReadChannel(QProcess::StandardOutput); connect(vpnProcess, SIGNAL(error(QProcess::ProcessError)), this, SLOT(readError(QProcess::ProcessError))); connect(vpnProcess, SIGNAL(readyRead()), this, SLOT(readData())); @@ -109,12 +222,75 @@ void VPNControl::vpnConnect() { vpnProcess->start(program, arguments); vpnProcess->closeWriteChannel(); setStatus(VPNSTATUS_INITIALISING); + arguments.clear(); + } +} + +void VPNControl::collectArguments () { + arguments.clear(); + + addArgumentNonempty("config", configFile); + addOption("client", true); + addOption("persist-key", true); + addOption("persist-tun", true); + addOption("nobind", true); + addArgument("resolv-retry", "infinite"); + addArgument("dev", "tun"); + addArgument("verb", "3"); + addArgument("proto", "udp"); + addArgument("user", "nemo"); + addArgument("group", "nemo"); + addArgumentNonempty("remote", server); + addArgumentNonempty("port", QString::number(port)); + addOption("comp-lzo", compressed); + if ((useTLS) && (!tlsKeyFile.isEmpty())) { + addArgument("tls-auth", tlsKeyFile); + addValue(QString::number(tlsDirection)); } + addArgumentNonempty("ca", caCertFile); + addArgumentNonempty("cert", clientCertFile); + addArgumentNonempty("key", clientKeyFile); +} + +void VPNControl::addArgument (QString key, QString value) { + QString argument; + + argument = "--" + key; + arguments.append(argument); + if (!value.isEmpty()) { + arguments.append(value); + } +} + +void VPNControl::addArgumentNonempty (QString key, QString value) { + QString argument; + + if (!value.isEmpty()) { + argument = "--" + key; + arguments.append(argument); + arguments.append(value); + } +} + +void VPNControl::addArgument (QString key) { + QString argument; + + argument = "--" + key; + arguments.append(argument); +} + +void VPNControl::addOption (QString key, bool add) { + if (add) { + addArgument (key); + } +} + +void VPNControl::addValue (QString key) { + arguments.append(key); } void VPNControl::vpnDisconnect() { if (vpnProcess != NULL) { - printf ("Disconnect\n"); vpnProcess->terminate(); setStatus(VPNSTATUS_DISCONNECTING); @@ -124,25 +300,26 @@ void VPNControl::vpnDisconnect() { void VPNControl::readData() { while (vpnProcess->canReadLine()) { QByteArray read = vpnProcess->readLine(); - printf ("Output: %s", read.data()); + //printf ("Output: %s", read.data()); + + logAppend(read); + if (read.endsWith("Initialization Sequence Completed\n")) { - printf ("We're connected!\n"); setStatus(VPNSTATUS_CONNECTED); } } } void VPNControl::started() { - printf ("Started\n"); setStatus(VPNSTATUS_CONNECTING); } void VPNControl::finished(int code) { - printf ("Finished with code %d\n", code); if (vpnProcess != NULL) { //delete vpnProcess; vpnProcess = NULL; } + logAppend("Finished with code " + QString::number(code)); setStatus(VPNSTATUS_UNINITIALISED); } @@ -165,3 +342,32 @@ void VPNControl::updateConfiguration() { printf ("Update configuration\n"); } + +void VPNControl::logAppend(const QString &text) +{ + if (!text.isEmpty()) { + QString append = text; + // Ensure we end with a newline + if (!append.endsWith('\n')) { + append += '\n'; + } + // How many lines to add + int newLines = append.count('\n'); + int currentLines = logText.count('\n'); + int removeLines = currentLines + newLines - 24; + + // Remove excess lines from the top + while (removeLines > 0) { + int nextLine = logText.indexOf('\n'); + if (nextLine > 0) { + logText = logText.mid(nextLine + 1); + } + removeLines--; + } + + // Add new lines + logText.append(append); + emit logTextChanged(logText); + } +} +