Skip to content

Commit

Permalink
content replacement - don't wait forever for traffic lock
Browse files Browse the repository at this point in the history
- rather than wait for traffic lock forever, use timed_guard
  and std::timed_mutex to get actually bytes across the network
  if something goes wrong.
  • Loading branch information
astibal committed Nov 3, 2024
1 parent 5cbcf27 commit ff58c2c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
2 changes: 1 addition & 1 deletion socle
Submodule socle updated 1 files
+54 −0 timed_guard.hpp
34 changes: 31 additions & 3 deletions src/proxy/mitmproxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
#include <algorithm>

#include <socle/common/base64.hpp>
#include <socle/timed_guard.hpp>

using namespace socle;

Expand Down Expand Up @@ -1044,13 +1045,40 @@ bool MitmProxy::handle_content_webhook(baseHostCX* from, baseHostCX* to, side_t
}
}

// traffic with applied webhook with enabled locking will block here
auto lc_ = std::scoped_lock(MitmProxy::Opts_ContentWriter::webhook_content_lock);
return content_webhook(from, side, from->to_read());
constexpr unsigned max_attempts = 3;
constexpr unsigned lock_timeout = 1000;
for(unsigned attempt_no = 1; attempt_no < max_attempts; ++attempt_no) {

// traffic with applied webhook with enabled locking will block here
auto lc_ = socle::threads::timed_guard(MitmProxy::Opts_ContentWriter::webhook_content_lock,
std::chrono::milliseconds(lock_timeout));
if (not lc_.owns_lock()) {
_war("[%s]: content_webhook: waiting for other content webhook to finish (attempt %d)",
to_string(iNOT).c_str(), attempt_no);
log.event(WAR, "[%s]: content_webhook: waiting for other content webhook to finish (attempt %d)",
to_string(iNOT).c_str(), attempt_no);
}
else {
if(attempt_no > 1) {
_war("[%s]: content_webhook: webhook is ready now (attempt %d)",
to_string(iNOT).c_str(), attempt_no);
log.event(WAR, "[%s]: content_webhook: webhook is ready now (attempt %d)",
to_string(iNOT).c_str(), attempt_no);

}
return content_webhook(from, side, from->to_read());
}
}
_err("[%s]: content_webhook: previous webhook takes too long to complete, content NOT sent to webhook.",
to_string(iNOT).c_str());
log.event(ERR, "[%s]: content_webhook: previous webhook takes too long to complete, content NOT sent to webhook.",
to_string(iNOT).c_str());

}
else {
return content_webhook(from, side, from->to_read());
}
return false;
}

void MitmProxy::proxy(baseHostCX* from, baseHostCX* to, side_t side, bool redirected) {
Expand Down
2 changes: 1 addition & 1 deletion src/proxy/mitmproxy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ class MitmProxy : public baseProxy, public socle::sobject, public IOController {
bool webhook_lock_traffic = false;

// if configured, all proxies will lock on this mutex (it's optional)
static inline std::mutex webhook_content_lock;
static inline std::timed_mutex webhook_content_lock;

};
lazy_ptr<Opts_ContentWriter> writer_opts_;
Expand Down

0 comments on commit ff58c2c

Please sign in to comment.