From 88f08fa58c0cf76917fa3189bb9813732d4131a2 Mon Sep 17 00:00:00 2001 From: Grant Limberg Date: Mon, 8 Sep 2025 10:29:30 -0700 Subject: [PATCH] append 8 random hex chars to the subscription id --- nonfree/controller/CtlUtil.cpp | 15 +++++++++++++++ nonfree/controller/CtlUtil.hpp | 4 +++- nonfree/controller/PubSubListener.cpp | 1 + 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/nonfree/controller/CtlUtil.cpp b/nonfree/controller/CtlUtil.cpp index 4ec5fa987..36aad01b0 100644 --- a/nonfree/controller/CtlUtil.cpp +++ b/nonfree/controller/CtlUtil.cpp @@ -80,6 +80,21 @@ std::string url_encode(const std::string& value) return escaped.str(); } +std::string random_hex_string(std::size_t length) +{ + static const char hex_chars[] = "0123456789abcdef"; + std::random_device rd; + std::mt19937 gen(rd()); + std::uniform_int_distribution<> dis(0, 15); + + std::string result; + result.reserve(length); + for (std::size_t i = 0; i < length; ++i) { + result += hex_chars[dis(gen)]; + } + return result; +} + #ifdef ZT1_CENTRAL_CONTROLLER void create_gcp_pubsub_topic_if_needed(std::string project_id, std::string topic_id) { diff --git a/nonfree/controller/CtlUtil.hpp b/nonfree/controller/CtlUtil.hpp index b010d1c79..5ae541959 100644 --- a/nonfree/controller/CtlUtil.hpp +++ b/nonfree/controller/CtlUtil.hpp @@ -16,8 +16,10 @@ std::vector split(std::string str, char delim); std::string url_encode(const std::string& value); +std::string random_hex_string(std::size_t length) + #ifdef ZT1_CENTRAL_CONTROLLER -void create_gcp_pubsub_topic_if_needed(std::string project_id, std::string topic_id); + void create_gcp_pubsub_topic_if_needed(std::string project_id, std::string topic_id); #endif } // namespace ZeroTier diff --git a/nonfree/controller/PubSubListener.cpp b/nonfree/controller/PubSubListener.cpp index 514a4522a..31f3eeff1 100644 --- a/nonfree/controller/PubSubListener.cpp +++ b/nonfree/controller/PubSubListener.cpp @@ -35,6 +35,7 @@ PubSubListener::PubSubListener(std::string controller_id, std::string project, s , _adminClient(pubsub_admin::MakeSubscriptionAdminConnection()) , _subscription(pubsub::Subscription(_project, _subscription_id)) { + _subscription_id = _subscription_id + "-" + random_hex_string(8); fprintf( stderr, "PubSubListener for controller %s project %s topic %s subscription %s\n", controller_id.c_str(), project.c_str(), topic.c_str(), _subscription_id.c_str());