mirror of
https://github.com/RetroShare/RetroShare.git
synced 2025-06-09 07:02:41 -04:00
Make JSON API server bind address configurable
Enforce it being 127.0.0.1 by default, I assumed 127.0.0.1 was restbed default, but as reported by sehraf is not alwayd the case JSON API bind address now is also configurable via commandline on retroshare-android-service and retroshare-nogui, while it obey the configuration of webui (execept for that port is incremented by 2) in retroshare-gui
This commit is contained in:
parent
4acbf775a5
commit
a194e4cf56
5 changed files with 76 additions and 24 deletions
|
@ -58,8 +58,9 @@ static bool checkRsServicePtrReady(
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonApiServer::JsonApiServer(
|
JsonApiServer::JsonApiServer(
|
||||||
uint16_t port, const std::function<void(int)> shutdownCallback) :
|
uint16_t port, const std::string& bindAddress,
|
||||||
mPort(port), mShutdownCallback(shutdownCallback)
|
const std::function<void(int)> shutdownCallback ) :
|
||||||
|
mPort(port), mBindAddress(bindAddress), mShutdownCallback(shutdownCallback)
|
||||||
{
|
{
|
||||||
registerHandler("/jsonApiServer/shutdown",
|
registerHandler("/jsonApiServer/shutdown",
|
||||||
[this](const std::shared_ptr<rb::Session> session)
|
[this](const std::shared_ptr<rb::Session> session)
|
||||||
|
@ -153,7 +154,7 @@ void JsonApiServer::run()
|
||||||
{
|
{
|
||||||
std::shared_ptr<rb::Settings> settings(new rb::Settings);
|
std::shared_ptr<rb::Settings> settings(new rb::Settings);
|
||||||
settings->set_port(mPort);
|
settings->set_port(mPort);
|
||||||
// settings->set_default_header("Connection", "close");
|
settings->set_bind_address(mBindAddress);
|
||||||
settings->set_default_header("Cache-Control", "no-cache");
|
settings->set_default_header("Cache-Control", "no-cache");
|
||||||
mService.start(settings);
|
mService.start(settings);
|
||||||
}
|
}
|
||||||
|
|
|
@ -35,7 +35,8 @@ namespace rb = restbed;
|
||||||
struct JsonApiServer : RsSingleJobThread
|
struct JsonApiServer : RsSingleJobThread
|
||||||
{
|
{
|
||||||
JsonApiServer(
|
JsonApiServer(
|
||||||
uint16_t port,
|
uint16_t port = 9092,
|
||||||
|
const std::string& bindAddress = "127.0.0.1",
|
||||||
const std::function<void(int)> shutdownCallback = [](int){} );
|
const std::function<void(int)> shutdownCallback = [](int){} );
|
||||||
|
|
||||||
/// @see RsSingleJobThread
|
/// @see RsSingleJobThread
|
||||||
|
@ -66,7 +67,8 @@ struct JsonApiServer : RsSingleJobThread
|
||||||
void shutdown(int exitCode = 0);
|
void shutdown(int exitCode = 0);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
uint16_t mPort;
|
const uint16_t mPort;
|
||||||
|
const std::string mBindAddress;
|
||||||
rb::Service mService;
|
rb::Service mService;
|
||||||
const std::function<void(int)> mShutdownCallback;
|
const std::function<void(int)> mShutdownCallback;
|
||||||
};
|
};
|
||||||
|
|
|
@ -39,8 +39,10 @@
|
||||||
#endif // ifdef LIBRESAPI_LOCAL_SERVER
|
#endif // ifdef LIBRESAPI_LOCAL_SERVER
|
||||||
|
|
||||||
#ifdef RS_JSONAPI
|
#ifdef RS_JSONAPI
|
||||||
# include <cstdint>
|
|
||||||
# include "jsonapi/jsonapi.h"
|
# include "jsonapi/jsonapi.h"
|
||||||
|
# include "util/rsnet.h"
|
||||||
|
|
||||||
|
# include <cstdint>
|
||||||
# include <QCommandLineParser>
|
# include <QCommandLineParser>
|
||||||
# include <QString>
|
# include <QString>
|
||||||
# include <iostream>
|
# include <iostream>
|
||||||
|
@ -96,29 +98,65 @@ int main(int argc, char *argv[])
|
||||||
|
|
||||||
#ifdef RS_JSONAPI
|
#ifdef RS_JSONAPI
|
||||||
uint16_t jsonApiPort = 9092;
|
uint16_t jsonApiPort = 9092;
|
||||||
|
std::string jsonApiBindAddress = "127.0.0.1";
|
||||||
|
|
||||||
{
|
{
|
||||||
QCommandLineOption jsonApiPortOpt(
|
QCommandLineOption jsonApiPortOpt(
|
||||||
"jsonApiPort", "JSON API listening port.", "port", "9092");
|
"jsonApiPort", "JSON API listening port.", "port", "9092");
|
||||||
|
QCommandLineOption jsonApiBindAddressOpt(
|
||||||
|
"jsonApiBindAddress", "JSON API Bind Address.",
|
||||||
|
"IP Address", "127.0.0.1");
|
||||||
|
|
||||||
QCommandLineParser cmdParser;
|
QCommandLineParser cmdParser;
|
||||||
cmdParser.addHelpOption();
|
cmdParser.addHelpOption();
|
||||||
cmdParser.addOption(jsonApiPortOpt);
|
cmdParser.addOption(jsonApiPortOpt);
|
||||||
|
cmdParser.addOption(jsonApiBindAddressOpt);
|
||||||
|
|
||||||
cmdParser.parse(app.arguments());
|
cmdParser.parse(app.arguments());
|
||||||
QString jsonApiPortStr = cmdParser.value(jsonApiPortOpt);
|
|
||||||
bool portOk;
|
if(cmdParser.isSet(jsonApiPortOpt))
|
||||||
jsonApiPort = jsonApiPortStr.toUShort(&portOk);
|
|
||||||
if(!portOk)
|
|
||||||
{
|
{
|
||||||
std::cerr << "ERROR: jsonApiPort option value must be a valid TCP "
|
QString jsonApiPortStr = cmdParser.value(jsonApiPortOpt);
|
||||||
<< "port!" << std::endl;
|
bool portOk;
|
||||||
cmdParser.showHelp();
|
jsonApiPort = jsonApiPortStr.toUShort(&portOk);
|
||||||
QCoreApplication::exit(EINVAL);
|
if(!portOk)
|
||||||
|
{
|
||||||
|
std::cerr << "ERROR: jsonApiPort option value must be a valid "
|
||||||
|
<< "TCP port!" << std::endl;
|
||||||
|
cmdParser.showHelp();
|
||||||
|
QCoreApplication::exit(EINVAL);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(cmdParser.isSet(jsonApiBindAddressOpt))
|
||||||
|
{
|
||||||
|
sockaddr_storage tmp;
|
||||||
|
jsonApiBindAddress =
|
||||||
|
cmdParser.value(jsonApiBindAddressOpt).toStdString();
|
||||||
|
if(!sockaddr_storage_inet_pton(tmp, jsonApiBindAddress))
|
||||||
|
{
|
||||||
|
std::cerr << "ERROR: jsonApiBindAddress option value must "
|
||||||
|
<< "be a valid IP address!" << std::endl;
|
||||||
|
cmdParser.showHelp();
|
||||||
|
QCoreApplication::exit(EINVAL);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonApiServer jas(jsonApiPort, [](int ec) { QCoreApplication::exit(ec); });
|
JsonApiServer jas( jsonApiPort, jsonApiBindAddress,
|
||||||
|
[](int ec) { QCoreApplication::exit(ec); } );
|
||||||
jas.start();
|
jas.start();
|
||||||
std::cerr << "JSON API listening on port " << jsonApiPort << std::endl;
|
|
||||||
|
{
|
||||||
|
sockaddr_storage tmp;
|
||||||
|
sockaddr_storage_inet_pton(tmp, jsonApiBindAddress);
|
||||||
|
sockaddr_storage_setport(tmp, jsonApiPort);
|
||||||
|
sockaddr_storage_ipv6_to_ipv4(tmp);
|
||||||
|
|
||||||
|
std::cerr << "JSON API listening on "
|
||||||
|
<< sockaddr_storage_tostring(tmp)
|
||||||
|
<< std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
#endif // ifdef RS_JSONAPI
|
#endif // ifdef RS_JSONAPI
|
||||||
|
|
||||||
|
|
|
@ -22,9 +22,12 @@ resource_api::ApiServerMHD* WebuiPage::apiServerMHD = 0;
|
||||||
resource_api::ApiServerLocal* WebuiPage::apiServerLocal = 0;
|
resource_api::ApiServerLocal* WebuiPage::apiServerLocal = 0;
|
||||||
#endif
|
#endif
|
||||||
resource_api::RsControlModule* WebuiPage::controlModule = 0;
|
resource_api::RsControlModule* WebuiPage::controlModule = 0;
|
||||||
|
|
||||||
#ifdef RS_JSONAPI
|
#ifdef RS_JSONAPI
|
||||||
|
# include <csignal>
|
||||||
|
|
||||||
JsonApiServer* WebuiPage::jsonApiServer = nullptr;
|
JsonApiServer* WebuiPage::jsonApiServer = nullptr;
|
||||||
#endif
|
#endif // ifdef RS_JSONAPI
|
||||||
|
|
||||||
WebuiPage::WebuiPage(QWidget */*parent*/, Qt::WindowFlags /*flags*/)
|
WebuiPage::WebuiPage(QWidget */*parent*/, Qt::WindowFlags /*flags*/)
|
||||||
{
|
{
|
||||||
|
@ -109,11 +112,16 @@ QString WebuiPage::helpText() const
|
||||||
#ifdef LIBRESAPI_LOCAL_SERVER
|
#ifdef LIBRESAPI_LOCAL_SERVER
|
||||||
apiServerLocal = new resource_api::ApiServerLocal(apiServer, resource_api::ApiServerLocal::serverPath());
|
apiServerLocal = new resource_api::ApiServerLocal(apiServer, resource_api::ApiServerLocal::serverPath());
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef RS_JSONAPI
|
#ifdef RS_JSONAPI
|
||||||
// Use same port of libresapi + 2
|
// Use same port of libresapi + 2
|
||||||
jsonApiServer = new JsonApiServer(Settings->getWebinterfacePort() + 2);
|
jsonApiServer = new JsonApiServer(
|
||||||
|
Settings->getWebinterfacePort() + 2,
|
||||||
|
Settings->getWebinterfaceAllowAllIps() ? "::" : "127.0.0.1",
|
||||||
|
[](int /*ec*/) { std::raise(SIGTERM); } );
|
||||||
jsonApiServer->start("WebuiPage::jsonApiServer");
|
jsonApiServer->start("WebuiPage::jsonApiServer");
|
||||||
#endif
|
#endif // ifdef RS_JSONAPI
|
||||||
|
|
||||||
return ok;
|
return ok;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -68,12 +68,16 @@ int main(int argc, char **argv)
|
||||||
#ifdef RS_JSONAPI
|
#ifdef RS_JSONAPI
|
||||||
JsonApiServer* jsonApiServer = nullptr;
|
JsonApiServer* jsonApiServer = nullptr;
|
||||||
uint16_t jsonApiPort = 0;
|
uint16_t jsonApiPort = 0;
|
||||||
|
std::string jsonApiBindAddress = "127.0.0.1";
|
||||||
|
|
||||||
{
|
{
|
||||||
argstream jsonApiArgs(argc, argv);
|
argstream jsonApiArgs(argc, argv);
|
||||||
jsonApiArgs >> parameter(
|
jsonApiArgs >> parameter(
|
||||||
"jsonApiPort", jsonApiPort, "jsonApiPort",
|
"jsonApiPort", jsonApiPort, "jsonApiPort",
|
||||||
"Enable JSON API on the specified port", false );
|
"Enable JSON API on the specified port", false );
|
||||||
|
jsonApiArgs >> parameter(
|
||||||
|
"jsonApiBindAddress", jsonApiBindAddress,
|
||||||
|
"jsonApiBindAddress", "JSON API Bind Address.", false);
|
||||||
jsonApiArgs >> help('h', "help", "Display this Help");
|
jsonApiArgs >> help('h', "help", "Display this Help");
|
||||||
|
|
||||||
if (jsonApiArgs.helpRequested())
|
if (jsonApiArgs.helpRequested())
|
||||||
|
@ -82,10 +86,9 @@ int main(int argc, char **argv)
|
||||||
|
|
||||||
if(jsonApiPort)
|
if(jsonApiPort)
|
||||||
{
|
{
|
||||||
jsonApiServer = new JsonApiServer( jsonApiPort, [](int /*ec*/)
|
jsonApiServer = new JsonApiServer(
|
||||||
{
|
jsonApiPort, jsonApiBindAddress,
|
||||||
std::raise(SIGTERM);
|
[](int /*ec*/) { std::raise(SIGTERM); } );
|
||||||
} );
|
|
||||||
|
|
||||||
jsonApiServer->start("JSON API Server");
|
jsonApiServer->start("JSON API Server");
|
||||||
}
|
}
|
||||||
|
@ -95,7 +98,7 @@ int main(int argc, char **argv)
|
||||||
|
|
||||||
std::string docroot = resource_api::getDefaultDocroot();
|
std::string docroot = resource_api::getDefaultDocroot();
|
||||||
uint16_t httpPort = 0;
|
uint16_t httpPort = 0;
|
||||||
std::string listenAddress;
|
std::string listenAddress;
|
||||||
bool allowAllIps = false;
|
bool allowAllIps = false;
|
||||||
|
|
||||||
argstream args(argc, argv);
|
argstream args(argc, argv);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue