Utils ip address regexp
This commit is contained in:
parent
a47ab15aef
commit
7dc1f1e225
3 changed files with 14 additions and 37 deletions
|
@ -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);
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
#include <QCoreApplication>
|
||||
#include <QDebug>
|
||||
#include <QDir>
|
||||
#include <QHostAddress>
|
||||
#include <QHostInfo>
|
||||
#include <QProcess>
|
||||
#include <QStandardPaths>
|
||||
|
||||
|
@ -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<QHostAddress> 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)
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#ifndef UTILS_H
|
||||
#define UTILS_H
|
||||
|
||||
#include <QRegExp>
|
||||
#include <QString>
|
||||
|
||||
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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue