The proxy mapping dialog ("代理映射") depended on HP-Socket: three vendored
SDK headers (4787 lines) plus #pragma comment(lib) lines pulling in four
prebuilt lib/DLL variants, all guarded by ENABLE_HPSOCKET, which has been 0
since the initial commit -- so the feature was dead code that only answered
"This feature is unavailable due to HPSocket disabled" and no build of this
repository ever ran it.
Replace that dependency with a small listener built on blocking sockets and
select, which is the same approach the client's CProxyManager and the
server's own SimpleWebSocket already take. The accept loop polls every
200 ms so Shutdown can return without closing a socket another thread is
selecting on, and each accepted connection gets a relay thread that only
has to recv and hand bytes to the dialog. Connection sockets are closed by
their relay thread alone; Disconnect only raises a stop flag and calls
shutdown() under the same lock the relay thread takes before closesocket,
so a recycled handle is never shut down by mistake. Finished threads are
parked in a graveyard and joined by the accept thread, which keeps the
thread handle count flat across open/close cycles.
ProxyMapDlg keeps the SOCKS5 state machine it had, but now stores its
per-connection state in a map keyed by the connection id instead of
relying on HP-Socket's GetConnectionExtra, and it logs through a posted
window message rather than touching CEdit from worker threads. The two log
panes in IDD_PROXY are ES_READONLY, so EM_REPLACESEL was silently ignored
(the original code had the same latent bug); the dialog now keeps the pane
text itself and writes it with SetWindowText, bounded to 256 KB per pane.
The vcxproj and filters lose the three SDK headers, and the dialog gate in
OnOpenProxyDialog loses its ENABLE_HPSOCKET check.
Tested against a Debug server with a Debug Windows client on the same
host: curl through the listener succeeds over both the IPv4 and the
hostname ATYP paths, a 4 MB transfer arrives byte-identical, a refused
target maps to SOCKS5 reply 5, killing the client mid-transfer closes the
dialog and releases the port, and five open/close cycles leave the server's
handle and thread counts unchanged.
Co-Authored-By: deepseek-flash
73 lines
2.8 KiB
C++
73 lines
2.8 KiB
C++
#pragma once
|
||
#include "stdafx.h"
|
||
#include "ProxyConnectServer.h"
|
||
#include "Resource.h"
|
||
|
||
#include <map>
|
||
#include <mutex>
|
||
#include <vector>
|
||
|
||
/////////////////////////////////////////////////////////////////////////////
|
||
// 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<CONNID, LocalSession> m_sessions;
|
||
|
||
// 两个日志框的全文(只在 UI 线程读写)。
|
||
CString m_strLog;
|
||
CString m_strLogOther;
|
||
};
|