From 7bbc47a0ee6ddeb3a366efe2d67027ddb0b7c8ba Mon Sep 17 00:00:00 2001 From: yuanyuanxiang <962914132@qq.com> Date: Thu, 6 Aug 2026 20:44:33 +0200 Subject: [PATCH] Feature: Add "Ban IP" to log message context menu in main dialog MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Add "Ban IP" menu item to the log message list (m_CList_Message) right-click context menu, allowing users to extract IPv4 addresses from selected log entries and add them directly to the IP blacklist - Reuses ExtractFirstIPFromMessage() from the previous commit for dependency-free IPv4 parsing with per-segment 0-255 validation - Checks against whitelist before banning (whitelisted IPs cannot be banned) and skips IPs already in blacklist with clear feedback - Immediately persists via THIS_CFG.SetStr to the config ini file; IPBlacklist singleton takes effect instantly — subsequent connections from banned IPs are rejected in IOCPServer::OnAccept() which already calls IsIPBlacklisted() on every new connection - Menu item is grayed out when no log entry is selected - Sync en_US / zh_TW language files with new translation strings Co-Authored-By: DeepSeek V4 Pro --- server/2015Remote/2015RemoteDlg.cpp | 76 ++++++++++++++++++++++++++++- server/2015Remote/2015RemoteDlg.h | 1 + server/2015Remote/lang/en_US.ini | 5 ++ server/2015Remote/lang/zh_TW.ini | 5 ++ server/2015Remote/resource.h | 3 +- 5 files changed, 88 insertions(+), 2 deletions(-) diff --git a/server/2015Remote/2015RemoteDlg.cpp b/server/2015Remote/2015RemoteDlg.cpp index 1f301c6..2a51969 100644 --- a/server/2015Remote/2015RemoteDlg.cpp +++ b/server/2015Remote/2015RemoteDlg.cpp @@ -943,6 +943,7 @@ BEGIN_MESSAGE_MAP(CMy2015RemoteDlg, CDialogEx) ON_COMMAND(ID_MSGLOG_CLEAR, &CMy2015RemoteDlg::OnMsglogClear) ON_COMMAND(ID_MSGLOG_SEARCH, &CMy2015RemoteDlg::OnMsglogSearch) ON_COMMAND(ID_MSGLOG_LOGIN_NOTIFY, &CMy2015RemoteDlg::OnMsglogLoginNotify) + ON_COMMAND(ID_MSGLOG_BAN_IP, &CMy2015RemoteDlg::OnMsglogBanIP) ON_COMMAND(ID_ONLINE_ADD_WATCH, &CMy2015RemoteDlg::OnOnlineAddWatch) ON_COMMAND(ID_ONLINE_LOGIN_NOTIFY, &CMy2015RemoteDlg::OnOnlineLoginNotify) ON_NOTIFY(NM_CUSTOMDRAW, IDC_ONLINE, &CMy2015RemoteDlg::OnNMCustomdrawOnline) @@ -9306,12 +9307,14 @@ void CMy2015RemoteDlg::OnRClickMessage(NMHDR* pNMHDR, LRESULT* pResult) m_bLogSearchVisible ? _TR("隐藏搜索") : _TR("搜索日志")); menu.AppendMenu(MF_SEPARATOR); menu.AppendMenu(MF_STRING, ID_MSGLOG_LOGIN_NOTIFY, _TR("上线提醒")); + menu.AppendMenu(MF_STRING, ID_MSGLOG_BAN_IP, _TR("封禁IP")); - // 没有选中项时禁用"删除选中"和"上线提醒" + // 没有选中项时禁用"删除选中"、"复制选中"、"上线提醒"和"封禁IP" if (m_CList_Message.GetSelectedCount() == 0) { menu.EnableMenuItem(ID_MSGLOG_DELETE, MF_GRAYED); menu.EnableMenuItem(ID_MSGLOG_COPY, MF_GRAYED); menu.EnableMenuItem(ID_MSGLOG_LOGIN_NOTIFY, MF_GRAYED); + menu.EnableMenuItem(ID_MSGLOG_BAN_IP, MF_GRAYED); } // PowerShell不可用时禁用"上线提醒" if (!GetNotifyManager().IsPowerShellAvailable()) { @@ -9549,6 +9552,77 @@ void CMy2015RemoteDlg::OnMsglogLoginNotify() MessageBoxL(msg, _TR("上线提醒"), MB_ICONINFORMATION); } +void CMy2015RemoteDlg::OnMsglogBanIP() +{ + // Get selected log messages and extract IP addresses from them + std::vector ipsToAdd; + + POSITION pos = m_CList_Message.GetFirstSelectedItemPosition(); + while (pos) { + int row = m_CList_Message.GetNextSelectedItem(pos); + // Column 2 is the message content ("信息内容") + CString msgText = m_CList_Message.GetItemText(row, 2); + + CString extractedIP; + if (ExtractFirstIPFromMessage(msgText, extractedIP)) { + std::string ipUtf8 = CT2A(extractedIP, CP_UTF8); + ipsToAdd.push_back(ipUtf8); + } + } + + if (ipsToAdd.empty()) { + MessageBoxL("所选日志消息中未检测到有效的 IP 地址", "提示", MB_ICONINFORMATION); + return; + } + + // Process each extracted IP + int bannedCount = 0; + int alreadyCount = 0; + int whitelistCount = 0; + + for (const auto& ip : ipsToAdd) { + // Cannot ban whitelisted IPs + if (IPWhitelist::getInstance().IsWhitelisted(ip)) { + whitelistCount++; + continue; + } + // Already in blacklist + if (IPBlacklist::getInstance().IsBlacklisted(ip)) { + alreadyCount++; + continue; + } + + IPBlacklist::getInstance().AddIP(ip); + bannedCount++; + } + + // Persist to config + if (bannedCount > 0) { + std::string blacklistStr = IPBlacklist::getInstance().Export(); + THIS_CFG.SetStr("settings", "IPBlacklist", blacklistStr.c_str()); + } + + // Build result message + CString details; + if (bannedCount > 0) { + details.AppendFormat(_TR("已封禁 %d 个 IP"), bannedCount); + } + if (alreadyCount > 0) { + if (!details.IsEmpty()) details += _T("\n"); + details.AppendFormat(_TR("%d 个 IP 已在黑名单中"), alreadyCount); + } + if (whitelistCount > 0) { + if (!details.IsEmpty()) details += _T("\n"); + details.AppendFormat(_TR("%d 个 IP 在白名单中,已跳过"), whitelistCount); + } + + if (details.IsEmpty()) { + details = _TR("未执行任何操作"); + } + + MessageBoxL(details, _TR("封禁IP"), MB_ICONINFORMATION); +} + void CMy2015RemoteDlg::OnOnlineAddWatch() { EnterCriticalSection(&m_cs); diff --git a/server/2015Remote/2015RemoteDlg.h b/server/2015Remote/2015RemoteDlg.h index 2461e8a..dbe81ea 100644 --- a/server/2015Remote/2015RemoteDlg.h +++ b/server/2015Remote/2015RemoteDlg.h @@ -565,6 +565,7 @@ public: afx_msg void OnMsglogClear(); afx_msg void OnMsglogSearch(); afx_msg void OnMsglogLoginNotify(); + afx_msg void OnMsglogBanIP(); afx_msg void OnOnlineAddWatch(); afx_msg void OnNMCustomdrawOnline(NMHDR* pNMHDR, LRESULT* pResult); afx_msg void OnOnlineRunAsAdmin(); diff --git a/server/2015Remote/lang/en_US.ini b/server/2015Remote/lang/en_US.ini index beb1880..37600e9 100644 --- a/server/2015Remote/lang/en_US.ini +++ b/server/2015Remote/lang/en_US.ini @@ -1968,3 +1968,8 @@ FRPC Զ ѡ־Ϣδ⵽Ч IP ַ=No valid IP address detected in the selected log messages н IP б=All extracted IPs are already in the login notify list %d IP б=Added %d IP(s) to login notify list +IP=Ban IP +ѷ %d IP=Banned %d IP(s) +%d IP ں=%d IP(s) already in blacklist +%d IP ڰУ=%d IP(s) in whitelist, skipped +δִκβ=No action taken diff --git a/server/2015Remote/lang/zh_TW.ini b/server/2015Remote/lang/zh_TW.ini index 6e7ffc2..6f6b210 100644 --- a/server/2015Remote/lang/zh_TW.ini +++ b/server/2015Remote/lang/zh_TW.ini @@ -1959,3 +1959,8 @@ FRPC Զ н IP б=н IP Ͼб %d IP б= %d IP Ͼб ѡ־Ϣδ⵽Ч IP ַ=xIϢδzyЧ IP λַ +IP=IP +ѷ %d IP=ѷ %d IP +%d IP ں=%d IP ں +%d IP ڰУ=%d IP ڰУ^ +δִκβ=δκβ diff --git a/server/2015Remote/resource.h b/server/2015Remote/resource.h index 4c7eeef..abee449 100644 --- a/server/2015Remote/resource.h +++ b/server/2015Remote/resource.h @@ -992,6 +992,7 @@ #define ID_MSGLOG_COPY 33043 #define ID_MSGLOG_SEARCH 33078 #define ID_MSGLOG_LOGIN_NOTIFY 33079 +#define ID_MSGLOG_BAN_IP 33080 #define ID_WEB_REMOTE_CONTROL 33044 #define ID_TOOL_PLUGIN_SETTINGS 33045 #define ID_33046 33046 @@ -1033,7 +1034,7 @@ #ifdef APSTUDIO_INVOKED #ifndef APSTUDIO_READONLY_SYMBOLS #define _APS_NEXT_RESOURCE_VALUE 398 -#define _APS_NEXT_COMMAND_VALUE 33080 +#define _APS_NEXT_COMMAND_VALUE 33081 #define _APS_NEXT_CONTROL_VALUE 2542 #define _APS_NEXT_SYMED_VALUE 105 #endif