add "Bind" option

This commit is contained in:
Syping 2021-02-25 16:45:12 +01:00
parent 3dee85fc5d
commit 8c987d1288
3 changed files with 28 additions and 6 deletions

View File

@ -11,7 +11,7 @@ int main(int argc, char *argv[])
{ {
QCoreApplication app(argc, argv); QCoreApplication app(argc, argv);
app.setApplicationName("proxylight"); app.setApplicationName("proxylight");
app.setApplicationVersion("0.1"); app.setApplicationVersion("0.2");
QCommandLineParser commandLineParser; QCommandLineParser commandLineParser;
commandLineParser.addHelpOption(); commandLineParser.addHelpOption();
@ -49,10 +49,16 @@ int main(int argc, char *argv[])
const QString serverName = it.key(); const QString serverName = it.key();
int localPort = -1; int localPort = -1;
int remotePort = -1; int remotePort = -1;
QString remoteHost; QString serverBind, remoteHost;
const QJsonObject object = value.toObject(); const QJsonObject object = value.toObject();
for (auto it = object.constBegin(); it != object.constEnd(); it++) { for (auto it = object.constBegin(); it != object.constEnd(); it++) {
const QString key = it.key(); const QString key = it.key();
if (key == "Bind") {
const QJsonValue value = it.value();
if (value.isString()) {
serverBind = value.toString();
}
}
if (key == "LocalPort") { if (key == "LocalPort") {
const QJsonValue value = it.value(); const QJsonValue value = it.value();
if (value.isDouble()) { if (value.isDouble()) {
@ -84,7 +90,7 @@ int main(int argc, char *argv[])
QTextStream(stderr) << serverName << ": Loading failed!" << Qt::endl; QTextStream(stderr) << serverName << ": Loading failed!" << Qt::endl;
break; break;
} }
ProxyServer *proxyServer = new ProxyServer(localPort, remotePort, remoteHost, serverName); ProxyServer *proxyServer = new ProxyServer(localPort, remotePort, remoteHost, serverName, serverBind);
if (!proxyServer->isListening()) { if (!proxyServer->isListening()) {
QTextStream(stderr) << serverName << ": Binding failed!" << Qt::endl; QTextStream(stderr) << serverName << ": Binding failed!" << Qt::endl;
proxyServer->deleteLater(); proxyServer->deleteLater();

View File

@ -2,9 +2,25 @@
#include <QTextStream> #include <QTextStream>
#include <QTcpSocket> #include <QTcpSocket>
ProxyServer::ProxyServer(int localPort, int remotePort, const QString &remoteHost, const QString &serverName) ProxyServer::ProxyServer(int localPort, int remotePort, const QString &remoteHost, const QString &serverName, const QString &serverBind)
{ {
listen(QHostAddress::LocalHost, localPort); QHostAddress bindAddress = QHostAddress::LocalHost;
if (!serverBind.isEmpty()) {
if (serverBind == "::0.0.0.0") {
bindAddress = QHostAddress::Any;
}
else if (serverBind == "0.0.0.0") {
bindAddress = QHostAddress::AnyIPv4;
}
else if (serverBind == "::0") {
bindAddress = QHostAddress::AnyIPv6;
}
else {
if (!bindAddress.setAddress(serverBind))
return;
}
}
listen(bindAddress, localPort);
p_remotePort = remotePort; p_remotePort = remotePort;
p_remoteHost = remoteHost; p_remoteHost = remoteHost;
p_serverName = serverName; p_serverName = serverName;

View File

@ -8,7 +8,7 @@ class ProxyServer : public QTcpServer
{ {
Q_OBJECT Q_OBJECT
public: public:
ProxyServer(int localPort, int remotePort, const QString &remoteHost, const QString &serverName); ProxyServer(int localPort, int remotePort, const QString &remoteHost, const QString &serverName, const QString &serverBind);
protected: protected:
void incomingConnection(qintptr socketDescriptor); void incomingConnection(qintptr socketDescriptor);