mirror of
https://github.com/yuezk/GlobalProtect-openconnect.git
synced 2025-04-02 18:31:50 -04:00
Improve the connect logic
This commit is contained in:
parent
462ed0fe7c
commit
c68338c180
@ -20,7 +20,6 @@ EnhancedWebView::~EnhancedWebView()
|
||||
void EnhancedWebView::initialize()
|
||||
{
|
||||
QString port = QProcessEnvironment::systemEnvironment().value(ENV_CDP_PORT);
|
||||
qDebug() << "port:" << port;
|
||||
cdp->initialize("http://127.0.0.1:" + port + "/json");
|
||||
}
|
||||
|
||||
|
@ -28,21 +28,13 @@ GPClient::GPClient(QWidget *parent)
|
||||
// QNetworkAccessManager setup
|
||||
networkManager = new QNetworkAccessManager(this);
|
||||
|
||||
// Login window setup
|
||||
loginWindow = new SAMLLoginWindow(this);
|
||||
QObject::connect(loginWindow, &SAMLLoginWindow::success, this, &GPClient::onLoginSuccess);
|
||||
QObject::connect(loginWindow, &SAMLLoginWindow::rejected, this, &GPClient::connectFailed);
|
||||
|
||||
// DBus service setup
|
||||
vpn = new com::yuezk::qt::GPService("com.yuezk.qt.GPService", "/", QDBusConnection::systemBus(), this);
|
||||
QObject::connect(vpn, &com::yuezk::qt::GPService::connected, this, &GPClient::onVPNConnected);
|
||||
QObject::connect(vpn, &com::yuezk::qt::GPService::disconnected, this, &GPClient::onVPNDisconnected);
|
||||
QObject::connect(vpn, &com::yuezk::qt::GPService::logAvailable, this, &GPClient::onVPNLogAvailable);
|
||||
|
||||
int status = vpn->status();
|
||||
if (status != 0) {
|
||||
updateConnectionStatus("connected");
|
||||
}
|
||||
initVpnStatus();
|
||||
}
|
||||
|
||||
GPClient::~GPClient()
|
||||
@ -50,7 +42,6 @@ GPClient::~GPClient()
|
||||
delete ui;
|
||||
delete networkManager;
|
||||
delete reply;
|
||||
delete loginWindow;
|
||||
delete vpn;
|
||||
delete settings;
|
||||
}
|
||||
@ -64,7 +55,7 @@ void GPClient::on_connectButton_clicked()
|
||||
settings->setValue("portal", portal);
|
||||
ui->statusLabel->setText("Authenticating...");
|
||||
updateConnectionStatus("pending");
|
||||
samlLogin(portal);
|
||||
doAuth(portal);
|
||||
} else if (btnText == "Cancel") {
|
||||
ui->statusLabel->setText("Canceling...");
|
||||
updateConnectionStatus("pending");
|
||||
@ -83,7 +74,7 @@ void GPClient::on_connectButton_clicked()
|
||||
void GPClient::preloginResultFinished()
|
||||
{
|
||||
if (reply->error()) {
|
||||
qDebug() << "request error";
|
||||
qDebug() << "Prelogin request error";
|
||||
emit connectFailed();
|
||||
return;
|
||||
}
|
||||
@ -109,20 +100,18 @@ void GPClient::preloginResultFinished()
|
||||
}
|
||||
|
||||
if (samlMethod == nullptr || samlRequest == nullptr) {
|
||||
qCritical("This does not appear to be a SAML prelogin response (<saml-auth-method> or <saml-request> tags missing)");
|
||||
qDebug("This does not appear to be a SAML prelogin response (<saml-auth-method> or <saml-request> tags missing)");
|
||||
emit connectFailed();
|
||||
return;
|
||||
}
|
||||
|
||||
if (samlMethod == "POST") {
|
||||
// TODO
|
||||
qInfo("TODO: SAML method is POST");
|
||||
qDebug("TODO: SAML method is POST");
|
||||
emit connectFailed();
|
||||
} else if (samlMethod == "REDIRECT") {
|
||||
qInfo() << "Request URL is: %s" << samlRequest;
|
||||
|
||||
loginWindow->login(samlRequest);
|
||||
loginWindow->exec();
|
||||
samlLogin(samlRequest);
|
||||
}
|
||||
}
|
||||
|
||||
@ -177,6 +166,7 @@ void GPClient::onVPNConnected()
|
||||
|
||||
void GPClient::onVPNDisconnected()
|
||||
{
|
||||
qDebug("========= disconnected");
|
||||
updateConnectionStatus("not_connected");
|
||||
}
|
||||
|
||||
@ -185,6 +175,20 @@ void GPClient::onVPNLogAvailable(QString log)
|
||||
qDebug() << log;
|
||||
}
|
||||
|
||||
void GPClient::initVpnStatus() {
|
||||
int status = vpn->status();
|
||||
qDebug() << "VPN status:" << status;
|
||||
if (status == 1) {
|
||||
ui->statusLabel->setText("Connecting...");
|
||||
updateConnectionStatus("pending");
|
||||
} else if (status == 2) {
|
||||
updateConnectionStatus("connected");
|
||||
} else if (status == 3) {
|
||||
ui->statusLabel->setText("Disconnecting...");
|
||||
updateConnectionStatus("pending");
|
||||
}
|
||||
}
|
||||
|
||||
void GPClient::moveCenter()
|
||||
{
|
||||
QDesktopWidget *desktop = QApplication::desktop();
|
||||
@ -207,7 +211,7 @@ void GPClient::moveCenter()
|
||||
move(x, y);
|
||||
}
|
||||
|
||||
void GPClient::samlLogin(const QString portal)
|
||||
void GPClient::doAuth(const QString portal)
|
||||
{
|
||||
const QString preloginUrl = "https://" + portal + "/ssl-vpn/prelogin.esp";
|
||||
qDebug("%s", preloginUrl.toStdString().c_str());
|
||||
@ -215,3 +219,15 @@ void GPClient::samlLogin(const QString portal)
|
||||
reply = networkManager->post(QNetworkRequest(preloginUrl), (QByteArray) nullptr);
|
||||
connect(reply, &QNetworkReply::finished, this, &GPClient::preloginResultFinished);
|
||||
}
|
||||
|
||||
void GPClient::samlLogin(const QString loginUrl)
|
||||
{
|
||||
SAMLLoginWindow *loginWindow = new SAMLLoginWindow(this);
|
||||
|
||||
QObject::connect(loginWindow, &SAMLLoginWindow::success, this, &GPClient::onLoginSuccess);
|
||||
QObject::connect(loginWindow, &SAMLLoginWindow::rejected, this, &GPClient::connectFailed);
|
||||
|
||||
loginWindow->login(loginUrl);
|
||||
loginWindow->exec();
|
||||
delete loginWindow;
|
||||
}
|
||||
|
@ -2,7 +2,6 @@
|
||||
#define GPCLIENT_H
|
||||
|
||||
#include "gpservice_interface.h"
|
||||
#include "samlloginwindow.h"
|
||||
#include <QMainWindow>
|
||||
#include <QNetworkAccessManager>
|
||||
#include <QNetworkReply>
|
||||
@ -34,14 +33,15 @@ private slots:
|
||||
|
||||
private:
|
||||
Ui::GPClient *ui;
|
||||
SAMLLoginWindow *loginWindow;
|
||||
QNetworkAccessManager *networkManager;
|
||||
QNetworkReply *reply;
|
||||
com::yuezk::qt::GPService *vpn;
|
||||
QSettings *settings;
|
||||
|
||||
void initVpnStatus();
|
||||
void moveCenter();
|
||||
void updateConnectionStatus(QString status);
|
||||
void samlLogin(const QString portal);
|
||||
void doAuth(const QString portal);
|
||||
void samlLogin(const QString loginUrl);
|
||||
};
|
||||
#endif // GPCLIENT_H
|
||||
|
@ -51,8 +51,8 @@ void GPService::quit()
|
||||
|
||||
void GPService::connect(QString server, QString username, QString passwd)
|
||||
{
|
||||
if (status() != QProcess::NotRunning) {
|
||||
log("Openconnect has already started on PID " + QString::number(openconnect->processId()) + ", nothing changed.");
|
||||
if (vpnStatus != GPService::VpnNotConnected) {
|
||||
log("VPN status is: " + QVariant::fromValue(vpnStatus).toString());
|
||||
return;
|
||||
}
|
||||
|
||||
@ -67,6 +67,7 @@ void GPService::connect(QString server, QString username, QString passwd)
|
||||
<< "--protocol=gp"
|
||||
<< "-u" << username
|
||||
<< "--passwd-on-stdin"
|
||||
<< "--timestamp"
|
||||
<< server;
|
||||
|
||||
openconnect->start(bin, args);
|
||||
@ -77,23 +78,26 @@ void GPService::connect(QString server, QString username, QString passwd)
|
||||
void GPService::disconnect()
|
||||
{
|
||||
if (openconnect->state() != QProcess::NotRunning) {
|
||||
vpnStatus = GPService::VpnDisconnecting;
|
||||
openconnect->terminate();
|
||||
}
|
||||
}
|
||||
|
||||
int GPService::status()
|
||||
{
|
||||
return openconnect->state();
|
||||
return vpnStatus;
|
||||
}
|
||||
|
||||
void GPService::onProcessStarted()
|
||||
{
|
||||
log("Openconnect started successfully, PID=" + QString::number(openconnect->processId()));
|
||||
vpnStatus = GPService::VpnConnecting;
|
||||
}
|
||||
|
||||
void GPService::onProcessError(QProcess::ProcessError error)
|
||||
{
|
||||
log("Error occurred: " + QVariant::fromValue(error).toString());
|
||||
vpnStatus = GPService::VpnNotConnected;
|
||||
emit disconnected();
|
||||
}
|
||||
|
||||
@ -102,7 +106,8 @@ void GPService::onProcessStdout()
|
||||
QString output = openconnect->readAllStandardOutput();
|
||||
|
||||
log(output);
|
||||
if (output.startsWith("Connected as")) {
|
||||
if (output.indexOf("Connected as") >= 0) {
|
||||
vpnStatus = GPService::VpnConnected;
|
||||
emit connected();
|
||||
}
|
||||
}
|
||||
@ -115,6 +120,7 @@ void GPService::onProcessStderr()
|
||||
void GPService::onProcessFinished(int exitCode, QProcess::ExitStatus exitStatus)
|
||||
{
|
||||
log("Openconnect process exited with code " + QString::number(exitCode) + " and exit status " + QVariant::fromValue(exitStatus).toString());
|
||||
vpnStatus = GPService::VpnNotConnected;
|
||||
emit disconnected();
|
||||
|
||||
if (aboutToQuit) {
|
||||
@ -124,8 +130,6 @@ void GPService::onProcessFinished(int exitCode, QProcess::ExitStatus exitStatus)
|
||||
|
||||
void GPService::log(QString msg)
|
||||
{
|
||||
// 2020-02-12 15:33:45.120: log messsage
|
||||
QString record = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz") + ": " + msg;
|
||||
qDebug() << record;
|
||||
emit logAvailable(record);
|
||||
qDebug() << msg;
|
||||
emit logAvailable(msg);
|
||||
}
|
||||
|
@ -21,6 +21,13 @@ public:
|
||||
explicit GPService(QObject *parent = nullptr);
|
||||
~GPService();
|
||||
|
||||
enum VpnStatus {
|
||||
VpnNotConnected,
|
||||
VpnConnecting,
|
||||
VpnConnected,
|
||||
VpnDisconnecting,
|
||||
};
|
||||
|
||||
signals:
|
||||
void connected();
|
||||
void disconnected();
|
||||
@ -42,6 +49,7 @@ private slots:
|
||||
private:
|
||||
QProcess *openconnect;
|
||||
bool aboutToQuit = false;
|
||||
int vpnStatus = GPService::VpnNotConnected;
|
||||
|
||||
void log(QString msg);
|
||||
static QString findBinary();
|
||||
|
Loading…
Reference in New Issue
Block a user