#pragma once #include "stdafx.h" #include "ProxyConnectServer.h" #include "Resource.h" #include #include #include ///////////////////////////////////////////////////////////////////////////// // CProxyMapDlg dialog // // 代理映射的「本地侧」:在本机监听一个 SOCKS5 端口,把浏览器/curl 的连接 // 转发到目标主机的客户端,再由客户端连真正的目标。本文件只处理 SOCKS5 与 // 代理报文,socket 层在 ProxyConnectServer(自实现,不再依赖 HP-Socket)。 // // 代理测试: curl --socks5 127.0.0.1:5543 https://www.example.com class CProxyMapDlg : public DialogBase { public: CProxyMapDlg(CWnd* pParent = NULL, Server* pIOCPServer = NULL, ClientContext* pContext = NULL); virtual ~CProxyMapDlg(); enum { IDD = IDD_PROXY }; // 监听器回调(运行在 accept 线程 / 每连接中继线程内) static void CALLBACK NotifyProc(void* user, CONNID id, const BYTE* pData, UINT nLen, UINT nCode); void OnReceiveComplete(); void OnReceive(); virtual void DoDataExchange(CDataExchange* pDX); virtual void OnCancel(); afx_msg void OnSysCommand(UINT nID, LPARAM lParam); virtual BOOL OnInitDialog(); afx_msg void OnSize(UINT nType, int cx, int cy); afx_msg LRESULT OnProxyLog(WPARAM wParam, LPARAM lParam); DECLARE_MESSAGE_MAP() private: // 每条本机连接在「本地侧」的状态。SOCKS5 握手在这里推进: // 0 待认证(等客户端的 VER/NMETHODS) // 1 已回「无需认证」,等 CONNECT 请求 // 2 隧道(CONNECT 已下发且客户端回连成功),之后全是裸数据 struct LocalSession { int state = 0; bool connectSent = false; // 是否已向客户端下发过 CONNECT(决定断开时要不要回 CLOSE) }; void OnLocalConnect(CONNID id); void OnLocalReceive(CONNID id, const BYTE* pData, UINT nLen); void OnLocalDisconnect(CONNID id); // 组 [token][id:4][payload] 发给客户端(与客户端 CProxyManager 的协议一致)。 BOOL SendProxyPacket(BYTE token, CONNID id, const BYTE* pData, UINT nLen); int GetSessionState(CONNID id); bool SetSessionState(CONNID id, int state); void LogLine(bool other, LPCTSTR text); // 日志框是只读的(EM_REPLACESEL 无效),文本先在 buffer 里累积再整段写入。 void AppendLog(CEdit& edit, CString& buffer, LPCTSTR text); CProxyConnectServer* m_iocpLocal; CEdit m_Edit; CEdit m_EditOther; USHORT m_nPort; std::mutex m_sessLock; std::map m_sessions; // 两个日志框的全文(只在 UI 线程读写)。 CString m_strLog; CString m_strLogOther; };