Feature: Terminate TLS for the MCP endpoint inside YAMA
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
This commit is contained in:
1 parent
b4cb34fea9
commit
a7dd86ff0f
15 files changed
+1218
-96
No files matched your search
@@ -27,6 +27,8 @@
|
||||
#define min(a,b) (((a) < (b)) ? (a) : (b))
|
||||
#endif
|
||||
|
||||
#include "McpTlsProxy.h"
|
||||
|
||||
#pragma comment(lib, "ws2_32.lib")
|
||||
|
||||
class CMy2015RemoteDlg;
|
||||
@@ -58,6 +60,18 @@ public:
|
||||
void SetParentDlg(CMy2015RemoteDlg* pDlg) { m_parent = pDlg; }
|
||||
void SetToken(const std::string& token) { m_token = token; }
|
||||
|
||||
// HTTPS(native TLS):开启后 httplib 退到 127.0.0.1:internalPort,对外由 McpTlsProxy
|
||||
// 在 bind:port 上同端口嗅探(TLS 解密 / 明文直通)。internalPort <= 0 时取 port+1。
|
||||
void SetHttps(bool enabled, const std::string& certPath, const std::string& keyPath,
|
||||
int internalPort) {
|
||||
m_httpsEnabled = enabled;
|
||||
m_certPath = certPath;
|
||||
m_keyPath = keyPath;
|
||||
m_internalPort = internalPort;
|
||||
}
|
||||
bool IsHttps() const { return m_httpsEnabled; }
|
||||
int GetInternalPort() const { return m_internalPort; }
|
||||
|
||||
bool Start(const std::string& bind, int port);
|
||||
void Stop();
|
||||
bool IsRunning() const { return m_running.load(); }
|
||||
@@ -501,11 +515,16 @@ private:
|
||||
PendingKind kind, context* mainCtx);
|
||||
|
||||
httplib::Server m_server;
|
||||
McpTlsProxy m_tlsProxy; // HTTPS 前端(仅在 m_httpsEnabled 时启动)
|
||||
std::thread m_thread;
|
||||
std::atomic<bool> m_running{false};
|
||||
std::string m_token;
|
||||
std::string m_bind; // Start 传入的监听地址
|
||||
int m_port = 0; // Start 传入的监听端口
|
||||
bool m_httpsEnabled = false; // SetHttps 设置(默认明文)
|
||||
std::string m_certPath; // MCP TLS 证书(PEM)
|
||||
std::string m_keyPath; // MCP TLS 私钥(PEM, PKCS#8)
|
||||
int m_internalPort = 0; // httplib 实际监听的回环端口
|
||||
CMy2015RemoteDlg* m_parent = nullptr;
|
||||
|
||||
// 挂起请求注册表(受 m_PendingMutex 保护,键 = device_id)。
|
||||
|
||||
Reference in new issue
Block a user