From 7dc1f1e22571a2bbb4fe1f0fe590593d1f13e902 Mon Sep 17 00:00:00 2001 From: pokamest Date: Thu, 25 Feb 2021 18:03:24 +0300 Subject: [PATCH] Utils ip address regexp --- client/ui/mainwindow.cpp | 3 ++- client/utils.cpp | 46 +++++++++------------------------------- client/utils.h | 2 ++ 3 files changed, 14 insertions(+), 37 deletions(-) diff --git a/client/ui/mainwindow.cpp b/client/ui/mainwindow.cpp index b5ccc81f..4062608a 100644 --- a/client/ui/mainwindow.cpp +++ b/client/ui/mainwindow.cpp @@ -98,7 +98,7 @@ MainWindow::MainWindow(QWidget *parent) : qDebug().noquote() << QString("Default config: %1").arg(Utils::defaultVpnConfigFileName()); - m_ipAddressValidator.setRegExp(QRegExp("^((25[0-5]|(2[0-4]|1[0-9]|[1-9]|)[0-9])(\\.(?!$)|$)){4}$")); + m_ipAddressValidator.setRegExp(Utils::ipAddressRegExp()); ui->lineEdit_new_server_ip->setValidator(&m_ipAddressValidator); ui->lineEdit_network_settings_dns1->setValidator(&m_ipAddressValidator); @@ -311,6 +311,7 @@ bool MainWindow::installServer(ServerCredentials credentials, ErrorCode e = ServerController::setupServer(credentials, Protocol::Any); + qDebug() << "Setup server finished with code" << e; if (e) { page->setEnabled(true); button->setVisible(true); diff --git a/client/utils.cpp b/client/utils.cpp index ad20df2b..c8038f4a 100644 --- a/client/utils.cpp +++ b/client/utils.cpp @@ -1,6 +1,8 @@ #include #include #include +#include +#include #include #include @@ -113,45 +115,17 @@ bool Utils::processIsRunning(const QString& fileName) QString Utils::getIPAddress(const QString& host) { - //TODO rewrite to api calls - qDebug().noquote() << "GetIPAddress: checking " + host; - if(host.isEmpty()) { - qDebug().noquote() << "GetIPAddress: host is empty."; - return QString(); + if (ipAddressRegExp().exactMatch(host)) { + return host; } - if(checkIPFormat(host)) { - qDebug().noquote() << "GetIPAddress host is ip:" << host << host; - return host; // it is a ip address. + QList adresses = QHostInfo::fromName(host).addresses(); + if (!adresses.isEmpty()) { + qDebug() << "Resolved address for" << host << adresses.first().toString(); + return adresses.first().toString(); } - QProcess ping; - -#ifdef Q_OS_MACX - ping.start("ping", QStringList() << "-c1" << host); -#endif -#ifdef Q_OS_WIN - ping.start("ping", QStringList() << QString("/n") << "1" << QString("/w") << "1" << host); -#endif - ping.waitForStarted(); - - QEventLoop loop; - loop.connect(&ping, SIGNAL(finished(int)), &loop, SLOT(quit())); - loop.exec(); - - QString d = ping.readAll(); - if(d.size() == 0) - return QString(); - qDebug().noquote() << d; - - QString ip; -#ifdef Q_OS_MACX - ip = getStringBetween(d, "(", ")"); -#endif -#ifdef Q_OS_WIN - ip = getStringBetween(d, "[", "]"); -#endif - qDebug().noquote() << "GetIPAddress:" << host << ip; - return ip; + qDebug() << "Unable to resolve address for " << host; + return ""; } QString Utils::getStringBetween(const QString& s, const QString& a, const QString& b) diff --git a/client/utils.h b/client/utils.h index 23eed83b..3bf659f3 100644 --- a/client/utils.h +++ b/client/utils.h @@ -1,6 +1,7 @@ #ifndef UTILS_H #define UTILS_H +#include #include class Utils { @@ -19,6 +20,7 @@ public: static QString getIPAddress(const QString& host); static QString getStringBetween(const QString& s, const QString& a, const QString& b); static bool checkIPFormat(const QString& ip); + static QRegExp ipAddressRegExp() { return QRegExp("^((25[0-5]|(2[0-4]|1[0-9]|[1-9]|)[0-9])(\\.(?!$)|$)){4}$"); } }; #endif // UTILS_H