Fix hostname/domain split (#1079)

This commit is contained in:
LordGrey 2020-11-14 16:35:55 +01:00 committed by GitHub
parent d5a1e7d19d
commit 56f45a4930
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 14 deletions

View File

@ -668,7 +668,8 @@ QJsonObject LedDeviceCololight::discover()
{
QJsonObject obj;
obj.insert("ip", i.key());
QString ipAddress = i.key();
obj.insert("ip", ipAddress);
obj.insert("model", i.value().value(COLOLIGHT_MODEL));
obj.insert("type", i.value().value(COLOLIGHT_MODEL_TYPE));
obj.insert("mac", i.value().value(COLOLIGHT_MAC));
@ -678,7 +679,6 @@ QJsonObject LedDeviceCololight::discover()
if (hostInfo.error() == QHostInfo::NoError)
{
QString hostname = hostInfo.hostName();
//Seems that for Windows no local domain name is resolved
if (!QHostInfo::localDomainName().isEmpty())
{
obj.insert("hostname", hostname.remove("." + QHostInfo::localDomainName()));
@ -686,9 +686,23 @@ QJsonObject LedDeviceCololight::discover()
}
else
{
int domainPos = hostname.indexOf('.');
obj.insert("hostname", hostname.left(domainPos));
obj.insert("domain", hostname.mid(domainPos + 1));
if (hostname.startsWith(ipAddress))
{
obj.insert("hostname", ipAddress);
QString domain = hostname.remove(ipAddress);
if (domain.at(0) == '.')
{
domain.remove(0, 1);
}
obj.insert("domain", domain);
}
else
{
int domainPos = hostname.indexOf('.');
obj.insert("hostname", hostname.left(domainPos));
obj.insert("domain", hostname.mid(domainPos + 1));
}
}
}

View File

@ -316,24 +316,40 @@ QJsonArray SSDPDiscover::getServicesDiscoveredJson() const
obj.insert("usn", i.value().uniqueServiceName);
QUrl url (i.value().location);
obj.insert("ip", url.host());
QString ipAddress = url.host();
obj.insert("ip", ipAddress);
obj.insert("port", url.port());
QHostInfo hostInfo = QHostInfo::fromName(url.host());
if (hostInfo.error() == QHostInfo::NoError )
if (hostInfo.error() == QHostInfo::NoError)
{
QString hostname = hostInfo.hostName();
//Seems that for Windows no local domain name is resolved
if (!hostInfo.localDomainName().isEmpty() )
if (!QHostInfo::localDomainName().isEmpty())
{
obj.insert("hostname", hostname.remove("."+hostInfo.localDomainName()));
obj.insert("domain", hostInfo.localDomainName());
obj.insert("hostname", hostname.remove("." + QHostInfo::localDomainName()));
obj.insert("domain", QHostInfo::localDomainName());
}
else
{
int domainPos = hostname.indexOf('.');
obj.insert("hostname", hostname.left(domainPos));
obj.insert("domain", hostname.mid(domainPos+1));
if (hostname.startsWith(ipAddress))
{
obj.insert("hostname", ipAddress);
QString domain = hostname.remove(ipAddress);
if (domain.at(0) == '.')
{
domain.remove(0, 1);
}
obj.insert("domain", domain);
}
else
{
int domainPos = hostname.indexOf('.');
obj.insert("hostname", hostname.left(domainPos));
obj.insert("domain", hostname.mid(domainPos + 1));
}
}
}