merge master

This commit is contained in:
Kevin Yue
2021-09-19 16:05:49 +08:00
59 changed files with 1049 additions and 366 deletions

57
GPService/CMakeLists.txt Normal file
View File

@@ -0,0 +1,57 @@
include("${CMAKE_SOURCE_DIR}/cmake/Add3rdParty.cmake")
project(GPService)
set(gpservice_GENERATED_SOURCES)
# generate the dbus xml definition
qt5_generate_dbus_interface(gpservice.h ${CMAKE_BINARY_DIR}/com.yuezk.qt.GPService.xml)
# generate dbus adaptor
qt5_add_dbus_adaptor(
gpservice_GENERATED_SOURCES
${CMAKE_BINARY_DIR}/com.yuezk.qt.GPService.xml
gpservice.h
GPService
)
add_executable(gpservice
gpservice.cpp
main.cpp
sigwatch.cpp
${gpservice_GENERATED_SOURCES}
)
add_3rdparty(
SingleApplication
GIT_REPOSITORY https://github.com/itay-grudev/SingleApplication.git
GIT_TAG v3.3.0
CMAKE_ARGS -DQAPPLICATION_CLASS=QCoreApplication
)
ExternalProject_Get_Property(SingleApplication-${PROJECT_NAME} SOURCE_DIR BINARY_DIR)
set(SingleApplication_INCLUDE_DIR ${SOURCE_DIR})
set(SingleApplication_LIBRARY ${BINARY_DIR}/libSingleApplication.a)
add_dependencies(gpservice SingleApplication-${PROJECT_NAME})
target_include_directories(gpservice PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_BINARY_DIR}
${SingleApplication_INCLUDE_DIR}
)
target_link_libraries(gpservice
${SingleApplication_LIBRARY}
Qt5::Core
Qt5::Network
Qt5::DBus
)
target_compile_definitions(gpservice PUBLIC QAPPLICATION_CLASS=QCoreApplication)
install(TARGETS gpservice DESTINATION "/usr/bin")
install(FILES "dbus/com.yuezk.qt.GPService.conf" DESTINATION "/usr/share/dbus-1/system.d" )
install(FILES "dbus/com.yuezk.qt.GPService.service" DESTINATION "/usr/share/dbus-1/system-services")
install(FILES "systemd/gpservice.service" DESTINATION "/etc/systemd/system")

View File

@@ -1,52 +0,0 @@
TARGET = gpservice
#QT += dbus
QT -= gui
CONFIG += c++11 console
CONFIG -= app_bundle
include(../singleapplication/singleapplication.pri)
DEFINES += QAPPLICATION_CLASS=QCoreApplication
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
HEADERS += \
# gpservice.h \
sigwatch.h
SOURCES += \
# gpservice.cpp \
main.cpp \
sigwatch.cpp
#DBUS_ADAPTORS += gpservice.xml
# Default rules for deployment.
target.path = /usr/bin
INSTALLS += target
#DISTFILES += \
# dbus/com.yuezk.qt.GPService.conf \
# dbus/com.yuezk.qt.GPService.service \
# systemd/gpservice.service
#dbus_config.path = /usr/share/dbus-1/system.d/
#dbus_config.files = dbus/com.yuezk.qt.GPService.conf
#dbus_service.path = /usr/share/dbus-1/system-services/
#dbus_service.files = dbus/com.yuezk.qt.GPService.service
#systemd_service.path = /etc/systemd/system/
#systemd_service.files = systemd/gpservice.service
#INSTALLS += dbus_config dbus_service systemd_service

View File

@@ -1,11 +1,13 @@
#include "gpservice.h"
#include "gpservice_adaptor.h"
#include <QtCore/QFileInfo>
#include <QtCore/QDateTime>
#include <QtCore/QVariant>
#include <QtCore/QRegularExpression>
#include <QtCore/QRegularExpressionMatch>
#include <QtDBus/QtDBus>
#include <QtCore/QProcessEnvironment>
#include <QFileInfo>
#include <QtDBus>
#include <QDateTime>
#include <QVariant>
#include <QProcessEnvironment>
#include "gpservice.h"
#include "gpserviceadaptor.h"
GPService::GPService(QObject *parent)
: QObject(parent)
@@ -107,13 +109,17 @@ 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(" "));
@@ -122,6 +128,32 @@ void GPService::connect(QString server, QString username, QString passwd, QStrin
openconnect->start(bin, args);
}
bool GPService::isValidVersion(QString &bin) {
QProcess p;
p.start(bin, QStringList("--version"));
p.waitForFinished();
QString output = p.readAllStandardError() + p.readAllStandardOutput();
QRegularExpression re("v(\\d+).*?(\\s|\\n)");
QRegularExpressionMatch match = re.match(output);
if (match.hasMatch()) {
log("Output of `openconnect --version`: " + output);
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) {

View File

@@ -1,8 +1,8 @@
#ifndef GLOBALPROTECTSERVICE_H
#define GLOBALPROTECTSERVICE_H
#include <QObject>
#include <QProcess>
#include <QtCore/QObject>
#include <QtCore/QProcess>
static const QString binaryPaths[] {
"/usr/local/bin/openconnect",
@@ -21,6 +21,8 @@ public:
explicit GPService(QObject *parent = nullptr);
~GPService();
void quit();
enum VpnStatus {
VpnNotConnected,
VpnConnecting,
@@ -38,7 +40,6 @@ public slots:
void connect(QString server, QString username, QString passwd, QString extraArgs);
void disconnect();
int status();
void quit();
private slots:
void onProcessStarted();
@@ -53,6 +54,7 @@ private:
int vpnStatus = GPService::VpnNotConnected;
void log(QString msg);
bool isValidVersion(QString &bin);
static QString findBinary();
static QStringList splitCommand(QString command);
};

View File

@@ -1,26 +0,0 @@
<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN" "http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
<node>
<interface name="com.yuezk.qt.GPService">
<signal name="connected">
</signal>
<signal name="disconnected">
</signal>
<signal name="logAvailable">
<arg name="log" type="s" />
</signal>
<signal name="error">
<arg name="errorMessage" type="s" />
</signal>
<method name="connect">
<arg name="server" type="s" direction="in"/>
<arg name="username" type="s" direction="in"/>
<arg name="passwd" type="s" direction="in"/>
<arg name="extraArgs" type="s" direction="in"/>
</method>
<method name="disconnect">
</method>
<method name="status">
<arg type="i" direction="out"/>
</method>
</interface>
</node>

View File

@@ -1,23 +1,23 @@
//#include "gpservice.h"
#include <iostream>
#include <QtDBus/QtDBus>
#include <QtCore/QProcessEnvironment>
#include "gpservice.h"
#include "singleapplication.h"
#include "sigwatch.h"
#include "iostream"
//#include <QtDBus>
#include <QProcessEnvironment>
int main(int argc, char *argv[])
{
SingleApplication app(argc, argv);
// if (!QDBusConnection::systemBus().isConnected()) {
// qWarning("Cannot connect to the D-Bus session bus.\n"
// "Please check your system settings and try again.\n");
// return 1;
// }
if (!QDBusConnection::systemBus().isConnected())
{
qWarning("Cannot connect to the D-Bus session bus.\n"
"Please check your system settings and try again.\n");
return 1;
}
// GPService service;
GPService service;
QString env = "ENV: " + QProcessEnvironment::systemEnvironment().toStringList().join("\n");
std::cout << env.toStdString();
@@ -27,7 +27,7 @@ int main(int argc, char *argv[])
sigwatch.watchForSignal(SIGTERM);
sigwatch.watchForSignal(SIGQUIT);
sigwatch.watchForSignal(SIGHUP);
// QObject::connect(&sigwatch, &UnixSignalWatcher::unixSignal, &service, &GPService::quit);
QObject::connect(&sigwatch, &UnixSignalWatcher::unixSignal, &service, &GPService::quit);
return app.exec();
}