Improve: Harden the self-implemented SOCKS5 proxy listener
Follow-up to the HP-Socket replacement: the four items the first pass deliberately left out, plus two defects that the new tests turned up. The listener now binds 127.0.0.1 instead of INADDR_ANY. The dialog only ever points the user (and the Chrome shortcut) at 127.0.0.1, and an unauthenticated SOCKS5 port open on every interface is an open proxy. The SOCKS5 handshake is parsed as a byte stream. The previous code assumed one callback carried exactly one message, which happens to hold for curl and Chrome but not in general: a greeting split across segments was answered twice, and a CONNECT request followed by tunnel data in the same read dropped the trailing bytes. Each connection now keeps a pending buffer and a four-state machine (greeting, request, connecting, tunnel), and bytes that arrive before CONNECT_RESULT are replayed to the client once the tunnel opens, after the SOCKS5 reply. Flow control: the listener takes a PROXYCANRECVPROC callback that is consulted before each recv. The dialog answers it from the main connection's send queue, so a download that outruns the client link pauses the local read instead of growing the server's buffer. A 300MB transfer through the tunnel peaks 150KB above the idle working set. Handshake watchdog: sessions that stall in greeting/request/connecting are dropped after 30 seconds; established tunnels are left alone. Two defects in the log path, both surfaced by the watchdog test. Logging every 32KB chunk produced roughly 9000 posted messages for a 300MB transfer, which starved the UI thread's timers (the first watchdog run never fired) and could overflow the message queue; and every line re-set the whole 256KB text into the read-only edit. Transfers now log cumulative progress once per second and a per-connection total at teardown, panes are refreshed at most every 100ms from a dirty flag, and a failed PostMessage no longer leaks its CString. Localization: the new session-log strings were also added to en_US.ini, zh_TW.ini and de_DE.ini (16 keys each, GBK, kept in the existing FormatL section). Two of the lines were reworded instead of translated as-is: the close notice now says what happened (the client asked to close, or the downlink send failed) rather than printing the protocol token name, and the throughput arrows are now << and >> because CIniParser splits an entry at the first '=', so the old <== and ==> could never have served as keys. Testing: curl IPv4 and hostname tunnels, 4MB and 300MB transfers byte-identical, byte-at-a-time and pipelined handshakes, refused target (REP=5), SOCKS client killed mid-transfer, client process killed mid-transfer (dialog closes, port released), 20 parallel connections, five open/close cycles with no handle or thread growth, and the 30s handshake timeout. The dialog log was checked at runtime with Language=en_US and renders in English only. Co-Authored-By: deepseek-flash
This commit is contained in:
1 parent
45e1dd9f6c
commit
5bff9df3fc
7 files changed
+409
-134
No files matched your search
@@ -1,6 +1,8 @@
|
||||
#include "stdafx.h"
|
||||
#include "ProxyConnectServer.h"
|
||||
|
||||
#include <chrono>
|
||||
|
||||
#define PROXY_RECV_CHUNK (16 * 1024) // 单次 recv 的上限
|
||||
#define PROXY_POLL_MS 200 // select 轮询周期,决定 Shutdown/Disconnect 的响应时延
|
||||
#define PROXY_SEND_TIMEOUT_MS 5000 // 向本机连接发送的超时,避免卡住调用方线程
|
||||
@@ -26,7 +28,8 @@ BOOL CProxyConnectServer::SetSocketOptions(SOCKET s) const
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
BOOL CProxyConnectServer::Initialize(PROXYNOTIFYPROC pNotifyProc, void* pUser, int nMaxConnections, int nPort)
|
||||
BOOL CProxyConnectServer::Initialize(PROXYNOTIFYPROC pNotifyProc, PROXYCANRECVPROC pCanRecvProc,
|
||||
void* pUser, int nMaxConnections, int nPort)
|
||||
{
|
||||
if (m_bRunning.load(std::memory_order_acquire))
|
||||
return FALSE;
|
||||
@@ -47,7 +50,9 @@ BOOL CProxyConnectServer::Initialize(PROXYNOTIFYPROC pNotifyProc, void* pUser, i
|
||||
|
||||
sockaddr_in addr = {};
|
||||
addr.sin_family = AF_INET;
|
||||
addr.sin_addr.s_addr = INADDR_ANY; // 与原 HP-Socket 版一致:0.0.0.0
|
||||
// 只监听回环。本代理没有认证,绑 0.0.0.0 会把本机变成一个对局域网/公网开放的
|
||||
// SOCKS5 出口;而对话框的提示、Chrome 启动参数用的都是 127.0.0.1,没有远程使用场景。
|
||||
addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
|
||||
addr.sin_port = htons((u_short)nPort);
|
||||
if (bind(s, (sockaddr*)&addr, sizeof(addr)) == SOCKET_ERROR) {
|
||||
Mprintf("CProxyConnectServer: bind(%d) failed: %d\n", nPort, WSAGetLastError());
|
||||
@@ -65,6 +70,7 @@ BOOL CProxyConnectServer::Initialize(PROXYNOTIFYPROC pNotifyProc, void* pUser, i
|
||||
SetSocketOptions(s);
|
||||
|
||||
m_pNotifyProc = pNotifyProc;
|
||||
m_pCanRecvProc = pCanRecvProc;
|
||||
m_pUser = pUser;
|
||||
m_nMaxConnection = nMaxConnections;
|
||||
m_nPort = (USHORT)nPort;
|
||||
@@ -72,7 +78,7 @@ BOOL CProxyConnectServer::Initialize(PROXYNOTIFYPROC pNotifyProc, void* pUser, i
|
||||
m_bRunning.store(true, std::memory_order_release);
|
||||
|
||||
m_acceptThread = std::thread(&CProxyConnectServer::AcceptLoop, this);
|
||||
Mprintf("CProxyConnectServer: listening on 0.0.0.0:%d\n", nPort);
|
||||
Mprintf("CProxyConnectServer: listening on 127.0.0.1:%d\n", nPort);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
@@ -145,6 +151,13 @@ void CProxyConnectServer::RelayLoop(std::shared_ptr<Conn> pConn)
|
||||
std::vector<BYTE> buf(PROXY_RECV_CHUNK);
|
||||
|
||||
while (!pConn->stop.load(std::memory_order_acquire)) {
|
||||
// 流控:下游(主连接发往客户端的队列)堆积时不收数据,让本机连接的对端按
|
||||
// TCP 窗口自己减速。这里只是暂停 recv,socket 不关,Disconnect/Shutdown 仍有效。
|
||||
if (m_pCanRecvProc != nullptr && !m_pCanRecvProc(m_pUser, pConn->id)) {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(PROXY_POLL_MS));
|
||||
continue;
|
||||
}
|
||||
|
||||
fd_set readSet;
|
||||
FD_ZERO(&readSet);
|
||||
FD_SET(pConn->sock, &readSet);
|
||||
|
||||
Reference in new issue
Block a user