From 13be4bd8ca72bd393e44a9bb5819a4f7d902f8f7 Mon Sep 17 00:00:00 2001 From: qing <1776177662@qq.com> Date: Sat, 25 Jul 2026 09:58:25 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=BA=BF=E7=A8=8B=E5=AE=89?= =?UTF-8?q?=E5=85=A8=E9=97=AE=E9=A2=98=EF=BC=8C=E4=BB=A5=E5=8F=8A=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=E5=88=AB=E7=9A=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gateway/src/main.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/gateway/src/main.cpp b/gateway/src/main.cpp index 7a90b26..c37ce5c 100644 --- a/gateway/src/main.cpp +++ b/gateway/src/main.cpp @@ -304,8 +304,10 @@ static std::mutex _CAPTCHA_MTX; std::string generate_captcha_text() { static const char chars[] = "ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz23456789"; - static std::mt19937 rng(std::random_device{}()); - static std::uniform_int_distribution dist(0, sizeof(chars) - 2); + // thread_local:每个线程独立 rng 实例,避免多线程并发调用 dist(rng) 时的数据竞争 + // (static 变量初始化本身线程安全,但 mt19937::operator() 修改内部状态非线程安全) + thread_local std::mt19937 rng(std::random_device{}()); + thread_local std::uniform_int_distribution dist(0, sizeof(chars) - 2); std::string text; for (int i = 0; i < 5; i++) { @@ -717,6 +719,10 @@ http::response handleRegister( // IP rate limit check std::string client_ip = "direct"; + auto it_host = req.base().find("X-Forwarded-For"); + if (it_host != req.base().end()) { + client_ip = std::string(it_host->value()); + } if (!_REGISTER_RATE.check_and_record(client_ip, 3, 600)) { json::value jv_r = { {"success", false},