diff --git a/sdrbase/CMakeLists.txt b/sdrbase/CMakeLists.txt index 3748c3a89..444d585a6 100644 --- a/sdrbase/CMakeLists.txt +++ b/sdrbase/CMakeLists.txt @@ -227,6 +227,7 @@ set(sdrbase_SOURCES util/callsign.cpp util/colormap.cpp util/coordinates.cpp + util/corsproxy.cpp util/countrydat.cpp util/crc.cpp util/CRC64.cpp @@ -482,6 +483,7 @@ set(sdrbase_HEADERS util/callsign.h util/colormap.h util/coordinates.h + util/corsproxy.h util/countrydat.h util/CRC64.h util/csv.h diff --git a/sdrbase/util/corsproxy.cpp b/sdrbase/util/corsproxy.cpp new file mode 100644 index 000000000..ca9961df5 --- /dev/null +++ b/sdrbase/util/corsproxy.cpp @@ -0,0 +1,77 @@ +/////////////////////////////////////////////////////////////////////////////////// +// Copyright (C) 2024 Jon Beniston, M7RCE // +// // +// This program is free software; you can redistribute it and/or modify // +// it under the terms of the GNU General Public License as published by // +// the Free Software Foundation as version 3 of the License, or // +// (at your option) any later version. // +// // +// This program is distributed in the hope that it will be useful, // +// but WITHOUT ANY WARRANTY; without even the implied warranty of // +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // +// GNU General Public License V3 for more details. // +// // +// You should have received a copy of the GNU General Public License // +// along with this program. If not, see . // +/////////////////////////////////////////////////////////////////////////////////// + +#include "corsproxy.h" + +#include + +#ifdef __EMSCRIPTEN__ +#include +#endif + +QString CORSProxy::adjustHost(const QString& url) +{ +#ifdef __EMSCRIPTEN__ + return CORSProxy::adjustHost(QUrl(url)).toString(); +#else + return url; +#endif +} + +// Some servers don't support CORS, so we assume proxy running on same server SDRangel is on +// Look at using https://corsproxy.io/ as alternative - although text only +// E.g: https://corsproxy.io/?https://db.satnogs.org/api/satellites/ +QUrl CORSProxy::adjustHost(const QUrl& url) +{ +#ifdef __EMSCRIPTEN__ + QUrl requestURL(url); + + emscripten::val location = emscripten::val::global("location"); + QString proxyHost = QString::fromStdString(location["hostname"].as()); + + // sdrangel.org doesn't currently support proxying + if (proxyHost == "www.sdrangel.org") { + proxyHost = "sdrangel.beniston.com"; + } + + QString host = requestURL.host(); + static const QStringList hosts = { + "db.satnogs.org", + "sdo.gsfc.nasa.gov", + "www.amsat.org", + "datacenter.stix.i4ds.net", + "user-web.icecube.wisc.edu", + "www.sws.bom.gov.au", + "www.spaceweather.gc.ca", + "airspy.com", + "kiwisdr.com", + "opensky-network.org", + "storage.googleapis.com" + }; + if (hosts.contains(host)) + { + requestURL.setHost(proxyHost); + QString newPath = "/" + host + requestURL.path(); + requestURL.setPath(newPath); + } + + return requestURL; + +#else + return url; +#endif +} diff --git a/sdrbase/util/corsproxy.h b/sdrbase/util/corsproxy.h new file mode 100644 index 000000000..510571bf6 --- /dev/null +++ b/sdrbase/util/corsproxy.h @@ -0,0 +1,35 @@ +/////////////////////////////////////////////////////////////////////////////////// +// Copyright (C) 2024 Jon Beniston, M7RCE // +// // +// This program is free software; you can redistribute it and/or modify // +// it under the terms of the GNU General Public License as published by // +// the Free Software Foundation as version 3 of the License, or // +// (at your option) any later version. // +// // +// This program is distributed in the hope that it will be useful, // +// but WITHOUT ANY WARRANTY; without even the implied warranty of // +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // +// GNU General Public License V3 for more details. // +// // +// You should have received a copy of the GNU General Public License // +// along with this program. If not, see . // +/////////////////////////////////////////////////////////////////////////////////// + +#ifndef INCLUDE_UTIL_CORSPROXY_H +#define INCLUDE_UTIL_CORSPROXY_H + +#include +#include + +#include "export.h" + +class SDRBASE_API CORSProxy +{ +public: + + static QString adjustHost(const QString& url); + static QUrl adjustHost(const QUrl& url); + +}; + +#endif // INCLUDE_UTIL_CORSPROXY_H