X-Git-Url: https://www.flypig.org.uk/git/?a=blobdiff_plain;f=src%2Fvpncontrol.cpp;h=057cb365941c81d4dccc66446c8689e77db0b71f;hb=e24363e314aca32e7bee952f02f517a04a8dc5f2;hp=8b0c79a96b3c575e39cec12c73e9db994afe7788;hpb=3aba2d3bed67a9d91350393dd4772f566aca7db1;p=openvpnui.git diff --git a/src/vpncontrol.cpp b/src/vpncontrol.cpp index 8b0c79a..057cb36 100644 --- a/src/vpncontrol.cpp +++ b/src/vpncontrol.cpp @@ -1,11 +1,27 @@ #include "vpncontrol.h" #include "stdio.h" +#include + VPNControl::VPNControl(QObject *parent) : QObject(parent), vpnProcess(NULL), - vpnStatus(VPNSTATUS_INVALID) + vpnStatus(VPNSTATUS_INVALID), + server(""), + port(1194), + compressed(true), + useTLS(true), + tlsDirection(1) { + // Read in the settings + QSettings 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(); + settings.setValue("showAll", false); } void VPNControl::initialise() @@ -18,23 +34,109 @@ void VPNControl::setStatus(VPNSTATUS newStatus) if (vpnStatus != newStatus) { vpnStatus = newStatus; emit statusChanged(newStatus); - printf ("Emitting status %d\n", newStatus); } } +int VPNControl::getTlsDirection() const +{ + return tlsDirection; +} + +void VPNControl::setTlsDirection(int value) +{ + if (value != tlsDirection) { + tlsDirection = value; + settingsSetValue("tlsDirection", value); + emit tlsDirectionChanged (value); + } +} + +bool VPNControl::getUseTLS() const +{ + return useTLS; +} + +void VPNControl::setUseTLS(bool value) +{ + if (value != useTLS) { + useTLS = value; + settingsSetValue("useTLS", value); + emit useTLSChanged(useTLS); + } +} + +bool VPNControl::getCompressed() const +{ + return compressed; +} + +void VPNControl::setCompressed(bool value) +{ + if (value != compressed) { + compressed = value; + settingsSetValue("compressed", value); + emit compressedChanged(compressed); + } +} + +unsigned int VPNControl::getPort() const +{ + return port; +} + +void VPNControl::setPort(unsigned int value) +{ + if (value != port) { + port = value; + settingsSetValue("port", value); + emit portChanged(port); + } +} + +QString VPNControl::getServer() const +{ + return server; +} + +void VPNControl::setServer(const QString &value) +{ + if (value != server) { + server = value; + settingsSetValue("server", value); + emit serverChanged(server); + } +} + +QString VPNControl::getLogText() const +{ + return logText; +} + +void VPNControl::setLogText(const QString &value) +{ + logText = value; + emit logTextChanged(value); +} + +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"; - + collectArguments (); vpnProcess->setReadChannel(QProcess::StandardOutput); connect(vpnProcess, SIGNAL(error(QProcess::ProcessError)), this, SLOT(readError(QProcess::ProcessError))); connect(vpnProcess, SIGNAL(readyRead()), this, SLOT(readData())); @@ -44,12 +146,55 @@ void VPNControl::vpnConnect() { vpnProcess->start(program, arguments); vpnProcess->closeWriteChannel(); setStatus(VPNSTATUS_INITIALISING); + arguments.clear(); + } +} + +void VPNControl::collectArguments () { + arguments.clear(); + + addArgument("config", "/home/nemo/Documents/Configure/OpenVPN/config.ovpn"); + addArgument("remote", server); + addArgument("port", QString::number(port)); + addOption("comp-lzo", compressed); + if (useTLS) { + addArgument("tls-auth", "/home/nemo/Documents/Configure/OpenVPN/ta.key"); + addValue(QString::number(tlsDirection)); } + addArgument("ca", "/home/nemo/Documents/Configure/OpenVPN/ca.crt"); + addArgument("cert", "/home/nemo/Documents/Configure/OpenVPN/Jolla.crt"); + addArgument("key", "/home/nemo/Documents/Configure/OpenVPN/Jolla.key"); +} + +void VPNControl::addArgument (QString key, QString value) { + QString argument; + + argument = "--" + key; + arguments.append(argument); + if (value != "") { + 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); @@ -59,21 +204,21 @@ 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; @@ -95,3 +240,31 @@ void VPNControl::readError(QProcess::ProcessError error) // Disconnect vpnDisconnect(); } + +void VPNControl::updateConfiguration() +{ + printf ("Update configuration\n"); +} + +void VPNControl::logAppend(const QString &text) +{ + if (!text.isEmpty()) { + // How many lines to add + int newLines = text.count('\n'); + int currentLines = logText.count('\n'); + int removeLines = currentLines + newLines - 18; + + // Remove excess lines + while (removeLines > 0) { + int nextLine = logText.lastIndexOf('\n'); + if (nextLine > 0) { + logText = logText.left(nextLine); + } + removeLines--; + } + + // Add new lines + logText.prepend(text); + emit logTextChanged(logText); + } +}