Quellcode durchsuchen

append 8 random hex chars to the subscription id

Grant Limberg vor 3 Tagen
Ursprung
Commit
88f08fa58c

+ 15 - 0
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)
 {

+ 3 - 1
nonfree/controller/CtlUtil.hpp

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

+ 1 - 0
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());