2015-04-24 10:31:13 -04:00
|
|
|
#include "WebuiPage.h"
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <QMessageBox>
|
|
|
|
#include <QDesktopServices>
|
|
|
|
#include <QUrl>
|
2017-07-30 12:29:03 -04:00
|
|
|
#include <QSpinBox>
|
2015-04-24 10:31:13 -04:00
|
|
|
|
2017-02-27 18:10:54 -05:00
|
|
|
#include "util/misc.h"
|
2015-04-24 10:31:13 -04:00
|
|
|
#include "api/ApiServer.h"
|
|
|
|
#include "api/ApiServerMHD.h"
|
2016-07-05 10:11:37 -04:00
|
|
|
#include "api/ApiServerLocal.h"
|
2015-04-24 10:31:13 -04:00
|
|
|
#include "api/RsControlModule.h"
|
|
|
|
#include "api/GetPluginInterfaces.h"
|
|
|
|
|
|
|
|
#include "rsharesettings.h"
|
|
|
|
|
|
|
|
resource_api::ApiServer* WebuiPage::apiServer = 0;
|
|
|
|
resource_api::ApiServerMHD* WebuiPage::apiServerMHD = 0;
|
2016-07-28 08:08:49 -04:00
|
|
|
// TODO: LIBRESAPI_LOCAL_SERVER Put indipendent option for libresapilocalserver in appropriate place
|
2016-07-14 16:48:44 -04:00
|
|
|
#ifdef LIBRESAPI_LOCAL_SERVER
|
2016-07-05 10:11:37 -04:00
|
|
|
resource_api::ApiServerLocal* WebuiPage::apiServerLocal = 0;
|
2016-07-14 16:48:44 -04:00
|
|
|
#endif
|
2015-04-24 10:31:13 -04:00
|
|
|
resource_api::RsControlModule* WebuiPage::controlModule = 0;
|
2018-09-04 18:08:56 -04:00
|
|
|
|
2015-04-24 10:31:13 -04:00
|
|
|
|
|
|
|
WebuiPage::WebuiPage(QWidget */*parent*/, Qt::WindowFlags /*flags*/)
|
|
|
|
{
|
|
|
|
ui.setupUi(this);
|
|
|
|
connect(ui.enableWebUI_CB, SIGNAL(clicked(bool)), this, SLOT(onEnableCBClicked(bool)));
|
2017-07-30 12:29:03 -04:00
|
|
|
connect(ui.port_SB, SIGNAL(valueChanged(int)), this, SLOT(onPortValueChanged(int)));
|
|
|
|
connect(ui.allIp_CB, SIGNAL(clicked(bool)), this, SLOT(onAllIPCBClicked(bool)));
|
2015-04-24 10:31:13 -04:00
|
|
|
connect(ui.applyStartBrowser_PB, SIGNAL(clicked()), this, SLOT(onApplyClicked()));
|
|
|
|
}
|
|
|
|
|
|
|
|
WebuiPage::~WebuiPage()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-01-30 15:38:06 -05:00
|
|
|
bool WebuiPage::updateParams(QString &errmsg)
|
2015-04-24 10:31:13 -04:00
|
|
|
{
|
|
|
|
std::cerr << "WebuiPage::save()" << std::endl;
|
|
|
|
bool ok = true;
|
|
|
|
bool changed = false;
|
|
|
|
if(ui.enableWebUI_CB->isChecked() != Settings->getWebinterfaceEnabled())
|
|
|
|
changed = true;
|
|
|
|
if(ui.port_SB->value() != Settings->getWebinterfacePort())
|
|
|
|
changed = true;
|
|
|
|
if(ui.allIp_CB->isChecked() != Settings->getWebinterfaceAllowAllIps())
|
|
|
|
changed = true;
|
|
|
|
if(changed)
|
|
|
|
{
|
|
|
|
// store config
|
|
|
|
Settings->setWebinterfaceEnabled(ui.enableWebUI_CB->isChecked());
|
|
|
|
Settings->setWebinterfacePort(ui.port_SB->value());
|
|
|
|
Settings->setWebinterfaceAllowAllIps(ui.allIp_CB->isChecked());
|
|
|
|
|
|
|
|
// apply config
|
|
|
|
checkShutdownWebui();
|
|
|
|
ok = checkStartWebui();
|
|
|
|
}
|
|
|
|
if(!ok)
|
|
|
|
errmsg = "Could not start webinterface.";
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebuiPage::load()
|
|
|
|
{
|
2017-07-30 12:29:03 -04:00
|
|
|
std::cerr << "WebuiPage::load()" << std::endl;
|
|
|
|
whileBlocking(ui.enableWebUI_CB)->setChecked(Settings->getWebinterfaceEnabled());
|
|
|
|
whileBlocking(ui.port_SB)->setValue(Settings->getWebinterfacePort());
|
|
|
|
whileBlocking(ui.allIp_CB)->setChecked(Settings->getWebinterfaceAllowAllIps());
|
|
|
|
onEnableCBClicked(Settings->getWebinterfaceEnabled());
|
2015-04-24 10:31:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
QString WebuiPage::helpText() const
|
|
|
|
{
|
2015-06-30 12:44:58 -04:00
|
|
|
return tr("<h1><img width=\"24\" src=\":/icons/help_64.png\"> Webinterface</h1> \
|
2015-09-10 05:29:15 -04:00
|
|
|
<p>The webinterface allows you to control Retroshare from the browser. Multiple devices can share control over one Retroshare instance. So you could start a conversation on a tablet computer and later use a desktop computer to continue it.</p>\
|
2015-04-24 10:31:13 -04:00
|
|
|
<p>Warning: don't expose the webinterface to the internet, because there is no access control and no encryption. If you want to use the webinterface over the internet, use a SSH tunnel or a proxy to secure the connection.</p>");
|
|
|
|
}
|
|
|
|
|
|
|
|
/*static*/ bool WebuiPage::checkStartWebui()
|
|
|
|
{
|
|
|
|
if(!Settings->getWebinterfaceEnabled())
|
|
|
|
return true;
|
|
|
|
if(apiServer || apiServerMHD || controlModule)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
apiServer = new resource_api::ApiServer();
|
|
|
|
controlModule = new resource_api::RsControlModule(0, 0, apiServer->getStateTokenServer(), apiServer, false);
|
|
|
|
apiServer->addResourceHandler("control", dynamic_cast<resource_api::ResourceRouter*>(controlModule), &resource_api::RsControlModule::handleRequest);
|
|
|
|
|
|
|
|
RsPlugInInterfaces ifaces;
|
|
|
|
resource_api::getPluginInterfaces(ifaces);
|
|
|
|
apiServer->loadMainModules(ifaces);
|
|
|
|
|
|
|
|
apiServerMHD = new resource_api::ApiServerMHD(apiServer);
|
|
|
|
bool ok = apiServerMHD->configure(resource_api::getDefaultDocroot(),
|
|
|
|
Settings->getWebinterfacePort(),
|
|
|
|
"",
|
|
|
|
Settings->getWebinterfaceAllowAllIps());
|
|
|
|
apiServerMHD->start();
|
2016-07-05 10:11:37 -04:00
|
|
|
|
2016-07-14 16:48:44 -04:00
|
|
|
// TODO: LIBRESAPI_LOCAL_SERVER Move in appropriate place
|
|
|
|
#ifdef LIBRESAPI_LOCAL_SERVER
|
2016-08-22 21:19:33 -04:00
|
|
|
apiServerLocal = new resource_api::ApiServerLocal(apiServer, resource_api::ApiServerLocal::serverPath());
|
2018-06-23 11:13:38 -04:00
|
|
|
#endif
|
2018-09-04 18:08:56 -04:00
|
|
|
|
2015-04-24 10:31:13 -04:00
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*static*/ void WebuiPage::checkShutdownWebui()
|
|
|
|
{
|
|
|
|
if(apiServer || apiServerMHD)
|
|
|
|
{
|
|
|
|
apiServerMHD->stop();
|
|
|
|
delete apiServerMHD;
|
|
|
|
apiServerMHD = 0;
|
2016-07-14 16:48:44 -04:00
|
|
|
// TODO: LIBRESAPI_LOCAL_SERVER Move in appropriate place
|
|
|
|
#ifdef LIBRESAPI_LOCAL_SERVER
|
2016-07-05 10:11:37 -04:00
|
|
|
delete apiServerLocal;
|
|
|
|
apiServerLocal = 0;
|
2016-07-14 16:48:44 -04:00
|
|
|
#endif
|
2015-04-24 10:31:13 -04:00
|
|
|
delete apiServer;
|
|
|
|
apiServer = 0;
|
|
|
|
delete controlModule;
|
|
|
|
controlModule = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-28 10:55:13 -04:00
|
|
|
/*static*/ void WebuiPage::showWebui()
|
|
|
|
{
|
|
|
|
if(Settings->getWebinterfaceEnabled())
|
|
|
|
{
|
|
|
|
QDesktopServices::openUrl(QUrl(QString("http://localhost:")+QString::number(Settings->getWebinterfacePort())));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-07-29 09:02:10 -04:00
|
|
|
QMessageBox::warning(0, tr("Webinterface not enabled"), tr("The webinterface is not enabled. Enable it in Settings -> Webinterface."));
|
2015-04-28 10:55:13 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-24 10:31:13 -04:00
|
|
|
void WebuiPage::onEnableCBClicked(bool checked)
|
|
|
|
{
|
2017-07-30 12:29:03 -04:00
|
|
|
ui.params_GB->setEnabled(checked);
|
|
|
|
ui.applyStartBrowser_PB->setEnabled(checked);
|
|
|
|
QString S;
|
|
|
|
updateParams(S);
|
|
|
|
}
|
2017-01-31 17:07:59 -05:00
|
|
|
|
2017-07-30 12:29:03 -04:00
|
|
|
void WebuiPage::onPortValueChanged(int /*value*/)
|
|
|
|
{
|
|
|
|
QString S;
|
|
|
|
updateParams(S);
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebuiPage::onAllIPCBClicked(bool /*checked*/)
|
|
|
|
{
|
|
|
|
QString S;
|
|
|
|
updateParams(S);
|
2015-04-24 10:31:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void WebuiPage::onApplyClicked()
|
|
|
|
{
|
|
|
|
QString errmsg;
|
2017-01-30 15:38:06 -05:00
|
|
|
bool ok = updateParams(errmsg);
|
2015-04-24 10:31:13 -04:00
|
|
|
if(!ok)
|
|
|
|
{
|
|
|
|
QMessageBox::warning(0, tr("failed to start Webinterface"), "Failed to start the webinterface.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
QDesktopServices::openUrl(QUrl(QString("http://localhost:")+QString::number(ui.port_SB->value())));
|
|
|
|
}
|