append 8 random hex chars to the subscription id

This commit is contained in:
Grant Limberg 2025-09-08 10:29:30 -07:00
parent f51bfdb666
commit 88f08fa58c
3 changed files with 19 additions and 1 deletions

View file

@ -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)
{

View file

@ -16,8 +16,10 @@ std::vector<std::string> 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

View file

@ -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());