Skip to content

Commit bebb527

Browse files
feat: support ip whitelist (#1038)
* feat: support ip whitelist * feat: support ip whitelist * style: Clang Format * feat: add client_filter * style: Clang Format * delete ip_whitelist.hpp * test: add client_filter test * fix: 1.clang-format issues in test files --------- Co-authored-by: qicosmos <qicosmos@linux.alibaba.com>
1 parent 50cb1cb commit bebb527

6 files changed

Lines changed: 1030 additions & 1 deletion

File tree

include/ylt/coro_rpc/impl/coro_rpc_server.hpp

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ class coro_rpc_server_base {
172172
}
173173
errc_ = listen();
174174
if (!errc_) {
175-
if constexpr (requires(typename server_config::executor_pool_t &pool) {
175+
if constexpr (requires(typename server_config::executor_pool_t & pool) {
176176
pool.run();
177177
}) {
178178
thd_ = std::thread([this] {
@@ -348,6 +348,16 @@ class coro_rpc_server_base {
348348

349349
auto &get_io_context_pool() noexcept { return pool_; }
350350

351+
/*!
352+
* Set client filter callback
353+
* @param filter callback function that takes endpoint and returns bool
354+
* true to allow connection, false to reject
355+
*/
356+
void client_filter(
357+
std::function<bool(const asio::ip::tcp::endpoint &)> filter) {
358+
client_filter_ = std::move(filter);
359+
}
360+
351361
private:
352362
coro_rpc::err_code listen() {
353363
ELOG_INFO << "begin to listen";
@@ -434,6 +444,26 @@ class coro_rpc_server_base {
434444
continue;
435445
}
436446

447+
// Client filter check
448+
if (client_filter_) {
449+
asio::error_code ec;
450+
auto remote_endpoint = socket.remote_endpoint(ec);
451+
if (ec) {
452+
ELOG_WARN << "Failed to get remote endpoint: " << ec.message();
453+
continue;
454+
}
455+
456+
if (!client_filter_(remote_endpoint)) {
457+
ELOG_WARN << "Connection from "
458+
<< remote_endpoint.address().to_string()
459+
<< " rejected by client filter";
460+
continue;
461+
}
462+
463+
ELOG_INFO << "Connection from " << remote_endpoint.address().to_string()
464+
<< " allowed by client filter";
465+
}
466+
437467
int64_t conn_id = ++conn_id_;
438468
ELOG_INFO << "new client conn_id " << conn_id << " coming";
439469
if (is_enable_tcp_no_delay_) {
@@ -545,5 +575,7 @@ class coro_rpc_server_base {
545575
#ifdef YLT_ENABLE_IBV
546576
std::optional<coro_io::ib_socket_t::config_t> ibv_config_;
547577
#endif
578+
579+
std::function<bool(const asio::ip::tcp::endpoint &)> client_filter_;
548580
};
549581
} // namespace coro_rpc

include/ylt/standalone/cinatra/coro_http_server.hpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,16 @@ class coro_http_server {
605605
std::string_view address() { return address_; }
606606
std::error_code get_errc() { return errc_; }
607607

608+
/*!
609+
* Set client filter callback
610+
* @param filter callback function that takes endpoint and returns bool
611+
* true to allow connection, false to reject
612+
*/
613+
void client_filter(
614+
std::function<bool(const asio::ip::tcp::endpoint &)> filter) {
615+
client_filter_ = std::move(filter);
616+
}
617+
608618
private:
609619
std::error_code listen() {
610620
CINATRA_LOG_INFO << "begin to listen " << port_;
@@ -749,6 +759,28 @@ class coro_http_server {
749759
}
750760
continue;
751761
}
762+
763+
// Client filter check
764+
if (client_filter_) {
765+
auto remote_endpoint = socket.remote_endpoint(error);
766+
if (error) {
767+
CINATRA_LOG_WARNING << "Failed to get remote endpoint: "
768+
<< error.message();
769+
continue;
770+
}
771+
772+
if (!client_filter_(remote_endpoint)) {
773+
CINATRA_LOG_WARNING << "HTTP connection rejected from "
774+
<< remote_endpoint.address().to_string()
775+
<< " by client filter";
776+
continue;
777+
}
778+
779+
CINATRA_LOG_DEBUG << "HTTP connection accepted from "
780+
<< remote_endpoint.address().to_string()
781+
<< " (client filter passed)";
782+
}
783+
752784
auto conn =
753785
accept_impl(coro_io::socket_wrapper_t{std::move(socket), executor});
754786
start_one(conn).via(conn->get_executor()).detach();
@@ -1044,6 +1076,8 @@ class coro_http_server {
10441076
bool write_failed_forever_ = false;
10451077
bool read_failed_forever_ = false;
10461078
#endif
1079+
1080+
std::function<bool(const asio::ip::tcp::endpoint &)> client_filter_;
10471081
};
10481082

10491083
using http_server = coro_http_server;

src/coro_http/tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ add_executable(coro_http_test
2626
test_cinatra.cpp
2727
test_cinatra_websocket.cpp
2828
test_http_parse.cpp
29+
test_http_client_filter.cpp
2930
main.cpp
3031
)
3132

0 commit comments

Comments
 (0)