From 0efcdf10a41de9adc4ec6311d0a6c11f75a699ff Mon Sep 17 00:00:00 2001 From: John Date: Mon, 8 Jul 2024 07:51:56 +0200 Subject: [PATCH] support cdn redirection --- contrib/updatecheck/calcversions.sh | 6 ++++- contrib/updatecheck/index.php | 2 +- contrib/updatecheck/prepend.inc | 41 ++++++++++++++++------------- src/ebusd/bushandler.cpp | 2 +- src/ebusd/main.cpp | 27 +++++++++++++++++-- src/ebusd/main_args.cpp | 2 +- src/ebusd/mainloop.cpp | 4 +++ src/ebusd/scan.h | 5 ++++ 8 files changed, 64 insertions(+), 25 deletions(-) diff --git a/contrib/updatecheck/calcversions.sh b/contrib/updatecheck/calcversions.sh index 42ffb2cb3..1c160f7d8 100755 --- a/contrib/updatecheck/calcversions.sh +++ b/contrib/updatecheck/calcversions.sh @@ -2,12 +2,13 @@ version=`head -n 1 ../../VERSION` revision=`git describe --always` echo "ebusd=${version},${revision}" > versions.txt -echo "ebusd=${version},${revision}" > oldversions.txt +# cp versions.txt > oldversions.txt devver=`curl -s https://adapter.ebusd.eu/v31/firmware/ChangeLog|grep "Version "|head -n 1|sed -e 's#.*]*>##' -e 's#<.*##' -e 's# ##'` devbl=`curl -s https://adapter.ebusd.eu/v31/firmware/ChangeLog|grep "Bootloader version"|head -n 1|sed -e 's#.*]*>##' -e 's#<.*##' -e 's# ##'` echo "device=${devver},${devbl}" >> versions.txt devver=`curl -s https://adapter.ebusd.eu/v5/ChangeLog|grep "Version "|head -n 1|sed -e 's#.*]*>##' -e 's#<.*##' -e 's# ##'` echo "device=${devver},${devver}" >> versions.txt +cp versions.txt cdnversions.txt files=`find config/de -type f -or -type l` ../../src/lib/ebus/test/test_filereader $files|sed -e 's#^config/de/##' -e 's#^\([^ ]*\) #\1=#' -e 's# #,#g'|sort >> versions.txt files=`find config/en -type f -or -type l` @@ -17,3 +18,6 @@ for filever in $(../../src/lib/ebus/test/test_filereader $files|sed -e 's#^confi sed -i -e "s#^$file=\(.*\)\$#$file=\1,$ver#" versions.txt done #./oldtest_filereader $files|sed -e 's#^config/##' -e 's#^\([^ ]*\) #\1=#' -e 's# #,#g'|sort >> oldversions.txt +curl -sS https://ebus.github.io/en/versions.json -o veren.json +curl -sS https://ebus.github.io/de/versions.json -o verde.json +node -e 'fs=require("fs");e=JSON.parse(fs.readFileSync("veren.json","utf-8"));d=JSON.parse(fs.readFileSync("verde.json","utf-8"));console.log(Object.entries(e).map(([k,v])=>{w=d[k];return `${k}=${v.hash},${v.size},${v.mtime},${w.hash},${w.size},${w.mtime}`;}).join("\n"))' >> cdnversions.txt diff --git a/contrib/updatecheck/index.php b/contrib/updatecheck/index.php index 74a10897e..372f3e625 100644 --- a/contrib/updatecheck/index.php +++ b/contrib/updatecheck/index.php @@ -5,7 +5,7 @@ $r = @file_get_contents('php://input'); header('Content-Type: text/plain'); $r = @json_decode($r, true); - echo checkUpdate(@$r['v'], @$r['r'], @$r['a'], @$r['dv'], @$r['l'], @$r['lc']); + echo checkUpdate(@$r['v'], @$r['r'], @$r['a'], @$r['dv'], @$r['l'], @$r['lc'], @$r['cp']); exit; } readVersions(); diff --git a/contrib/updatecheck/prepend.inc b/contrib/updatecheck/prepend.inc index 16f4792ef..1af94c2f0 100644 --- a/contrib/updatecheck/prepend.inc +++ b/contrib/updatecheck/prepend.inc @@ -1,31 +1,34 @@ 1) { - $key = $val[0]; - $val = explode(',', $val[1]); - if ($key==='device' && count($val)===2 && $val[0]===$val[1]) { - $key = 'devicesame'; - } - $versions[$key] = $val; - } - }; - array_walk($v, $func); + $v = @file_get_contents($prefix.'versions.txt'); + if (!$v) { + return false; } + $v = explode("\n", $v); + $func = function($val, $k) { + global $versions; + $val = explode('=', $val); + if (count($val)>1) { + $key = $val[0]; + $val = explode(',', $val[1]); + if ($key==='device' && count($val)===2 && $val[0]===$val[1]) { + $key = 'devicesame'; + } + $versions[$key] = $val; + } + }; + array_walk($v, $func); + return true; } -function checkUpdate($ebusdVersion, $ebusdRelease, $architecture, $deviceVersion, $loadedFiles, $language) { +function checkUpdate($ebusdVersion, $ebusdRelease, $architecture, $deviceVersion, $loadedFiles, $language, $cdnPath) { if (!$ebusdVersion) { return 'invalid request'; } - readVersions($architecture && substr($architecture, 0, 3)!=='arm' && (((float)$ebusdVersion)<3.3 || ($ebusdVersion==='3.3' && ($ebusdRelease==='v3.3' || (strtok($ebusdRelease, '-')==='v3.3') && strtok('-')<18)))); + $old = $architecture && substr($architecture, 0, 3)!=='arm' && (((float)$ebusdVersion)<3.3 || ($ebusdVersion==='3.3' && ($ebusdRelease==='v3.3' || (strtok($ebusdRelease, '-')==='v3.3') && strtok('-')<18))); + readVersions($cdnPath ? 'cdn' : ($old ? 'old' : '')) || ($cdnPath && readVersions()); global $versions; $ret = 'unknown'; if ($ebusdVersion==$versions['ebusd'][0] && $ebusdRelease==$versions['ebusd'][1]) { diff --git a/src/ebusd/bushandler.cpp b/src/ebusd/bushandler.cpp index b37d05ef7..5f31976e0 100644 --- a/src/ebusd/bushandler.cpp +++ b/src/ebusd/bushandler.cpp @@ -742,7 +742,7 @@ void BusHandler::formatUpdateInfo(ostringstream* output) const { } unsigned char address = 0; for (int index = 0; index < 256; index++, address++) { - bool ownAddress = !m_protocol->isOwnAddress(address); + bool ownAddress = m_protocol->isOwnAddress(address); if (!isValidAddress(address, false) || ((m_seenAddresses[address]&SEEN) == 0 && !ownAddress)) { continue; } diff --git a/src/ebusd/main.cpp b/src/ebusd/main.cpp index bf8b83ee3..8fd380fac 100644 --- a/src/ebusd/main.cpp +++ b/src/ebusd/main.cpp @@ -288,8 +288,10 @@ int main(int argc, char* argv[], char* envp[]) { logWrite(lf_main, ll_error, "invalid configPath URL"); // force logging on exit return EINVAL; } - if (configHost == CONFIG_HOST) { - string suffix = (lang != "en" ? "de" : lang) + "/"; + bool isCdn = configHost == CONFIG_HOST; + string suffix; + if (isCdn) { + suffix = (lang != "en" ? "de" : lang) + "/"; configUriPrefix += suffix; configPath += suffix; // only for informational purposes } else { @@ -306,6 +308,27 @@ int main(int argc, char* argv[], char* envp[]) { cleanup(); return EINVAL; } + if (isCdn) { + // check load balancing redirection + string redirPath; + uint16_t redirPort = 443; + string redirProto, redirHost, redirUriPrefix; + bool repeat = false; + bool json = true; + if (configHttpClient->get("/redirect.json", "", &redirPath, nullptr, nullptr, &json) && !json + && HttpClient::parseUrl(redirPath, &redirProto, &redirHost, &redirPort, &redirUriPrefix) + && redirHost != configHost) { + configHttpClient->disconnect(); + ebusd::HttpClient* redirClient = new HttpClient(); + if (redirClient->connect(redirHost, redirPort, redirProto == "https", PACKAGE_NAME "/" PACKAGE_VERSION)) { + configUriPrefix = redirUriPrefix + suffix; + configPath = redirPath + suffix; // only for informational purposes + delete configHttpClient; + configHttpClient = redirClient; + logNotice(lf_main, "configPath URL redirected to %s", configPath.c_str()); + } + } + } logInfo(lf_main, "configPath URL is valid"); configHttpClient->disconnect(); } diff --git a/src/ebusd/main_args.cpp b/src/ebusd/main_args.cpp index 38f426abe..009724e94 100755 --- a/src/ebusd/main_args.cpp +++ b/src/ebusd/main_args.cpp @@ -157,7 +157,7 @@ static const argDef argDefs[] = { "arguments for checking a particular scan configuration, e.g. \"FF08070400/0AB5454850303003277201\"."}, {"scanretries", O_SCNRET, "COUNT", 0, "Retry scanning devices COUNT times [5]"}, {"configlang", O_CFGLNG, "LANG", 0, - "Prefer LANG in multilingual configuration files [system default language]"}, + "Prefer LANG in multilingual configuration files [system default language, DE as fallback]"}, {"checkconfig", O_CHKCFG, nullptr, 0, "Check config files, then stop"}, {"dumpconfig", O_DMPCFG, "FORMAT", af_optional, "Check and dump config files in FORMAT (\"json\" or \"csv\"), then stop"}, diff --git a/src/ebusd/mainloop.cpp b/src/ebusd/mainloop.cpp index c7ebe7331..2c55af91e 100644 --- a/src/ebusd/mainloop.cpp +++ b/src/ebusd/mainloop.cpp @@ -321,6 +321,10 @@ void MainLoop::run() { << ",\"a\":\"other\"" #endif << ",\"u\":" << (now-start); + const string configPathCDN = m_scanHelper->getConfigPathCDN(); + if (!configPathCDN.empty()) { + ostr << ",\"cp\":\"" << configPathCDN << "\""; + } m_protocol->formatInfoJson(&ostr); if (m_reconnectCount) { ostr << ",\"rc\":" << m_reconnectCount; diff --git a/src/ebusd/scan.h b/src/ebusd/scan.h index 754c1e5fe..1c68866d0 100644 --- a/src/ebusd/scan.h +++ b/src/ebusd/scan.h @@ -71,6 +71,11 @@ class ScanHelper : public Resolver { */ const string getConfigPath() const { return m_configPath; } + /** + * @return the config path when pointing to CDN, empty otherwise. + */ + const string getConfigPathCDN() const { return m_configUriPrefix.empty() ? "" : m_configPath; } + /** * Try to connect to the specified server. * @param host the host name to connect to.