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:
Gioacchino Mazzurco 2018-09-05 00:08:56 +02:00
parent 4acbf775a5
commit a194e4cf56
No known key found for this signature in database
GPG Key ID: A1FBCA3872E87051
5 changed files with 76 additions and 24 deletions

View File

@ -58,8 +58,9 @@ static bool checkRsServicePtrReady(
}
JsonApiServer::JsonApiServer(
uint16_t port, const std::function<void(int)> shutdownCallback) :
mPort(port), mShutdownCallback(shutdownCallback)
uint16_t port, const std::string& bindAddress,
const std::function<void(int)> shutdownCallback ) :
mPort(port), mBindAddress(bindAddress), mShutdownCallback(shutdownCallback)
{
registerHandler("/jsonApiServer/shutdown",
[this](const std::shared_ptr<rb::Session> session)
@ -153,7 +154,7 @@ void JsonApiServer::run()
{
std::shared_ptr<rb::Settings> settings(new rb::Settings);
settings->set_port(mPort);
// settings->set_default_header("Connection", "close");
settings->set_bind_address(mBindAddress);
settings->set_default_header("Cache-Control", "no-cache");
mService.start(settings);
}

View File

@ -35,7 +35,8 @@ namespace rb = restbed;
struct JsonApiServer : RsSingleJobThread
{
JsonApiServer(
uint16_t port,
uint16_t port = 9092,
const std::string& bindAddress = "127.0.0.1",
const std::function<void(int)> shutdownCallback = [](int){} );
/// @see RsSingleJobThread
@ -66,7 +67,8 @@ struct JsonApiServer : RsSingleJobThread
void shutdown(int exitCode = 0);
private:
uint16_t mPort;
const uint16_t mPort;
const std::string mBindAddress;
rb::Service mService;
const std::function<void(int)> mShutdownCallback;
};

View File

@ -39,8 +39,10 @@
#endif // ifdef LIBRESAPI_LOCAL_SERVER
#ifdef RS_JSONAPI
# include <cstdint>
# include "jsonapi/jsonapi.h"
# include "util/rsnet.h"
# include <cstdint>
# include <QCommandLineParser>
# include <QString>
# include <iostream>
@ -96,29 +98,65 @@ int main(int argc, char *argv[])
#ifdef RS_JSONAPI
uint16_t jsonApiPort = 9092;
std::string jsonApiBindAddress = "127.0.0.1";
{
QCommandLineOption jsonApiPortOpt(
"jsonApiPort", "JSON API listening port.", "port", "9092");
QCommandLineOption jsonApiBindAddressOpt(
"jsonApiBindAddress", "JSON API Bind Address.",
"IP Address", "127.0.0.1");
QCommandLineParser cmdParser;
cmdParser.addHelpOption();
cmdParser.addOption(jsonApiPortOpt);
cmdParser.addOption(jsonApiBindAddressOpt);
cmdParser.parse(app.arguments());
QString jsonApiPortStr = cmdParser.value(jsonApiPortOpt);
bool portOk;
jsonApiPort = jsonApiPortStr.toUShort(&portOk);
if(!portOk)
if(cmdParser.isSet(jsonApiPortOpt))
{
std::cerr << "ERROR: jsonApiPort option value must be a valid TCP "
<< "port!" << std::endl;
cmdParser.showHelp();
QCoreApplication::exit(EINVAL);
QString jsonApiPortStr = cmdParser.value(jsonApiPortOpt);
bool portOk;
jsonApiPort = jsonApiPortStr.toUShort(&portOk);
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();
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

View File

@ -22,9 +22,12 @@ resource_api::ApiServerMHD* WebuiPage::apiServerMHD = 0;
resource_api::ApiServerLocal* WebuiPage::apiServerLocal = 0;
#endif
resource_api::RsControlModule* WebuiPage::controlModule = 0;
#ifdef RS_JSONAPI
# include <csignal>
JsonApiServer* WebuiPage::jsonApiServer = nullptr;
#endif
#endif // ifdef RS_JSONAPI
WebuiPage::WebuiPage(QWidget */*parent*/, Qt::WindowFlags /*flags*/)
{
@ -109,11 +112,16 @@ QString WebuiPage::helpText() const
#ifdef LIBRESAPI_LOCAL_SERVER
apiServerLocal = new resource_api::ApiServerLocal(apiServer, resource_api::ApiServerLocal::serverPath());
#endif
#ifdef RS_JSONAPI
// 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");
#endif
#endif // ifdef RS_JSONAPI
return ok;
}

View File

@ -68,12 +68,16 @@ int main(int argc, char **argv)
#ifdef RS_JSONAPI
JsonApiServer* jsonApiServer = nullptr;
uint16_t jsonApiPort = 0;
std::string jsonApiBindAddress = "127.0.0.1";
{
argstream jsonApiArgs(argc, argv);
jsonApiArgs >> parameter(
"jsonApiPort", jsonApiPort, "jsonApiPort",
"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");
if (jsonApiArgs.helpRequested())
@ -82,10 +86,9 @@ int main(int argc, char **argv)
if(jsonApiPort)
{
jsonApiServer = new JsonApiServer( jsonApiPort, [](int /*ec*/)
{
std::raise(SIGTERM);
} );
jsonApiServer = new JsonApiServer(
jsonApiPort, jsonApiBindAddress,
[](int /*ec*/) { std::raise(SIGTERM); } );
jsonApiServer->start("JSON API Server");
}
@ -95,7 +98,7 @@ int main(int argc, char **argv)
std::string docroot = resource_api::getDefaultDocroot();
uint16_t httpPort = 0;
std::string listenAddress;
std::string listenAddress;
bool allowAllIps = false;
argstream args(argc, argv);