Skip to content

Commit

Permalink
set arguments for external reverse proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
missdeer committed Jul 31, 2023
1 parent 1a8d9da commit 6b00057
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 14 deletions.
50 changes: 48 additions & 2 deletions desktop/externalreverseproxyrunner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@

#include "externalreverseproxyrunner.h"

ExternalReverseProxyRunner::ExternalReverseProxyRunner(QObject *parent) {}
ExternalReverseProxyRunner::ExternalReverseProxyRunner(QObject *parent) : m_process(new QProcess) {}

ExternalReverseProxyRunner::~ExternalReverseProxyRunner()
{
m_process->terminate();
delete m_process;
}

void ExternalReverseProxyRunner::start()
{
Expand All @@ -32,7 +38,47 @@ void ExternalReverseProxyRunner::restart()
start();
}

void ExternalReverseProxyRunner::applySettings(QSettings &settings) {}
void ExternalReverseProxyRunner::applySettings(QSettings &settings)
{
m_process->setProgram(settings.value("externalReverseProxyPath").toString());
QStringList args;
bool ok = false;
int state = settings.value("reverseProxyAutoRedirect", 2).toInt(&ok);
if (ok && state == Qt::Checked)
{
args << "--auto-redirect";
}
state = settings.value("reverseProxyRedirect", 2).toInt(&ok);
if (ok)
{
args << "--redirect";
}
#if !defined(Q_OS_WIN)
auto networkInterface = settings.value("reverseProxyBindNetworkInterface").toString();
if (!networkInterface.isEmpty())
{
args << "-i" << networkInterface;
}
#endif
auto port = settings.value("reverseProxyListenPort", 8090).toInt(&ok);
if (ok)
{
args << "-b" << QStringLiteral("localhost:%1").arg(port);
}
auto proxyType = settings.value("reverseProxyProxyType").toString();
if (proxyType == QStringLiteral("Http"))
{
auto proxyAddr = settings.value("reverseProxyProxyAddress").toString();
args << "-t" << proxyAddr;
}
if (proxyType == QStringLiteral("Socks5"))
{
auto proxyAddr = settings.value("reverseProxyProxyAddress").toString();
args << "-s" << proxyAddr;
}

m_process->setArguments(args);
}

bool ExternalReverseProxyRunner::isRunning()
{
Expand Down
2 changes: 1 addition & 1 deletion desktop/externalreverseproxyrunner.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class ExternalReverseProxyRunner : public QObject
Q_OBJECT
public:
explicit ExternalReverseProxyRunner(QObject *parent = nullptr);

~ExternalReverseProxyRunner();
void start();
void stop();
void restart();
Expand Down
5 changes: 2 additions & 3 deletions desktop/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,13 +247,12 @@ int main(int argc, char *argv[])
builtinReverseProxyRunner.start();
}
QObject::connect(&app, &QCoreApplication::aboutToQuit, [&builtinReverseProxyRunner, &externalReverseProxyRunner]() {
builtinReverseProxyRunner.stop();
builtinReverseProxyRunner.wait();

if (externalReverseProxyRunner.isRunning())
{
externalReverseProxyRunner.stop();
}
builtinReverseProxyRunner.stop();
builtinReverseProxyRunner.wait();
});

return QtSingleApplication::exec();
Expand Down
22 changes: 14 additions & 8 deletions desktop/ui/configurationwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,6 @@ ConfigurationWindow::ConfigurationWindow(BeastServerRunner &runner, ExternalReve

m_trayIcon->show();
connect(m_trayIcon, &QSystemTrayIcon::activated, this, &ConfigurationWindow::onSystemTrayIconActivated);

m_reverseProxyAddr = QString("localhost:%1").arg(ui->reverseProxyListenPort->value()).toUtf8();
}

ConfigurationWindow::~ConfigurationWindow()
Expand Down Expand Up @@ -341,17 +339,25 @@ void ConfigurationWindow::initNetworkInterfaces()
}
void ConfigurationWindow::restartReverseProxy()
{
m_builtinReverseProxyRunner.stop();
m_builtinReverseProxyRunner.wait();
m_builtinReverseProxyRunner.start();
if (m_settings->value("useExternalReverseProxy", 2).toInt() == Qt::Checked)
{
m_externalReverseProxyRunner.stop();
m_externalReverseProxyRunner.start();
}
else
{
m_builtinReverseProxyRunner.stop();
m_builtinReverseProxyRunner.wait();
m_builtinReverseProxyRunner.start();
}
}

void ConfigurationWindow::onReverseProxyListenPortValueChanged(int port)
{
Q_ASSERT(m_settings);
m_settings->setValue("reverseProxyListenPort", port);
m_settings->sync();
m_reverseProxyAddr = QString("localhost:%1").arg(ui->reverseProxyListenPort->value()).toUtf8();

restartReverseProxy();
}

Expand All @@ -369,7 +375,7 @@ void ConfigurationWindow::onReverseProxyAutoRedirectStateChanged(int state)
Q_ASSERT(m_settings);
m_settings->setValue("reverseProxyAutoRedirect", state);
m_settings->sync();

restartReverseProxy();
}

Expand All @@ -378,7 +384,7 @@ void ConfigurationWindow::onReverseProxyRedirectStateChanged(int state)
Q_ASSERT(m_settings);
m_settings->setValue("reverseProxyRedirect", state);
m_settings->sync();

restartReverseProxy();
}

Expand Down

0 comments on commit 6b00057

Please sign in to comment.