mirror of
https://mirror.suhoan.cn/https://github.com/EasyTier/EasyTier.git
synced 2025-12-16 14:47:25 +08:00
feat(gui): add service and remote mode support (#1578)
This PR fundamentally restructures the EasyTier GUI, introducing support for service mode and remote mode, transforming it from a simple desktop application into a powerful network management terminal. This change allows users to persistently run the EasyTier core as a background service or remotely manage multiple EasyTier instances, greatly improving deployment flexibility and manageability.
This commit is contained in:
1412
easytier/src/core.rs
Normal file
1412
easytier/src/core.rs
Normal file
File diff suppressed because it is too large
Load Diff
@@ -1,6 +1,5 @@
|
||||
use std::{
|
||||
ffi::OsString,
|
||||
fmt::Write,
|
||||
net::{IpAddr, SocketAddr},
|
||||
path::PathBuf,
|
||||
str::FromStr,
|
||||
@@ -17,6 +16,8 @@ use humansize::format_size;
|
||||
use rust_i18n::t;
|
||||
use service_manager::*;
|
||||
use tabled::settings::Style;
|
||||
|
||||
use easytier::service_manager::{Service, ServiceInstallOptions};
|
||||
use tokio::time::timeout;
|
||||
|
||||
use easytier::{
|
||||
@@ -1376,274 +1377,6 @@ impl CommandHandler<'_> {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct ServiceInstallOptions {
|
||||
pub program: PathBuf,
|
||||
pub args: Vec<OsString>,
|
||||
pub work_directory: PathBuf,
|
||||
pub disable_autostart: bool,
|
||||
pub description: Option<String>,
|
||||
pub display_name: Option<String>,
|
||||
pub disable_restart_on_failure: bool,
|
||||
}
|
||||
pub struct Service {
|
||||
lable: ServiceLabel,
|
||||
kind: ServiceManagerKind,
|
||||
service_manager: Box<dyn ServiceManager>,
|
||||
}
|
||||
|
||||
impl Service {
|
||||
pub fn new(name: String) -> Result<Self, Error> {
|
||||
#[cfg(target_os = "windows")]
|
||||
let service_manager = Box::new(crate::win_service_manager::WinServiceManager::new()?);
|
||||
|
||||
#[cfg(not(target_os = "windows"))]
|
||||
let service_manager = <dyn ServiceManager>::native()?;
|
||||
let kind = ServiceManagerKind::native()?;
|
||||
|
||||
println!("service manager kind: {:?}", kind);
|
||||
|
||||
Ok(Self {
|
||||
lable: name.parse()?,
|
||||
kind,
|
||||
service_manager,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn install(&self, options: &ServiceInstallOptions) -> Result<(), Error> {
|
||||
let ctx = ServiceInstallCtx {
|
||||
label: self.lable.clone(),
|
||||
program: options.program.clone(),
|
||||
args: options.args.clone(),
|
||||
contents: self.make_install_content_option(options),
|
||||
autostart: !options.disable_autostart,
|
||||
username: None,
|
||||
working_directory: Some(options.work_directory.clone()),
|
||||
environment: None,
|
||||
disable_restart_on_failure: options.disable_restart_on_failure,
|
||||
};
|
||||
if self.status()? != ServiceStatus::NotInstalled {
|
||||
return Err(anyhow::anyhow!(
|
||||
"Service is already installed! Service Name: {}",
|
||||
self.lable
|
||||
));
|
||||
}
|
||||
|
||||
self.service_manager
|
||||
.install(ctx.clone())
|
||||
.map_err(|e| anyhow::anyhow!("failed to install service: {:?}", e))?;
|
||||
|
||||
println!(
|
||||
"Service installed successfully! Service Name: {}",
|
||||
self.lable
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn uninstall(&self) -> Result<(), Error> {
|
||||
let ctx = ServiceUninstallCtx {
|
||||
label: self.lable.clone(),
|
||||
};
|
||||
let status = self.status()?;
|
||||
|
||||
if status == ServiceStatus::NotInstalled {
|
||||
return Err(anyhow::anyhow!("Service is not installed"));
|
||||
}
|
||||
|
||||
if status == ServiceStatus::Running {
|
||||
self.service_manager.stop(ServiceStopCtx {
|
||||
label: self.lable.clone(),
|
||||
})?;
|
||||
}
|
||||
|
||||
self.service_manager
|
||||
.uninstall(ctx)
|
||||
.map_err(|e| anyhow::anyhow!("failed to uninstall service: {}", e))
|
||||
}
|
||||
|
||||
pub fn status(&self) -> Result<ServiceStatus, Error> {
|
||||
let ctx = ServiceStatusCtx {
|
||||
label: self.lable.clone(),
|
||||
};
|
||||
let status = self.service_manager.status(ctx)?;
|
||||
|
||||
Ok(status)
|
||||
}
|
||||
|
||||
pub fn start(&self) -> Result<(), Error> {
|
||||
let ctx = ServiceStartCtx {
|
||||
label: self.lable.clone(),
|
||||
};
|
||||
let status = self.status()?;
|
||||
|
||||
match status {
|
||||
ServiceStatus::Running => Err(anyhow::anyhow!("Service is already running")),
|
||||
ServiceStatus::Stopped(_) => {
|
||||
self.service_manager
|
||||
.start(ctx)
|
||||
.map_err(|e| anyhow::anyhow!("failed to start service: {}", e))?;
|
||||
Ok(())
|
||||
}
|
||||
ServiceStatus::NotInstalled => Err(anyhow::anyhow!("Service is not installed")),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn stop(&self) -> Result<(), Error> {
|
||||
let ctx = ServiceStopCtx {
|
||||
label: self.lable.clone(),
|
||||
};
|
||||
let status = self.status()?;
|
||||
|
||||
match status {
|
||||
ServiceStatus::Running => {
|
||||
self.service_manager
|
||||
.stop(ctx)
|
||||
.map_err(|e| anyhow::anyhow!("failed to stop service: {}", e))?;
|
||||
Ok(())
|
||||
}
|
||||
ServiceStatus::Stopped(_) => Err(anyhow::anyhow!("Service is already stopped")),
|
||||
ServiceStatus::NotInstalled => Err(anyhow::anyhow!("Service is not installed")),
|
||||
}
|
||||
}
|
||||
|
||||
fn make_install_content_option(&self, options: &ServiceInstallOptions) -> Option<String> {
|
||||
match self.kind {
|
||||
ServiceManagerKind::Systemd => Some(self.make_systemd_unit(options).unwrap()),
|
||||
ServiceManagerKind::Rcd => Some(self.make_rcd_script(options).unwrap()),
|
||||
ServiceManagerKind::OpenRc => Some(self.make_open_rc_script(options).unwrap()),
|
||||
ServiceManagerKind::Launchd => None, // 使用 service-manager-rs 的默认 plist 生成
|
||||
_ => {
|
||||
#[cfg(target_os = "windows")]
|
||||
{
|
||||
let win_options = win_service_manager::WinServiceInstallOptions {
|
||||
description: options.description.clone(),
|
||||
display_name: options.display_name.clone(),
|
||||
dependencies: Some(vec!["rpcss".to_string(), "dnscache".to_string()]),
|
||||
};
|
||||
|
||||
Some(serde_json::to_string(&win_options).unwrap())
|
||||
}
|
||||
|
||||
#[cfg(not(target_os = "windows"))]
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn make_systemd_unit(
|
||||
&self,
|
||||
options: &ServiceInstallOptions,
|
||||
) -> Result<String, std::fmt::Error> {
|
||||
let args = options
|
||||
.args
|
||||
.iter()
|
||||
.map(|a| a.to_string_lossy())
|
||||
.collect::<Vec<_>>()
|
||||
.join(" ");
|
||||
let target_app = options.program.display().to_string();
|
||||
let work_dir = options.work_directory.display().to_string();
|
||||
let mut unit_content = String::new();
|
||||
|
||||
writeln!(unit_content, "[Unit]")?;
|
||||
writeln!(unit_content, "After=network.target syslog.target")?;
|
||||
if let Some(ref d) = options.description {
|
||||
writeln!(unit_content, "Description={d}")?;
|
||||
}
|
||||
writeln!(unit_content, "StartLimitIntervalSec=0")?;
|
||||
writeln!(unit_content)?;
|
||||
writeln!(unit_content, "[Service]")?;
|
||||
writeln!(unit_content, "Type=simple")?;
|
||||
writeln!(unit_content, "WorkingDirectory={work_dir}")?;
|
||||
writeln!(unit_content, "ExecStart={target_app} {args}")?;
|
||||
writeln!(unit_content, "Restart=always")?;
|
||||
writeln!(unit_content, "RestartSec=1")?;
|
||||
writeln!(unit_content, "LimitNOFILE=infinity")?;
|
||||
writeln!(unit_content)?;
|
||||
writeln!(unit_content, "[Install]")?;
|
||||
writeln!(unit_content, "WantedBy=multi-user.target")?;
|
||||
|
||||
std::result::Result::Ok(unit_content)
|
||||
}
|
||||
|
||||
fn make_rcd_script(&self, options: &ServiceInstallOptions) -> Result<String, std::fmt::Error> {
|
||||
let name = self.lable.to_qualified_name();
|
||||
let args = options
|
||||
.args
|
||||
.iter()
|
||||
.map(|a| a.to_string_lossy())
|
||||
.collect::<Vec<_>>()
|
||||
.join(" ");
|
||||
let target_app = options.program.display().to_string();
|
||||
let work_dir = options.work_directory.display().to_string();
|
||||
let mut script = String::new();
|
||||
|
||||
writeln!(script, "#!/bin/sh")?;
|
||||
writeln!(script, "#")?;
|
||||
writeln!(script, "# PROVIDE: {name}")?;
|
||||
writeln!(script, "# REQUIRE: LOGIN FILESYSTEMS NETWORKING ")?;
|
||||
writeln!(script, "# KEYWORD: shutdown")?;
|
||||
writeln!(script)?;
|
||||
writeln!(script, ". /etc/rc.subr")?;
|
||||
writeln!(script)?;
|
||||
writeln!(script, "name=\"{name}\"")?;
|
||||
if let Some(ref d) = options.description {
|
||||
writeln!(script, "desc=\"{d}\"")?;
|
||||
}
|
||||
writeln!(script, "rcvar=\"{name}_enable\"")?;
|
||||
writeln!(script)?;
|
||||
writeln!(script, "load_rc_config ${{name}}")?;
|
||||
writeln!(script)?;
|
||||
writeln!(script, ": ${{{name}_options=\"{args}\"}}")?;
|
||||
writeln!(script)?;
|
||||
writeln!(script, "{name}_chdir=\"{work_dir}\"")?;
|
||||
writeln!(script, "pidfile=\"/var/run/${{name}}.pid\"")?;
|
||||
writeln!(script, "procname=\"{target_app}\"")?;
|
||||
writeln!(script, "command=\"/usr/sbin/daemon\"")?;
|
||||
writeln!(
|
||||
script,
|
||||
"command_args=\"-c -S -T ${{name}} -p ${{pidfile}} ${{procname}} ${{{name}_options}}\""
|
||||
)?;
|
||||
writeln!(script)?;
|
||||
writeln!(script, "run_rc_command \"$1\"")?;
|
||||
|
||||
std::result::Result::Ok(script)
|
||||
}
|
||||
|
||||
fn make_open_rc_script(
|
||||
&self,
|
||||
options: &ServiceInstallOptions,
|
||||
) -> Result<String, std::fmt::Error> {
|
||||
let args = options
|
||||
.args
|
||||
.iter()
|
||||
.map(|a| a.to_string_lossy())
|
||||
.collect::<Vec<_>>()
|
||||
.join(" ");
|
||||
let target_app = options.program.display().to_string();
|
||||
let work_dir = options.work_directory.display().to_string();
|
||||
let mut script = String::new();
|
||||
|
||||
writeln!(script, "#!/sbin/openrc-run")?;
|
||||
writeln!(script)?;
|
||||
if let Some(ref d) = options.description {
|
||||
writeln!(script, "description=\"{d}\"")?;
|
||||
}
|
||||
writeln!(script, "command=\"{target_app}\"")?;
|
||||
writeln!(script, "command_args=\"{args}\"")?;
|
||||
writeln!(script, "pidfile=\"/run/${{RC_SVCNAME}}.pid\"")?;
|
||||
writeln!(script, "command_background=\"yes\"")?;
|
||||
writeln!(script, "directory=\"{work_dir}\"")?;
|
||||
writeln!(script)?;
|
||||
writeln!(script, "depend() {{")?;
|
||||
writeln!(script, " need net")?;
|
||||
writeln!(script, " use looger")?;
|
||||
writeln!(script, "}}")?;
|
||||
|
||||
std::result::Result::Ok(script)
|
||||
}
|
||||
}
|
||||
|
||||
fn print_output<T>(items: &[T], format: &OutputFormat) -> Result<(), Error>
|
||||
where
|
||||
T: tabled::Tabled + serde::Serialize,
|
||||
@@ -2225,180 +1958,3 @@ async fn main() -> Result<(), Error> {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
mod win_service_manager {
|
||||
use std::{ffi::OsStr, ffi::OsString, io, path::PathBuf};
|
||||
use windows_service::{
|
||||
service::{
|
||||
ServiceAccess, ServiceDependency, ServiceErrorControl, ServiceInfo, ServiceStartType,
|
||||
ServiceType,
|
||||
},
|
||||
service_manager::{ServiceManager, ServiceManagerAccess},
|
||||
};
|
||||
|
||||
use service_manager::{
|
||||
ServiceInstallCtx, ServiceLevel, ServiceStartCtx, ServiceStatus, ServiceStatusCtx,
|
||||
ServiceStopCtx, ServiceUninstallCtx,
|
||||
};
|
||||
|
||||
use winreg::{enums::*, RegKey};
|
||||
|
||||
use easytier::common::constants::WIN_SERVICE_WORK_DIR_REG_KEY;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct WinServiceInstallOptions {
|
||||
pub dependencies: Option<Vec<String>>,
|
||||
pub description: Option<String>,
|
||||
pub display_name: Option<String>,
|
||||
}
|
||||
|
||||
pub struct WinServiceManager {
|
||||
service_manager: ServiceManager,
|
||||
}
|
||||
|
||||
impl WinServiceManager {
|
||||
pub fn new() -> Result<Self, crate::Error> {
|
||||
let service_manager =
|
||||
ServiceManager::local_computer(None::<&str>, ServiceManagerAccess::ALL_ACCESS)?;
|
||||
Ok(Self { service_manager })
|
||||
}
|
||||
}
|
||||
impl service_manager::ServiceManager for WinServiceManager {
|
||||
fn available(&self) -> io::Result<bool> {
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
fn install(&self, ctx: ServiceInstallCtx) -> io::Result<()> {
|
||||
let start_type_ = if ctx.autostart {
|
||||
ServiceStartType::AutoStart
|
||||
} else {
|
||||
ServiceStartType::OnDemand
|
||||
};
|
||||
let srv_name = OsString::from(ctx.label.to_qualified_name());
|
||||
let mut dis_name = srv_name.clone();
|
||||
let mut description: Option<OsString> = None;
|
||||
let mut dependencies = Vec::<ServiceDependency>::new();
|
||||
|
||||
if let Some(s) = ctx.contents.as_ref() {
|
||||
let options: WinServiceInstallOptions = serde_json::from_str(s.as_str()).unwrap();
|
||||
if let Some(d) = options.dependencies {
|
||||
dependencies = d
|
||||
.iter()
|
||||
.map(|dep| ServiceDependency::Service(OsString::from(dep.clone())))
|
||||
.collect::<Vec<_>>();
|
||||
}
|
||||
if let Some(d) = options.description {
|
||||
description = Some(OsString::from(d));
|
||||
}
|
||||
if let Some(d) = options.display_name {
|
||||
dis_name = OsString::from(d);
|
||||
}
|
||||
}
|
||||
|
||||
let service_info = ServiceInfo {
|
||||
name: srv_name,
|
||||
display_name: dis_name,
|
||||
service_type: ServiceType::OWN_PROCESS,
|
||||
start_type: start_type_,
|
||||
error_control: ServiceErrorControl::Normal,
|
||||
executable_path: ctx.program,
|
||||
launch_arguments: ctx.args,
|
||||
dependencies: dependencies.clone(),
|
||||
account_name: None,
|
||||
account_password: None,
|
||||
};
|
||||
|
||||
let service = self
|
||||
.service_manager
|
||||
.create_service(&service_info, ServiceAccess::ALL_ACCESS)
|
||||
.map_err(io::Error::other)?;
|
||||
|
||||
if let Some(s) = description {
|
||||
service
|
||||
.set_description(s.clone())
|
||||
.map_err(io::Error::other)?;
|
||||
}
|
||||
|
||||
if let Some(work_dir) = ctx.working_directory {
|
||||
set_service_work_directory(&ctx.label.to_qualified_name(), work_dir)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn uninstall(&self, ctx: ServiceUninstallCtx) -> io::Result<()> {
|
||||
let service = self
|
||||
.service_manager
|
||||
.open_service(ctx.label.to_qualified_name(), ServiceAccess::ALL_ACCESS)
|
||||
.map_err(io::Error::other)?;
|
||||
|
||||
service.delete().map_err(io::Error::other)
|
||||
}
|
||||
|
||||
fn start(&self, ctx: ServiceStartCtx) -> io::Result<()> {
|
||||
let service = self
|
||||
.service_manager
|
||||
.open_service(ctx.label.to_qualified_name(), ServiceAccess::ALL_ACCESS)
|
||||
.map_err(io::Error::other)?;
|
||||
|
||||
service.start(&[] as &[&OsStr]).map_err(io::Error::other)
|
||||
}
|
||||
|
||||
fn stop(&self, ctx: ServiceStopCtx) -> io::Result<()> {
|
||||
let service = self
|
||||
.service_manager
|
||||
.open_service(ctx.label.to_qualified_name(), ServiceAccess::ALL_ACCESS)
|
||||
.map_err(io::Error::other)?;
|
||||
|
||||
_ = service.stop().map_err(io::Error::other)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn level(&self) -> ServiceLevel {
|
||||
ServiceLevel::System
|
||||
}
|
||||
|
||||
fn set_level(&mut self, level: ServiceLevel) -> io::Result<()> {
|
||||
match level {
|
||||
ServiceLevel::System => Ok(()),
|
||||
_ => Err(io::Error::other("Unsupported service level")),
|
||||
}
|
||||
}
|
||||
|
||||
fn status(&self, ctx: ServiceStatusCtx) -> io::Result<ServiceStatus> {
|
||||
let service = match self
|
||||
.service_manager
|
||||
.open_service(ctx.label.to_qualified_name(), ServiceAccess::QUERY_STATUS)
|
||||
{
|
||||
Ok(s) => s,
|
||||
Err(e) => {
|
||||
if let windows_service::Error::Winapi(ref win_err) = e {
|
||||
if win_err.raw_os_error() == Some(0x424) {
|
||||
return Ok(ServiceStatus::NotInstalled);
|
||||
}
|
||||
}
|
||||
return Err(io::Error::other(e));
|
||||
}
|
||||
};
|
||||
|
||||
let status = service.query_status().map_err(io::Error::other)?;
|
||||
|
||||
match status.current_state {
|
||||
windows_service::service::ServiceState::Stopped => Ok(ServiceStatus::Stopped(None)),
|
||||
_ => Ok(ServiceStatus::Running),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn set_service_work_directory(service_name: &str, work_directory: PathBuf) -> io::Result<()> {
|
||||
let (reg_key, _) =
|
||||
RegKey::predef(HKEY_LOCAL_MACHINE).create_subkey(WIN_SERVICE_WORK_DIR_REG_KEY)?;
|
||||
reg_key
|
||||
.set_value::<OsString, _>(service_name, &work_directory.as_os_str().to_os_string())?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -13,11 +13,11 @@ use crate::{
|
||||
rpc_service::InstanceRpcService,
|
||||
};
|
||||
|
||||
pub(crate) struct WebClientGuard {
|
||||
pub(crate) struct DaemonGuard {
|
||||
guard: Option<Arc<()>>,
|
||||
stop_check_notifier: Arc<tokio::sync::Notify>,
|
||||
}
|
||||
impl Drop for WebClientGuard {
|
||||
impl Drop for DaemonGuard {
|
||||
fn drop(&mut self) {
|
||||
drop(self.guard.take());
|
||||
self.stop_check_notifier.notify_one();
|
||||
@@ -30,7 +30,7 @@ pub struct NetworkInstanceManager {
|
||||
stop_check_notifier: Arc<tokio::sync::Notify>,
|
||||
instance_error_messages: Arc<DashMap<uuid::Uuid, String>>,
|
||||
config_dir: Option<PathBuf>,
|
||||
web_client_counter: Arc<()>,
|
||||
guard_counter: Arc<()>,
|
||||
}
|
||||
|
||||
impl Default for NetworkInstanceManager {
|
||||
@@ -47,7 +47,7 @@ impl NetworkInstanceManager {
|
||||
stop_check_notifier: Arc::new(tokio::sync::Notify::new()),
|
||||
instance_error_messages: Arc::new(DashMap::new()),
|
||||
config_dir: None,
|
||||
web_client_counter: Arc::new(()),
|
||||
guard_counter: Arc::new(()),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -233,9 +233,9 @@ impl NetworkInstanceManager {
|
||||
self.config_dir.as_ref()
|
||||
}
|
||||
|
||||
pub(crate) fn register_web_client(&self) -> WebClientGuard {
|
||||
WebClientGuard {
|
||||
guard: Some(self.web_client_counter.clone()),
|
||||
pub(crate) fn register_daemon(&self) -> DaemonGuard {
|
||||
DaemonGuard {
|
||||
guard: Some(self.guard_counter.clone()),
|
||||
stop_check_notifier: self.stop_check_notifier.clone(),
|
||||
}
|
||||
}
|
||||
@@ -250,9 +250,9 @@ impl NetworkInstanceManager {
|
||||
.instance_map
|
||||
.iter()
|
||||
.any(|item| item.value().is_easytier_running());
|
||||
let web_client_running = Arc::strong_count(&self.web_client_counter) > 1;
|
||||
let daemon_running = Arc::strong_count(&self.guard_counter) > 1;
|
||||
|
||||
if !local_instance_running && !web_client_running {
|
||||
if !local_instance_running && !daemon_running {
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
@@ -13,11 +13,13 @@ mod vpn_portal;
|
||||
|
||||
pub mod common;
|
||||
pub mod connector;
|
||||
pub mod core;
|
||||
pub mod instance_manager;
|
||||
pub mod launcher;
|
||||
pub mod peers;
|
||||
pub mod proto;
|
||||
pub mod rpc_service;
|
||||
pub mod service_manager;
|
||||
pub mod tunnel;
|
||||
pub mod utils;
|
||||
pub mod web_client;
|
||||
|
||||
@@ -39,6 +39,7 @@ pub struct StandAloneServer<L> {
|
||||
inflight_server: Arc<AtomicU32>,
|
||||
tasks: JoinSet<()>,
|
||||
hook: Option<Arc<dyn RpcServerHook>>,
|
||||
rx_timeout: Option<Duration>,
|
||||
}
|
||||
|
||||
impl<L: TunnelListener + 'static> StandAloneServer<L> {
|
||||
@@ -50,9 +51,14 @@ impl<L: TunnelListener + 'static> StandAloneServer<L> {
|
||||
tasks: JoinSet::new(),
|
||||
|
||||
hook: None,
|
||||
rx_timeout: Some(Duration::from_secs(60)),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_rx_timeout(&mut self, timeout: Option<Duration>) {
|
||||
self.rx_timeout = timeout;
|
||||
}
|
||||
|
||||
pub fn set_hook(&mut self, hook: Arc<dyn RpcServerHook>) {
|
||||
self.hook = Some(hook);
|
||||
}
|
||||
@@ -66,6 +72,7 @@ impl<L: TunnelListener + 'static> StandAloneServer<L> {
|
||||
inflight: Arc<AtomicU32>,
|
||||
registry: Arc<ServiceRegistry>,
|
||||
hook: Arc<dyn RpcServerHook>,
|
||||
rx_timeout: Option<Duration>,
|
||||
) -> Result<(), Error> {
|
||||
let tasks = Arc::new(Mutex::new(JoinSet::new()));
|
||||
join_joinset_background(tasks.clone(), "standalone serve_loop".to_string());
|
||||
@@ -87,8 +94,7 @@ impl<L: TunnelListener + 'static> StandAloneServer<L> {
|
||||
|
||||
inflight_server.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
|
||||
tasks.lock().unwrap().spawn(async move {
|
||||
let server =
|
||||
BidirectRpcManager::new().set_rx_timeout(Some(Duration::from_secs(60)));
|
||||
let server = BidirectRpcManager::new().set_rx_timeout(rx_timeout);
|
||||
server.rpc_server().registry().replace_registry(®istry);
|
||||
server.run_with_tunnel(tunnel);
|
||||
server.wait().await;
|
||||
@@ -101,6 +107,7 @@ impl<L: TunnelListener + 'static> StandAloneServer<L> {
|
||||
pub async fn serve(&mut self) -> Result<(), Error> {
|
||||
let mut listener = self.listener.take().unwrap();
|
||||
let hook = self.hook.take().unwrap_or_else(|| Arc::new(DefaultHook));
|
||||
let rx_timeout = self.rx_timeout;
|
||||
|
||||
listener
|
||||
.listen()
|
||||
@@ -118,6 +125,7 @@ impl<L: TunnelListener + 'static> StandAloneServer<L> {
|
||||
inflight_server.clone(),
|
||||
registry.clone(),
|
||||
hook.clone(),
|
||||
rx_timeout,
|
||||
)
|
||||
.await;
|
||||
if let Err(e) = ret {
|
||||
|
||||
@@ -70,6 +70,11 @@ impl<T: TunnelListener + 'static> ApiRpcServer<T> {
|
||||
self.rpc_server.serve().await?;
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn with_rx_timeout(mut self, timeout: Option<std::time::Duration>) -> Self {
|
||||
self.rpc_server.set_rx_timeout(timeout);
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: TunnelListener + 'static> Drop for ApiRpcServer<T> {
|
||||
|
||||
@@ -26,7 +26,7 @@ impl LoggerRpcService {
|
||||
}
|
||||
}
|
||||
|
||||
fn string_to_log_level(level_str: &str) -> LogLevel {
|
||||
pub fn string_to_log_level(level_str: &str) -> LogLevel {
|
||||
match level_str.to_lowercase().as_str() {
|
||||
"off" | "disabled" => LogLevel::Disabled,
|
||||
"error" => LogLevel::Error,
|
||||
|
||||
541
easytier/src/service_manager/mod.rs
Normal file
541
easytier/src/service_manager/mod.rs
Normal file
@@ -0,0 +1,541 @@
|
||||
use std::ffi::OsString;
|
||||
use std::fmt::Write;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use service_manager::ServiceManager as _;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct ServiceInstallOptions {
|
||||
pub program: PathBuf,
|
||||
pub args: Vec<OsString>,
|
||||
pub work_directory: PathBuf,
|
||||
pub disable_autostart: bool,
|
||||
pub description: Option<String>,
|
||||
pub display_name: Option<String>,
|
||||
pub disable_restart_on_failure: bool,
|
||||
}
|
||||
|
||||
pub type ServiceStatus = service_manager::ServiceStatus;
|
||||
|
||||
trait ServiceManager: service_manager::ServiceManager {
|
||||
fn update(&self, ctx: service_manager::ServiceInstallCtx) -> std::io::Result<()>;
|
||||
}
|
||||
impl ServiceManager for service_manager::TypedServiceManager {
|
||||
fn update(&self, ctx: service_manager::ServiceInstallCtx) -> std::io::Result<()> {
|
||||
let status = self.status(service_manager::ServiceStatusCtx {
|
||||
label: ctx.label.clone(),
|
||||
})?;
|
||||
if status == ServiceStatus::Running {
|
||||
self.stop(service_manager::ServiceStopCtx {
|
||||
label: ctx.label.clone(),
|
||||
})?;
|
||||
}
|
||||
if status != ServiceStatus::NotInstalled {
|
||||
self.uninstall(service_manager::ServiceUninstallCtx {
|
||||
label: ctx.label.clone(),
|
||||
})?;
|
||||
}
|
||||
self.install(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Service {
|
||||
label: service_manager::ServiceLabel,
|
||||
kind: service_manager::ServiceManagerKind,
|
||||
service_manager: Box<dyn ServiceManager>,
|
||||
}
|
||||
|
||||
impl Service {
|
||||
pub fn new(name: String) -> Result<Self, anyhow::Error> {
|
||||
#[cfg(target_os = "windows")]
|
||||
let service_manager = Box::new(self::win_service_manager::WinServiceManager::new()?);
|
||||
#[cfg(target_os = "macos")]
|
||||
let service_manager: Box<dyn ServiceManager> =
|
||||
Box::new(service_manager::TypedServiceManager::Launchd(
|
||||
service_manager::LaunchdServiceManager::system().with_config(
|
||||
service_manager::LaunchdConfig {
|
||||
install: service_manager::LaunchdInstallConfig {
|
||||
keep_alive: service_manager::KeepAlive::conditions()
|
||||
.crashed(true)
|
||||
.successful_exit(false),
|
||||
},
|
||||
},
|
||||
),
|
||||
));
|
||||
|
||||
#[cfg(not(any(target_os = "windows", target_os = "macos")))]
|
||||
let service_manager: Box<dyn ServiceManager> =
|
||||
Box::new(service_manager::TypedServiceManager::native()?);
|
||||
|
||||
let kind = service_manager::ServiceManagerKind::native()?;
|
||||
|
||||
println!("service manager kind: {:?}", kind);
|
||||
|
||||
Ok(Self {
|
||||
label: name.parse()?,
|
||||
kind,
|
||||
service_manager,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn install(&self, options: &ServiceInstallOptions) -> Result<(), anyhow::Error> {
|
||||
let ctx = service_manager::ServiceInstallCtx {
|
||||
label: self.label.clone(),
|
||||
program: options.program.clone(),
|
||||
args: options.args.clone(),
|
||||
contents: self.make_install_content_option(options),
|
||||
autostart: !options.disable_autostart,
|
||||
username: None,
|
||||
working_directory: Some(options.work_directory.clone()),
|
||||
environment: None,
|
||||
disable_restart_on_failure: options.disable_restart_on_failure,
|
||||
};
|
||||
|
||||
if self.status()? != service_manager::ServiceStatus::NotInstalled {
|
||||
self.service_manager
|
||||
.update(ctx)
|
||||
.map_err(|e| anyhow::anyhow!("failed to update service: {:?}", e))?;
|
||||
println!("Service updated successfully! Service Name: {}", self.label);
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
self.service_manager
|
||||
.install(ctx.clone())
|
||||
.map_err(|e| anyhow::anyhow!("failed to install service: {:?}", e))?;
|
||||
|
||||
println!(
|
||||
"Service installed successfully! Service Name: {}",
|
||||
self.label
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn uninstall(&self) -> Result<(), anyhow::Error> {
|
||||
let ctx = service_manager::ServiceUninstallCtx {
|
||||
label: self.label.clone(),
|
||||
};
|
||||
let status = self.status()?;
|
||||
|
||||
if status == service_manager::ServiceStatus::NotInstalled {
|
||||
return Err(anyhow::anyhow!("Service is not installed"))?;
|
||||
}
|
||||
|
||||
if status == service_manager::ServiceStatus::Running {
|
||||
self.service_manager.stop(service_manager::ServiceStopCtx {
|
||||
label: self.label.clone(),
|
||||
})?;
|
||||
}
|
||||
|
||||
self.service_manager
|
||||
.uninstall(ctx)
|
||||
.map_err(|e| anyhow::anyhow!("failed to uninstall service: {}", e))
|
||||
}
|
||||
|
||||
pub fn status(&self) -> Result<service_manager::ServiceStatus, anyhow::Error> {
|
||||
let ctx = service_manager::ServiceStatusCtx {
|
||||
label: self.label.clone(),
|
||||
};
|
||||
let status = self.service_manager.status(ctx)?;
|
||||
|
||||
Ok(status)
|
||||
}
|
||||
|
||||
pub fn start(&self) -> Result<(), anyhow::Error> {
|
||||
let ctx = service_manager::ServiceStartCtx {
|
||||
label: self.label.clone(),
|
||||
};
|
||||
let status = self.status()?;
|
||||
|
||||
match status {
|
||||
service_manager::ServiceStatus::Running => {
|
||||
Err(anyhow::anyhow!("Service is already running"))?
|
||||
}
|
||||
service_manager::ServiceStatus::Stopped(_) => {
|
||||
self.service_manager
|
||||
.start(ctx)
|
||||
.map_err(|e| anyhow::anyhow!("failed to start service: {}", e))?;
|
||||
Ok(())
|
||||
}
|
||||
service_manager::ServiceStatus::NotInstalled => {
|
||||
Err(anyhow::anyhow!("Service is not installed"))?
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn stop(&self) -> Result<(), anyhow::Error> {
|
||||
let ctx = service_manager::ServiceStopCtx {
|
||||
label: self.label.clone(),
|
||||
};
|
||||
let status = self.status()?;
|
||||
|
||||
match status {
|
||||
service_manager::ServiceStatus::Running => {
|
||||
self.service_manager
|
||||
.stop(ctx)
|
||||
.map_err(|e| anyhow::anyhow!("failed to stop service: {}", e))?;
|
||||
Ok(())
|
||||
}
|
||||
service_manager::ServiceStatus::Stopped(_) => {
|
||||
Err(anyhow::anyhow!("Service is already stopped"))?
|
||||
}
|
||||
service_manager::ServiceStatus::NotInstalled => {
|
||||
Err(anyhow::anyhow!("Service is not installed"))?
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn make_install_content_option(&self, options: &ServiceInstallOptions) -> Option<String> {
|
||||
match self.kind {
|
||||
service_manager::ServiceManagerKind::Systemd => {
|
||||
Some(self.make_systemd_unit(options).unwrap())
|
||||
}
|
||||
service_manager::ServiceManagerKind::Rcd => {
|
||||
Some(self.make_rcd_script(options).unwrap())
|
||||
}
|
||||
service_manager::ServiceManagerKind::OpenRc => {
|
||||
Some(self.make_open_rc_script(options).unwrap())
|
||||
}
|
||||
service_manager::ServiceManagerKind::Launchd => {
|
||||
None // 使用 service-manager-rs 的默认 plist 生成
|
||||
}
|
||||
_ => {
|
||||
#[cfg(target_os = "windows")]
|
||||
{
|
||||
let win_options = self::win_service_manager::WinServiceInstallOptions {
|
||||
description: options.description.clone(),
|
||||
display_name: options.display_name.clone(),
|
||||
dependencies: Some(vec!["rpcss".to_string(), "dnscache".to_string()]),
|
||||
};
|
||||
|
||||
Some(serde_json::to_string(&win_options).unwrap())
|
||||
}
|
||||
|
||||
#[cfg(not(target_os = "windows"))]
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn make_systemd_unit(
|
||||
&self,
|
||||
options: &ServiceInstallOptions,
|
||||
) -> Result<String, std::fmt::Error> {
|
||||
let args = options
|
||||
.args
|
||||
.iter()
|
||||
.map(|a| a.to_string_lossy())
|
||||
.collect::<Vec<_>>()
|
||||
.join(" ");
|
||||
let target_app = options.program.display().to_string();
|
||||
let work_dir = options.work_directory.display().to_string();
|
||||
let mut unit_content = String::new();
|
||||
|
||||
writeln!(unit_content, "[Unit]")?;
|
||||
writeln!(unit_content, "After=network.target syslog.target")?;
|
||||
if let Some(ref d) = options.description {
|
||||
writeln!(unit_content, "Description={d}")?;
|
||||
}
|
||||
writeln!(unit_content, "StartLimitIntervalSec=0")?;
|
||||
writeln!(unit_content)?;
|
||||
writeln!(unit_content, "[Service]")?;
|
||||
writeln!(unit_content, "Type=simple")?;
|
||||
writeln!(unit_content, "WorkingDirectory={work_dir}")?;
|
||||
writeln!(unit_content, "ExecStart={target_app} {args}")?;
|
||||
writeln!(unit_content, "Restart=always")?;
|
||||
writeln!(unit_content, "RestartSec=1")?;
|
||||
writeln!(unit_content, "LimitNOFILE=infinity")?;
|
||||
writeln!(unit_content)?;
|
||||
writeln!(unit_content, "[Install]")?;
|
||||
writeln!(unit_content, "WantedBy=multi-user.target")?;
|
||||
|
||||
std::result::Result::Ok(unit_content)
|
||||
}
|
||||
|
||||
fn make_rcd_script(&self, options: &ServiceInstallOptions) -> Result<String, std::fmt::Error> {
|
||||
let name = self.label.to_qualified_name();
|
||||
let args = options
|
||||
.args
|
||||
.iter()
|
||||
.map(|a| a.to_string_lossy())
|
||||
.collect::<Vec<_>>()
|
||||
.join(" ");
|
||||
let target_app = options.program.display().to_string();
|
||||
let work_dir = options.work_directory.display().to_string();
|
||||
let mut script = String::new();
|
||||
|
||||
writeln!(script, "#!/bin/sh")?;
|
||||
writeln!(script, "#")?;
|
||||
writeln!(script, "# PROVIDE: {name}")?;
|
||||
writeln!(script, "# REQUIRE: LOGIN FILESYSTEMS NETWORKING ")?;
|
||||
writeln!(script, "# KEYWORD: shutdown")?;
|
||||
writeln!(script)?;
|
||||
writeln!(script, ". /etc/rc.subr")?;
|
||||
writeln!(script)?;
|
||||
writeln!(script, "name=\"{name}\"")?;
|
||||
if let Some(ref d) = options.description {
|
||||
writeln!(script, "desc=\"{d}\"")?;
|
||||
}
|
||||
writeln!(script, "rcvar=\"{name}_enable\"")?;
|
||||
writeln!(script)?;
|
||||
writeln!(script, "load_rc_config ${{name}}")?;
|
||||
writeln!(script)?;
|
||||
writeln!(script, ": ${{{name}_options=\"{args}\"}}")?;
|
||||
writeln!(script)?;
|
||||
writeln!(script, "{name}_chdir=\"{work_dir}\"")?;
|
||||
writeln!(script, "pidfile=\"/var/run/${{name}}.pid\"")?;
|
||||
writeln!(script, "procname=\"{target_app}\"")?;
|
||||
writeln!(script, "command=\"/usr/sbin/daemon\"")?;
|
||||
writeln!(
|
||||
script,
|
||||
"command_args=\"-c -S -T ${{name}} -p ${{pidfile}} ${{procname}} ${{{name}_options}}\""
|
||||
)?;
|
||||
writeln!(script)?;
|
||||
writeln!(script, "run_rc_command \"$1\"")?;
|
||||
|
||||
std::result::Result::Ok(script)
|
||||
}
|
||||
|
||||
fn make_open_rc_script(
|
||||
&self,
|
||||
options: &ServiceInstallOptions,
|
||||
) -> Result<String, std::fmt::Error> {
|
||||
let args = options
|
||||
.args
|
||||
.iter()
|
||||
.map(|a| a.to_string_lossy())
|
||||
.collect::<Vec<_>>()
|
||||
.join(" ");
|
||||
let target_app = options.program.display().to_string();
|
||||
let work_dir = options.work_directory.display().to_string();
|
||||
let mut script = String::new();
|
||||
|
||||
writeln!(script, "#!/sbin/openrc-run")?;
|
||||
writeln!(script)?;
|
||||
if let Some(ref d) = options.description {
|
||||
writeln!(script, "description=\"{d}\"")?;
|
||||
}
|
||||
writeln!(script, "command=\"{target_app}\"")?;
|
||||
writeln!(script, "command_args=\"{args}\"")?;
|
||||
writeln!(script, "pidfile=\"/run/${{RC_SVCNAME}}.pid\"")?;
|
||||
writeln!(script, "command_background=\"yes\"")?;
|
||||
writeln!(script, "directory=\"{work_dir}\"")?;
|
||||
writeln!(script)?;
|
||||
writeln!(script, "depend() {{")?;
|
||||
writeln!(script, " need net")?;
|
||||
writeln!(script, " use looger")?;
|
||||
writeln!(script, "}}")?;
|
||||
|
||||
std::result::Result::Ok(script)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
mod win_service_manager {
|
||||
use std::{ffi::OsStr, ffi::OsString, io, path::PathBuf};
|
||||
use windows_service::{
|
||||
service::{
|
||||
ServiceAccess, ServiceDependency, ServiceErrorControl, ServiceInfo, ServiceStartType,
|
||||
ServiceType,
|
||||
},
|
||||
service_manager::{ServiceManager, ServiceManagerAccess},
|
||||
};
|
||||
|
||||
use service_manager::{
|
||||
ServiceInstallCtx, ServiceLevel, ServiceStartCtx, ServiceStatus, ServiceStatusCtx,
|
||||
ServiceStopCtx, ServiceUninstallCtx,
|
||||
};
|
||||
|
||||
use winreg::{enums::*, RegKey};
|
||||
|
||||
use crate::common::constants::WIN_SERVICE_WORK_DIR_REG_KEY;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct WinServiceInstallOptions {
|
||||
pub dependencies: Option<Vec<String>>,
|
||||
pub description: Option<String>,
|
||||
pub display_name: Option<String>,
|
||||
}
|
||||
|
||||
pub struct WinServiceManager {
|
||||
service_manager: ServiceManager,
|
||||
}
|
||||
|
||||
fn generate_service_info(ctx: &ServiceInstallCtx) -> (ServiceInfo, Option<OsString>) {
|
||||
let start_type = if ctx.autostart {
|
||||
ServiceStartType::AutoStart
|
||||
} else {
|
||||
ServiceStartType::OnDemand
|
||||
};
|
||||
let srv_name = OsString::from(ctx.label.to_qualified_name());
|
||||
let mut dis_name = srv_name.clone();
|
||||
let mut description: Option<OsString> = None;
|
||||
let mut dependencies = Vec::<ServiceDependency>::new();
|
||||
|
||||
if let Some(s) = ctx.contents.as_ref() {
|
||||
let options: WinServiceInstallOptions = serde_json::from_str(s.as_str()).unwrap();
|
||||
if let Some(d) = options.dependencies {
|
||||
dependencies = d
|
||||
.iter()
|
||||
.map(|dep| ServiceDependency::Service(OsString::from(dep.clone())))
|
||||
.collect::<Vec<_>>();
|
||||
}
|
||||
if let Some(d) = options.description {
|
||||
description = Some(OsString::from(d));
|
||||
}
|
||||
if let Some(d) = options.display_name {
|
||||
dis_name = OsString::from(d);
|
||||
}
|
||||
}
|
||||
|
||||
let service_info = ServiceInfo {
|
||||
name: srv_name,
|
||||
display_name: dis_name,
|
||||
service_type: ServiceType::OWN_PROCESS,
|
||||
start_type,
|
||||
error_control: ServiceErrorControl::Normal,
|
||||
executable_path: ctx.program.clone(),
|
||||
launch_arguments: ctx.args.clone(),
|
||||
dependencies: dependencies.clone(),
|
||||
account_name: None,
|
||||
account_password: None,
|
||||
};
|
||||
|
||||
(service_info, description)
|
||||
}
|
||||
|
||||
impl WinServiceManager {
|
||||
pub fn new() -> Result<Self, anyhow::Error> {
|
||||
let service_manager =
|
||||
ServiceManager::local_computer(None::<&str>, ServiceManagerAccess::ALL_ACCESS)?;
|
||||
Ok(Self { service_manager })
|
||||
}
|
||||
}
|
||||
impl service_manager::ServiceManager for WinServiceManager {
|
||||
fn available(&self) -> io::Result<bool> {
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
fn install(&self, ctx: ServiceInstallCtx) -> io::Result<()> {
|
||||
let (service_info, description) = generate_service_info(&ctx);
|
||||
|
||||
let service = self
|
||||
.service_manager
|
||||
.create_service(&service_info, ServiceAccess::ALL_ACCESS)
|
||||
.map_err(io::Error::other)?;
|
||||
|
||||
if let Some(s) = description {
|
||||
service
|
||||
.set_description(s.clone())
|
||||
.map_err(io::Error::other)?;
|
||||
}
|
||||
|
||||
if let Some(work_dir) = ctx.working_directory {
|
||||
set_service_work_directory(&ctx.label.to_qualified_name(), work_dir)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn uninstall(&self, ctx: ServiceUninstallCtx) -> io::Result<()> {
|
||||
let service = self
|
||||
.service_manager
|
||||
.open_service(ctx.label.to_qualified_name(), ServiceAccess::ALL_ACCESS)
|
||||
.map_err(io::Error::other)?;
|
||||
|
||||
service.delete().map_err(io::Error::other)
|
||||
}
|
||||
|
||||
fn start(&self, ctx: ServiceStartCtx) -> io::Result<()> {
|
||||
let service = self
|
||||
.service_manager
|
||||
.open_service(ctx.label.to_qualified_name(), ServiceAccess::ALL_ACCESS)
|
||||
.map_err(io::Error::other)?;
|
||||
|
||||
service.start(&[] as &[&OsStr]).map_err(io::Error::other)
|
||||
}
|
||||
|
||||
fn stop(&self, ctx: ServiceStopCtx) -> io::Result<()> {
|
||||
let service = self
|
||||
.service_manager
|
||||
.open_service(ctx.label.to_qualified_name(), ServiceAccess::ALL_ACCESS)
|
||||
.map_err(io::Error::other)?;
|
||||
|
||||
_ = service.stop().map_err(io::Error::other)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn level(&self) -> ServiceLevel {
|
||||
ServiceLevel::System
|
||||
}
|
||||
|
||||
fn set_level(&mut self, level: ServiceLevel) -> io::Result<()> {
|
||||
match level {
|
||||
ServiceLevel::System => Ok(()),
|
||||
_ => Err(io::Error::other("Unsupported service level")),
|
||||
}
|
||||
}
|
||||
|
||||
fn status(&self, ctx: ServiceStatusCtx) -> io::Result<ServiceStatus> {
|
||||
let service = match self
|
||||
.service_manager
|
||||
.open_service(ctx.label.to_qualified_name(), ServiceAccess::QUERY_STATUS)
|
||||
{
|
||||
Ok(s) => s,
|
||||
Err(e) => {
|
||||
if let windows_service::Error::Winapi(ref win_err) = e {
|
||||
if win_err.raw_os_error() == Some(0x424) {
|
||||
return Ok(ServiceStatus::NotInstalled);
|
||||
}
|
||||
}
|
||||
return Err(io::Error::other(e));
|
||||
}
|
||||
};
|
||||
|
||||
let status = service.query_status().map_err(io::Error::other)?;
|
||||
|
||||
match status.current_state {
|
||||
windows_service::service::ServiceState::Stopped => Ok(ServiceStatus::Stopped(None)),
|
||||
_ => Ok(ServiceStatus::Running),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl super::ServiceManager for WinServiceManager {
|
||||
fn update(&self, ctx: service_manager::ServiceInstallCtx) -> io::Result<()> {
|
||||
let (service_info, description) = generate_service_info(&ctx);
|
||||
|
||||
let service = self
|
||||
.service_manager
|
||||
.open_service(ctx.label.to_qualified_name(), ServiceAccess::ALL_ACCESS)
|
||||
.map_err(io::Error::other)?;
|
||||
|
||||
service
|
||||
.change_config(&service_info)
|
||||
.map_err(io::Error::other)?;
|
||||
|
||||
if let Some(s) = description {
|
||||
service
|
||||
.set_description(s.clone())
|
||||
.map_err(io::Error::other)?;
|
||||
}
|
||||
|
||||
if let Some(work_dir) = ctx.working_directory {
|
||||
set_service_work_directory(&ctx.label.to_qualified_name(), work_dir)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn set_service_work_directory(service_name: &str, work_directory: PathBuf) -> io::Result<()> {
|
||||
let (reg_key, _) =
|
||||
RegKey::predef(HKEY_LOCAL_MACHINE).create_subkey(WIN_SERVICE_WORK_DIR_REG_KEY)?;
|
||||
reg_key
|
||||
.set_value::<OsString, _>(service_name, &work_directory.as_os_str().to_os_string())?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,7 @@ use crate::{
|
||||
set_default_machine_id, stun::MockStunInfoCollector,
|
||||
},
|
||||
connector::create_connector_by_url,
|
||||
instance_manager::{NetworkInstanceManager, WebClientGuard},
|
||||
instance_manager::{DaemonGuard, NetworkInstanceManager},
|
||||
proto::common::NatType,
|
||||
tunnel::{IpVersion, TunnelConnector},
|
||||
};
|
||||
@@ -19,7 +19,7 @@ pub mod session;
|
||||
pub struct WebClient {
|
||||
controller: Arc<controller::Controller>,
|
||||
tasks: ScopedTask<()>,
|
||||
manager_guard: WebClientGuard,
|
||||
manager_guard: DaemonGuard,
|
||||
}
|
||||
|
||||
impl WebClient {
|
||||
@@ -29,7 +29,7 @@ impl WebClient {
|
||||
hostname: H,
|
||||
manager: Arc<NetworkInstanceManager>,
|
||||
) -> Self {
|
||||
let manager_guard = manager.register_web_client();
|
||||
let manager_guard = manager.register_daemon();
|
||||
let controller = Arc::new(controller::Controller::new(
|
||||
token.to_string(),
|
||||
hostname.to_string(),
|
||||
|
||||
Reference in New Issue
Block a user