From 4611ac66b0b10019b655d8ad528336916a90cfc9 Mon Sep 17 00:00:00 2001 From: Geoffrey Merck Date: Thu, 12 Sep 2024 21:18:51 +0200 Subject: [PATCH] Download all host files on startup #50 --- Common/Defs.h | 1 + Common/HostsFileDownloader.cpp | 19 ++++++++++++------- Common/HostsFileDownloader.h | 2 +- DStarGateway/DStarGatewayApp.cpp | 16 ++++++++++------ DStarGateway/DStarGatewayConfig.cpp | 7 +++++-- DStarGateway/DStarGatewayConfig.h | 5 ++++- DStarGateway/DStarGatewayThread.cpp | 7 +++---- DStarGateway/DStarGatewayThread.h | 3 +-- DStarGateway/example.cfg | 9 +++++++-- 9 files changed, 44 insertions(+), 25 deletions(-) diff --git a/Common/Defs.h b/Common/Defs.h index 2f12568..9cb2d07 100644 --- a/Common/Defs.h +++ b/Common/Defs.h @@ -25,6 +25,7 @@ const std::string DEXTRA_HOSTS_FILE_NAME("DExtra_Hosts.txt"); const std::string DCS_HOSTS_FILE_NAME("DCS_Hosts.txt"); const std::string DPLUS_HOSTS_FILE_NAME("DPlus_Hosts.txt"); +const std::string XLX_HOSTS_FILE_NAME("XLX_Hosts.txt"); const std::string GATEWAY_HOSTS_FILE_NAME("Gateway_Hosts.txt"); const std::string LINKS_BASE_NAME("Links"); diff --git a/Common/HostsFileDownloader.cpp b/Common/HostsFileDownloader.cpp index 12062c8..74fdb29 100644 --- a/Common/HostsFileDownloader.cpp +++ b/Common/HostsFileDownloader.cpp @@ -18,6 +18,7 @@ #include #include +#include #include #include "HostsFileDownloader.h" @@ -30,21 +31,21 @@ size_t CHostsFileDownloader::write_data(void *ptr, size_t size, size_t nmemb, FI } -std::string CHostsFileDownloader::download(const std::string & xlxHostsFileURL) +bool CHostsFileDownloader::download(const std::string & hostsFileURL, const std::string & hostFilePath) { CURL *curl; FILE *fp; bool ok = false; std::string outFileName; - char outFileNameBuf[] = "/tmp/XLXHostFile_XXXXXX"; + char outFileNameBuf[] = "/tmp/HostFile_XXXXXX"; - CLog::logInfo("Downloading XLX host file from %s", xlxHostsFileURL.c_str()); + CLog::logInfo("Downloading host file from %s", hostsFileURL.c_str()); curl = curl_easy_init(); if (curl) { int filedes = mkstemp(outFileNameBuf); - if(filedes > 0 && (fp = fdopen(filedes,"wb")) != NULL) { - curl_easy_setopt(curl, CURLOPT_URL, xlxHostsFileURL.c_str()); + if(filedes > 0 && (fp = fdopen(filedes,"wb")) != nullptr) { + curl_easy_setopt(curl, CURLOPT_URL, hostsFileURL.c_str()); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp); CURLcode res = curl_easy_perform(curl); @@ -58,9 +59,13 @@ std::string CHostsFileDownloader::download(const std::string & xlxHostsFileURL) if(ok) { outFileName = std::string(outFileNameBuf); + ok = ok && std::filesystem::copy_file(outFileName, hostFilePath, std::filesystem::copy_options::overwrite_existing); + if(!ok) { + CLog::logError("Failed to copy host file to %s", hostFilePath.c_str()); + } } else { - CLog::logError("Failed to download XLx Host file from %s", xlxHostsFileURL.c_str()); + CLog::logWarning("Failed to download Host file from %s, using previous file", hostsFileURL.c_str()); } - return outFileName; + return ok; } diff --git a/Common/HostsFileDownloader.h b/Common/HostsFileDownloader.h index 3f26182..dab6e27 100644 --- a/Common/HostsFileDownloader.h +++ b/Common/HostsFileDownloader.h @@ -23,7 +23,7 @@ class CHostsFileDownloader { public: - static std::string download(const std::string & xlxHostsFileURL); + static bool download(const std::string & hostsFileURL, const std::string & hostFilePath); private: static size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream); }; diff --git a/DStarGateway/DStarGatewayApp.cpp b/DStarGateway/DStarGatewayApp.cpp index 9cf45c5..da8f50e 100644 --- a/DStarGateway/DStarGatewayApp.cpp +++ b/DStarGateway/DStarGatewayApp.cpp @@ -35,13 +35,13 @@ #include "DStarGatewayConfig.h" #include "DStarGatewayApp.h" #include "Version.h" +#include "HostsFileDownloader.h" #include "IRCDDBMultiClient.h" #include "IRCDDBClient.h" #include "Utils.h" #include "Version.h" #include "GitVersion.h" #include "RepeaterProtocolHandlerFactory.h" -#include "HostsFileDownloader.h" #include "Log.h" #include "LogFileTarget.h" #include "LogConsoleTarget.h" @@ -324,26 +324,30 @@ bool CDStarGatewayApp::createThread() // Setup Dextra TDextra dextraConfig; m_config->getDExtra(dextraConfig); - CLog::logInfo("DExtra enabled: %d, max. dongles: %u", int(dextraConfig.enabled), dextraConfig.maxDongles); + CLog::logInfo("DExtra enabled: %d, max. dongles: %u, url: %s", int(dextraConfig.enabled), dextraConfig.maxDongles, dextraConfig.hostfileUrl.c_str()); + if(dextraConfig.enabled) CHostsFileDownloader::download(dextraConfig.hostfileUrl, paths.dataDir + "/" + DEXTRA_HOSTS_FILE_NAME); m_thread->setDExtra(dextraConfig.enabled, dextraConfig.maxDongles); // Setup DCS TDCS dcsConfig; m_config->getDCS(dcsConfig); - CLog::logInfo("DCS enabled: %d", int(dcsConfig.enabled)); + CLog::logInfo("DCS enabled: %d, url: %s", int(dcsConfig.enabled), dcsConfig.hostfileUrl.c_str()); + if(dextraConfig.enabled) CHostsFileDownloader::download(dcsConfig.hostfileUrl, paths.dataDir + "/" + DCS_HOSTS_FILE_NAME); m_thread->setDCS(dcsConfig.enabled); // Setup DPlus TDplus dplusConfig; m_config->getDPlus(dplusConfig); - CLog::logInfo("D-Plus enabled: %d, max. dongles: %u, login: %s", int(dplusConfig.enabled), dplusConfig.maxDongles, dplusConfig.login.c_str()); + CLog::logInfo("D-Plus enabled: %d, max. dongles: %u, login: %s, url: %s", int(dplusConfig.enabled), dplusConfig.maxDongles, dplusConfig.login.c_str(), dplusConfig.hostfileUrl.c_str()); + if(dplusConfig.enabled) CHostsFileDownloader::download(dplusConfig.hostfileUrl, paths.dataDir + "/" + DPLUS_HOSTS_FILE_NAME); m_thread->setDPlus(dplusConfig.enabled, dplusConfig.maxDongles, dplusConfig.login); // Setup XLX TXLX xlxConfig; m_config->getXLX(xlxConfig); - CLog::logInfo("XLX enabled: %d, Hosts file url: %s", int(xlxConfig.enabled), xlxConfig.url.c_str()); - m_thread->setXLX(xlxConfig.enabled, xlxConfig.enabled ? CHostsFileDownloader::download(xlxConfig.url) : ""); + CLog::logInfo("XLX enabled: %d, Hosts file url: %s", int(xlxConfig.enabled), xlxConfig.hostfileUrl.c_str()); + if(xlxConfig.enabled) CHostsFileDownloader::download(xlxConfig.hostfileUrl, paths.dataDir + "/" + XLX_HOSTS_FILE_NAME); + m_thread->setXLX(xlxConfig.enabled); // Setup Remote TRemote remoteConfig; diff --git a/DStarGateway/DStarGatewayConfig.cpp b/DStarGateway/DStarGatewayConfig.cpp index da334be..eade624 100644 --- a/DStarGateway/DStarGatewayConfig.cpp +++ b/DStarGateway/DStarGatewayConfig.cpp @@ -83,9 +83,9 @@ bool CDStarGatewayConfig::loadDaemon(const CConfig & cfg) bool CDStarGatewayConfig::loadXLX(const CConfig & cfg) { bool ret = cfg.getValue("xlx", "enabled", m_xlx.enabled, true); - ret = cfg.getValue("xlx", "hostfileUrl", m_xlx.url, 0, 1024, "") && ret; + ret = cfg.getValue("xlx", "hostfileUrl", m_xlx.hostfileUrl, 0, 1024, "") && ret; - m_xlx.enabled = m_xlx.enabled && !m_xlx.url.empty(); + m_xlx.enabled = m_xlx.enabled; return ret; } @@ -105,6 +105,7 @@ bool CDStarGatewayConfig::loadDextra(const CConfig & cfg) { bool ret = cfg.getValue("dextra", "enabled", m_dextra.enabled, true); ret = cfg.getValue("dextra", "maxDongles", m_dextra.maxDongles, 1U, 5U, 5U) && ret; + ret = cfg.getValue("dextra", "hostfileUrl", m_dextra.hostfileUrl, 0, 1024, "") && ret; return ret; } @@ -113,6 +114,7 @@ bool CDStarGatewayConfig::loadDPlus(const CConfig & cfg) bool ret = cfg.getValue("dplus", "enabled", m_dplus.enabled, true); ret = cfg.getValue("dplus", "maxDongles", m_dplus.maxDongles, 1U, 5U, 5U) && ret; ret = cfg.getValue("dplus", "login", m_dplus.login, 0, LONG_CALLSIGN_LENGTH, m_gateway.callsign) && ret; + ret = cfg.getValue("dplus", "hostfileUrl", m_dplus.hostfileUrl, 0, 1024, "") && ret; m_dplus.enabled = m_dplus.enabled && !m_dplus.login.empty(); m_dplus.login = CUtils::ToUpper(m_dplus.login); @@ -123,6 +125,7 @@ bool CDStarGatewayConfig::loadDPlus(const CConfig & cfg) bool CDStarGatewayConfig::loadDCS(const CConfig & cfg) { bool ret = cfg.getValue("dcs", "enabled", m_dcs.enabled, true); + ret = cfg.getValue("dcs", "hostfileUrl", m_dcs.hostfileUrl, 0, 1024, "") && ret; return ret; } diff --git a/DStarGateway/DStarGatewayConfig.h b/DStarGateway/DStarGatewayConfig.h index 1a15d65..016df97 100644 --- a/DStarGateway/DStarGatewayConfig.h +++ b/DStarGateway/DStarGatewayConfig.h @@ -102,16 +102,19 @@ typedef struct { typedef struct { bool enabled; unsigned int maxDongles; + std::string hostfileUrl; } TDextra; typedef struct { bool enabled; std::string login; unsigned int maxDongles; + std::string hostfileUrl; } TDplus; typedef struct { bool enabled; + std::string hostfileUrl; } TDCS; typedef struct { @@ -120,7 +123,7 @@ typedef struct { typedef struct { bool enabled; - std::string url; + std::string hostfileUrl; } TXLX; typedef struct { diff --git a/DStarGateway/DStarGatewayThread.cpp b/DStarGateway/DStarGatewayThread.cpp index 3f4c218..ac8cbe8 100644 --- a/DStarGateway/DStarGatewayThread.cpp +++ b/DStarGateway/DStarGatewayThread.cpp @@ -88,7 +88,6 @@ m_dplusMaxDongles(0U), m_dplusLogin(), m_dcsEnabled(true), m_xlxEnabled(true), -m_xlxHostsFileName(), m_ccsEnabled(true), m_ccsHost(), m_infoEnabled(true), @@ -599,10 +598,9 @@ void CDStarGatewayThread::setDCS(bool enabled) m_dcsEnabled = enabled; } -void CDStarGatewayThread::setXLX(bool enabled, const std::string& xlxHostsFileName) +void CDStarGatewayThread::setXLX(bool enabled) { m_xlxEnabled = enabled; - m_xlxHostsFileName = xlxHostsFileName; } #ifdef USE_CCS @@ -1150,7 +1148,8 @@ void CDStarGatewayThread::loadGateways() void CDStarGatewayThread::loadAllReflectors() { if (m_xlxEnabled) { - loadReflectors(m_xlxHostsFileName, DP_DCS); + std::string fileName = m_dataDir + "/" + DPLUS_HOSTS_FILE_NAME; + loadReflectors(fileName, DP_DCS); } if (m_dplusEnabled) { diff --git a/DStarGateway/DStarGatewayThread.h b/DStarGateway/DStarGatewayThread.h index 26a799b..20e6664 100644 --- a/DStarGateway/DStarGatewayThread.h +++ b/DStarGateway/DStarGatewayThread.h @@ -61,7 +61,7 @@ public: virtual void setDExtra(bool enabled, unsigned int maxDongles); virtual void setDPlus(bool enabled, unsigned int maxDongles, const std::string& login); virtual void setDCS(bool enabled); - virtual void setXLX(bool enabled, const std::string& fileName); + virtual void setXLX(bool enabled); #ifdef USE_CCS virtual void setCCS(bool enabled, const std::string& host); #endif @@ -112,7 +112,6 @@ private: std::string m_dplusLogin; bool m_dcsEnabled; bool m_xlxEnabled; - std::string m_xlxHostsFileName; bool m_ccsEnabled; std::string m_ccsHost; bool m_infoEnabled; diff --git a/DStarGateway/example.cfg b/DStarGateway/example.cfg index 73d7b09..23ae692 100644 --- a/DStarGateway/example.cfg +++ b/DStarGateway/example.cfg @@ -155,23 +155,28 @@ displayLevel= # defaults to info, valid values are trace, debug, info, warning repeatThreshold=#defaults to 2, valid values are disbaled and 1 to 10. Prevents flooding of logs from repeated log messages. [Paths] -data=/usr/local/share/dstargateway.d/ #Path where the data (hostfiles, audio files etc) can be found +data=/usr/local/share/dstargateway.d/ # Path where the data (auto downbloaded hostfiles, audio files etc) can be found +customHostfiles=/usr/local/share/dstargateway.d/hostfiles.d/ # Place your custom host files in this directory, this dir must be different from datadir. + # Any hosts found here will override same host in downloaded files [DExtra] enabled=true # There is no reason to disable this maxDongles=5 +hostfileUrl=http://www.pistar.uk/downloads/DExtra_Hosts.txt # URL where to download the host file on startup and every X minutes. Leave empty to disable. [DPlus] enabled=true # There is no reason to disable this maxDongles=5 login= # defaults to gateway callsign +hostfileUrl=http://www.pistar.uk/downloads/DPlus_Hosts.txt # URL where to download the host file on startup and every X minutes. Leave empty to disable. [DCS] enabled=true # There is no reason to disable this +hostfileUrl=http://www.pistar.uk/downloads/DCS_Hosts.txt # URL where to download the host file on startup and every X minutes. Leave empty to disable. [XLX] enabled=true -hostfileUrl=http://xlxapi.rlx.lu/api.php?do=GetXLXDMRMaster +hostfileUrl=http://xlxapi.rlx.lu/api.php?do=GetXLXDMRMaster # URL where to download the host file on startup and every X minutes. Leave empty to disable. [DRats] enabled=false # Defaults to false. The program need to be compiled with DRats support for DRats to be actually enabled