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
97 lines
4.5 KiB
C++
97 lines
4.5 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);
|
||
// 流控回调(运行在该连接的中继线程内):主连接发送队列堆积时暂停读取本地连接
|
||
static BOOL CALLBACK CanRecvProc(void* user, CONNID id);
|
||
|
||
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 void OnTimer(UINT_PTR nIDEvent);
|
||
afx_msg LRESULT OnProxyLog(WPARAM wParam, LPARAM lParam);
|
||
DECLARE_MESSAGE_MAP()
|
||
|
||
private:
|
||
// SOCKS5 握手的阶段。只有 ST_TUNNEL 之后流里才是裸数据。
|
||
enum {
|
||
ST_GREETING = 0, // 等客户端的 VER/NMETHODS
|
||
ST_REQUEST = 1, // 已回「无需认证」,等 CONNECT 请求
|
||
ST_CONNECTING = 2, // CONNECT 已交给客户端,等它回结果
|
||
ST_TUNNEL = 3 // 隧道已建立,字节原样转发
|
||
};
|
||
|
||
// 每条本机连接的「本地侧」状态。
|
||
struct LocalSession {
|
||
int state = ST_GREETING;
|
||
bool connectSent = false; // 是否已向客户端下发过 CONNECT(决定断开时要不要回 CLOSE)
|
||
ULONGLONG lastTick = 0; // 最近一次收到数据 / 状态推进的时刻(GetTickCount64)
|
||
// 隧道字节数(连接结束时打一行汇总)。分片级日志会淹没界面、拖慢 UI 线程,
|
||
// 300MB 的下载能产生上万条消息。
|
||
ULONGLONG bytesToTarget = 0; // 本机 -> 客户端
|
||
ULONGLONG bytesFromTarget = 0; // 客户端 -> 本机
|
||
ULONGLONG lastLogTick = 0; // 上次打印进度日志的时刻
|
||
// 握手阶段攒下的字节。TCP 是流:一次 recv 可能是半条消息,也可能连隧道数据一起
|
||
// 带过来,所以先攒着,按状态机逐条切分。
|
||
std::vector<BYTE> pending;
|
||
};
|
||
|
||
void OnLocalConnect(CONNID id);
|
||
void OnLocalReceive(CONNID id, const BYTE* pData, UINT nLen);
|
||
void OnLocalDisconnect(CONNID id);
|
||
// 按 SOCKS5 字节流推进握手。需要下发的报文放进 toLocal(回本机客户端)/
|
||
// toClient(发给客户端的 CONNECT),由调用方在锁外发送。
|
||
// 返回 false 表示协议不符,调用方应断开这条连接。
|
||
bool ParseSocks5(CONNID id, LocalSession& s, std::vector<BYTE>& toLocal, std::vector<BYTE>& toClient);
|
||
|
||
// 组 [token][id:4][payload] 发给客户端(与客户端 CProxyManager 的协议一致)。
|
||
BOOL SendProxyPacket(BYTE token, CONNID id, const BYTE* pData, UINT nLen);
|
||
void LogLine(bool other, LPCTSTR text);
|
||
// 日志框是只读的(EM_REPLACESEL 无效),文本先在 buffer 里累积,再由 FlushLog
|
||
// 合并刷进控件(每行都 SetWindowText 会拖死 UI 线程)。
|
||
void AppendLog(CString& buffer, LPCTSTR text);
|
||
void FlushLog();
|
||
|
||
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;
|
||
bool m_bLogDirtyMain = false;
|
||
bool m_bLogDirtyOther = false;
|
||
bool m_bLogFlushPending = false;
|
||
};
|