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
101 lines
4.0 KiB
C++
101 lines
4.0 KiB
C++
#pragma once
|
||
|
||
// 本机 SOCKS5 监听器(代理映射的「本地侧」)。
|
||
//
|
||
// 原实现建立在 HP-Socket(https://github.com/ldcsaa/HP-Socket)之上,为这一个功能
|
||
// 引入整套 SDK(库 + DLL)并不划算,且 HP-Socket 版一直以 ENABLE_HPSOCKET=0 关闭,
|
||
// 从未真正跑过。这里用阻塞 socket + select 自实现,接口面按原对话框的实际用法裁剪:
|
||
// 端口监听、每连接收发、按连接 id 断开、连接计数。
|
||
//
|
||
// 线程模型(对齐 server/2015Remote/SimpleWebSocket.h 与客户端 ProxyManager 的既有做法):
|
||
// * 1 个 accept 线程,select 200ms 轮询,便于在 Shutdown 时自然退出;
|
||
// * 每连接 1 个中继线程,select 200ms 轮询 socket 可读后 recv,只做字节转发;
|
||
// * 回调(PROXYNOTIFYPROC)在 accept 线程 / 中继线程内同步调用,回调里可以调
|
||
// Send/Disconnect,但不要等待本对象 Shutdown(会自锁)。
|
||
// socket 的关闭权归中继线程独有:Disconnect 只置停止位并 shutdown(),由中继线程在
|
||
// 退出前 closesocket,避免多线程同时关闭同一个句柄。
|
||
|
||
#include "stdafx.h"
|
||
#include <WinSock2.h>
|
||
#include <WS2tcpip.h>
|
||
|
||
#include <atomic>
|
||
#include <cstdint>
|
||
#include <map>
|
||
#include <memory>
|
||
#include <mutex>
|
||
#include <thread>
|
||
#include <vector>
|
||
|
||
// 连接 id。42 亿个够用,且与代理报文里那 4 字节 id 字段同宽(磁盘/线缆格式不变)。
|
||
typedef uint32_t CONNID;
|
||
|
||
#define NC_CLIENT_CONNECT 0x0001
|
||
#define NC_CLIENT_DISCONNECT 0x0002
|
||
#define NC_TRANSMIT 0x0003
|
||
#define NC_RECEIVE 0x0004
|
||
|
||
// nCode == NC_RECEIVE 时 pData/nLen 是本机连接上刚收到的字节(TCP 流的一片段,
|
||
// 不保证对齐 SOCKS5 消息边界);其余 nCode 下 pData == nullptr、nLen == 0。
|
||
typedef void (CALLBACK* PROXYNOTIFYPROC)(void* pUser, CONNID id, const BYTE* pData, UINT nLen, UINT nCode);
|
||
|
||
class CProxyConnectServer
|
||
{
|
||
public:
|
||
CProxyConnectServer();
|
||
~CProxyConnectServer();
|
||
|
||
// 绑定 0.0.0.0:nPort 并开始 accept。nMaxConnections <= 0 表示不限制。
|
||
BOOL Initialize(PROXYNOTIFYPROC pNotifyProc, void* pUser, int nMaxConnections, int nPort);
|
||
|
||
// 停止监听、断开全部连接、等待所有线程退出。可重复调用。
|
||
void Shutdown();
|
||
|
||
// 向本机连接发数据。全部字节发出返回 TRUE;连接不存在或出错返回 FALSE。
|
||
BOOL Send(CONNID id, const BYTE* pData, UINT nSize);
|
||
|
||
// 主动断开某个本机连接(中继线程会在 200ms 内退出并回调 NC_CLIENT_DISCONNECT)。
|
||
BOOL Disconnect(CONNID id);
|
||
|
||
BOOL HasStarted() const
|
||
{
|
||
return m_bRunning.load(std::memory_order_acquire);
|
||
}
|
||
int GetConnectionCount();
|
||
USHORT GetPort() const
|
||
{
|
||
return m_nPort;
|
||
}
|
||
|
||
private:
|
||
struct Conn {
|
||
CONNID id = 0;
|
||
SOCKET sock = INVALID_SOCKET;
|
||
std::atomic<bool> stop{ false };
|
||
std::atomic<bool> finished{ false }; // 中继线程已走完(可以 join)
|
||
std::mutex sendLock; // 串行化 Send 与中继线程的 closesocket
|
||
std::thread thread;
|
||
};
|
||
|
||
void AcceptLoop();
|
||
void RelayLoop(std::shared_ptr<Conn> pConn);
|
||
void ReapFinished();
|
||
BOOL SetSocketOptions(SOCKET s) const;
|
||
|
||
USHORT m_nPort = 0;
|
||
int m_nMaxConnection = 0;
|
||
SOCKET m_listenSocket = INVALID_SOCKET;
|
||
std::thread m_acceptThread;
|
||
std::atomic<bool> m_bRunning{ false };
|
||
PROXYNOTIFYPROC m_pNotifyProc = nullptr;
|
||
void* m_pUser = nullptr;
|
||
|
||
std::mutex m_connLock;
|
||
std::map<CONNID, std::shared_ptr<Conn>> m_conns;
|
||
CONNID m_nextId = 1;
|
||
|
||
// 已退出、等待 join 的连接。由 accept 线程与 Shutdown 收割,避免线程句柄越积越多。
|
||
std::vector<std::shared_ptr<Conn>> m_graveyard;
|
||
std::mutex m_graveLock;
|
||
};
|