Refine the code

This commit is contained in:
Kevin Yue 2020-02-15 22:15:04 +08:00
parent 5d57fd5fd1
commit 7fa8357a1c
2 changed files with 51 additions and 37 deletions

View File

@ -47,23 +47,28 @@ GPService::GPService(QObject *parent)
: QObject(parent) : QObject(parent)
, openconnect(new SandboxProcess) , openconnect(new SandboxProcess)
{ {
// Register the DBus service
new GPServiceAdaptor(this); new GPServiceAdaptor(this);
QDBusConnection dbus = QDBusConnection::systemBus(); QDBusConnection dbus = QDBusConnection::systemBus();
dbus.registerObject("/", this); dbus.registerObject("/", this);
dbus.registerService("com.yuezk.qt.GPService"); dbus.registerService("com.yuezk.qt.GPService");
// Setup the openconnect process
QObject::connect(openconnect, &QProcess::started, this, &GPService::onProcessStarted);
QObject::connect(openconnect, &QProcess::errorOccurred, this, &GPService::onProcessError);
QObject::connect(openconnect, &QProcess::readyReadStandardOutput, this, &GPService::onProcessStdout);
QObject::connect(openconnect, &QProcess::readyReadStandardError, this, &GPService::onProcessStderr);
QObject::connect(openconnect, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), this, &GPService::onProcessFinished);
} }
void GPService::connect(QString server, QString username, QString passwd) void GPService::connect(QString server, QString username, QString passwd)
{ {
qDebug() << server << username << passwd;
if (status() != QProcess::NotRunning) { if (status() != QProcess::NotRunning) {
log("Openconnect has already started on PID " + QString::number(openconnect->processId()) + ", nothing changed."); log("Openconnect has already started on PID " + QString::number(openconnect->processId()) + ", nothing changed.");
return; return;
} }
QString bin = findBinary(); QString bin = findBinary();
if (bin == nullptr) { if (bin == nullptr) {
log("Could not found openconnect binary, make sure openconnect is installed, exiting."); log("Could not found openconnect binary, make sure openconnect is installed, exiting.");
return; return;
@ -76,11 +81,10 @@ void GPService::connect(QString server, QString username, QString passwd)
return; return;
} }
qDebug() << tunName;
// openconnect --protocol=gp -i vpn0 -s 'sudo -E /etc/vpnc/vpnc-script' -u "zyue@microstrategy.com" --passwd-on-stdin "https://vpn.microstrategy.com/gateway:prelogin-cookie" // openconnect --protocol=gp -i vpn0 -s 'sudo -E /etc/vpnc/vpnc-script' -u "zyue@microstrategy.com" --passwd-on-stdin "https://vpn.microstrategy.com/gateway:prelogin-cookie"
QStringList args; QStringList args;
args << "--protocol=gp" args << "--protocol=gp"
<< "--no-dtls"
// << "-i" << tunName // << "-i" << tunName
// << "-s" << "sudo -E /etc/vpnc/vpnc-script" // << "-s" << "sudo -E /etc/vpnc/vpnc-script"
// << "-U" << NM_OPENCONNECT_USER // << "-U" << NM_OPENCONNECT_USER
@ -91,35 +95,6 @@ void GPService::connect(QString server, QString username, QString passwd)
openconnect->start(bin, args); openconnect->start(bin, args);
openconnect->write(passwd.toUtf8()); openconnect->write(passwd.toUtf8());
openconnect->closeWriteChannel(); openconnect->closeWriteChannel();
QObject::connect(openconnect, &QProcess::started, [this]() {
log("Openconnect started successfully, PID=" + QString::number(openconnect->processId()));
});
QObject::connect(openconnect, &QProcess::errorOccurred, [tunName, this](QProcess::ProcessError error) {
log("Error occurred: Openconnect started failed");
destroyPersistentTundev(tunName);
emit disconnected();
});
QObject::connect(openconnect, &QProcess::readyReadStandardOutput, [this] () {
QString output = openconnect->readAllStandardOutput();
log(output);
if (output.startsWith("Connected as")) {
emit connected();
}
});
QObject::connect(openconnect, &QProcess::readyReadStandardError, [this] () {
log(openconnect->readAllStandardError());
});
QObject::connect(openconnect, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), [tunName, this](int exitCode, QProcess::ExitStatus exitStatus) {
log("Openconnect process exited with code " + QString::number(exitCode) + " and exit status " + QVariant::fromValue(exitStatus).toString());
destroyPersistentTundev(tunName);
emit disconnected();
});
} }
void GPService::disconnect() void GPService::disconnect()
@ -134,6 +109,38 @@ int GPService::status()
return openconnect->state(); return openconnect->state();
} }
void GPService::onProcessStarted()
{
log("Openconnect started successfully, PID=" + QString::number(openconnect->processId()));
}
void GPService::onProcessError(QProcess::ProcessError error)
{
log("Error occurred: Openconnect started failed " + QVariant::fromValue(error).toString());
emit disconnected();
}
void GPService::onProcessStdout()
{
QString output = openconnect->readAllStandardOutput();
log(output);
if (output.startsWith("Connected as")) {
emit connected();
}
}
void GPService::onProcessStderr()
{
log(openconnect->readAllStandardError());
}
void GPService::onProcessFinished(int exitCode, QProcess::ExitStatus exitStatus)
{
log("Openconnect process exited with code " + QString::number(exitCode) + " and exit status " + QVariant::fromValue(exitStatus).toString());
emit disconnected();
}
void GPService::log(QString msg) void GPService::log(QString msg)
{ {
// 2020-02-12 15:33:45.120: log messsage // 2020-02-12 15:33:45.120: log messsage

View File

@ -32,6 +32,13 @@ public slots:
void disconnect(); void disconnect();
int status(); int status();
private slots:
void onProcessStarted();
void onProcessError(QProcess::ProcessError error);
void onProcessStdout();
void onProcessStderr();
void onProcessFinished(int exitCode, QProcess::ExitStatus exitStatus);
private: private:
QProcess *openconnect; QProcess *openconnect;