Utils: Add Platform helpers

This commit is contained in:
tt2468 2021-04-28 14:24:05 -07:00
parent af46271b27
commit 1dcf6460f1
4 changed files with 50 additions and 3 deletions

View File

@ -48,7 +48,7 @@ find_package(LibObs REQUIRED)
# Find Qt5
find_package(Qt5 REQUIRED COMPONENTS Core Widgets Concurrent)
find_package(Qt5 REQUIRED COMPONENTS Core Widgets Network Concurrent)
# Find nlohmann
@ -78,7 +78,8 @@ set(obs-websocket_SOURCES
src/requesthandler/rpc/RequestResult.cpp
src/forms/SettingsDialog.cpp
src/utils/Json.cpp
src/utils/Crypto.cpp)
src/utils/Crypto.cpp
src/utils/Platform.cpp)
set(obs-websocket_HEADERS
src/obs-websocket.h
@ -101,6 +102,7 @@ include_directories(
"${LIBOBS_INCLUDE_DIR}/../UI/obs-frontend-api"
${Qt5Core_INCLUDES}
${Qt5Widgets_INCLUDES}
${Qt5Network_INCLUDES}
${Qt5Concurrent_INCLUDES}
"${CMAKE_SOURCE_DIR}/deps/asio/asio/include"
"${CMAKE_SOURCE_DIR}/deps/websocketpp")
@ -109,6 +111,7 @@ target_link_libraries(obs-websocket
libobs
Qt5::Core
Qt5::Widgets
Qt5::Network
Qt5::Concurrent
nlohmann_json::nlohmann_json)

View File

@ -103,7 +103,7 @@ void WebSocketServer::Start()
_serverThread = std::thread(&WebSocketServer::ServerRunner, this);
blog(LOG_INFO, "[Start] Server started successfully on port %d", _serverPort);
blog(LOG_INFO, "[Start] Server started successfully on port %d. Possible connect address: %s", _serverPort, Utils::Platform::GetLocalAddress().c_str());
}
void WebSocketServer::Stop()

40
src/utils/Platform.cpp Normal file
View File

@ -0,0 +1,40 @@
#include <QNetworkInterface>
#include <QHostAddress>
#include "Utils.h"
#include "../plugin-macros.generated.h"
std::string Utils::Platform::GetLocalAddress()
{
std::vector<QString> validAddresses;
for (auto address : QNetworkInterface::allAddresses()) {
// Exclude addresses which won't work
if (address == QHostAddress::LocalHost)
continue;
else if (address == QHostAddress::LocalHostIPv6)
continue;
else if (address.isLoopback())
continue;
else if (address.isLinkLocal())
continue;
else if (address.isNull())
continue;
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 "0.0.0.0";
}

View File

@ -19,4 +19,8 @@ namespace Utils {
std::string GenerateSecret(std::string password, std::string salt);
bool CheckAuthenticationString(std::string secret, std::string challenge, std::string authenticationString);
}
namespace Platform {
std::string GetLocalAddress();
}
}