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());
|
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_new_server_ip->setValidator(&m_ipAddressValidator);
|
||||||
ui->lineEdit_network_settings_dns1->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);
|
ErrorCode e = ServerController::setupServer(credentials, Protocol::Any);
|
||||||
|
qDebug() << "Setup server finished with code" << e;
|
||||||
if (e) {
|
if (e) {
|
||||||
page->setEnabled(true);
|
page->setEnabled(true);
|
||||||
button->setVisible(true);
|
button->setVisible(true);
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
#include <QCoreApplication>
|
#include <QCoreApplication>
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
#include <QDir>
|
#include <QDir>
|
||||||
|
#include <QHostAddress>
|
||||||
|
#include <QHostInfo>
|
||||||
#include <QProcess>
|
#include <QProcess>
|
||||||
#include <QStandardPaths>
|
#include <QStandardPaths>
|
||||||
|
|
||||||
|
@ -113,45 +115,17 @@ bool Utils::processIsRunning(const QString& fileName)
|
||||||
|
|
||||||
QString Utils::getIPAddress(const QString& host)
|
QString Utils::getIPAddress(const QString& host)
|
||||||
{
|
{
|
||||||
//TODO rewrite to api calls
|
if (ipAddressRegExp().exactMatch(host)) {
|
||||||
qDebug().noquote() << "GetIPAddress: checking " + host;
|
return host;
|
||||||
if(host.isEmpty()) {
|
|
||||||
qDebug().noquote() << "GetIPAddress: host is empty.";
|
|
||||||
return QString();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if(checkIPFormat(host)) {
|
QList<QHostAddress> adresses = QHostInfo::fromName(host).addresses();
|
||||||
qDebug().noquote() << "GetIPAddress host is ip:" << host << host;
|
if (!adresses.isEmpty()) {
|
||||||
return host; // it is a ip address.
|
qDebug() << "Resolved address for" << host << adresses.first().toString();
|
||||||
|
return adresses.first().toString();
|
||||||
}
|
}
|
||||||
QProcess ping;
|
qDebug() << "Unable to resolve address for " << host;
|
||||||
|
return "";
|
||||||
#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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QString Utils::getStringBetween(const QString& s, const QString& a, const QString& b)
|
QString Utils::getStringBetween(const QString& s, const QString& a, const QString& b)
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
#ifndef UTILS_H
|
#ifndef UTILS_H
|
||||||
#define UTILS_H
|
#define UTILS_H
|
||||||
|
|
||||||
|
#include <QRegExp>
|
||||||
#include <QString>
|
#include <QString>
|
||||||
|
|
||||||
class Utils {
|
class Utils {
|
||||||
|
@ -19,6 +20,7 @@ public:
|
||||||
static QString getIPAddress(const QString& host);
|
static QString getIPAddress(const QString& host);
|
||||||
static QString getStringBetween(const QString& s, const QString& a, const QString& b);
|
static QString getStringBetween(const QString& s, const QString& a, const QString& b);
|
||||||
static bool checkIPFormat(const QString& ip);
|
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
|
#endif // UTILS_H
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue