Feature: switch hosts to the experimental UDP and KCP transports
The switch only offered direct TCP, WS and WSS; UDP and KCP are now experimental menu entries, and the radio item follows the real transport. The server maps ghost/UDPOption to real listeners (a "UDP;KCP" setup used to collapse to one UDP port). A switch sends the master address and that transport's port, refused behind an upper-level FRP. The client persists the override, recreates its connection object when the transport changes, and disconnects an unacknowledged switch after 60 s so the 300 s revert can run. TCP/WS/WSS behaviour is unchanged. Reconnecting a KCP client reused the same object: a fresh std::thread over the old one aborted the process and leaked the ikcp session; ConnectServer now joins the update thread and releases the session first, and the UDP one also clears the stale login-ack flag. KCP contexts no longer touch the shared listener socket: CancelIO is a no-op, Disconnect looks up by IP:port and defers deletion behind the receive thread's reference, COMMAND_BYE leaves the socket alone, and Disconnect stays silent like UDP's. Oversized messages are delivered in chunks. A datagram host has no disconnect event, so a new login for the same id was parked as standby for up to 120 s while the client gave up after 60 s; a UDP/KCP or different-transport login now replaces it immediately. The new labels, prompts and settings hint are translated in the en_US/de_DE/zh_TW packs; UDPOption also accepts 0/1 in port order. Verified: Debug build; switch directions plus KCP delete/reconnect/file-transfer tested. Co-Authored-By: deepseek-flash
This commit is contained in:
1 parent
914553ab4e
commit
dfac2b5179
21 files changed
+534
-91
No files matched your search
+50
-1
@@ -767,7 +767,9 @@ const int kProtoSwitchRevertSeconds = 300; // 新入口 5 分钟内登不上
|
||||
|
||||
bool IsSwitchableProto(int p)
|
||||
{
|
||||
return p == PROTO_TCP || p == PROTO_WS || p == PROTO_WSS;
|
||||
// UDP/KCP 是实验传输:主控在 ghost/UDPOption 里配置了对应端口才可切。
|
||||
return p == PROTO_TCP || p == PROTO_WS || p == PROTO_WSS
|
||||
|| p == PROTO_UDP || p == PROTO_KCP;
|
||||
}
|
||||
|
||||
// 本进程首次见到的入口(外壳注入的直连入口)。用于识别「客户端被换装/重新生成」:
|
||||
@@ -996,6 +998,12 @@ DWORD WINAPI StartClient(LPVOID lParam)
|
||||
State& bExit(app.g_bExit);
|
||||
IOCPClient *ClientObject = NewNetClient(&settings, bExit, pubIP);
|
||||
if (nullptr == ClientObject) return -1;
|
||||
// 当前主连接对象是按哪种协议创建的:NewNetClient 只在进 StartClient 时选一次类,
|
||||
// 协议切到 UDP/KCP(不同的传输类)后必须重建,WS/WSS 则复用同一个 IOCPClient。
|
||||
// 记录“实际类”而不是配置值:PROTO_RANDOM 的客户端可能随机实例化出 UDP 对象。
|
||||
int createdProto = (unsigned char)settings.protoType;
|
||||
bool createdDatagram = ClientObject->IsDatagramTransport();
|
||||
bool createdKcp = ClientObject->IsKcpTransport();
|
||||
// 主连接:登录被接受后发布头加密类型;上一条连接没被登录接受时,重连换下一种类型
|
||||
// 轮转探测。其余 IOCPClient(文件/进程/屏幕等子连接)都跟随这里发布的值。
|
||||
ClientObject->MarkMainLink();
|
||||
@@ -1046,6 +1054,39 @@ DWORD WINAPI StartClient(LPVOID lParam)
|
||||
// 过主控的入口优先)。
|
||||
if (!assigned && !app.m_bShared)
|
||||
ApplyProtoSwitch(cfg, settings);
|
||||
// 传输类变化(TCP 系 ↔ UDP/KCP,以及 UDP ↔ KCP)必须重建主连接对象:
|
||||
// WS/WSS 能在同一个 IOCPClient 里生效(每次连接重读 protoType),而 UDP/KCP
|
||||
// 是不同的客户端类,不重建会以 TCP 连到实验端口(每个端口都有 TCP 监听),
|
||||
// 表现为"切换成功但列表仍显示 TCP"。
|
||||
// - 只在显式协议变化(protoNow != createdProto)时比较,PROTO_RANDOM 不重建;
|
||||
// - 不排除 assigned:转移可能把入口换成 TCP/WS/WSS,旧的数据报对象必须换掉。
|
||||
{
|
||||
const int protoNow = (unsigned char)settings.protoType;
|
||||
if (!app.m_bShared && protoNow != createdProto && protoNow != PROTO_RANDOM) {
|
||||
const bool wantDatagram = (protoNow == PROTO_UDP || protoNow == PROTO_KCP);
|
||||
const bool wantKcp = (protoNow == PROTO_KCP);
|
||||
if (wantDatagram != createdDatagram || (wantDatagram && wantKcp != createdKcp)) {
|
||||
Mprintf("[proto-switch] transport class changes (proto %d, datagram %d/%d -> %d/%d), recreating main client\n",
|
||||
protoNow, (int)createdDatagram, (int)createdKcp, (int)wantDatagram, (int)wantKcp);
|
||||
SAFE_DELETE(Manager); // 旧连接的管理器可能仍引用旧对象
|
||||
delete ClientObject; // 析构会停工作线程、清理 socket
|
||||
ClientObject = NewNetClient(&settings, bExit, pubIP);
|
||||
if (nullptr == ClientObject) return -1;
|
||||
ClientObject->MarkMainLink();
|
||||
createdProto = protoNow;
|
||||
createdDatagram = ClientObject->IsDatagramTransport();
|
||||
createdKcp = ClientObject->IsKcpTransport();
|
||||
}
|
||||
}
|
||||
}
|
||||
// 协议切换待确认:UDP/KCP 没有握手,"已连接"永远为真;远端静默丢弃数据时
|
||||
// 内层心跳循环不会退出,ApplyProtoSwitch 的 300 秒回退检查就跑不到。这里
|
||||
// 记下本轮是否在等新入口的登录确认,由下面的心跳循环兜底断开。
|
||||
const bool switchPending =
|
||||
!assigned && !app.m_bShared &&
|
||||
!cfg.GetStr("settings", "proto_override").empty() &&
|
||||
cfg.GetStr("settings", "entry_try_at") != "0" &&
|
||||
!cfg.GetStr("settings", "entry_try_at").empty();
|
||||
if (!ClientObject->ConnectServer(settings.ServerIP(), settings.ServerPort())) {
|
||||
Mprintf("[ConnectServer] ---> %s:%d.\n", settings.ServerIP(), settings.ServerPort());
|
||||
for (int k = 300+(IsDebug ? rand()%600:rand()%6000); app.m_bIsRunning(&app) && --k; Sleep(10));
|
||||
@@ -1080,6 +1121,14 @@ DWORD WINAPI StartClient(LPVOID lParam)
|
||||
// 不放在循环前一次判定:MasterSettings 可能比第一轮心跳晚到。
|
||||
if (ClientObject->IsConnected() && ClientObject->IsLoginAcked())
|
||||
ConfirmProtoSwitch(cfg);
|
||||
// 新入口 60 秒还没被登录接受就主动断开,让外层循环执行 300 秒回退检查。
|
||||
// 没有这条,UDP/KCP 在"连得上但服务端不回包"时 IsConnected 永远为真,
|
||||
// 主机会一直挂在错误入口,只能重启进程。
|
||||
if (switchPending && !ClientObject->IsLoginAcked() &&
|
||||
GetTickCount64() - dwTickCount > 60 * 1000) {
|
||||
Mprintf("[proto-switch] new entry not acked in 60s, disconnecting to trigger revert\n");
|
||||
ClientObject->Disconnect();
|
||||
}
|
||||
// 连接期间到期也要立刻复原;不可达的情况由外层循环开头兜底。
|
||||
restoreExpiredAssignment();
|
||||
} while (ClientObject->IsRunning() && ClientObject->IsConnected() && app.m_bIsRunning(&app));
|
||||
|
||||
Reference in new issue
Block a user