Files
SimpleRemoter/server/2015Remote/McpTlsProxy.cpp
yuanyuanxianganddeepseek-flash 5ee59415f9 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
2026-12-05 10:50:50 +01:00

443 lines
13 KiB
C++

// McpTlsProxy.cpp — MCP 端口的 TLS 前端实现(Schannel 入站,同端口嗅探)
#include "StdAfx.h"
#include "McpTlsProxy.h"
#include <ws2tcpip.h>
namespace
{
// MCP 身份在 CNG KSP 里的键名基名,与设备端口的 YamaServerTlsKey 区分,避免互相覆盖。
// 实际键名还会拼上证书指纹(见 BuildIdentityFromDer)。
const wchar_t* kMcpKeyName = L"YamaMcpTlsKey";
const size_t kMaxConns = 64; // 并发连接上限(MCP 客户端数量很少,防资源耗尽)
const DWORD kSniffTimeoutMs = 10000; // 首字节嗅探超时:连上不说话的直接断开
}
bool McpTlsProxy::WaitReady(SOCKET sock, bool forRead, int timeoutMs)
{
fd_set fds;
FD_ZERO(&fds);
FD_SET(sock, &fds);
timeval tv;
tv.tv_sec = timeoutMs / 1000;
tv.tv_usec = (timeoutMs % 1000) * 1000;
return select(0, forRead ? &fds : NULL, forRead ? NULL : &fds, NULL, &tv) > 0;
}
bool McpTlsProxy::SendAll(SOCKET sock, const void* data, int len)
{
const char* p = (const char*)data;
int sent = 0;
while (sent < len)
{
int n = send(sock, p + sent, len - sent, 0);
if (n == SOCKET_ERROR)
{
if (WSAGetLastError() == WSAEWOULDBLOCK)
{
if (!WaitReady(sock, false, 30000))
return false;
continue;
}
return false;
}
if (n <= 0)
return false;
sent += n;
}
return true;
}
SOCKET McpTlsProxy::ConnectLoopback(int port)
{
SOCKET s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (s == INVALID_SOCKET)
return INVALID_SOCKET;
sockaddr_in addr = {};
addr.sin_family = AF_INET;
addr.sin_port = htons((u_short)port);
addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
if (connect(s, (sockaddr*)&addr, sizeof(addr)) != 0)
{
closesocket(s);
return INVALID_SOCKET;
}
u_long nb = 1;
ioctlsocket(s, FIONBIO, &nb); // 连接建立后转非阻塞,交给 select 驱动
return s;
}
bool McpTlsProxy::Start(const std::string& bind, int listenPort, int backendPort,
const std::string& certPath, const std::string& keyPath)
{
if (m_running.load())
return true;
if (listenPort <= 0 || backendPort <= 0)
return false;
// 身份必须先加载成功:证书坏了就 fail-closed,绝不以明文对外监听。
if (!ServerTlsLoadIdentityFromPem(certPath, keyPath, kMcpKeyName, m_identity))
{
Mprintf("[McpTls] FATAL: cannot load MCP TLS identity (cert=%s key=%s); "
"refusing to start MCP listener\n", certPath.c_str(), keyPath.c_str());
return false;
}
m_identityLoaded = true;
m_backendPort = backendPort;
addrinfo hints = {};
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
addrinfo* res = nullptr;
char portStr[16] = {};
sprintf_s(portStr, "%d", listenPort);
const char* node = (bind.empty() || bind == "0.0.0.0") ? "0.0.0.0" : bind.c_str();
if (getaddrinfo(node, portStr, &hints, &res) != 0 || res == nullptr)
{
Mprintf("[McpTls] FATAL: cannot resolve bind address %s\n", node);
ServerTlsFreeIdentity(m_identity);
m_identityLoaded = false;
return false;
}
SOCKET ls = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (ls == INVALID_SOCKET)
{
freeaddrinfo(res);
ServerTlsFreeIdentity(m_identity);
m_identityLoaded = false;
return false;
}
BOOL reuse = TRUE;
setsockopt(ls, SOL_SOCKET, SO_REUSEADDR, (const char*)&reuse, sizeof(reuse));
u_long nb = 1;
ioctlsocket(ls, FIONBIO, &nb); // 非阻塞监听,accept 用 select 驱动
if (::bind(ls, res->ai_addr, (int)res->ai_addrlen) != 0 ||
listen(ls, SOMAXCONN) != 0)
{
Mprintf("[McpTls] FATAL: bind/listen %s:%d failed (%d)\n", node, listenPort, WSAGetLastError());
freeaddrinfo(res);
closesocket(ls);
ServerTlsFreeIdentity(m_identity);
m_identityLoaded = false;
return false;
}
freeaddrinfo(res);
m_listen = ls;
m_running.store(true);
m_acceptThread = std::thread(&McpTlsProxy::AcceptLoop, this);
Mprintf("[McpTls] listening on %s:%d (TLS + plaintext passthrough) -> 127.0.0.1:%d\n",
node, listenPort, backendPort);
return true;
}
void McpTlsProxy::Stop()
{
if (!m_running.exchange(false) && m_listen == INVALID_SOCKET && !m_acceptThread.joinable())
{
// 从未启动或已停止
if (m_identityLoaded)
{
ServerTlsFreeIdentity(m_identity);
m_identityLoaded = false;
}
return;
}
SOCKET ls = m_listen;
m_listen = INVALID_SOCKET;
if (ls != INVALID_SOCKET)
{
shutdown(ls, SD_BOTH); // 唤醒阻塞 accept
closesocket(ls);
}
if (m_acceptThread.joinable())
m_acceptThread.join();
std::vector<std::shared_ptr<Conn>> all;
{
std::lock_guard<std::mutex> lk(m_connMtx);
all.swap(m_conns);
}
for (auto& c : all)
{
if (c->done.load())
continue; // 已自行收尾,句柄可能已关闭,勿再触碰
SOCKET cl = c->client.load();
if (cl != INVALID_SOCKET)
shutdown(cl, SD_BOTH);
SOCKET b = c->backend.load();
if (b != INVALID_SOCKET)
shutdown(b, SD_BOTH);
}
for (auto& c : all)
{
if (c->th.joinable())
c->th.join();
}
if (m_identityLoaded)
{
ServerTlsFreeIdentity(m_identity);
m_identityLoaded = false;
}
Mprintf("[McpTls] stopped\n");
}
void McpTlsProxy::SweepFinished()
{
std::vector<std::shared_ptr<Conn>> finished;
{
std::lock_guard<std::mutex> lk(m_connMtx);
for (auto it = m_conns.begin(); it != m_conns.end();)
{
if ((*it)->done.load())
{
finished.push_back(*it);
it = m_conns.erase(it);
}
else
{
++it;
}
}
}
for (auto& c : finished)
{
if (c->th.joinable())
c->th.join();
}
}
void McpTlsProxy::AcceptLoop()
{
while (m_running.load())
{
if (!WaitReady(m_listen, true, 500))
continue; // 超时轮询 m_running,便于 Stop 及时退出
SOCKET cs = accept(m_listen, NULL, NULL);
if (cs == INVALID_SOCKET)
{
if (!m_running.load())
break;
int e = WSAGetLastError();
if (e == WSAEWOULDBLOCK)
continue;
Mprintf("[McpTls] accept failed (%d); listener stops\n", e);
break;
}
SweepFinished();
std::lock_guard<std::mutex> lk(m_connMtx);
if (m_conns.size() >= kMaxConns)
{
closesocket(cs);
continue;
}
u_long nb = 1;
ioctlsocket(cs, FIONBIO, &nb); // 嗅探/泵送全部走 select
auto c = std::make_shared<Conn>();
c->client.store(cs);
m_conns.push_back(c);
c->th = std::thread([this, c]() { ServeConn(c); });
}
}
void McpTlsProxy::CloseConn(Conn& c)
{
SOCKET cl = c.client.exchange(INVALID_SOCKET);
if (cl != INVALID_SOCKET)
{
shutdown(cl, SD_BOTH);
closesocket(cl);
}
SOCKET b = c.backend.exchange(INVALID_SOCKET);
if (b != INVALID_SOCKET)
{
shutdown(b, SD_BOTH);
closesocket(b);
}
c.done.store(true);
}
void McpTlsProxy::ServeConn(std::shared_ptr<Conn> sp)
{
Conn& c = *sp;
SOCKET client = c.client.load();
// ---- 1) 首字节嗅探(同设备端口 §5.2):0x16 0x03 = TLS ClientHello ----
BYTE peek[8] = {};
int pn = 0;
DWORD start = GetTickCount();
for (;;)
{
int r = recv(client, (char*)peek, sizeof(peek), MSG_PEEK);
if (r > 0)
{
pn = r;
break;
}
if (r == 0)
{
CloseConn(c); // 对端直接关闭
return;
}
if (WSAGetLastError() != WSAEWOULDBLOCK)
{
CloseConn(c);
return;
}
if (GetTickCount() - start > kSniffTimeoutMs || !m_running.load())
{
CloseConn(c); // 连上不说话的,直接断开
return;
}
WaitReady(client, true, 200);
}
const bool isTls = (peek[0] == 0x16) && (pn < 2 || peek[1] == 0x03);
// ---- 2) 后端:httplib 只监听 127.0.0.1 的内部端口 ----
SOCKET backend = ConnectLoopback(m_backendPort);
if (backend == INVALID_SOCKET)
{
Mprintf("[McpTls] backend 127.0.0.1:%d not reachable\n", m_backendPort);
CloseConn(c);
return;
}
c.backend.store(backend);
// ---- 3) 分流 ----
if (isTls)
{
// 嗅探用的是 MSG_PEEK(不消费),ClientHello 原样留在内核缓冲里;交多少字节
// HandshakeWithCred 都能自己补齐,这里直接让它从 socket 读完整首条记录。
ServerTlsSession sess;
if (!sess.HandshakeWithCred(client, NULL, 0, m_identity.cred))
{
Mprintf("[McpTls] TLS handshake failed: %s; closing connection\n",
sess.LastError().empty() ? "unknown" : sess.LastError().c_str());
CloseConn(c);
return;
}
PumpTls(c, sess);
}
else
{
PumpRaw(c); // 本机 http://127.0.0.1:port 的既有用法原样透传
}
CloseConn(c);
}
void McpTlsProxy::PumpRaw(Conn& c)
{
char buf[16384];
const SOCKET client = c.client.load();
const SOCKET backend = c.backend.load();
for (;;)
{
fd_set rd;
FD_ZERO(&rd);
FD_SET(client, &rd);
FD_SET(backend, &rd);
int n = select(0, &rd, NULL, NULL, NULL);
if (n <= 0)
break;
if (FD_ISSET(client, &rd))
{
int r = recv(client, buf, sizeof(buf), 0);
if (r == 0) break;
if (r < 0)
{
if (WSAGetLastError() == WSAEWOULDBLOCK) continue;
break;
}
if (!SendAll(backend, buf, r)) break;
}
if (FD_ISSET(backend, &rd))
{
int r = recv(backend, buf, sizeof(buf), 0);
if (r == 0) break;
if (r < 0)
{
if (WSAGetLastError() == WSAEWOULDBLOCK) continue;
break;
}
if (!SendAll(client, buf, r)) break;
}
}
}
void McpTlsProxy::PumpTls(Conn& c, ServerTlsSession& sess)
{
char buf[16384];
const SOCKET client = c.client.load();
const SOCKET backend = c.backend.load();
std::vector<BYTE> plain;
std::vector<BYTE> wire;
// 握手阶段已经收到的明文要先送给后端:客户端复用 TLS 会话时会把 ChangeCipherSpec +
// Finished + 首条请求挤在同一批字节里,握手只会消费前两个,请求留在会话缓冲里。
// 不在这里取走,后端就一个字节也收不到,5s 后按 keep-alive 超时关连接,客户端看到的是
// “fetch failed (other side closed)”(实测 undici/Node 24 稳定复现)。
{
std::vector<BYTE> head;
if (sess.Decrypt(NULL, 0, head) < 0)
return;
if (!head.empty() && !SendAll(backend, head.data(), (int)head.size()))
return;
}
for (;;)
{
fd_set rd;
FD_ZERO(&rd);
FD_SET(client, &rd);
FD_SET(backend, &rd);
int n = select(0, &rd, NULL, NULL, NULL);
if (n <= 0)
break;
if (FD_ISSET(client, &rd))
{
int r = recv(client, buf, sizeof(buf), 0);
if (r == 0) break;
if (r < 0)
{
if (WSAGetLastError() == WSAEWOULDBLOCK) continue;
break;
}
plain.clear();
if (sess.Decrypt((const BYTE*)buf, r, plain) < 0)
break; // 对端 close_notify / 解密失败
if (!plain.empty() && !SendAll(backend, plain.data(), (int)plain.size()))
break;
}
if (FD_ISSET(backend, &rd))
{
int r = recv(backend, buf, sizeof(buf), 0);
if (r == 0) break;
if (r < 0)
{
if (WSAGetLastError() == WSAEWOULDBLOCK) continue;
break;
}
wire.clear();
if (sess.Encrypt((const BYTE*)buf, r, wire) < 0)
break;
if (!wire.empty() && !SendAll(client, wire.data(), (int)wire.size()))
break;
}
}
}