Files
SimpleRemoter/server/2015Remote/SettingDlg.cpp
T
yuanyuanxianganddeepseek-flash dfac2b5179 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
2026-11-29 20:34:08 +01:00

381 lines
16 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// SettingDlg.cpp : 实现文件
//
#include "stdafx.h"
#include "2015Remote.h"
#include "SettingDlg.h"
#include "afxdialogex.h"
#include "client/CursorInfo.h"
#include "common/location.h"
// CSettingDlg 对话框
IMPLEMENT_DYNAMIC(CSettingDlg, CDialog)
CSettingDlg::CSettingDlg(CMy2015RemoteDlg* pParent)
: CDialogLang(CSettingDlg::IDD, pParent)
, m_nListenPort("6543")
, m_nMax_Connect(0)
, m_sScreenCapture(_T("GDI"))
, m_sScreenCompress(_T("算法自适应"))
, m_nReportInterval(5)
, m_sSoftwareDetect(_T("摄像头"))
, m_sPublicIP(_T(""))
, m_sUdpOption(_T("UDP"))
, m_nFrpPort(7000)
, m_sFrpToken(_T(""))
, m_nFileServerPort(-1)
{
g_2015RemoteDlg = pParent;
}
CSettingDlg::~CSettingDlg()
{
}
void CSettingDlg::DoDataExchange(CDataExchange* pDX)
{
__super::DoDataExchange(pDX);
DDX_Text(pDX, IDC_EDIT_PORT, m_nListenPort);
DDV_MaxChars(pDX, m_nListenPort, 32);
DDX_Text(pDX, IDC_EDIT_MAX, m_nMax_Connect);
DDX_Control(pDX, IDC_BUTTON_SETTINGAPPLY, m_ApplyButton);
DDX_Control(pDX, IDC_COMBO_SCREEN_CAPTURE, m_ComboScreenCapture);
DDX_CBString(pDX, IDC_COMBO_SCREEN_CAPTURE, m_sScreenCapture);
DDV_MaxChars(pDX, m_sScreenCapture, 32);
DDX_Control(pDX, IDC_COMBO_SCREEN_COMPRESS, m_ComboScreenCompress);
DDX_CBString(pDX, IDC_COMBO_SCREEN_COMPRESS, m_sScreenCompress);
DDX_Control(pDX, IDC_EDIT_REPORTINTERVAL, m_EditReportInterval);
DDX_Text(pDX, IDC_EDIT_REPORTINTERVAL, m_nReportInterval);
DDV_MinMaxInt(pDX, m_nReportInterval, 0, 3600);
DDX_Control(pDX, IDC_COMBO_SOFTWAREDETECT, m_ComboSoftwareDetect);
DDX_CBString(pDX, IDC_COMBO_SOFTWAREDETECT, m_sSoftwareDetect);
DDV_MaxChars(pDX, m_sSoftwareDetect, 256);
DDX_Control(pDX, IDC_EDIT_PUBLIC_IP, m_EditPublicIP);
DDX_Text(pDX, IDC_EDIT_PUBLIC_IP, m_sPublicIP);
DDV_MaxChars(pDX, m_sPublicIP, 100);
DDX_Control(pDX, IDC_EDIT_UDP_OPTION, m_EditUdpOption);
DDX_Text(pDX, IDC_EDIT_UDP_OPTION, m_sUdpOption);
DDV_MaxChars(pDX, m_sUdpOption, 24);
DDX_Control(pDX, IDC_EDIT_FRP_PORT, m_EditFrpPort);
DDX_Text(pDX, IDC_EDIT_FRP_PORT, m_nFrpPort);
DDV_MinMaxInt(pDX, m_nFrpPort, 1, 65535);
DDX_Control(pDX, IDC_EDIT_FRP_TOKEN, m_EditFrpToken);
DDX_Text(pDX, IDC_EDIT_FRP_TOKEN, m_sFrpToken);
DDV_MaxChars(pDX, m_sFrpToken, 24);
DDX_Control(pDX, IDC_COMBO_VIDEO_WALL, m_ComboVideoWall);
DDX_Control(pDX, IDC_EDIT_FILESERVER_PORT, m_EditFileServerPort);
DDX_Text(pDX, IDC_EDIT_FILESERVER_PORT, m_nFileServerPort);
DDV_MinMaxInt(pDX, m_nFileServerPort, -1, 65535);
}
BEGIN_MESSAGE_MAP(CSettingDlg, CDialog)
ON_BN_CLICKED(IDC_BUTTON_SETTINGAPPLY, &CSettingDlg::OnBnClickedButtonSettingapply)
ON_EN_CHANGE(IDC_EDIT_PORT, &CSettingDlg::OnEnChangeEditPort)
ON_EN_CHANGE(IDC_EDIT_MAX, &CSettingDlg::OnEnChangeEditMax)
ON_BN_CLICKED(IDC_RADIO_ALL_SCREEN, &CSettingDlg::OnBnClickedRadioAllScreen)
ON_BN_CLICKED(IDC_RADIO_MAIN_SCREEN, &CSettingDlg::OnBnClickedRadioMainScreen)
ON_BN_CLICKED(IDC_RADIO_FRP_OFF, &CSettingDlg::OnBnClickedRadioFrpOff)
ON_BN_CLICKED(IDC_RADIO_FRP_ON, &CSettingDlg::OnBnClickedRadioFrpOn)
ON_EN_KILLFOCUS(IDC_EDIT_PUBLIC_IP, &CSettingDlg::OnEnKillfocusEditPublicIp)
END_MESSAGE_MAP()
// CSettingDlg 消息处理程序
BOOL CSettingDlg::OnInitDialog()
{
__super::OnInitDialog();
// 多语言翻译 - Static控件
SetDlgItemText(IDC_STATIC_SET_LISTEN_PORT, _TR("监听端口:"));
SetDlgItemText(IDC_STATIC_SET_MAX_CONN, _TR("最大连接数:"));
SetDlgItemText(IDC_STATIC_SET_TIP1, _TR("操作提示: 1.监听端口支持填写多个,用英文分号分隔;程序同时监听TCP和UDP,且支持基于UDP的KCP;"));
SetDlgItemText(IDC_STATIC_SET_TIP2, _TR("操作提示: 2.如果被控端跨网、地区或国家,务必设置公网IP;勾选FRP反向代理并设置服务端口和 token;"));
SetDlgItemText(IDC_STATIC_SET_TIP3, _TR("操作提示: 3.如果以下载的方式提供上线载荷 (如图片),必须设置Web端口,受管机器上线时会下载载荷。"));
SetDlgItemText(IDC_STATIC_SET_SCREEN_CAP, _TR("屏幕截图方法:"));
SetDlgItemText(IDC_STATIC_SET_IMG_COMP, _TR("图像压缩方法:"));
SetDlgItemText(IDC_STATIC_SET_REPORT_INT, _TR("上报间隔:"));
SetDlgItemText(IDC_STATIC_SET_SW_DETECT, _TR("软件检测:"));
SetDlgItemText(IDC_STATIC_SET_MULTI_MON, _TR("多显示器支持:"));
SetDlgItemText(IDC_STATIC_SET_UDP_PARAM, _TR("UDP协议参数:"));
SetDlgItemText(IDC_STATIC_SET_FRP_PROXY, _TR("FRP 代理:"));
SetDlgItemText(IDC_STATIC_SET_FRP_PORT, _TR("服务端口:"));
SetDlgItemText(IDC_STATIC_SET_TOKEN, _TR("token:"));
SetDlgItemText(IDC_STATIC_SET_VIDEO_WALL, _TR("多屏上墙:"));
SetDlgItemText(IDC_STATIC_SET_DL_PORT, _TR("Web端口:"));
SetDlgItemText(IDC_GROUP_SET_GENERAL, _TR("常规设置"));
SetDlgItemText(IDC_GROUP_SET_DESKTOP, _TR("桌面管理"));
SetDlgItemText(IDC_GROUP_SET_PARAMS, _TR("参数设置"));
// UDP 协议参数与监听端口是按顺序一一对应的,提示行放不下说明,用悬浮提示讲格式。
if (m_tip.Create(this)) {
m_tip.Activate(TRUE);
m_tip.AddTool(&m_EditUdpOption,
_TR("UDP 协议参数与监听端口按顺序一一对应:例如监听端口填 6543;7543,这里就填 UDP;KCP。"));
m_tip.SetMaxTipWidth(480);
}
// 设置对话框标题和控件文本(解决英语系统乱码问题)
SetWindowText(_TR("设置"));
SetDlgItemText(IDOK, _TR("确定"));
SetDlgItemText(IDCANCEL, _TR("取消"));
SetDlgItemText(IDC_BUTTON_SETTINGAPPLY, _TR("应用"));
SetDlgItemText(IDC_RADIO_FRP_OFF, _TR("否"));
SetDlgItemText(IDC_RADIO_FRP_ON, _TR("是"));
SetDlgItemText(IDC_RADIO_ALL_SCREEN, _TR("否"));
SetDlgItemText(IDC_RADIO_MAIN_SCREEN, _TR("是"));
// 检测本机 IP 并设置标签和提示
std::string localPublicIP, localPrivateIP;
g_2015RemoteDlg->m_IPConverter->GetLocalIPs(localPublicIP, localPrivateIP);
std::string frpAutoServer = THIS_CFG.GetStr("frp_auto", "server", "");
BOOL frpEnabled = THIS_CFG.GetInt("frp", "UseFrp");
std::string savedMaster = THIS_CFG.GetStr("settings", "master", "");
if (!localPublicIP.empty()) {
// 场景 1: 本机有公网 IP
SetDlgItemText(IDC_STATIC_SET_PUBLIC_IP, _TR("公网地址:"));
m_sPublicIP = savedMaster.empty() ? localPublicIP.c_str() : savedMaster.c_str();
SetDlgItemText(IDC_STATIC_SET_IP_HINT, _T(""));
} else if (!frpAutoServer.empty() && !(frpEnabled && !savedMaster.empty())) {
// 场景 2: 上级配置了 FRP
// 本地已启用自有 FRP(UseFrp + 已配置主控地址)时不走这里:
// 此时主控地址以本地 FRP 为准,与 GetEffectiveMasterAddress 的规则一致。
SetDlgItemText(IDC_STATIC_SET_PUBLIC_IP, _TR("公网地址:"));
m_sPublicIP = frpAutoServer.c_str();
SetDlgItemText(IDC_STATIC_SET_IP_HINT,
_TR("该地址由上级自动 FRP 提供(端口由上级分配),无需修改"));
} else if (frpEnabled) {
// 场景 3: 启用了本地 FRP(FRP服务器应有公网IP)
SetDlgItemText(IDC_STATIC_SET_PUBLIC_IP, _TR("公网地址:"));
m_sPublicIP = savedMaster.c_str();
SetDlgItemText(IDC_STATIC_SET_IP_HINT, _TR("该地址必须为FRP代理服务器IP"));
} else {
// 场景 4: 无公网,无 FRP(仅限局域网)
SetDlgItemText(IDC_STATIC_SET_PUBLIC_IP, _TR("内网地址:"));
m_sPublicIP = savedMaster.empty() ? localPrivateIP.c_str() : savedMaster.c_str();
SetDlgItemText(IDC_STATIC_SET_IP_HINT, _TR("跨网使用请配置 FRP 反向代理"));
}
m_sOriginalMaster = m_sPublicIP; // 缓存原始值,用于检测修改
std::string nPort = THIS_CFG.GetStr("settings", "ghost", "6543");
// UDPOption 与 ghost 是平行的分号列表("UDP;KCP" 对应 "6543;7543")。这里原样
// 显示、原样写回;不要做"UDP/KCP 二选一"的归一化,否则打开设置点确定就会把
// KCP 端口条目抹掉(本次新增的多端口实验传输依赖它)。
m_sUdpOption = THIS_CFG.GetStr("settings", "UDPOption", "UDP").c_str();
int DXGI = THIS_CFG.GetInt("settings", "DXGI");
CString algo = THIS_CFG.GetStr("settings", "ScreenCompress", ALGORITHM_NULL).c_str();
m_nListenPort = nPort.c_str();
int n = algo.IsEmpty() ? ALGORITHM_DIFF : atoi(algo.GetString());
switch (n) {
case ALGORITHM_NUL:
m_sScreenCompress = _L(_T("算法自适应"));
break;
case ALGORITHM_GRAY:
m_sScreenCompress = _L(_T("灰度图像传输"));
break;
case ALGORITHM_DIFF:
m_sScreenCompress = _L(_T("屏幕差异算法"));
break;
case ALGORITHM_H264:
m_sScreenCompress = _L(_T("H264压缩算法"));
break;
case ALGORITHM_RGB565:
m_sScreenCompress = _L(_T("RGBA->RGB565"));
break;
default:
break;
}
m_ComboScreenCompress.InsertStringL(1+ALGORITHM_NUL, "算法自适应");
m_ComboScreenCompress.InsertStringL(1+ALGORITHM_GRAY, "灰度图像传输");
m_ComboScreenCompress.InsertStringL(1+ALGORITHM_DIFF, "屏幕差异算法");
m_ComboScreenCompress.InsertStringL(1+ALGORITHM_H264, "H264压缩算法");
m_ComboScreenCompress.InsertStringL(1+ALGORITHM_RGB565, "RGBA->RGB565");
m_ComboScreenCapture.InsertStringL(0, "GDI");
m_ComboScreenCapture.InsertStringL(1, "DXGI");
m_ComboScreenCapture.InsertStringL(2, "VIRTUAL");
m_sScreenCapture = DXGI==1 ? "DXGI" : (DXGI == 2 ? "VIRTUAL" : "GDI");
m_ComboSoftwareDetect.InsertStringL(SOFTWARE_CAMERA, "摄像头");
m_ComboSoftwareDetect.InsertStringL(SOFTWARE_TELEGRAM, "电报");
auto str = THIS_CFG.GetStr("settings", "ReportInterval", "5");
m_nReportInterval = atoi(str.c_str());
n = THIS_CFG.GetInt("settings", "SoftwareDetect");
switch (n) {
case SOFTWARE_CAMERA:
m_sSoftwareDetect = _L(_T("摄像头"));
break;
case SOFTWARE_TELEGRAM:
m_sSoftwareDetect = _L(_T("电报"));
break;
default:
m_sSoftwareDetect = _L(_T("摄像头"));
break;
}
BOOL all = THIS_CFG.GetInt("settings", "MultiScreen", TRUE);
((CButton*)GetDlgItem(IDC_RADIO_ALL_SCREEN))->SetCheck(!all);
((CButton*)GetDlgItem(IDC_RADIO_MAIN_SCREEN))->SetCheck(all);
BOOL frp = THIS_CFG.GetInt("frp", "UseFrp");
((CButton*)GetDlgItem(IDC_RADIO_FRP_OFF))->SetCheck(!frp);
((CButton*)GetDlgItem(IDC_RADIO_FRP_ON))->SetCheck(frp);
GetDlgItem(IDC_EDIT_FRP_PORT)->EnableWindow(frp);
GetDlgItem(IDC_EDIT_FRP_TOKEN)->EnableWindow(frp);
#ifndef _WIN64
GetDlgItem(IDC_RADIO_FRP_OFF)->EnableWindow(FALSE);
GetDlgItem(IDC_RADIO_FRP_ON)->EnableWindow(FALSE);
GetDlgItem(IDC_EDIT_FRP_PORT)->EnableWindow(FALSE);
GetDlgItem(IDC_EDIT_FRP_TOKEN)->EnableWindow(FALSE);
#endif
m_nFrpPort = THIS_CFG.GetInt("frp", "server_port", 7000);
m_sFrpToken = THIS_CFG.GetStr("frp", "token").c_str();
m_nFileServerPort = THIS_CFG.GetInt("settings", "WebSvrPort", 8080);
int size = THIS_CFG.GetInt("settings", "VideoWallSize");
m_ComboVideoWall.InsertStringL(0, "无");
m_ComboVideoWall.InsertStringL(1, "2 x 2");
m_ComboVideoWall.InsertStringL(2, "3 x 3");
m_ComboVideoWall.InsertStringL(3, "4 x 4");
m_ComboVideoWall.InsertStringL(4, "5 x 5");
if (size < 1 || size > 5) size = 1;
m_ComboVideoWall.SetCurSel(size-1);
UpdateData(FALSE);
return TRUE;
}
void CSettingDlg::OnBnClickedButtonSettingapply()
{
UpdateData(TRUE);
THIS_CFG.SetStr("settings", "master", m_sPublicIP.GetBuffer());
THIS_CFG.SetStr("settings", "ghost", m_nListenPort.GetString());
THIS_CFG.SetStr("settings", "UDPOption", m_sUdpOption.GetString());
int n = m_ComboScreenCapture.GetCurSel();
THIS_CFG.SetInt("settings", "DXGI", n);
n = m_ComboScreenCompress.GetCurSel() - 1;
THIS_CFG.SetInt("settings", "ScreenCompress", n);
THIS_CFG.SetInt("settings", "ReportInterval", m_nReportInterval);
n = m_ComboSoftwareDetect.GetCurSel();
THIS_CFG.SetInt("settings", "SoftwareDetect", n);
BOOL all = ((CButton*)GetDlgItem(IDC_RADIO_MAIN_SCREEN))->GetCheck();
THIS_CFG.SetInt("settings", "MultiScreen", all);
BOOL frp = ((CButton*)GetDlgItem(IDC_RADIO_FRP_ON))->GetCheck();
THIS_CFG.SetInt("frp", "UseFrp", frp);
THIS_CFG.SetInt("frp", "server_port", m_nFrpPort);
THIS_CFG.SetStr("frp", "token", m_sFrpToken.GetString());
THIS_CFG.SetInt("settings", "WebSvrPort", m_nFileServerPort);
THIS_CFG.SetInt("settings", "VideoWallSize", m_ComboVideoWall.GetCurSel()+1);
m_ApplyButton.EnableWindow(FALSE);
m_ApplyButton.ShowWindow(SW_HIDE);
}
void CSettingDlg::OnEnChangeEditPort()
{
// TODO: 如果该控件是 RICHEDIT 控件,它将不
// 发送此通知,除非重写 __super::OnInitDialog()
// 函数并调用 CRichEditCtrl().SetEventMask(),
// 同时将 ENM_CHANGE 标志“或”运算到掩码中。
// TODO: 在此添加控件通知处理程序代码
// 给Button添加变量
m_ApplyButton.ShowWindow(SW_NORMAL);
m_ApplyButton.EnableWindow(TRUE);
}
void CSettingDlg::OnEnChangeEditMax()
{
// TODO: 如果该控件是 RICHEDIT 控件,它将不
// 发送此通知,除非重写 __super::OnInitDialog()
// 函数并调用 CRichEditCtrl().SetEventMask(),
// 同时将 ENM_CHANGE 标志“或”运算到掩码中。
// TODO: 在此添加控件通知处理程序代码
HWND hApplyButton = ::GetDlgItem(m_hWnd,IDC_BUTTON_SETTINGAPPLY);
::ShowWindow(hApplyButton,SW_NORMAL);
::EnableWindow(hApplyButton,TRUE);
}
void CSettingDlg::OnOK()
{
OnBnClickedButtonSettingapply();
__super::OnOK();
}
void CSettingDlg::OnBnClickedRadioAllScreen()
{
BOOL b = ((CButton*)GetDlgItem(IDC_RADIO_ALL_SCREEN))->GetCheck();
((CButton*)GetDlgItem(IDC_RADIO_MAIN_SCREEN))->SetCheck(!b);
}
void CSettingDlg::OnBnClickedRadioMainScreen()
{
BOOL b = ((CButton*)GetDlgItem(IDC_RADIO_MAIN_SCREEN))->GetCheck();
((CButton*)GetDlgItem(IDC_RADIO_ALL_SCREEN))->SetCheck(!b);
}
void CSettingDlg::OnBnClickedRadioFrpOff()
{
BOOL b = ((CButton*)GetDlgItem(IDC_RADIO_FRP_OFF))->GetCheck();
((CButton*)GetDlgItem(IDC_RADIO_FRP_ON))->SetCheck(!b);
GetDlgItem(IDC_EDIT_FRP_PORT)->EnableWindow(!b);
GetDlgItem(IDC_EDIT_FRP_TOKEN)->EnableWindow(!b);
}
void CSettingDlg::OnBnClickedRadioFrpOn()
{
BOOL b = ((CButton*)GetDlgItem(IDC_RADIO_FRP_ON))->GetCheck();
((CButton*)GetDlgItem(IDC_RADIO_FRP_OFF))->SetCheck(!b);
GetDlgItem(IDC_EDIT_FRP_PORT)->EnableWindow(b);
GetDlgItem(IDC_EDIT_FRP_TOKEN)->EnableWindow(b);
}
void CSettingDlg::OnEnKillfocusEditPublicIp()
{
auto bindType = THIS_CFG.GetInt("settings", "BindType", 0);
if (bindType == 1 && !THIS_CFG.GetStr("settings", "Password").empty()) {
GetDlgItemText(IDC_EDIT_PUBLIC_IP, m_sPublicIP);
if (m_sPublicIP != m_sOriginalMaster) {
if (IDYES == MessageBox(_TR("修改绑定的公网地址将导致授权失效! 是否继续?"),
_TR("提示"), MB_ICONWARNING | MB_YESNO)) {
// 用户确认修改,更新缓存值避免重复警告
m_sOriginalMaster = m_sPublicIP;
} else {
// 用户取消,恢复原值
m_sPublicIP = m_sOriginalMaster;
SetDlgItemText(IDC_EDIT_PUBLIC_IP, m_sPublicIP);
}
}
}
}
BOOL CSettingDlg::PreTranslateMessage(MSG* pMsg)
{
if (m_tip.GetSafeHwnd()) {
m_tip.RelayEvent(pMsg);
}
return __super::PreTranslateMessage(pMsg);
}