From 8c987d1288814206720896994e07e965f7e328c0 Mon Sep 17 00:00:00 2001
From: Syping <schiedelrafael@keppe.org>
Date: Thu, 25 Feb 2021 16:45:12 +0100
Subject: [PATCH] add "Bind" option

---
 src/main.cpp        | 12 +++++++++---
 src/proxyserver.cpp | 20 ++++++++++++++++++--
 src/proxyserver.h   |  2 +-
 3 files changed, 28 insertions(+), 6 deletions(-)

diff --git a/src/main.cpp b/src/main.cpp
index c463704..d934364 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -11,7 +11,7 @@ int main(int argc, char *argv[])
 {
     QCoreApplication app(argc, argv);
     app.setApplicationName("proxylight");
-    app.setApplicationVersion("0.1");
+    app.setApplicationVersion("0.2");
 
     QCommandLineParser commandLineParser;
     commandLineParser.addHelpOption();
@@ -49,10 +49,16 @@ int main(int argc, char *argv[])
         const QString serverName = it.key();
         int localPort = -1;
         int remotePort = -1;
-        QString remoteHost;
+        QString serverBind, remoteHost;
         const QJsonObject object = value.toObject();
         for (auto it = object.constBegin(); it != object.constEnd(); it++) {
             const QString key = it.key();
+            if (key == "Bind") {
+                const QJsonValue value = it.value();
+                if (value.isString()) {
+                    serverBind = value.toString();
+                }
+            }
             if (key == "LocalPort") {
                 const QJsonValue value = it.value();
                 if (value.isDouble()) {
@@ -84,7 +90,7 @@ int main(int argc, char *argv[])
             QTextStream(stderr) << serverName << ": Loading failed!" << Qt::endl;
             break;
         }
-        ProxyServer *proxyServer = new ProxyServer(localPort, remotePort, remoteHost, serverName);
+        ProxyServer *proxyServer = new ProxyServer(localPort, remotePort, remoteHost, serverName, serverBind);
         if (!proxyServer->isListening()) {
             QTextStream(stderr) << serverName << ": Binding failed!" << Qt::endl;
             proxyServer->deleteLater();
diff --git a/src/proxyserver.cpp b/src/proxyserver.cpp
index a92443b..da9dd68 100644
--- a/src/proxyserver.cpp
+++ b/src/proxyserver.cpp
@@ -2,9 +2,25 @@
 #include <QTextStream>
 #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_remoteHost = remoteHost;
     p_serverName = serverName;
diff --git a/src/proxyserver.h b/src/proxyserver.h
index 47fe32a..6965765 100644
--- a/src/proxyserver.h
+++ b/src/proxyserver.h
@@ -8,7 +8,7 @@ class ProxyServer : public QTcpServer
 {
     Q_OBJECT
 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:
     void incomingConnection(qintptr socketDescriptor);