diff --git a/src/tox_ui_utils.cpp b/src/tox_ui_utils.cpp index 7ba15ed..cc82b7e 100644 --- a/src/tox_ui_utils.cpp +++ b/src/tox_ui_utils.cpp @@ -79,6 +79,10 @@ void ToxUIUtils::render(void) { _tc.runBootstrap(); } + if (ImGui::MenuItem("connect node", nullptr, _show_dht_connect_node)) { + _show_dht_connect_node = !_show_dht_connect_node; + } + ImGui::EndMenu(); } @@ -236,5 +240,81 @@ void ToxUIUtils::render(void) { } ImGui::End(); } + + if (_show_dht_connect_node) { + if (ImGui::Begin("Tox connect DHT node", &_show_dht_connect_node)) { + ImGui::BeginDisabled(); + ImGui::TextWrapped( + "Here you can manually connect to a DHT node (or/and tcp-relay) by address and pubkey.\n" + "This is equivalent to what 'DHT Bootstrapping' does, but not with hardcoded nodes.\n" + "Keep in mind that your own DHT pubkey changes everytime you start the program, unlike dedicated bootstrap nodes.\n" + // see https://nodes.tox.chat/ + "If DNS querries where not disabled at launch, domain names can be used too." + ); + ImGui::EndDisabled(); + + static std::string addr; + static uint16_t port {33445}; + static char pubkey[TOX_PUBLIC_KEY_SIZE*2 + 1]; // 1 for null terminator + + if (ImGui::BeginTable("node", 2, ImGuiTableFlags_SizingFixedFit)) { + ImGui::TableSetupColumn(nullptr, ImGuiTableColumnFlags_WidthFixed); + ImGui::TableSetupColumn(nullptr, ImGuiTableColumnFlags_WidthStretch); + + ImGui::TableNextColumn(); + ImGui::TextUnformatted("address"); + ImGui::TableNextColumn(); + ImGui::SetNextItemWidth(-1); + ImGui::InputText("##address", &addr); + + ImGui::TableNextColumn(); + ImGui::TextUnformatted("port"); + ImGui::TableNextColumn(); + ImGui::SetNextItemWidth(-1); + ImGui::InputScalar("##port", ImGuiDataType_U16, &port); + + ImGui::TableNextColumn(); + ImGui::TextUnformatted("pubkey"); + ImGui::TableNextColumn(); + ImGui::SetNextItemWidth(-1); + ImGui::InputText("##pubkey", pubkey, TOX_PUBLIC_KEY_SIZE*2+1); + + // add as + // - udp dht node (default) + // - udp dht node + tcp relay + // - tcp relay + + ImGui::EndTable(); + } + + static std::string last_error; + + bool valid_input = !addr.empty() && port != 0; + if (!valid_input) ImGui::BeginDisabled(); + if (ImGui::Button("connect")) { + std::vector bin_pubkey = hex2bin(std::string_view{pubkey, TOX_PUBLIC_KEY_SIZE*2}); + + last_error.clear(); + + { + Tox_Err_Bootstrap err = _tc.toxBootstrap(addr, port, bin_pubkey); + if (err != Tox_Err_Bootstrap::TOX_ERR_BOOTSTRAP_OK) { + last_error += "add udp node failed with " + std::to_string(err) + "\n"; + } + } + { + Tox_Err_Bootstrap err = _tc.toxAddTcpRelay(addr, port, bin_pubkey); + if (err != Tox_Err_Bootstrap::TOX_ERR_BOOTSTRAP_OK) { + last_error += "add tcp relay failed with " + std::to_string(err) + "\n"; + } + } + } + if (!valid_input) ImGui::EndDisabled(); + if (!last_error.empty()) { + ImGui::TextUnformatted(last_error.c_str()); + } + } + ImGui::End(); + } } diff --git a/src/tox_ui_utils.hpp b/src/tox_ui_utils.hpp index 9dffe80..9c9aca8 100644 --- a/src/tox_ui_utils.hpp +++ b/src/tox_ui_utils.hpp @@ -11,6 +11,7 @@ class ToxUIUtils { bool _show_add_friend_window {false}; bool _show_add_group_window {false}; bool _show_new_group_window {false}; + bool _show_dht_connect_node {false}; ToxClient& _tc; ToxContactModel2& _tcm;