From 1c3bdad657ee87d2a8ec3e91d4246fc788826c55 Mon Sep 17 00:00:00 2001 From: ales stibal Date: Thu, 2 May 2024 20:53:25 +0200 Subject: [PATCH] allow neighbor updates in reply to new neighbor webhook - this will allow to use only webhooks to update tags in neighbors - to recap: now we have two options to set/update neighbor tags in smithproxy: a) API direct call to smithproxy API b) in **'new neighbor' webhook response** sent from webhook server (this patch) Feature allows to change arbitrary neighbor entry, so it can be used to update all neighbors in proxy. Caveat: update (API or response) will not do any CIDR network calculations. Hostnames must be IP strings as they appear in neighbor cache. This may change in the future. --- src/proxy/nbrhood.hpp | 20 ++++++++++---------- src/service/http/webhooks.cpp | 21 +++++++++++++++++++-- 2 files changed, 29 insertions(+), 12 deletions(-) diff --git a/src/proxy/nbrhood.hpp b/src/proxy/nbrhood.hpp index 0e23c2f..e6241af 100644 --- a/src/proxy/nbrhood.hpp +++ b/src/proxy/nbrhood.hpp @@ -277,17 +277,17 @@ class NbrHood { nbr_cache_t const& cache() const { return nbrs_; } void update(std::string const& hostname) { - auto lc_ = std::scoped_lock(cache().lock()); - - if(auto nbr = cache().get_ul(hostname); nbr) { - nbr.value()->update(); - } - else { - auto ptr = std::make_shared(hostname); - ptr->update(); - cache().put_ul(hostname, ptr); - sx::http::webhooks::neighbor_new(hostname); + { + auto lc_ = std::scoped_lock(cache().lock()); + if (auto nbr = cache().get_ul(hostname); nbr) { + nbr.value()->update(); + } else { + auto ptr = std::make_shared(hostname); + ptr->update(); + cache().put_ul(hostname, ptr); + } } + sx::http::webhooks::neighbor_new(hostname); } bool apply(std::string const& hostname, std::function mod) { diff --git a/src/service/http/webhooks.cpp b/src/service/http/webhooks.cpp index 80e2878..7f10dfb 100644 --- a/src/service/http/webhooks.cpp +++ b/src/service/http/webhooks.cpp @@ -3,7 +3,7 @@ #include #include #include - +#include #include @@ -35,6 +35,8 @@ namespace sx::http::webhooks { } struct default_callback { + default_callback() = default; + virtual ~default_callback() = default; void operator() (sx::http::expected_reply rep) const { @@ -52,6 +54,10 @@ namespace sx::http::webhooks { entry.update_incr(is_error); } } + + virtual void on_reply([[maybe_unused]] sx::http::expected_reply const& rep) const { + // this cannot be abstract class, it's used with a no-op response + }; }; void ping() { @@ -95,9 +101,20 @@ namespace sx::http::webhooks { {"type", "proxy"}, { "address", address_str } }; + + struct new_neighbor_reply : public default_callback { + void on_reply(sx::http::expected_reply const& rep) const override { + auto const& body = rep->response.second; + if(not body.empty()) { + SmithProxy::api().neighbor_update(body); + } + } + }; + + sx::http::AsyncRequest::emit( to_string(nbr_update), - default_callback()); + new_neighbor_reply()); } }