mirror of
https://github.com/kunkundi/crossdesk.git
synced 2026-05-11 22:51:22 +08:00
Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 1d5d6f5121 | |||
| 5f541f5c8b | |||
| 71bce08549 | |||
| 37b9badb2a | |||
| 4089e80fe8 |
@@ -8,6 +8,8 @@ APP_VERSION="$1"
|
||||
ARCHITECTURE="amd64"
|
||||
MAINTAINER="Junkun Di <junkun.di@hotmail.com>"
|
||||
DESCRIPTION="A simple cross-platform remote desktop client."
|
||||
ALSA_RUNTIME_DEP="libasound2 | libasound2t64"
|
||||
PIPEWIRE_RUNTIME_DEP="libpipewire-0.3-0 | libpipewire-0.3-0t64"
|
||||
|
||||
# Remove 'v' prefix from version for Debian package (Debian version must start with digit)
|
||||
DEB_VERSION="${APP_VERSION#v}"
|
||||
@@ -41,9 +43,9 @@ Maintainer: $MAINTAINER
|
||||
Description: $DESCRIPTION
|
||||
Depends: libc6 (>= 2.29), libstdc++6 (>= 9), libx11-6, libxcb1,
|
||||
libxcb-randr0, libxcb-xtest0, libxcb-xinerama0, libxcb-shape0,
|
||||
libxcb-xkb1, libxcb-xfixes0, libxv1, libxtst6, libasound2,
|
||||
libxcb-xkb1, libxcb-xfixes0, libxv1, libxtst6, $ALSA_RUNTIME_DEP,
|
||||
libsndio7.0, libxcb-shm0, libpulse0, libdrm2, libdbus-1-3,
|
||||
libpipewire-0.3-0, xdg-desktop-portal,
|
||||
$PIPEWIRE_RUNTIME_DEP, xdg-desktop-portal,
|
||||
xdg-desktop-portal-gtk | xdg-desktop-portal-kde | xdg-desktop-portal-wlr
|
||||
Recommends: nvidia-cuda-toolkit
|
||||
Priority: optional
|
||||
|
||||
@@ -8,6 +8,8 @@ APP_VERSION="$1"
|
||||
ARCHITECTURE="arm64"
|
||||
MAINTAINER="Junkun Di <junkun.di@hotmail.com>"
|
||||
DESCRIPTION="A simple cross-platform remote desktop client."
|
||||
ALSA_RUNTIME_DEP="libasound2 | libasound2t64"
|
||||
PIPEWIRE_RUNTIME_DEP="libpipewire-0.3-0 | libpipewire-0.3-0t64"
|
||||
|
||||
# Remove 'v' prefix from version for Debian package (Debian version must start with digit)
|
||||
DEB_VERSION="${APP_VERSION#v}"
|
||||
@@ -41,9 +43,9 @@ Maintainer: $MAINTAINER
|
||||
Description: $DESCRIPTION
|
||||
Depends: libc6 (>= 2.29), libstdc++6 (>= 9), libx11-6, libxcb1,
|
||||
libxcb-randr0, libxcb-xtest0, libxcb-xinerama0, libxcb-shape0,
|
||||
libxcb-xkb1, libxcb-xfixes0, libxv1, libxtst6, libasound2,
|
||||
libxcb-xkb1, libxcb-xfixes0, libxv1, libxtst6, $ALSA_RUNTIME_DEP,
|
||||
libsndio7.0, libxcb-shm0, libpulse0, libdrm2, libdbus-1-3,
|
||||
libpipewire-0.3-0, xdg-desktop-portal,
|
||||
$PIPEWIRE_RUNTIME_DEP, xdg-desktop-portal,
|
||||
xdg-desktop-portal-gtk | xdg-desktop-portal-kde | xdg-desktop-portal-wlr
|
||||
Priority: optional
|
||||
Section: utils
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
!define PRODUCT_WEB_SITE "https://www.crossdesk.cn/"
|
||||
!define APP_NAME "CrossDesk"
|
||||
!define UNINSTALL_REG_KEY "CrossDesk"
|
||||
!define PRODUCT_SERVICE_NAME "CrossDeskService"
|
||||
|
||||
; Installer icon path
|
||||
!define MUI_ICON "${__FILEDIR__}\..\..\icons\windows\crossdesk.ico"
|
||||
@@ -68,14 +69,21 @@ cancelInstall:
|
||||
Abort
|
||||
|
||||
installApp:
|
||||
Call StopInstalledService
|
||||
|
||||
SetOutPath "$INSTDIR"
|
||||
SetOverwrite ifnewer
|
||||
|
||||
; Main application executable path
|
||||
File /oname=CrossDesk.exe "..\..\build\windows\x64\release\crossdesk.exe"
|
||||
; Bundle service-side binaries required by the Windows service flow
|
||||
File "..\..\build\windows\x64\release\crossdesk_service.exe"
|
||||
File "..\..\build\windows\x64\release\crossdesk_session_helper.exe"
|
||||
; Bundle runtime DLLs from the release output directory
|
||||
File "..\..\build\windows\x64\release\*.dll"
|
||||
|
||||
Call RegisterInstalledService
|
||||
|
||||
; Write uninstall information
|
||||
WriteUninstaller "$INSTDIR\uninstall.exe"
|
||||
|
||||
@@ -122,8 +130,12 @@ cancelUninstall:
|
||||
Abort
|
||||
|
||||
uninstallApp:
|
||||
Call un.UnregisterInstalledService
|
||||
|
||||
; Delete main executable and uninstaller
|
||||
Delete "$INSTDIR\CrossDesk.exe"
|
||||
Delete "$INSTDIR\crossdesk_service.exe"
|
||||
Delete "$INSTDIR\crossdesk_session_helper.exe"
|
||||
Delete "$INSTDIR\uninstall.exe"
|
||||
|
||||
; Recursively delete installation directory
|
||||
@@ -148,3 +160,78 @@ SectionEnd
|
||||
Function LaunchApp
|
||||
Exec "$INSTDIR\CrossDesk.exe"
|
||||
FunctionEnd
|
||||
|
||||
Function StopInstalledService
|
||||
IfFileExists "$INSTDIR\CrossDesk.exe" 0 stop_with_sc
|
||||
IfFileExists "$INSTDIR\crossdesk_service.exe" 0 stop_with_sc
|
||||
|
||||
DetailPrint "Stopping existing CrossDesk service"
|
||||
ExecWait '"$INSTDIR\CrossDesk.exe" --service-stop' $0
|
||||
${If} $0 = 0
|
||||
Return
|
||||
${EndIf}
|
||||
|
||||
stop_with_sc:
|
||||
DetailPrint "Stopping existing CrossDesk service via Service Control Manager"
|
||||
ExecWait '"$SYSDIR\sc.exe" stop ${PRODUCT_SERVICE_NAME}' $0
|
||||
${If} $0 != 0
|
||||
${AndIf} $0 != 1060
|
||||
${AndIf} $0 != 1062
|
||||
MessageBox MB_ICONSTOP|MB_OK "Failed to stop the existing CrossDesk service. The installation will be aborted."
|
||||
Abort
|
||||
${EndIf}
|
||||
Sleep 1500
|
||||
FunctionEnd
|
||||
|
||||
Function RegisterInstalledService
|
||||
IfFileExists "$INSTDIR\CrossDesk.exe" 0 missing_service_binary
|
||||
IfFileExists "$INSTDIR\crossdesk_service.exe" 0 missing_service_binary
|
||||
IfFileExists "$INSTDIR\crossdesk_session_helper.exe" 0 missing_service_binary
|
||||
|
||||
DetailPrint "Registering CrossDesk service"
|
||||
ExecWait '"$INSTDIR\CrossDesk.exe" --service-install' $0
|
||||
${If} $0 != 0
|
||||
MessageBox MB_ICONSTOP|MB_OK "Failed to register the CrossDesk service. The installation will be aborted."
|
||||
Abort
|
||||
${EndIf}
|
||||
|
||||
DetailPrint "CrossDesk service registered for on-demand start"
|
||||
|
||||
Return
|
||||
|
||||
missing_service_binary:
|
||||
MessageBox MB_ICONSTOP|MB_OK "CrossDesk service files are missing from the installer package. The installation will be aborted."
|
||||
Abort
|
||||
FunctionEnd
|
||||
|
||||
Function un.UnregisterInstalledService
|
||||
IfFileExists "$INSTDIR\CrossDesk.exe" 0 unregister_with_sc
|
||||
|
||||
DetailPrint "Stopping CrossDesk service"
|
||||
ExecWait '"$INSTDIR\CrossDesk.exe" --service-stop' $0
|
||||
${If} $0 = 0
|
||||
DetailPrint "Removing CrossDesk service"
|
||||
ExecWait '"$INSTDIR\CrossDesk.exe" --service-uninstall' $0
|
||||
${If} $0 = 0
|
||||
Return
|
||||
${EndIf}
|
||||
${EndIf}
|
||||
|
||||
unregister_with_sc:
|
||||
DetailPrint "Removing CrossDesk service via Service Control Manager"
|
||||
ExecWait '"$SYSDIR\sc.exe" stop ${PRODUCT_SERVICE_NAME}' $0
|
||||
${If} $0 != 0
|
||||
${AndIf} $0 != 1060
|
||||
${AndIf} $0 != 1062
|
||||
MessageBox MB_ICONSTOP|MB_OK "Failed to stop the CrossDesk service. Uninstall will be aborted."
|
||||
Abort
|
||||
${EndIf}
|
||||
Sleep 1500
|
||||
|
||||
ExecWait '"$SYSDIR\sc.exe" delete ${PRODUCT_SERVICE_NAME}' $0
|
||||
${If} $0 != 0
|
||||
${AndIf} $0 != 1060
|
||||
MessageBox MB_ICONSTOP|MB_OK "Failed to remove the CrossDesk service. Uninstall will be aborted."
|
||||
Abort
|
||||
${EndIf}
|
||||
FunctionEnd
|
||||
|
||||
+22
-4
@@ -14,6 +14,7 @@
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <cstdio>
|
||||
|
||||
#include "service_host.h"
|
||||
#endif
|
||||
|
||||
@@ -53,7 +54,8 @@ void PrintServiceCliUsage() {
|
||||
<< " --service-uninstall Remove the installed Windows service\n"
|
||||
<< " --service-start Start the Windows service\n"
|
||||
<< " --service-stop Stop the Windows service\n"
|
||||
<< " --service-sas Ask the service to send Secure Attention Sequence\n"
|
||||
<< " --service-sas Ask the service to send Secure Attention "
|
||||
"Sequence\n"
|
||||
<< " --service-ping Ping the service over named pipe IPC\n"
|
||||
<< " --service-status Query service runtime status\n"
|
||||
<< " --service-help Show this help\n";
|
||||
@@ -87,12 +89,25 @@ bool IsServiceCliCommand(const char* arg) {
|
||||
std::strcmp(arg, "--service-uninstall") == 0 ||
|
||||
std::strcmp(arg, "--service-start") == 0 ||
|
||||
std::strcmp(arg, "--service-stop") == 0 ||
|
||||
std::strcmp(arg, "--service-sas") == 0 ||
|
||||
std::strcmp(arg, "--service-sas") == 0 ||
|
||||
std::strcmp(arg, "--service-ping") == 0 ||
|
||||
std::strcmp(arg, "--service-status") == 0 ||
|
||||
std::strcmp(arg, "--service-help") == 0;
|
||||
}
|
||||
|
||||
void TryStartManagedWindowsService() {
|
||||
std::filesystem::path service_path = GetSiblingServiceExecutablePath();
|
||||
if (service_path.empty() || !std::filesystem::exists(service_path)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!crossdesk::IsCrossDeskServiceInstalled()) {
|
||||
return;
|
||||
}
|
||||
|
||||
crossdesk::StartCrossDeskService();
|
||||
}
|
||||
|
||||
int HandleServiceCliCommand(const std::string& command) {
|
||||
EnsureConsoleForCli();
|
||||
|
||||
@@ -120,8 +135,7 @@ int HandleServiceCliCommand(const std::string& command) {
|
||||
|
||||
if (command == "--service-uninstall") {
|
||||
bool success = crossdesk::UninstallCrossDeskService();
|
||||
std::cout << (success ? "uninstall ok" : "uninstall failed")
|
||||
<< std::endl;
|
||||
std::cout << (success ? "uninstall ok" : "uninstall failed") << std::endl;
|
||||
return success ? 0 : 1;
|
||||
}
|
||||
|
||||
@@ -182,6 +196,10 @@ int main(int argc, char* argv[]) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
TryStartManagedWindowsService();
|
||||
#endif
|
||||
|
||||
bool enable_daemon = false;
|
||||
auto path_manager = std::make_unique<crossdesk::PathManager>("CrossDesk");
|
||||
if (path_manager) {
|
||||
|
||||
@@ -122,9 +122,8 @@ int ScreenCapturerX11::Init(const int fps, cb_desktop_data cb) {
|
||||
height_ = attr.height;
|
||||
|
||||
if ((width_ & 1) != 0 || (height_ & 1) != 0) {
|
||||
LOG_WARN(
|
||||
"X11 root size {}x{} is not even, aligning down to {}x{} for NV12",
|
||||
width_, height_, width_ & ~1, height_ & ~1);
|
||||
LOG_WARN("X11 root size {}x{} is not even, aligning down to {}x{} for NV12",
|
||||
width_, height_, width_ & ~1, height_ & ~1);
|
||||
width_ &= ~1;
|
||||
height_ &= ~1;
|
||||
}
|
||||
@@ -183,8 +182,9 @@ int ScreenCapturerX11::Start(bool show_cursor) {
|
||||
OnFrame();
|
||||
}
|
||||
|
||||
const auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(
|
||||
clock::now() - frame_start);
|
||||
const auto elapsed =
|
||||
std::chrono::duration_cast<std::chrono::milliseconds>(clock::now() -
|
||||
frame_start);
|
||||
if (elapsed < frame_interval) {
|
||||
std::this_thread::sleep_for(frame_interval - elapsed);
|
||||
}
|
||||
@@ -282,21 +282,17 @@ void ScreenCapturerX11::OnFrame() {
|
||||
}
|
||||
}
|
||||
|
||||
bool needs_copy = image->bytes_per_line != width_ * 4;
|
||||
std::vector<uint8_t> argb_buf;
|
||||
uint8_t* src_argb = nullptr;
|
||||
|
||||
if (needs_copy) {
|
||||
argb_buf.resize(width_ * height_ * 4);
|
||||
for (int y = 0; y < height_; ++y) {
|
||||
memcpy(&argb_buf[y * width_ * 4], image->data + y * image->bytes_per_line,
|
||||
width_ * 4);
|
||||
}
|
||||
src_argb = argb_buf.data();
|
||||
} else {
|
||||
src_argb = reinterpret_cast<uint8_t*>(image->data);
|
||||
if (image->bits_per_pixel != 32 || image->bytes_per_line <= 0) {
|
||||
LOG_WARN(
|
||||
"Unsupported X11 image layout: bits_per_pixel={}, bytes_per_line={}",
|
||||
image->bits_per_pixel, image->bytes_per_line);
|
||||
XDestroyImage(image);
|
||||
return;
|
||||
}
|
||||
|
||||
const uint8_t* src_argb = reinterpret_cast<const uint8_t*>(image->data);
|
||||
const int src_stride_argb = image->bytes_per_line;
|
||||
|
||||
const size_t y_size =
|
||||
static_cast<size_t>(width_) * static_cast<size_t>(height_);
|
||||
const size_t uv_size = y_size / 2;
|
||||
@@ -307,8 +303,20 @@ void ScreenCapturerX11::OnFrame() {
|
||||
uv_plane_.resize(uv_size);
|
||||
}
|
||||
|
||||
libyuv::ARGBToNV12(src_argb, width_ * 4, y_plane_.data(), width_,
|
||||
uv_plane_.data(), width_, width_, height_);
|
||||
const int convert_ret =
|
||||
use_abgr_to_nv12_
|
||||
? libyuv::ABGRToNV12(src_argb, src_stride_argb, y_plane_.data(),
|
||||
width_, uv_plane_.data(), width_, width_,
|
||||
height_)
|
||||
: libyuv::ARGBToNV12(src_argb, src_stride_argb, y_plane_.data(),
|
||||
width_, uv_plane_.data(), width_, width_,
|
||||
height_);
|
||||
if (convert_ret != 0) {
|
||||
LOG_WARN("X11 {} failed: {}",
|
||||
use_abgr_to_nv12_ ? "ABGRToNV12" : "ARGBToNV12", convert_ret);
|
||||
XDestroyImage(image);
|
||||
return;
|
||||
}
|
||||
|
||||
std::vector<uint8_t> nv12;
|
||||
nv12.reserve(y_plane_.size() + uv_plane_.size());
|
||||
@@ -416,16 +424,18 @@ bool ScreenCapturerX11::ProbeCapture() {
|
||||
x11_error = trap.SyncAndGetError();
|
||||
}
|
||||
|
||||
if (probe_image) {
|
||||
XDestroyImage(probe_image);
|
||||
}
|
||||
|
||||
if (x11_error != 0 || !probe_image) {
|
||||
LOG_WARN("X11 probe XGetImage failed: x11_error={}, image={}", x11_error,
|
||||
probe_image ? "valid" : "null");
|
||||
return false;
|
||||
}
|
||||
|
||||
const bool red_in_low_byte = (probe_image->red_mask & 0x000000FFu) != 0;
|
||||
const bool blue_in_low_byte = (probe_image->blue_mask & 0x000000FFu) != 0;
|
||||
use_abgr_to_nv12_ = red_in_low_byte && !blue_in_low_byte;
|
||||
|
||||
XDestroyImage(probe_image);
|
||||
|
||||
return true;
|
||||
}
|
||||
} // namespace crossdesk
|
||||
|
||||
@@ -71,6 +71,7 @@ class ScreenCapturerX11 : public ScreenCapturer {
|
||||
cb_desktop_data callback_;
|
||||
std::vector<DisplayInfo> display_info_list_;
|
||||
int capture_error_count_ = 0;
|
||||
bool use_abgr_to_nv12_ = false;
|
||||
|
||||
std::vector<uint8_t> y_plane_;
|
||||
std::vector<uint8_t> uv_plane_;
|
||||
|
||||
@@ -1,5 +1,11 @@
|
||||
#ifndef _CROSSDESK_INTERACTIVE_STATE_H_
|
||||
#define _CROSSDESK_INTERACTIVE_STATE_H_
|
||||
/*
|
||||
* @Author: DI JUNKUN
|
||||
* @Date: 2026-04-21
|
||||
* Copyright (c) 2026 by DI JUNKUN, All Rights Reserved.
|
||||
*/
|
||||
|
||||
#ifndef _INTERACTIVE_STATE_H_
|
||||
#define _INTERACTIVE_STATE_H_
|
||||
|
||||
#include <string>
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#include "service_host.h"
|
||||
|
||||
#include <Aclapi.h>
|
||||
#include <TlHelp32.h>
|
||||
#include <Userenv.h>
|
||||
#include <WtsApi32.h>
|
||||
@@ -27,6 +28,9 @@ using Json = nlohmann::json;
|
||||
|
||||
constexpr char kSecureDesktopKeyboardIpcCommandPrefix[] = "secure-input-key:";
|
||||
constexpr char kSecureDesktopMouseIpcCommandPrefix[] = "secure-input-mouse:";
|
||||
constexpr wchar_t kCrossDeskClientProcessName[] = L"crossdesk.exe";
|
||||
constexpr DWORD kCrossDeskClientMonitorIntervalMs = 1000;
|
||||
constexpr ULONGLONG kCrossDeskClientMonitorStartupGraceMs = 5000;
|
||||
|
||||
using SendSasFunction = VOID(WINAPI*)(BOOL);
|
||||
|
||||
@@ -157,6 +161,97 @@ std::string BuildErrorJson(const char* error, DWORD error_code = 0) {
|
||||
return stream.str();
|
||||
}
|
||||
|
||||
bool HasRunningCrossDeskClientProcess() {
|
||||
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
|
||||
if (snapshot == INVALID_HANDLE_VALUE) {
|
||||
LOG_ERROR("CreateToolhelp32Snapshot failed, error={}", GetLastError());
|
||||
return true;
|
||||
}
|
||||
|
||||
PROCESSENTRY32W entry{};
|
||||
entry.dwSize = sizeof(entry);
|
||||
if (!Process32FirstW(snapshot, &entry)) {
|
||||
DWORD error = GetLastError();
|
||||
CloseHandle(snapshot);
|
||||
if (error != ERROR_NO_MORE_FILES) {
|
||||
LOG_ERROR("Process32FirstW failed, error={}", error);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
do {
|
||||
if (_wcsicmp(entry.szExeFile, kCrossDeskClientProcessName) == 0) {
|
||||
CloseHandle(snapshot);
|
||||
return true;
|
||||
}
|
||||
} while (Process32NextW(snapshot, &entry));
|
||||
|
||||
DWORD error = GetLastError();
|
||||
CloseHandle(snapshot);
|
||||
if (error != ERROR_NO_MORE_FILES) {
|
||||
LOG_ERROR("Process32NextW failed, error={}", error);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool GrantCrossDeskServiceStartAccessToAuthenticatedUsers(SC_HANDLE service) {
|
||||
if (service == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
PACL existing_dacl = nullptr;
|
||||
PACL updated_dacl = nullptr;
|
||||
PSECURITY_DESCRIPTOR security_descriptor = nullptr;
|
||||
DWORD error =
|
||||
GetSecurityInfo(service, SE_SERVICE, DACL_SECURITY_INFORMATION, nullptr,
|
||||
nullptr, &existing_dacl, nullptr, &security_descriptor);
|
||||
if (error != ERROR_SUCCESS) {
|
||||
LOG_ERROR("GetSecurityInfo failed, error={}", error);
|
||||
return false;
|
||||
}
|
||||
|
||||
BYTE sid_buffer[SECURITY_MAX_SID_SIZE] = {0};
|
||||
DWORD sid_size = sizeof(sid_buffer);
|
||||
if (!CreateWellKnownSid(WinAuthenticatedUserSid, nullptr, sid_buffer,
|
||||
&sid_size)) {
|
||||
error = GetLastError();
|
||||
LOG_ERROR("CreateWellKnownSid failed, error={}", error);
|
||||
LocalFree(security_descriptor);
|
||||
return false;
|
||||
}
|
||||
|
||||
EXPLICIT_ACCESSW access{};
|
||||
access.grfAccessPermissions = SERVICE_START | SERVICE_QUERY_STATUS;
|
||||
access.grfAccessMode = SET_ACCESS;
|
||||
access.grfInheritance = NO_INHERITANCE;
|
||||
access.Trustee.TrusteeForm = TRUSTEE_IS_SID;
|
||||
access.Trustee.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP;
|
||||
access.Trustee.ptstrName = reinterpret_cast<LPWSTR>(sid_buffer);
|
||||
|
||||
error = SetEntriesInAclW(1, &access, existing_dacl, &updated_dacl);
|
||||
if (error != ERROR_SUCCESS) {
|
||||
LOG_ERROR("SetEntriesInAclW failed, error={}", error);
|
||||
LocalFree(security_descriptor);
|
||||
return false;
|
||||
}
|
||||
|
||||
error = SetSecurityInfo(service, SE_SERVICE, DACL_SECURITY_INFORMATION,
|
||||
nullptr, nullptr, updated_dacl, nullptr);
|
||||
if (error != ERROR_SUCCESS) {
|
||||
LOG_ERROR("SetSecurityInfo failed, error={}", error);
|
||||
LocalFree(updated_dacl);
|
||||
LocalFree(security_descriptor);
|
||||
return false;
|
||||
}
|
||||
|
||||
LocalFree(updated_dacl);
|
||||
LocalFree(security_descriptor);
|
||||
return true;
|
||||
}
|
||||
|
||||
std::string QueryNamedPipeMessage(const std::wstring& pipe_name,
|
||||
const std::string& command,
|
||||
DWORD timeout_ms) {
|
||||
@@ -812,6 +907,10 @@ int CrossDeskServiceHost::InitializeRuntime() {
|
||||
RefreshSessionState();
|
||||
EnsureSessionHelper();
|
||||
ipc_thread_ = std::thread(&CrossDeskServiceHost::IpcServerLoop, this);
|
||||
if (!console_mode_) {
|
||||
client_process_monitor_thread_ =
|
||||
std::thread(&CrossDeskServiceHost::ClientProcessMonitorLoop, this);
|
||||
}
|
||||
LOG_INFO("CrossDesk service runtime initialized, session_id={}",
|
||||
active_session_id_);
|
||||
return 0;
|
||||
@@ -825,6 +924,10 @@ void CrossDeskServiceHost::ShutdownRuntime() {
|
||||
SetEvent(stop_event_);
|
||||
}
|
||||
|
||||
if (client_process_monitor_thread_.joinable()) {
|
||||
client_process_monitor_thread_.join();
|
||||
}
|
||||
|
||||
if (ipc_thread_.joinable()) {
|
||||
ipc_thread_.join();
|
||||
}
|
||||
@@ -841,6 +944,34 @@ void CrossDeskServiceHost::RequestStop() {
|
||||
}
|
||||
}
|
||||
|
||||
void CrossDeskServiceHost::ClientProcessMonitorLoop() {
|
||||
const ULONGLONG monitor_started_at = GetTickCount64();
|
||||
|
||||
while (stop_event_ != nullptr) {
|
||||
DWORD wait_result =
|
||||
WaitForSingleObject(stop_event_, kCrossDeskClientMonitorIntervalMs);
|
||||
if (wait_result == WAIT_OBJECT_0) {
|
||||
return;
|
||||
}
|
||||
if (wait_result != WAIT_TIMEOUT) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (GetTickCount64() - monitor_started_at <
|
||||
kCrossDeskClientMonitorStartupGraceMs) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (HasRunningCrossDeskClientProcess()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
LOG_INFO("No crossdesk client process detected, stopping service");
|
||||
RequestStop();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void CrossDeskServiceHost::ReportServiceStatus(DWORD current_state,
|
||||
DWORD win32_exit_code,
|
||||
DWORD wait_hint) {
|
||||
@@ -1845,7 +1976,7 @@ bool InstallCrossDeskService(const std::wstring& binary_path) {
|
||||
std::wstring service_command = L"\"" + binary_path + L"\" --service";
|
||||
SC_HANDLE service = CreateServiceW(
|
||||
manager, kCrossDeskServiceName, kCrossDeskServiceDisplayName,
|
||||
SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START,
|
||||
SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS, SERVICE_DEMAND_START,
|
||||
SERVICE_ERROR_NORMAL, service_command.c_str(), nullptr, nullptr, nullptr,
|
||||
nullptr, nullptr);
|
||||
|
||||
@@ -1858,14 +1989,15 @@ bool InstallCrossDeskService(const std::wstring& binary_path) {
|
||||
}
|
||||
|
||||
service = OpenServiceW(manager, kCrossDeskServiceName,
|
||||
SERVICE_CHANGE_CONFIG | SERVICE_QUERY_STATUS);
|
||||
SERVICE_CHANGE_CONFIG | SERVICE_QUERY_STATUS |
|
||||
READ_CONTROL | WRITE_DAC);
|
||||
if (service == nullptr) {
|
||||
LOG_ERROR("OpenServiceW failed, error={}", GetLastError());
|
||||
CloseServiceHandle(manager);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!ChangeServiceConfigW(service, SERVICE_NO_CHANGE, SERVICE_AUTO_START,
|
||||
if (!ChangeServiceConfigW(service, SERVICE_NO_CHANGE, SERVICE_DEMAND_START,
|
||||
SERVICE_NO_CHANGE, service_command.c_str(),
|
||||
nullptr, nullptr, nullptr, nullptr, nullptr,
|
||||
kCrossDeskServiceDisplayName)) {
|
||||
@@ -1876,6 +2008,39 @@ bool InstallCrossDeskService(const std::wstring& binary_path) {
|
||||
}
|
||||
}
|
||||
|
||||
if (!GrantCrossDeskServiceStartAccessToAuthenticatedUsers(service)) {
|
||||
CloseServiceHandle(service);
|
||||
CloseServiceHandle(manager);
|
||||
return false;
|
||||
}
|
||||
|
||||
CloseServiceHandle(service);
|
||||
CloseServiceHandle(manager);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool IsCrossDeskServiceInstalled() {
|
||||
SC_HANDLE manager = OpenSCManagerW(nullptr, nullptr, SC_MANAGER_CONNECT);
|
||||
if (manager == nullptr) {
|
||||
LOG_ERROR("OpenSCManagerW failed, error={}", GetLastError());
|
||||
return false;
|
||||
}
|
||||
|
||||
SC_HANDLE service =
|
||||
OpenServiceW(manager, kCrossDeskServiceName, SERVICE_QUERY_STATUS);
|
||||
if (service == nullptr) {
|
||||
DWORD error = GetLastError();
|
||||
CloseServiceHandle(manager);
|
||||
if (error == ERROR_SERVICE_DOES_NOT_EXIST) {
|
||||
return false;
|
||||
}
|
||||
if (error == ERROR_ACCESS_DENIED) {
|
||||
return true;
|
||||
}
|
||||
LOG_ERROR("OpenServiceW failed, error={}", error);
|
||||
return false;
|
||||
}
|
||||
|
||||
CloseServiceHandle(service);
|
||||
CloseServiceHandle(manager);
|
||||
return true;
|
||||
@@ -1891,7 +2056,10 @@ bool StartCrossDeskService() {
|
||||
SC_HANDLE service =
|
||||
OpenServiceW(manager, kCrossDeskServiceName, SERVICE_START);
|
||||
if (service == nullptr) {
|
||||
LOG_ERROR("OpenServiceW failed, error={}", GetLastError());
|
||||
DWORD error = GetLastError();
|
||||
if (error != ERROR_SERVICE_DOES_NOT_EXIST) {
|
||||
LOG_ERROR("OpenServiceW failed, error={}", error);
|
||||
}
|
||||
CloseServiceHandle(manager);
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,11 @@
|
||||
#ifndef _CROSSDESK_SERVICE_HOST_H_
|
||||
#define _CROSSDESK_SERVICE_HOST_H_
|
||||
/*
|
||||
* @Author: DI JUNKUN
|
||||
* @Date: 2026-04-21
|
||||
* Copyright (c) 2026 by DI JUNKUN, All Rights Reserved.
|
||||
*/
|
||||
|
||||
#ifndef _SERVICE_HOST_H_
|
||||
#define _SERVICE_HOST_H_
|
||||
|
||||
#include <Windows.h>
|
||||
|
||||
@@ -27,6 +33,7 @@ class CrossDeskServiceHost {
|
||||
int InitializeRuntime();
|
||||
void ShutdownRuntime();
|
||||
void RequestStop();
|
||||
void ClientProcessMonitorLoop();
|
||||
void ReportServiceStatus(DWORD current_state, DWORD win32_exit_code,
|
||||
DWORD wait_hint);
|
||||
void IpcServerLoop();
|
||||
@@ -65,6 +72,7 @@ class CrossDeskServiceHost {
|
||||
SERVICE_STATUS service_status_{};
|
||||
HANDLE stop_event_ = nullptr;
|
||||
std::thread ipc_thread_;
|
||||
std::thread client_process_monitor_thread_;
|
||||
std::mutex state_mutex_;
|
||||
DWORD active_session_id_ = 0xFFFFFFFF;
|
||||
DWORD process_session_id_ = 0xFFFFFFFF;
|
||||
@@ -122,6 +130,7 @@ class CrossDeskServiceHost {
|
||||
static CrossDeskServiceHost* instance_;
|
||||
};
|
||||
|
||||
bool IsCrossDeskServiceInstalled();
|
||||
bool InstallCrossDeskService(const std::wstring& binary_path);
|
||||
bool UninstallCrossDeskService();
|
||||
bool StartCrossDeskService();
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
#include <TlHelp32.h>
|
||||
// clang-format off
|
||||
#include <Windows.h>
|
||||
#include <TlHelp32.h>
|
||||
// clang-format on
|
||||
#include <WtsApi32.h>
|
||||
#include <libyuv.h>
|
||||
#include <sddl.h>
|
||||
|
||||
@@ -1,5 +1,11 @@
|
||||
#ifndef _CROSSDESK_SESSION_HELPER_SHARED_H_
|
||||
#define _CROSSDESK_SESSION_HELPER_SHARED_H_
|
||||
/*
|
||||
* @Author: DI JUNKUN
|
||||
* @Date: 2026-04-21
|
||||
* Copyright (c) 2026 by DI JUNKUN, All Rights Reserved.
|
||||
*/
|
||||
|
||||
#ifndef _SESSION_HELPER_SHARED_H_
|
||||
#define _SESSION_HELPER_SHARED_H_
|
||||
|
||||
#include <Windows.h>
|
||||
|
||||
|
||||
+1
-1
@@ -192,7 +192,7 @@ function setup_targets()
|
||||
add_files("src/service/windows/service_host.cpp")
|
||||
add_includedirs("src/service/windows", {public = true})
|
||||
add_links("Advapi32", "Wtsapi32", "Ole32", "Userenv")
|
||||
add_deps("wgc_plugin")
|
||||
add_deps("wgc_plugin", "crossdesk_service", "crossdesk_session_helper")
|
||||
add_files("scripts/windows/crossdesk.rc")
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user