From c84a2abb14d365fa3cc206fdd6ff73dd4a895fb5 Mon Sep 17 00:00:00 2001 From: Kevin Yue Date: Sat, 18 Sep 2021 20:43:42 +0800 Subject: [PATCH] check the openconnect version --- GPService/gpservice.cpp | 48 +++++++++++++++++++++++++++++++++-------- GPService/gpservice.h | 5 +++-- 2 files changed, 42 insertions(+), 11 deletions(-) diff --git a/GPService/gpservice.cpp b/GPService/gpservice.cpp index 0d5fe4e..0676b9f 100644 --- a/GPService/gpservice.cpp +++ b/GPService/gpservice.cpp @@ -1,10 +1,12 @@ #include "gpservice.h" #include "gpserviceadaptor.h" -#include -#include -#include -#include +#include +#include +#include +#include +#include +#include GPService::GPService(QObject *parent) : QObject(parent) @@ -104,19 +106,47 @@ void GPService::connect(QString server, QString username, QString passwd, QStrin return; } + if (!isValidVersion(bin)) { + return; + } + QStringList args; args << QCoreApplication::arguments().mid(1) - << "--protocol=gp" - << splitCommand(extraArgs) - << "-u" << username - << "-C" << passwd - << server; + << "--protocol=gp" + << splitCommand(extraArgs) + << "-u" << username + << "-C" << passwd + << server; log("Start process with arugments: " + args.join(" ")); openconnect->start(bin, args); } +bool GPService::isValidVersion(QString &bin) { + QProcess p; + p.start(bin, QStringList("--version")); + p.waitForFinished(); + QString output = p.readAllStandardOutput(); + + QRegularExpression re("v(\\d+).*?\\n"); + QRegularExpressionMatch match = re.match(output); + + if (match.hasMatch()) { + QString fullVersion = match.captured(0); + QString majorVersion = match.captured(1); + + if (majorVersion.toInt() < 8) { + emit error("The OpenConnect version must greater than v8.0.0, but got " + fullVersion); + return false; + } + } else { + log("Failed to parse the OpenConnect version from " + output); + } + + return true; +} + void GPService::disconnect() { if (openconnect->state() != QProcess::NotRunning) { diff --git a/GPService/gpservice.h b/GPService/gpservice.h index db72f8a..a3b4856 100644 --- a/GPService/gpservice.h +++ b/GPService/gpservice.h @@ -1,8 +1,8 @@ #ifndef GLOBALPROTECTSERVICE_H #define GLOBALPROTECTSERVICE_H -#include -#include +#include +#include static const QString binaryPaths[] { "/usr/local/bin/openconnect", @@ -54,6 +54,7 @@ private: int vpnStatus = GPService::VpnNotConnected; void log(QString msg); + bool isValidVersion(QString &bin); static QString findBinary(); static QStringList splitCommand(QString command); };