Migrate core infrastructure to VoidLibrary

This commit is contained in:
faumaray committed 2026-06-22 15:53:23 +03:00
1 parent 53801f2b53
commit 2bdb63766d
65 files changed
+16109 -12011

No files matched your search

+109
View File
@@ -0,0 +1,109 @@
#include "echoion/core/NetworkClient.hpp"
#include "echoion/core/commands/RpcInfoCommand.hpp"
#include <ConfigManager.hpp>
#include <format>
#include <future>
#include <regex>
namespace Echoion::Core
{
std::string getPublicIpAddress()
{
// Try to get public IP from external services
static std::string cachedIp;
static std::chrono::steady_clock::time_point lastFetch;
// Cache IP for 5 minutes to avoid excessive API calls
auto now = std::chrono::steady_clock::now();
if (!cachedIp.empty() &&
std::chrono::duration_cast<std::chrono::minutes>(now - lastFetch)
.count() < 5) {
return cachedIp;
}
// List of services to try
const std::vector<std::string> ipServices = {
"https://api.ipify.org", "https://checkip.amazonaws.com",
"https://icanhazip.com", "https://ifconfig.me/ip"};
// Create a temporary network client for IP lookup
auto networkClient = std::make_shared<NetworkClient>();
auto initResult = networkClient->initialize();
if (!initResult) {
return cachedIp.empty() ? "127.0.0.1" : cachedIp;
}
for (const auto& service : ipServices) {
try {
auto result = networkClient->get(service);
if (result && result->isSuccess()) {
// Extract IP from response body (trim whitespace)
std::string ip = result->body;
// Remove whitespace and newlines
ip.erase(std::remove_if(ip.begin(), ip.end(),
[](unsigned char c) {
return std::isspace(c);
}),
ip.end());
// Validate IP format (basic check)
std::regex ipRegex(
R"(^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$)");
if (std::regex_match(ip, ipRegex)) {
cachedIp = ip;
lastFetch = now;
return cachedIp;
}
}
} catch (...) {
// Try next service
continue;
}
}
// Return cached IP or localhost if all services failed
return cachedIp.empty() ? "127.0.0.1" : cachedIp;
}
void RpcInfoCommand::execute(const CommandContext& ctx)
{
// Get RPC configuration from global ConfigManager
const auto& networkConfig = ECHOION_NETWORK;
uint16_t rpcPort = networkConfig.rpcPort;
std::string rpcBindAddress = networkConfig.rpcBindAddress;
// Determine the public IP to show
std::string publicIp = getPublicIpAddress();
// If bind address is not localhost, use it
if (rpcBindAddress != "127.0.0.1" && rpcBindAddress != "localhost") {
publicIp = rpcBindAddress;
}
// Format message using plain text (compatible with TS3 BBCode and TS6
// Markdown)
std::string message = std::format(
"**RPC Server Information**\n\n"
"The RPC server allows remote control via WebSocket API.\n\n"
"**Connection Details:**\n"
"Address: {}:{}\n"
"Protocol: WebSocket (ws://)\n\n"
"**To connect:**\n"
"1. Subscribe to the bot with !subscribe\n"
"2. Connect to ws://{}:{}\n"
"3. Send auth request with your nickname: "
"{{\"nickname\":\"YourName\"}}\n"
"4. Check your TeamSpeak PM for the token\n"
"5. Authenticate with: {{\"token\":\"YOUR_TOKEN\"}}\n\n"
"**Available endpoints:**\n"
"- Playback control (play, pause, skip, volume)\n"
"- Queue management\n"
"- Track information\n"
"- Real-time event updates",
publicIp, rpcPort, publicIp, rpcPort);
ctx.reply(message);
}
} // namespace Echoion::Core