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