From 577f2a06458140d06213e289c9b656b49533aa80 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Adrien=20B=C3=A9raud?= <adrien.beraud@savoirfairelinux.com>
Date: Tue, 5 Jul 2022 19:32:29 -0400
Subject: [PATCH] proxy server: allow to configure address to bind

---
 include/opendht/dht_proxy_server.h | 1 +
 src/dht_proxy_server.cpp           | 4 ++++
 tools/dhtnode.cpp                  | 1 +
 tools/tools_common.h               | 5 +++++
 4 files changed, 11 insertions(+)

diff --git a/include/opendht/dht_proxy_server.h b/include/opendht/dht_proxy_server.h
index 8615f816..33f7e3b3 100644
--- a/include/opendht/dht_proxy_server.h
+++ b/include/opendht/dht_proxy_server.h
@@ -63,6 +63,7 @@ using RestRouter = restinio::router::express_router_t<>;
 using RequestStatus = restinio::request_handling_status_t;
 
 struct ProxyServerConfig {
+    std::string address {};
     in_port_t port {8000};
     std::string pushServer {};
     std::string persistStatePath {};
diff --git a/src/dht_proxy_server.cpp b/src/dht_proxy_server.cpp
index fcab0b22..f391fa6f 100644
--- a/src/dht_proxy_server.cpp
+++ b/src/dht_proxy_server.cpp
@@ -265,6 +265,8 @@ DhtProxyServer::DhtProxyServer(const std::shared_ptr<DhtRunner>& dht,
         // build http server
         auto settings = restinio::run_on_this_thread_settings_t<RestRouterTraitsTls>();
         addServerSettings(settings);
+        if (not config.address.empty())
+            settings.address(config.address);
         settings.port(config.port);
         settings.tls_context(std::move(tls_context));
         httpsServer_ = std::make_unique<restinio::http_server_t<RestRouterTraitsTls>>(
@@ -282,6 +284,8 @@ DhtProxyServer::DhtProxyServer(const std::shared_ptr<DhtRunner>& dht,
     else {
         auto settings = restinio::run_on_this_thread_settings_t<RestRouterTraits>();
         addServerSettings(settings);
+        if (not config.address.empty())
+            settings.address(config.address);
         settings.port(config.port);
         httpServer_ = std::make_unique<restinio::http_server_t<RestRouterTraits>>(
             ioContext_,
diff --git a/tools/dhtnode.cpp b/tools/dhtnode.cpp
index 69dd1ffa..5767641e 100644
--- a/tools/dhtnode.cpp
+++ b/tools/dhtnode.cpp
@@ -551,6 +551,7 @@ main(int argc, char **argv)
             ProxyServerConfig serverConfig;
             serverConfig.pushServer = params.pushserver;
             serverConfig.bundleId = params.bundle_id;
+            serverConfig.address = params.proxy_address;
             if (params.proxyserverssl and params.proxy_id.first and params.proxy_id.second){
                 serverConfig.identity = params.proxy_id;
                 serverConfig.port = params.proxyserverssl;
diff --git a/tools/tools_common.h b/tools/tools_common.h
index 889cd055..258482ab 100644
--- a/tools/tools_common.h
+++ b/tools/tools_common.h
@@ -131,6 +131,7 @@ struct dht_params {
     in_port_t proxyserver {0};
     in_port_t proxyserverssl {0};
     std::string proxyclient {};
+    std::string proxy_address {};
     std::string pushserver {};
     std::string devicekey {};
     std::string bundle_id {};
@@ -224,6 +225,7 @@ static const constexpr struct option long_options[] = {
     {"syslog",                  no_argument      , nullptr, 'L'},
     {"proxyserver",             required_argument, nullptr, 'S'},
     {"proxyserverssl",          required_argument, nullptr, 'e'},
+    {"proxy-addr",              required_argument, nullptr, 'a'},
     {"proxy-certificate",       required_argument, nullptr, 'w'},
     {"proxy-privkey",           required_argument, nullptr, 'K'},
     {"proxy-privkey-password",  required_argument, nullptr, 'M'},
@@ -270,6 +272,9 @@ parseArgs(int argc, char **argv) {
         case 'D':
             params.peer_discovery = true;
             break;
+        case 'a':
+            params.proxy_address = optarg;
+            break;
         case 'y':
             params.pushserver = optarg;
             break;
-- 
GitLab