1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2025-09-02 13:17:48 -04:00

Add CORS proxy for Emscripten.

This commit is contained in:
srcejon 2025-06-04 20:24:58 +01:00
parent aa28bc700d
commit 6a9bba0656
3 changed files with 114 additions and 0 deletions

View File

@ -227,6 +227,7 @@ set(sdrbase_SOURCES
util/callsign.cpp util/callsign.cpp
util/colormap.cpp util/colormap.cpp
util/coordinates.cpp util/coordinates.cpp
util/corsproxy.cpp
util/countrydat.cpp util/countrydat.cpp
util/crc.cpp util/crc.cpp
util/CRC64.cpp util/CRC64.cpp
@ -482,6 +483,7 @@ set(sdrbase_HEADERS
util/callsign.h util/callsign.h
util/colormap.h util/colormap.h
util/coordinates.h util/coordinates.h
util/corsproxy.h
util/countrydat.h util/countrydat.h
util/CRC64.h util/CRC64.h
util/csv.h util/csv.h

View File

@ -0,0 +1,77 @@
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2024 Jon Beniston, M7RCE <jon@beniston.com> //
// //
// 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 <http://www.gnu.org/licenses/>. //
///////////////////////////////////////////////////////////////////////////////////
#include "corsproxy.h"
#include <QDebug>
#ifdef __EMSCRIPTEN__
#include <emscripten/val.h>
#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<std::string>());
// 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
}

35
sdrbase/util/corsproxy.h Normal file
View File

@ -0,0 +1,35 @@
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2024 Jon Beniston, M7RCE <jon@beniston.com> //
// //
// 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 <http://www.gnu.org/licenses/>. //
///////////////////////////////////////////////////////////////////////////////////
#ifndef INCLUDE_UTIL_CORSPROXY_H
#define INCLUDE_UTIL_CORSPROXY_H
#include <QString>
#include <QUrl>
#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