MCP was served by a plain httplib::Server, so remote clients could only use cleartext HTTP. YAMA now terminates TLS itself: a small front-end owns the MCP port, sniffs the first byte and either runs a Schannel handshake and forwards the decrypted bytes or passes plaintext through, with httplib moving to a loopback-only internal port. No OpenSSL, no frp change, existing local HTTP clients keep working, and an unusable certificate fails closed instead of falling back to cleartext. The identity loader grew the two things a public certificate needs. ECDSA (P-256/P-384/P-521) keys are persisted by importing PKCS#8 with an NCRYPTBUFFER_PKCS_KEY_NAME parameter, the only route MS Software KSP supports for Schannel; RSA keeps its old export-and-repersist path. Fullchains are accepted, and the intermediates are added to the current user's CA store so Schannel, which builds chains only from system stores, actually sends them. The shared handshake also now buffers a full TLS record before the first AcceptSecurityContext, which a partial ClientHello would otherwise poison (SEC_E_UNKNOWN_CREDENTIALS). Session resumption exposed a second trap: when a client resumes a session, ChangeCipherSpec, Finished and the first application record arrive in one segment and AcceptSecurityContext leaves the application record pending in the session. Nothing drained it, because both callers only decrypt when new ciphertext arrives, so the request waited until the peer gave up (the MCP front-end saw no request at all and the client reported "fetch failed (other side closed)"; the device port had the same hole behind its occasional login timeouts). Decrypt now drains pending data on len == 0, and both the front-end and the device port's handshake thread use it before entering their receive loops. Configuration is McpHttps, McpCertPath, McpKeyPath and McpInternalPort, the first three editable in the MCP settings dialog (the certificate rows stay greyed out until HTTPS is enabled and saving rejects missing files). Verified with a real Let's Encrypt ECDSA fullchain (53 tools over TLS, 401 with no or bad token), a local three-level chain whose intermediate was absent from the store, Node 24's fetch on a resumed session (initialize, notifications/initialized and tools/list all complete), and the device port's RSA handshake as a regression check. Details in docs/Mcp_Design.md section 13. Co-Authored-By: deepseek-flash
78 lines
2.8 KiB
C++
78 lines
2.8 KiB
C++
// McpTlsProxy.h — MCP 端口的 TLS 前端(同端口嗅探:TLS 解密 / 明文直通)
|
||
//
|
||
// 背景:CMcpServer 用的 httplib::Server 是明文 HTTP。要让 MCP 客户端经公网用 https
|
||
// 访问,又不引入 OpenSSL 依赖,这里在 MCP 前面加一层基于 Schannel 的 TLS 前端:
|
||
//
|
||
// 客户端 → :McpPort(本类)→ 127.0.0.1:<内部端口>(httplib)
|
||
//
|
||
// 首字节嗅探与设备端口(IOCPServer.cpp §5.2)同一套路:首字节 0x16 且次字节 0x03
|
||
// 视为 TLS ClientHello,走 ServerTlsSession 入站握手;否则按明文原样转发。因此本机
|
||
// 现有的 http://127.0.0.1:6544 用法不需要改动,远端可同时用 https。
|
||
//
|
||
// 身份:独立于设备端口的 [tls] 证书(MCP 面向公网客户端,需公共 CA 证书),
|
||
// 由 ServerTlsLoadIdentityFromPem 加载,CNG 键名 YamaMcpTlsKey,与设备端口互不覆盖。
|
||
#pragma once
|
||
|
||
#include <WinSock2.h>
|
||
|
||
#include <atomic>
|
||
#include <memory>
|
||
#include <mutex>
|
||
#include <string>
|
||
#include <thread>
|
||
#include <vector>
|
||
|
||
#include "ServerTlsSession.h"
|
||
|
||
class ServerTlsSession;
|
||
|
||
class McpTlsProxy
|
||
{
|
||
public:
|
||
McpTlsProxy() {}
|
||
~McpTlsProxy() { Stop(); }
|
||
|
||
McpTlsProxy(const McpTlsProxy&) = delete;
|
||
McpTlsProxy& operator=(const McpTlsProxy&) = delete;
|
||
|
||
// 监听 bind:listenPort,把解密后的字节转发到 127.0.0.1:backendPort。
|
||
// certPath/keyPath 为 PEM(证书 + PKCS#8 私钥)。任一环节失败返回 false(fail-closed)。
|
||
bool Start(const std::string& bind, int listenPort, int backendPort,
|
||
const std::string& certPath, const std::string& keyPath);
|
||
void Stop();
|
||
|
||
bool IsRunning() const { return m_running.load(); }
|
||
|
||
private:
|
||
// 单条前端连接:客户端套接字 + 连到 httplib 的后端套接字。
|
||
// Stop() 需要能打断正在 select/recv 的线程,故后端套接字放在原子量里随时可取。
|
||
struct Conn
|
||
{
|
||
std::atomic<SOCKET> client{ INVALID_SOCKET };
|
||
std::atomic<SOCKET> backend{ INVALID_SOCKET };
|
||
std::atomic<bool> done{ false };
|
||
std::thread th;
|
||
};
|
||
|
||
void AcceptLoop();
|
||
void ServeConn(std::shared_ptr<Conn> c);
|
||
void PumpRaw(Conn& c);
|
||
void PumpTls(Conn& c, ServerTlsSession& sess);
|
||
void SweepFinished();
|
||
void CloseConn(Conn& c);
|
||
|
||
static SOCKET ConnectLoopback(int port);
|
||
static bool WaitReady(SOCKET sock, bool forRead, int timeoutMs);
|
||
static bool SendAll(SOCKET sock, const void* data, int len);
|
||
|
||
SOCKET m_listen = INVALID_SOCKET;
|
||
std::thread m_acceptThread;
|
||
std::atomic<bool> m_running{ false };
|
||
int m_backendPort = 0;
|
||
ServerTlsExtraIdentity m_identity;
|
||
bool m_identityLoaded = false;
|
||
|
||
std::mutex m_connMtx;
|
||
std::vector<std::shared_ptr<Conn>> m_conns;
|
||
};
|