mirror of
https://github.com/Palakis/obs-websocket.git
synced 2024-08-30 18:12:16 +00:00
Utils::Platform: Refactor GetLocalAddress()
It was pointed out that the existing functionality was not effective at filtering out invalid interfaces, so we add a priority system to try harder at finding a valid address.
This commit is contained in:
parent
1ecf1e5dfd
commit
d375bbc98b
@ -28,19 +28,31 @@ std::string Utils::Platform::GetLocalAddress()
|
||||
validAddresses.push_back(address.toString());
|
||||
}
|
||||
|
||||
for (auto address : validAddresses) {
|
||||
// Hacks to try to pick the best address
|
||||
if (address.startsWith("192.168")) {
|
||||
return address.toStdString();
|
||||
} else if (address.startsWith("172.16")) {
|
||||
return address.toStdString();
|
||||
}
|
||||
}
|
||||
|
||||
if (validAddresses.size() > 0)
|
||||
return validAddresses[0].toStdString();
|
||||
|
||||
// Return early if no valid addresses were found
|
||||
if (validAddresses.size() == 0)
|
||||
return "0.0.0.0";
|
||||
|
||||
std::vector<std::pair<QString, uint8_t>> preferredAddresses;
|
||||
for (auto address : validAddresses) {
|
||||
// Attribute a priority (0 is best) to the address to choose the best picks
|
||||
if (address.startsWith("192.168.1") || address.startsWith("192.168.0")) { // Prefer common consumer router network prefixes
|
||||
preferredAddresses.push_back(std::make_pair(address, 0));
|
||||
} else if (address.startsWith("172.16")) { // Slightly less common consumer router network prefixes
|
||||
preferredAddresses.push_back(std::make_pair(address, 1));
|
||||
} else if (address.startsWith("10.")) { // Even less common consumer router network prefixes
|
||||
preferredAddresses.push_back(std::make_pair(address, 2));
|
||||
} else { // Set all other addresses to equal priority
|
||||
preferredAddresses.push_back(std::make_pair(address, 255));
|
||||
}
|
||||
}
|
||||
|
||||
// Sort by priority
|
||||
std::sort(preferredAddresses.begin(), preferredAddresses.end(), [=](std::pair<QString, uint8_t> a, std::pair<QString, uint8_t> b) {
|
||||
return a.second < b.second;
|
||||
});
|
||||
|
||||
// Return highest priority address
|
||||
return preferredAddresses[0].first.toStdString();
|
||||
}
|
||||
|
||||
QString Utils::Platform::GetCommandLineArgument(QString arg)
|
||||
|
Loading…
Reference in New Issue
Block a user