diff --git a/CMakeLists.txt b/CMakeLists.txt index 6aee9485..20f7a879 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/src/WebSocketServer.cpp b/src/WebSocketServer.cpp index 616ce120..393ba84b 100644 --- a/src/WebSocketServer.cpp +++ b/src/WebSocketServer.cpp @@ -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() diff --git a/src/utils/Platform.cpp b/src/utils/Platform.cpp new file mode 100644 index 00000000..9e9dcabe --- /dev/null +++ b/src/utils/Platform.cpp @@ -0,0 +1,40 @@ +#include +#include + +#include "Utils.h" + +#include "../plugin-macros.generated.h" + +std::string Utils::Platform::GetLocalAddress() +{ + std::vector 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"; +} \ No newline at end of file diff --git a/src/utils/Utils.h b/src/utils/Utils.h index 76cf3a09..d7afc14d 100644 --- a/src/utils/Utils.h +++ b/src/utils/Utils.h @@ -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(); + } }