Fix: throttle TLS/WS handshake failure logs
Exposed ports see a constant stream of scanners and HTTP probes. Every TLS handshake attempt logged an ioctlsocket(FIONBIO=0) failure, but WSAEINVAL is the documented result once WSAEventSelect owns the listening socket and the code already falls back to select; that line alone accounted for 6678 of the 8970 "handshake" lines in a four-day log. It is now logged only for other error codes. A failed handshake used to print three or four lines (recv EOF, AcceptSecurityContext, final status, failed-for-IP) with no per-peer dedup, and WS upgrade failures printed once per HTTP probe. ServerTlsSession now records a human-readable reason (Schannel status name for ASC failures) instead of logging per connection; IOCPServer prints one line per peer per hour with the reason and a suppressed count, for both TLS and WS. Peer close_notify is a normal shutdown and is no longer logged; MCP TLS failures now carry the same reason. Failure details are copied before the context's I/O reference is released, so the log path cannot touch a context that another thread may recycle. No handshake, crypto or connection behaviour changes. Verified with a Debug x64 build and a live probe run: six failed connections to the device port produced exactly one TLS and one WS line, with no ioctlsocket lines. Co-Authored-By: deepseek-flash
This commit is contained in:
1 parent
3d2dd08be5
commit
5ee59415f9
4 files changed
+176
-19
No files matched your search
@@ -8,6 +8,9 @@
|
||||
|
||||
#include <iostream>
|
||||
#include <ws2tcpip.h>
|
||||
#include <map>
|
||||
#include <mutex>
|
||||
#include <ctime>
|
||||
|
||||
// 服务端 RTT 反代理(试用版执法)。声明在主对话框 cpp 中,无单独头文件。
|
||||
BOOL IsTrail(const std::string& passcode);
|
||||
@@ -896,6 +899,54 @@ BOOL ParseReceivedData(CONTEXT_OBJECT * ContextObject, PBYTE data, DWORD dataLen
|
||||
return ret;
|
||||
}
|
||||
|
||||
// 握手失败日志限流。
|
||||
//
|
||||
// 设备端口/WS 端口暴露在公网时,扫描器会持续制造两类噪声:只发半截 ClientHello 就
|
||||
// 断开的连接(Schannel 报 EOF / SEC_E_INTERNAL_ERROR),以及用 HTTP GET 探测端口的
|
||||
// 请求(WS 升级因缺 Upgrade 头失败)。按「(类别, 对端) 每小时一条、首条必记」输出,
|
||||
// 并把窗口内被抑制的条数在下次放行时一并带出:既保留「谁在失败、失败多少」的有效
|
||||
// 信息,又不会让几千行相同日志淹没真正的问题。
|
||||
static bool ShouldLogHandshakeFailure(const char* kind, const std::string& peer,
|
||||
unsigned* suppressed)
|
||||
{
|
||||
const int kWindowSec = 3600;
|
||||
struct Entry { time_t lastLog; unsigned pending; };
|
||||
static std::mutex s_mtx;
|
||||
static std::map<std::string, Entry> s_cache;
|
||||
|
||||
*suppressed = 0;
|
||||
const std::string key = std::string(kind) + "|" + peer;
|
||||
const time_t now = time(nullptr);
|
||||
|
||||
std::lock_guard<std::mutex> lk(s_mtx);
|
||||
|
||||
// 对端 IP 随机轮换时表会无限增长:超过阈值先清过期项,仍有异常量则整体重置。
|
||||
if (s_cache.size() > 2048) {
|
||||
for (auto it = s_cache.begin(); it != s_cache.end(); ) {
|
||||
if (now - it->second.lastLog >= kWindowSec)
|
||||
it = s_cache.erase(it);
|
||||
else
|
||||
++it;
|
||||
}
|
||||
if (s_cache.size() > 4096)
|
||||
s_cache.clear();
|
||||
}
|
||||
|
||||
auto it = s_cache.find(key);
|
||||
if (it == s_cache.end()) {
|
||||
s_cache[key] = Entry{ now, 0 };
|
||||
return true;
|
||||
}
|
||||
if (now - it->second.lastLog >= kWindowSec) {
|
||||
*suppressed = it->second.pending;
|
||||
it->second.lastLog = now;
|
||||
it->second.pending = 0;
|
||||
return true;
|
||||
}
|
||||
it->second.pending++;
|
||||
return false;
|
||||
}
|
||||
|
||||
// 握手线程参数:把嗅探阶段已收到的 ClientHello 字节与连接上下文一起移交。
|
||||
struct TlsHandshakeParam {
|
||||
IOCPServer* server;
|
||||
@@ -920,11 +971,15 @@ DWORD WINAPI IOCPServer::TlsHandshakeThreadProc(LPVOID lParam)
|
||||
}
|
||||
|
||||
// 握手期间做同步 recv/send。注意:监听套接字经 WSAEventSelect 置为非阻塞后,accept 出的
|
||||
// 子套接字继承非阻塞态,FIONBIO=0 不一定能切回阻塞;ServerTlsSession::Handshake 内部已用
|
||||
// select 兜底 WSAEWOULDBLOCK,故这里仍设 FIONBIO=0 只是「尽量阻塞」的优化。
|
||||
// 子套接字继承非阻塞态,FIONBIO=0 切不回阻塞模式(文档明确如此,返回 WSAEINVAL);
|
||||
// ServerTlsSession::Handshake 内部已用 select 兜底 WSAEWOULDBLOCK,故这里只是
|
||||
// 「尽量阻塞」的优化,WSAEINVAL 属预期结果,不记日志。其它错误码才值得关注。
|
||||
u_long nb = 0;
|
||||
if (ioctlsocket(ctx->sClientSocket, FIONBIO, &nb) == SOCKET_ERROR)
|
||||
Mprintf("[TLS] handshake: ioctlsocket(FIONBIO=0) failed err=%d\n", WSAGetLastError());
|
||||
if (ioctlsocket(ctx->sClientSocket, FIONBIO, &nb) == SOCKET_ERROR) {
|
||||
int err = WSAGetLastError();
|
||||
if (err != WSAEINVAL)
|
||||
Mprintf("[TLS] handshake: ioctlsocket(FIONBIO=0) failed err=%d\n", err);
|
||||
}
|
||||
int tmo = 15000; // 15s 握手超时
|
||||
setsockopt(ctx->sClientSocket, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tmo, sizeof(tmo));
|
||||
setsockopt(ctx->sClientSocket, SOL_SOCKET, SO_SNDTIMEO, (const char*)&tmo, sizeof(tmo));
|
||||
@@ -969,6 +1024,17 @@ DWORD WINAPI IOCPServer::TlsHandshakeThreadProc(LPVOID lParam)
|
||||
}
|
||||
}
|
||||
|
||||
// 失败信息必须在释放引用之前取好:fetch_sub 之后对象可能被其它线程回收/复用(#215),
|
||||
// 此时再解引用 ctx->tls 会读到已释放的会话。
|
||||
bool logHandshakeFailure = false;
|
||||
std::string failPeer, failReason;
|
||||
if (!ok && (ctx->tls == NULL || !ctx->tls->Ready())) {
|
||||
logHandshakeFailure = true;
|
||||
failPeer = ctx->GetPeerName();
|
||||
failReason = (ctx->tls != NULL && !ctx->tls->LastError().empty())
|
||||
? ctx->tls->LastError() : "handshake failed";
|
||||
}
|
||||
|
||||
ctx->IoRefCount.fetch_sub(1);
|
||||
|
||||
if (ok) {
|
||||
@@ -976,7 +1042,19 @@ DWORD WINAPI IOCPServer::TlsHandshakeThreadProc(LPVOID lParam)
|
||||
This->PostRecv(ctx); // 交还 IOCP 接收
|
||||
}
|
||||
} else {
|
||||
Mprintf("[TLS] handshake failed for %s (fail-closed)\n", ctx->GetPeerName().c_str());
|
||||
// 握手本身失败时输出一行汇总(含具体原因,按对端限流);握手成功但首包解密/
|
||||
// 解析失败的情况已在各自分支记录过,这里不再重复。
|
||||
if (logHandshakeFailure) {
|
||||
unsigned suppressed = 0;
|
||||
if (ShouldLogHandshakeFailure("TLS", failPeer, &suppressed)) {
|
||||
if (suppressed)
|
||||
Mprintf("[TLS] handshake failed for %s: %s (fail-closed, +%u suppressed)\n",
|
||||
failPeer.c_str(), failReason.c_str(), suppressed);
|
||||
else
|
||||
Mprintf("[TLS] handshake failed for %s: %s (fail-closed)\n",
|
||||
failPeer.c_str(), failReason.c_str());
|
||||
}
|
||||
}
|
||||
This->RemoveStaleContext(ctx);
|
||||
}
|
||||
return 0;
|
||||
@@ -1039,9 +1117,13 @@ DWORD WINAPI IOCPServer::WsHandshakeThreadProc(LPVOID lParam)
|
||||
}
|
||||
|
||||
// 与 TLS 握手线程相同的 socket 准备工作:尽量切回阻塞,并加超时,避免慢速对端拖住线程。
|
||||
// WSAEINVAL 是预期结果(见 TLS 分支注释),不记日志。
|
||||
u_long nb = 0;
|
||||
if (ioctlsocket(ctx->sClientSocket, FIONBIO, &nb) == SOCKET_ERROR)
|
||||
Mprintf("[WS] handshake: ioctlsocket(FIONBIO=0) failed err=%d\n", WSAGetLastError());
|
||||
if (ioctlsocket(ctx->sClientSocket, FIONBIO, &nb) == SOCKET_ERROR) {
|
||||
int err = WSAGetLastError();
|
||||
if (err != WSAEINVAL)
|
||||
Mprintf("[WS] handshake: ioctlsocket(FIONBIO=0) failed err=%d\n", err);
|
||||
}
|
||||
int tmo = 15000; // 15s 升级超时
|
||||
setsockopt(ctx->sClientSocket, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tmo, sizeof(tmo));
|
||||
setsockopt(ctx->sClientSocket, SOL_SOCKET, SO_SNDTIMEO, (const char*)&tmo, sizeof(tmo));
|
||||
@@ -1054,8 +1136,18 @@ DWORD WINAPI IOCPServer::WsHandshakeThreadProc(LPVOID lParam)
|
||||
ctx->ws->Handshake(ctx->sClientSocket, tlsReady ? ctx->tls : NULL, initial,
|
||||
pending, ctx->GetPeerName(), &err);
|
||||
if (!ok) {
|
||||
Mprintf("[WS] upgrade failed for %s: %s (fail-closed)\n",
|
||||
ctx->GetPeerName().c_str(), err.c_str());
|
||||
// 公网端口上的 HTTP/半截握手探测会大量触发这条路径:按对端限流成一行/小时,
|
||||
// 原因(缺 Upgrade/Connection/Sec-WebSocket-Key、超时等)照常输出,不再逐条刷屏。
|
||||
unsigned suppressed = 0;
|
||||
const char* why = err.empty() ? "unknown" : err.c_str();
|
||||
if (ShouldLogHandshakeFailure("WS", ctx->GetPeerName(), &suppressed)) {
|
||||
if (suppressed)
|
||||
Mprintf("[WS] upgrade failed for %s: %s (fail-closed, +%u suppressed)\n",
|
||||
ctx->GetPeerName().c_str(), why, suppressed);
|
||||
else
|
||||
Mprintf("[WS] upgrade failed for %s: %s (fail-closed)\n",
|
||||
ctx->GetPeerName().c_str(), why);
|
||||
}
|
||||
}
|
||||
|
||||
bool wsTookOver = false;
|
||||
|
||||
Reference in new issue
Block a user