mirror of
https://github.com/kunkundi/crossdesk.git
synced 2026-11-07 06:39:10 +08:00
[refactor] use MiniRTC public APIs across clients
This commit is contained in:
@@ -10,7 +10,7 @@
|
||||
#include "SimpleIni.h"
|
||||
#include "autostart.h"
|
||||
#include "rd_log.h"
|
||||
#include "signal_server_address.h"
|
||||
#include <minirtc.h>
|
||||
|
||||
namespace crossdesk {
|
||||
|
||||
@@ -349,22 +349,24 @@ int ConfigCenter::SetForceRelay(bool force_relay) {
|
||||
}
|
||||
|
||||
int ConfigCenter::SetServerHost(const std::string& host) {
|
||||
const auto address = minirtc::ParseSignalServerAddress(host);
|
||||
if (!address) return -1;
|
||||
if (address->port) return SetServerEndpoint(host, *address->port);
|
||||
if (StoreValues({{"signal_server_host", address->Address()}}) != 0) return -1;
|
||||
signal_server_host_ = address->Address();
|
||||
MiniRtcSignalServerAddress address{};
|
||||
if (ParseMiniRtcSignalServerAddress(host.data(), host.size(), &address) != 0) return -1;
|
||||
if (address.port) return SetServerEndpoint(host, address.port);
|
||||
const auto normalized = std::string(address.host) + address.target;
|
||||
if (StoreValues({{"signal_server_host", normalized}}) != 0) return -1;
|
||||
signal_server_host_ = normalized;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ConfigCenter::SetServerEndpoint(const std::string& host, int port) {
|
||||
const auto address = minirtc::ParseSignalServerAddress(host);
|
||||
if (!address) return -1;
|
||||
port = address->port.value_or(port);
|
||||
MiniRtcSignalServerAddress address{};
|
||||
if (ParseMiniRtcSignalServerAddress(host.data(), host.size(), &address) != 0) return -1;
|
||||
if (address.port) port = address.port;
|
||||
if (port < 1 || port > 65535) return -1;
|
||||
if (StoreValues({{"signal_server_host", address->Address()},
|
||||
const auto normalized = std::string(address.host) + address.target;
|
||||
if (StoreValues({{"signal_server_host", normalized},
|
||||
{"signal_server_port", std::to_string(port)}}) != 0) return -1;
|
||||
signal_server_host_ = address->Address();
|
||||
signal_server_host_ = normalized;
|
||||
signal_server_port_ = port;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -87,7 +87,7 @@
|
||||
#include "rd_log.h"
|
||||
#include "server_window_state.h"
|
||||
#include "signal_server_settings.h"
|
||||
#include "signal_server_address.h"
|
||||
#include <minirtc.h>
|
||||
#include "ui/announcement_text.h"
|
||||
#include "ui/model_update.h"
|
||||
#include "ui/release_notes_text.h"
|
||||
@@ -3532,11 +3532,11 @@ void GuiApplication::SyncConnectionDialog() {
|
||||
: auth.incorrect_password
|
||||
? localization::reinput_password[localization_language_index_]
|
||||
: localization::input_password[localization_language_index_];
|
||||
} else if (auth.error == minirtc::JoinFailureKind::Throttled) {
|
||||
} else if (auth.error == MiniRtcJoinFailureKind::Throttled) {
|
||||
text = localization::connection_auth_throttled[localization_language_index_];
|
||||
} else if (auth.error == minirtc::JoinFailureKind::Busy) {
|
||||
} else if (auth.error == MiniRtcJoinFailureKind::Busy) {
|
||||
text = localization::connection_service_busy[localization_language_index_];
|
||||
} else if (auth.error == minirtc::JoinFailureKind::NotAuthenticated) {
|
||||
} else if (auth.error == MiniRtcJoinFailureKind::NotAuthenticated) {
|
||||
text = localization::connection_not_authenticated[localization_language_index_];
|
||||
} else {
|
||||
text = props->lan_session_ && !props->lan_session_->error.empty()
|
||||
@@ -4782,20 +4782,20 @@ void GuiApplication::SyncFileTransferNotifications() {
|
||||
|
||||
bool GuiApplication::ValidateSelfHostedSettingsFromUi(bool gallery_window) {
|
||||
auto& main = ui_->Window(gallery_window);
|
||||
const auto address = minirtc::ParseSignalServerAddress(
|
||||
std::string(main->get_server_host()));
|
||||
if (!address) {
|
||||
const auto input = std::string(main->get_server_host());
|
||||
MiniRtcSignalServerAddress address{};
|
||||
if (ParseMiniRtcSignalServerAddress(input.data(), input.size(), &address) != 0) {
|
||||
main->set_self_host_settings_error(UiText(
|
||||
localization::self_hosted_address_invalid[localization_language_index_]));
|
||||
return false;
|
||||
}
|
||||
const auto port = address->port ? address->port : ParsePort(main->get_server_port());
|
||||
const auto port = address.port ? std::optional<int>(address.port) : ParsePort(main->get_server_port());
|
||||
if (!port) {
|
||||
main->set_self_host_settings_error(UiText(
|
||||
localization::self_hosted_port_invalid[localization_language_index_]));
|
||||
return false;
|
||||
}
|
||||
main->set_server_host(UiText(address->Address()));
|
||||
main->set_server_host(UiText(std::string(address.host) + address.target));
|
||||
main->set_server_port(UiText(std::to_string(*port)));
|
||||
main->set_self_host_settings_error("");
|
||||
return true;
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
#include <set>
|
||||
#include <system_error>
|
||||
|
||||
#include "signal_server_address.h"
|
||||
#include <minirtc.h>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
@@ -53,12 +53,13 @@ void AnnouncementInbox::Configure(std::string host, int port,
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
if (directory_.empty() || host.empty() || port <= 0 || device_id.empty())
|
||||
return;
|
||||
auto address = minirtc::ParseSignalServerAddress(host);
|
||||
if (!address) return;
|
||||
for (auto& ch : address->host)
|
||||
MiniRtcSignalServerAddress address{};
|
||||
if (ParseMiniRtcSignalServerAddress(host.data(), host.size(), &address) != 0) return;
|
||||
std::string normalized_host(address.host);
|
||||
for (auto& ch : normalized_host)
|
||||
if (ch >= 'A' && ch <= 'Z') ch += 'a' - 'A';
|
||||
if (address->host.back() == '.') address->host.pop_back();
|
||||
host = address->Address();
|
||||
if (normalized_host.back() == '.') normalized_host.pop_back();
|
||||
host = normalized_host + address.target;
|
||||
const auto scope = json::array({host, port, device_id}).dump();
|
||||
if (scope_ == scope) return;
|
||||
scope_ = scope;
|
||||
|
||||
@@ -5,7 +5,8 @@
|
||||
#include <mutex>
|
||||
#include <string_view>
|
||||
|
||||
#include "join_failure.h"
|
||||
#include <minirtc.h>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
namespace crossdesk {
|
||||
|
||||
@@ -18,7 +19,7 @@ class ConnectionAuthState {
|
||||
bool password_required = false;
|
||||
bool validating = false;
|
||||
bool incorrect_password = false;
|
||||
minirtc::JoinFailureKind error = minirtc::JoinFailureKind::Unknown;
|
||||
MiniRtcJoinFailureKind error = MiniRtcJoinFailureKind::Unknown;
|
||||
int retry_after = 0;
|
||||
};
|
||||
|
||||
@@ -34,15 +35,16 @@ class ConnectionAuthState {
|
||||
|
||||
void Rejected(const nlohmann::json& reply,
|
||||
Clock::time_point now = Clock::now()) {
|
||||
const auto failure = minirtc::ParseJoinFailure(reply);
|
||||
const auto message = reply.dump();
|
||||
const auto failure = ParseMiniRtcJoinFailure(message.data(), message.size());
|
||||
std::lock_guard lock(mutex_);
|
||||
state_ = {};
|
||||
state_.error = failure.kind;
|
||||
state_.incorrect_password =
|
||||
failure.kind == minirtc::JoinFailureKind::AuthenticationFailed;
|
||||
failure.kind == MiniRtcJoinFailureKind::AuthenticationFailed;
|
||||
state_.password_required =
|
||||
state_.incorrect_password ||
|
||||
failure.kind == minirtc::JoinFailureKind::PasswordRequired;
|
||||
failure.kind == MiniRtcJoinFailureKind::PasswordRequired;
|
||||
retry_at_ = now + std::chrono::seconds(failure.retry_after);
|
||||
}
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
#include <stdexcept>
|
||||
#include <string_view>
|
||||
|
||||
#include "direct_address.h"
|
||||
#include <minirtc.h>
|
||||
#include "lan_password.h"
|
||||
#ifdef _WIN32
|
||||
// Winsock must precede windows.h; the networking headers need its types.
|
||||
@@ -212,12 +212,14 @@ unsigned ParsePort(const std::string& value) {
|
||||
return port;
|
||||
}
|
||||
bool IsIpAddress(const std::string& value) {
|
||||
return minirtc::ParseDirectAddress(value).has_value();
|
||||
char normalized[129]{};
|
||||
return NormalizeMiniRtcDirectAddress(value.data(), value.size(), normalized, sizeof(normalized)) == 0;
|
||||
}
|
||||
std::string NormalizeAddress(const std::string& value) {
|
||||
const auto address = minirtc::ParseDirectAddress(value);
|
||||
Require(address.has_value(), "Invalid LAN endpoint");
|
||||
return minirtc::DirectAddressString(*address);
|
||||
char normalized[129]{};
|
||||
Require(NormalizeMiniRtcDirectAddress(value.data(), value.size(), normalized, sizeof(normalized)) == 0,
|
||||
"Invalid LAN endpoint");
|
||||
return normalized;
|
||||
}
|
||||
std::string FormatEndpoint(const std::string& address, unsigned port) {
|
||||
return (address.find(':') == std::string::npos ? address
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
#include <websocketpp/config/asio_client.hpp>
|
||||
#include <websocketpp/server.hpp>
|
||||
|
||||
#include "direct_address.h"
|
||||
#include <minirtc.h>
|
||||
|
||||
namespace crossdesk::lan {
|
||||
namespace {
|
||||
@@ -147,7 +147,11 @@ bool SupportsDualStack() {
|
||||
std::string SocketAddress(asio::ip::address address) {
|
||||
if (address.is_v6() && address.to_v6().is_v4_mapped())
|
||||
address = asio::ip::make_address_v4(asio::ip::v4_mapped, address.to_v6());
|
||||
return minirtc::DirectAddressString(address);
|
||||
if (address.is_v4()) return address.to_string();
|
||||
auto ipv6 = address.to_v6();
|
||||
const auto scope = ipv6.scope_id();
|
||||
ipv6.scope_id(0);
|
||||
return ipv6.to_string() + (scope ? "%" + std::to_string(scope) : "");
|
||||
}
|
||||
std::shared_ptr<Context> TlsContext() {
|
||||
auto context = std::make_shared<Context>(Context::tls);
|
||||
@@ -323,7 +327,8 @@ struct Transport::Impl {
|
||||
c->local_address = SocketAddress(local.address());
|
||||
c->remote_address = SocketAddress(remote.address());
|
||||
const auto interfaces = LocalIpInterfaces();
|
||||
if (!minirtc::IsDirectAddressPair(c->local_address, c->remote_address) ||
|
||||
if (IsMiniRtcDirectAddressPair(c->local_address.data(), c->local_address.size(),
|
||||
c->remote_address.data(), c->remote_address.size()) != 1 ||
|
||||
std::none_of(
|
||||
interfaces.begin(), interfaces.end(),
|
||||
[&](const auto& i) { return i.address == c->local_address; })) {
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
#include <optional>
|
||||
|
||||
#include "config_center.h"
|
||||
#include "signal_server_address.h"
|
||||
#include <minirtc.h>
|
||||
#include "signal_server_settings.h"
|
||||
|
||||
namespace crossdesk {
|
||||
@@ -88,7 +88,8 @@ SettingResult ApplyConsoleSetting(ConfigCenter& config, const std::string& key,
|
||||
canonical = value;
|
||||
save = [&config, mode] { return config.SetTurnMode(static_cast<ConfigCenter::TURN_MODE>(mode)); };
|
||||
} else if (key == "server_host") {
|
||||
if (!minirtc::ParseSignalServerAddress(input)) return result;
|
||||
MiniRtcSignalServerAddress address{};
|
||||
if (ParseMiniRtcSignalServerAddress(input.data(), input.size(), &address) != 0) return result;
|
||||
save = [&] { return config.SetServerHost(input); };
|
||||
} else if (key == "server_port") {
|
||||
int port = 0;
|
||||
|
||||
Reference in New Issue
Block a user