Address omissions

This commit is contained in:
chao wan 2024-11-16 02:31:19 +08:00
parent d5c9c588d4
commit 7dcc0ae70e
2 changed files with 222 additions and 207 deletions

View File

@ -1,19 +1,13 @@
use std::{ use std::{
ffi::OsString, ffi::OsString, fmt::Write, net::SocketAddr, path::PathBuf, sync::Mutex, time::Duration, vec,
fmt::Write,
net::SocketAddr,
path::PathBuf,
sync::Mutex,
time::Duration,
vec
}; };
use anyhow::{Context, Ok}; use anyhow::{Context, Ok};
use clap::{command, Args, Parser, Subcommand}; use clap::{command, Args, Parser, Subcommand};
use humansize::format_size; use humansize::format_size;
use service_manager::*;
use tabled::settings::Style; use tabled::settings::Style;
use tokio::time::timeout; use tokio::time::timeout;
use service_manager::*;
use easytier::{ use easytier::{
common::{ common::{
@ -35,7 +29,7 @@ use easytier::{
rpc_types::controller::BaseController, rpc_types::controller::BaseController,
}, },
tunnel::tcp::TcpTunnelConnector, tunnel::tcp::TcpTunnelConnector,
utils::{cost_to_str, float_to_str, PeerRoutePair}, utils::{cost_to_str, float_to_str, PeerRoutePair},
}; };
rust_i18n::i18n!("locales", fallback = "en"); rust_i18n::i18n!("locales", fallback = "en");
@ -63,7 +57,7 @@ enum SubCommand {
PeerCenter, PeerCenter,
VpnPortal, VpnPortal,
Node(NodeArgs), Node(NodeArgs),
Service(ServiceArgs) Service(ServiceArgs),
} }
#[derive(Args, Debug)] #[derive(Args, Debug)]
@ -84,7 +78,7 @@ enum PeerSubCommand {
Remove, Remove,
List(PeerListArgs), List(PeerListArgs),
ListForeign, ListForeign,
ListGlobalForeign, ListGlobalForeign,
} }
#[derive(Args, Debug)] #[derive(Args, Debug)]
@ -131,12 +125,12 @@ struct NodeArgs {
} }
#[derive(Args, Debug)] #[derive(Args, Debug)]
struct ServiceArgs{ struct ServiceArgs {
#[arg(short, long, default_value = env!("CARGO_PKG_NAME"), help = "service name")] #[arg(short, long, default_value = env!("CARGO_PKG_NAME"), help = "service name")]
name: String, name: String,
#[command(subcommand)] #[command(subcommand)]
sub_command: ServiceSubCommand sub_command: ServiceSubCommand,
} }
#[derive(Subcommand, Debug)] #[derive(Subcommand, Debug)]
@ -145,7 +139,7 @@ enum ServiceSubCommand {
Uninstall, Uninstall,
Status, Status,
Start, Start,
Stop Stop,
} }
#[derive(Args, Debug)] #[derive(Args, Debug)]
@ -166,7 +160,7 @@ struct InstallArgs {
service_work_dir: Option<PathBuf>, service_work_dir: Option<PathBuf>,
#[arg(trailing_var_arg = true, allow_hyphen_values = true)] #[arg(trailing_var_arg = true, allow_hyphen_values = true)]
core_args: Option<Vec<OsString>> core_args: Option<Vec<OsString>>,
} }
type Error = anyhow::Error; type Error = anyhow::Error;
@ -527,24 +521,22 @@ impl CommandHandler {
pub struct ServiceInstallOptions { pub struct ServiceInstallOptions {
pub program: PathBuf, pub program: PathBuf,
pub args: Vec<OsString>, pub args: Vec<OsString>,
pub work_directory: PathBuf, pub work_directory: PathBuf,
pub disable_autostart: bool, pub disable_autostart: bool,
pub description: Option<String>, pub description: Option<String>,
pub display_name: Option<String>, pub display_name: Option<String>,
} }
pub struct Service{ pub struct Service {
lable: ServiceLabel, lable: ServiceLabel,
kind: ServiceManagerKind, kind: ServiceManagerKind,
service_manager: Box<dyn ServiceManager> service_manager: Box<dyn ServiceManager>,
} }
impl Service { impl Service {
pub fn new(name: String) -> Result<Self, Error> { pub fn new(name: String) -> Result<Self, Error> {
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
let service_manager = Box::new( let service_manager = Box::new(crate::win_service_manager::WinServiceManager::new()?);
crate::win_service_manager::WinServiceManager::new()?
);
#[cfg(not(target_os = "windows"))] #[cfg(not(target_os = "windows"))]
let service_manager = <dyn ServiceManager>::native()?; let service_manager = <dyn ServiceManager>::native()?;
@ -553,7 +545,7 @@ impl Service {
Ok(Self { Ok(Self {
lable: name.parse()?, lable: name.parse()?,
kind, kind,
service_manager service_manager,
}) })
} }
@ -572,7 +564,9 @@ impl Service {
return Err(anyhow::anyhow!("Service is already installed")); return Err(anyhow::anyhow!("Service is already installed"));
} }
self.service_manager.install(ctx).map_err(|e| anyhow::anyhow!("failed to install service: {}", e)) self.service_manager
.install(ctx)
.map_err(|e| anyhow::anyhow!("failed to install service: {}", e))
} }
pub fn uninstall(&self) -> Result<(), Error> { pub fn uninstall(&self) -> Result<(), Error> {
@ -591,7 +585,9 @@ impl Service {
})?; })?;
} }
self.service_manager.uninstall(ctx).map_err(|e| anyhow::anyhow!("failed to uninstall service: {}", e)) self.service_manager
.uninstall(ctx)
.map_err(|e| anyhow::anyhow!("failed to uninstall service: {}", e))
} }
pub fn status(&self) -> Result<ServiceStatus, Error> { pub fn status(&self) -> Result<ServiceStatus, Error> {
@ -610,16 +606,14 @@ impl Service {
let status = self.status()?; let status = self.status()?;
match status { match status {
ServiceStatus::Running => { ServiceStatus::Running => Err(anyhow::anyhow!("Service is already running")),
Err(anyhow::anyhow!("Service is already running"))
}
ServiceStatus::Stopped(_) => { ServiceStatus::Stopped(_) => {
self.service_manager.start(ctx).map_err(|e| anyhow::anyhow!("failed to start service: {}", e))?; self.service_manager
.start(ctx)
.map_err(|e| anyhow::anyhow!("failed to start service: {}", e))?;
Ok(()) Ok(())
} }
ServiceStatus::NotInstalled => { ServiceStatus::NotInstalled => Err(anyhow::anyhow!("Service is not installed")),
Err(anyhow::anyhow!("Service is not installed"))
}
} }
} }
@ -631,82 +625,85 @@ impl Service {
match status { match status {
ServiceStatus::Running => { ServiceStatus::Running => {
self.service_manager.stop(ctx).map_err(|e| anyhow::anyhow!("failed to stop service: {}", e))?; self.service_manager
.stop(ctx)
.map_err(|e| anyhow::anyhow!("failed to stop service: {}", e))?;
Ok(()) Ok(())
} }
ServiceStatus::Stopped(_) => { ServiceStatus::Stopped(_) => Err(anyhow::anyhow!("Service is already stopped")),
Err(anyhow::anyhow!("Service is already stopped")) ServiceStatus::NotInstalled => Err(anyhow::anyhow!("Service is not installed")),
}
ServiceStatus::NotInstalled => {
Err(anyhow::anyhow!("Service is not installed"))
}
} }
} }
fn make_install_content_option(&self, options: &ServiceInstallOptions) -> Option<String> { fn make_install_content_option(&self, options: &ServiceInstallOptions) -> Option<String> {
match self.kind { match self.kind {
ServiceManagerKind::Systemd => { ServiceManagerKind::Systemd => Some(self.make_systemd_unit(options).unwrap()),
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::Rcd => {
Some(self.make_rcd_script(options).unwrap())
}
ServiceManagerKind::OpenRc => {
Some(self.make_open_rc_script(options).unwrap())
}
_ => { _ => {
#[cfg(target_os = "windows")] { #[cfg(target_os = "windows")]
let win_options = win_service_manager::WinServiceInstallOptions{ {
let win_options = win_service_manager::WinServiceInstallOptions {
description: options.description.clone(), description: options.description.clone(),
display_name: options.display_name.clone(), display_name: options.display_name.clone(),
dependencies: Some(vec!["rpcss".to_string(), "dnscache".to_string()]) dependencies: Some(vec!["rpcss".to_string(), "dnscache".to_string()]),
}; };
Some(serde_json::to_string(&win_options).unwrap()) Some(serde_json::to_string(&win_options).unwrap())
} }
#[cfg(not(target_os = "windows"))] #[cfg(not(target_os = "windows"))]
None None
} }
} }
} }
fn make_systemd_unit(&self, options: &ServiceInstallOptions) -> Result<String, std::fmt::Error> { fn make_systemd_unit(
let args = options.args.iter() &self,
options: &ServiceInstallOptions,
) -> Result<String, std::fmt::Error> {
let args = options
.args
.iter()
.map(|a| a.to_string_lossy()) .map(|a| a.to_string_lossy())
.collect::<Vec<_>>() .collect::<Vec<_>>()
.join(" "); .join(" ");
let target_app = options.program.display().to_string(); let target_app = options.program.display().to_string();
let work_dir = options.work_directory.display().to_string(); let work_dir = options.work_directory.display().to_string();
let mut unit_content = String::new(); let mut unit_content = String::new();
writeln!(unit_content, "[Unit]")?; writeln!(unit_content, "[Unit]")?;
writeln!(unit_content, "After=network.target syslog.target")?; writeln!(unit_content, "After=network.target syslog.target")?;
if let Some(ref d) = options.description { if let Some(ref d) = options.description {
writeln!(unit_content, "Description={d}")?; writeln!(unit_content, "Description={d}")?;
} }
writeln!(unit_content, "StartLimitIntervalSec=0")?;
writeln!(unit_content)?;
writeln!(unit_content, "[Service]")?; writeln!(unit_content, "[Service]")?;
writeln!(unit_content, "Type=simple")?; writeln!(unit_content, "Type=simple")?;
writeln!(unit_content, "WorkingDirectory={work_dir}")?; writeln!(unit_content, "WorkingDirectory={work_dir}")?;
writeln!(unit_content, "ExecStart={target_app} {args}")?; writeln!(unit_content, "ExecStart={target_app} {args}")?;
writeln!(unit_content, "Restart=Always")?;
writeln!(unit_content, "LimitNOFILE=infinity")?; writeln!(unit_content, "LimitNOFILE=infinity")?;
writeln!(unit_content)?; writeln!(unit_content)?;
writeln!(unit_content, "[Install]")?; writeln!(unit_content, "[Install]")?;
writeln!(unit_content, "WantedBy=multi-user.target")?; writeln!(unit_content, "WantedBy=multi-user.target")?;
std::result::Result::Ok(unit_content) std::result::Result::Ok(unit_content)
} }
fn make_rcd_script(&self, options: &ServiceInstallOptions) -> Result<String, std::fmt::Error> { fn make_rcd_script(&self, options: &ServiceInstallOptions) -> Result<String, std::fmt::Error> {
let name = self.lable.to_qualified_name(); let name = self.lable.to_qualified_name();
let args = options.args.iter() let args = options
.args
.iter()
.map(|a| a.to_string_lossy()) .map(|a| a.to_string_lossy())
.collect::<Vec<_>>() .collect::<Vec<_>>()
.join(" "); .join(" ");
let target_app = options.program.display().to_string(); let target_app = options.program.display().to_string();
let work_dir = options.work_directory.display().to_string(); let work_dir = options.work_directory.display().to_string();
let mut script = String::new(); let mut script = String::new();
writeln!(script, "#!/bin/sh")?; writeln!(script, "#!/bin/sh")?;
writeln!(script, "#")?; writeln!(script, "#")?;
writeln!(script, "# PROVIDE: {name}")?; writeln!(script, "# PROVIDE: {name}")?;
@ -729,22 +726,30 @@ impl Service {
writeln!(script, "pidfile=\"/var/run/${{name}}.pid\"")?; writeln!(script, "pidfile=\"/var/run/${{name}}.pid\"")?;
writeln!(script, "procname=\"{target_app}\"")?; writeln!(script, "procname=\"{target_app}\"")?;
writeln!(script, "command=\"/usr/sbin/daemon\"")?; writeln!(script, "command=\"/usr/sbin/daemon\"")?;
writeln!(script, "command_args=\"-c -S -T ${{name}} -p ${{pidfile}} ${{procname}} ${{{name}_options}}\"")?; writeln!(
script,
"command_args=\"-c -S -T ${{name}} -p ${{pidfile}} ${{procname}} ${{{name}_options}}\""
)?;
writeln!(script)?; writeln!(script)?;
writeln!(script, "run_rc_command \"$1\"")?; writeln!(script, "run_rc_command \"$1\"")?;
std::result::Result::Ok(script) std::result::Result::Ok(script)
} }
fn make_open_rc_script(&self, options: &ServiceInstallOptions) -> Result<String, std::fmt::Error> { fn make_open_rc_script(
let args = options.args.iter() &self,
options: &ServiceInstallOptions,
) -> Result<String, std::fmt::Error> {
let args = options
.args
.iter()
.map(|a| a.to_string_lossy()) .map(|a| a.to_string_lossy())
.collect::<Vec<_>>() .collect::<Vec<_>>()
.join(" "); .join(" ");
let target_app = options.program.display().to_string(); let target_app = options.program.display().to_string();
let work_dir = options.work_directory.display().to_string(); let work_dir = options.work_directory.display().to_string();
let mut script = String::new(); let mut script = String::new();
writeln!(script, "#!/sbin/openrc-run")?; writeln!(script, "#!/sbin/openrc-run")?;
writeln!(script)?; writeln!(script)?;
if let Some(ref d) = options.description { if let Some(ref d) = options.description {
@ -762,11 +767,9 @@ impl Service {
writeln!(script, "}}")?; writeln!(script, "}}")?;
std::result::Result::Ok(script) std::result::Result::Ok(script)
} }
} }
#[tokio::main] #[tokio::main]
#[tracing::instrument] #[tracing::instrument]
async fn main() -> Result<(), Error> { async fn main() -> Result<(), Error> {
@ -932,7 +935,7 @@ async fn main() -> Result<(), Error> {
SubCommand::Service(service_args) => { SubCommand::Service(service_args) => {
let service = Service::new(service_args.name)?; let service = Service::new(service_args.name)?;
match service_args.sub_command { match service_args.sub_command {
ServiceSubCommand::Install(install_args) => { ServiceSubCommand::Install(install_args) => {
let bin_path = install_args.core_path.unwrap_or_else(|| { let bin_path = install_args.core_path.unwrap_or_else(|| {
let mut ret = std::env::current_exe() let mut ret = std::env::current_exe()
.unwrap() .unwrap()
@ -959,13 +962,17 @@ async fn main() -> Result<(), Error> {
}); });
let work_dir = std::fs::canonicalize(&work_dir).map_err(|e| { let work_dir = std::fs::canonicalize(&work_dir).map_err(|e| {
anyhow::anyhow!("failed to get service work directory[{}]: {}", work_dir.display(), e) anyhow::anyhow!(
"failed to get service work directory[{}]: {}",
work_dir.display(),
e
)
})?; })?;
if !work_dir.is_dir() { if !work_dir.is_dir() {
return Err(anyhow::anyhow!("work directory is not a directory")); return Err(anyhow::anyhow!("work directory is not a directory"));
} }
let install_options = ServiceInstallOptions { let install_options = ServiceInstallOptions {
program: bin_path, program: bin_path,
args: bin_args, args: bin_args,
@ -1002,45 +1009,25 @@ async fn main() -> Result<(), Error> {
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
mod win_service_manager { mod win_service_manager {
use std::{ffi::OsStr, ffi::OsString, io, path::PathBuf};
use windows_service::{ use windows_service::{
service::{ service::{
ServiceAccess, ServiceDependency, ServiceErrorControl, ServiceInfo, ServiceStartType,
ServiceType, ServiceType,
ServiceErrorControl,
ServiceDependency,
ServiceInfo,
ServiceStartType,
ServiceAccess
}, },
service_manager::{ service_manager::{ServiceManager, ServiceManagerAccess},
ServiceManagerAccess,
ServiceManager
}
};
use std::{
io,
ffi::OsString,
ffi::OsStr,
path::PathBuf
}; };
use service_manager::{ use service_manager::{
ServiceInstallCtx, ServiceInstallCtx, ServiceLevel, ServiceStartCtx, ServiceStatus, ServiceStatusCtx,
ServiceLevel, ServiceStopCtx, ServiceUninstallCtx,
ServiceStartCtx,
ServiceStatus,
ServiceStatusCtx,
ServiceUninstallCtx,
ServiceStopCtx
}; };
use winreg::{enums::*, RegKey}; use winreg::{enums::*, RegKey};
use easytier::common::constants::WIN_SERVICE_WORK_DIR_REG_KEY; use easytier::common::constants::WIN_SERVICE_WORK_DIR_REG_KEY;
use serde::{ use serde::{Deserialize, Serialize};
Serialize,
Deserialize
};
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
pub struct WinServiceInstallOptions { pub struct WinServiceInstallOptions {
@ -1050,36 +1037,39 @@ mod win_service_manager {
} }
pub struct WinServiceManager { pub struct WinServiceManager {
service_manager: ServiceManager service_manager: ServiceManager,
} }
impl WinServiceManager { impl WinServiceManager {
pub fn new() -> Result<Self, crate::Error> { pub fn new() -> Result<Self, crate::Error> {
let service_manager = ServiceManager::local_computer( let service_manager =
None::<&str>, ServiceManager::local_computer(None::<&str>, ServiceManagerAccess::ALL_ACCESS)?;
ServiceManagerAccess::ALL_ACCESS, Ok(Self { service_manager })
)?;
Ok(Self {
service_manager
})
} }
} }
impl service_manager::ServiceManager for WinServiceManager { impl service_manager::ServiceManager for WinServiceManager {
fn available(&self) -> io::Result<bool> { fn available(&self) -> io::Result<bool> {
Ok(true) Ok(true)
} }
fn install(&self, ctx: ServiceInstallCtx) -> io::Result<()> { fn install(&self, ctx: ServiceInstallCtx) -> io::Result<()> {
let start_type_ = if ctx.autostart { ServiceStartType::AutoStart } else { ServiceStartType::OnDemand }; let start_type_ = if ctx.autostart {
ServiceStartType::AutoStart
} else {
ServiceStartType::OnDemand
};
let srv_name = OsString::from(ctx.label.to_qualified_name()); let srv_name = OsString::from(ctx.label.to_qualified_name());
let mut dis_name = srv_name.clone(); let mut dis_name = srv_name.clone();
let mut description: Option<OsString> = None; let mut description: Option<OsString> = None;
let mut dependencies = Vec::<ServiceDependency>::new(); let mut dependencies = Vec::<ServiceDependency>::new();
if let Some(s) = ctx.contents.as_ref() { if let Some(s) = ctx.contents.as_ref() {
let options: WinServiceInstallOptions = serde_json::from_str(s.as_str()).unwrap(); let options: WinServiceInstallOptions = serde_json::from_str(s.as_str()).unwrap();
if let Some(d) = options.dependencies { if let Some(d) = options.dependencies {
dependencies = d.iter().map(|dep| ServiceDependency::Service(OsString::from(dep.clone()))).collect::<Vec<_>>(); dependencies = d
.iter()
.map(|dep| ServiceDependency::Service(OsString::from(dep.clone())))
.collect::<Vec<_>>();
} }
if let Some(d) = options.description { if let Some(d) = options.description {
description = Some(OsString::from(d)); description = Some(OsString::from(d));
@ -1099,71 +1089,81 @@ mod win_service_manager {
launch_arguments: ctx.args, launch_arguments: ctx.args,
dependencies: dependencies.clone(), dependencies: dependencies.clone(),
account_name: None, account_name: None,
account_password: None account_password: None,
}; };
let service = self.service_manager.create_service(&service_info, ServiceAccess::ALL_ACCESS).map_err(|e| { let service = self
io::Error::new(io::ErrorKind::Other, e) .service_manager
})?; .create_service(&service_info, ServiceAccess::ALL_ACCESS)
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
if let Some(s) = description { if let Some(s) = description {
service.set_description(s.clone()).map_err(|e| { service
io::Error::new(io::ErrorKind::Other, e) .set_description(s.clone())
})?; .map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
} }
if let Some(work_dir) = ctx.working_directory { if let Some(work_dir) = ctx.working_directory {
set_service_work_directory(&ctx.label.to_qualified_name(), work_dir)?; set_service_work_directory(&ctx.label.to_qualified_name(), work_dir)?;
} }
Ok(()) Ok(())
} }
fn uninstall(&self, ctx: ServiceUninstallCtx) -> io::Result<()> { fn uninstall(&self, ctx: ServiceUninstallCtx) -> io::Result<()> {
let service = self.service_manager.open_service(ctx.label.to_qualified_name(), ServiceAccess::ALL_ACCESS).map_err(|e|{ let service = self
io::Error::new(io::ErrorKind::Other, e) .service_manager
})?; .open_service(ctx.label.to_qualified_name(), ServiceAccess::ALL_ACCESS)
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
service.delete().map_err(|e|{
io::Error::new(io::ErrorKind::Other, e) service
}) .delete()
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))
} }
fn start(&self, ctx: ServiceStartCtx) -> io::Result<()> { fn start(&self, ctx: ServiceStartCtx) -> io::Result<()> {
let service = self.service_manager.open_service(ctx.label.to_qualified_name(), ServiceAccess::ALL_ACCESS).map_err(|e|{ let service = self
io::Error::new(io::ErrorKind::Other, e) .service_manager
})?; .open_service(ctx.label.to_qualified_name(), ServiceAccess::ALL_ACCESS)
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
service.start(&[] as &[&OsStr]).map_err(|e|{
io::Error::new(io::ErrorKind::Other, e) service
}) .start(&[] as &[&OsStr])
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))
} }
fn stop(&self, ctx: ServiceStopCtx) -> io::Result<()> { fn stop(&self, ctx: ServiceStopCtx) -> io::Result<()> {
let service = self.service_manager.open_service(ctx.label.to_qualified_name(), ServiceAccess::ALL_ACCESS).map_err(|e|{ let service = self
io::Error::new(io::ErrorKind::Other, e) .service_manager
})?; .open_service(ctx.label.to_qualified_name(), ServiceAccess::ALL_ACCESS)
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
_ = service.stop().map_err(|e|{
io::Error::new(io::ErrorKind::Other, e) _ = service
})?; .stop()
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
Ok(()) Ok(())
} }
fn level(&self) -> ServiceLevel { fn level(&self) -> ServiceLevel {
ServiceLevel::System ServiceLevel::System
} }
fn set_level(&mut self, level: ServiceLevel) -> io::Result<()> { fn set_level(&mut self, level: ServiceLevel) -> io::Result<()> {
match level { match level {
ServiceLevel::System => Ok(()), ServiceLevel::System => Ok(()),
_ => Err(io::Error::new(io::ErrorKind::Other, "Unsupported service level")) _ => Err(io::Error::new(
io::ErrorKind::Other,
"Unsupported service level",
)),
} }
} }
fn status(&self, ctx: ServiceStatusCtx) -> io::Result<ServiceStatus> { fn status(&self, ctx: ServiceStatusCtx) -> io::Result<ServiceStatus> {
let service = match self.service_manager.open_service(ctx.label.to_qualified_name(), ServiceAccess::QUERY_STATUS) { let service = match self
.service_manager
.open_service(ctx.label.to_qualified_name(), ServiceAccess::QUERY_STATUS)
{
Ok(s) => s, Ok(s) => s,
Err(e) => { Err(e) => {
if let windows_service::Error::Winapi(ref win_err) = e { if let windows_service::Error::Winapi(ref win_err) = e {
@ -1174,11 +1174,11 @@ mod win_service_manager {
return Err(io::Error::new(io::ErrorKind::Other, e)); return Err(io::Error::new(io::ErrorKind::Other, e));
} }
}; };
let status = service.query_status().map_err(|e|{ let status = service
io::Error::new(io::ErrorKind::Other, e) .query_status()
})?; .map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
match status.current_state { match status.current_state {
windows_service::service::ServiceState::Stopped => Ok(ServiceStatus::Stopped(None)), windows_service::service::ServiceState::Stopped => Ok(ServiceStatus::Stopped(None)),
_ => Ok(ServiceStatus::Running), _ => Ok(ServiceStatus::Running),
@ -1187,8 +1187,10 @@ mod win_service_manager {
} }
fn set_service_work_directory(service_name: &str, work_directory: PathBuf) -> io::Result<()> { 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)?; let (reg_key, _) =
reg_key.set_value::<OsString, _>(service_name, &work_directory.as_os_str().to_os_string())?; 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(()) Ok(())
} }
} }

View File

@ -357,7 +357,7 @@ impl Cli {
} }
return Ok("0.0.0.0:0".parse().unwrap()); return Ok("0.0.0.0:0".parse().unwrap());
} }
return Ok("0.0.0.0:0".parse().unwrap()); return Ok(format!("0.0.0.0:{}", port).parse().unwrap());
} }
Ok(rpc_portal.parse()?) Ok(rpc_portal.parse()?)
@ -374,28 +374,32 @@ impl TryFrom<&Cli> for TomlConfigLoader {
config_file config_file
); );
return Ok(TomlConfigLoader::new(config_file) return Ok(TomlConfigLoader::new(config_file)
.with_context(|| format!("failed to load config file: {:?}", cli.config_file))?) .with_context(|| format!("failed to load config file: {:?}", cli.config_file))?);
} }
let cfg = TomlConfigLoader::default(); let cfg = TomlConfigLoader::default();
cfg.set_hostname(cli.hostname.clone()); cfg.set_hostname(cli.hostname.clone());
cfg.set_network_identity(NetworkIdentity::new(cli.network_name.clone(), cli.network_secret.clone())); cfg.set_network_identity(NetworkIdentity::new(
cli.network_name.clone(),
cli.network_secret.clone(),
));
cfg.set_dhcp(cli.dhcp); cfg.set_dhcp(cli.dhcp);
if let Some(ipv4) = &cli.ipv4 { if let Some(ipv4) = &cli.ipv4 {
cfg.set_ipv4(Some( cfg.set_ipv4(Some(ipv4.parse().with_context(|| {
ipv4.parse() format!("failed to parse ipv4 address: {}", ipv4)
.with_context(|| format!("failed to parse ipv4 address: {}", ipv4))? })?))
))
} }
let mut peers = Vec::<PeerConfig>::with_capacity(cli.peers.len()); let mut peers = Vec::<PeerConfig>::with_capacity(cli.peers.len());
for p in &cli.peers { for p in &cli.peers {
peers.push(PeerConfig { peers.push(PeerConfig {
uri: p.parse().with_context(|| format!("failed to parse peer uri: {}", p))?, uri: p
.parse()
.with_context(|| format!("failed to parse peer uri: {}", p))?,
}); });
} }
cfg.set_peers(peers); cfg.set_peers(peers);
@ -410,22 +414,21 @@ impl TryFrom<&Cli> for TomlConfigLoader {
for n in cli.proxy_networks.iter() { for n in cli.proxy_networks.iter() {
cfg.add_proxy_cidr( cfg.add_proxy_cidr(
n.parse() n.parse()
.with_context(|| format!("failed to parse proxy network: {}", n))? .with_context(|| format!("failed to parse proxy network: {}", n))?,
); );
} }
cfg.set_rpc_portal(Cli::parse_rpc_portal(cli.rpc_portal.clone()).with_context(|| { cfg.set_rpc_portal(
format!("failed to parse rpc portal: {}", cli.rpc_portal) Cli::parse_rpc_portal(cli.rpc_portal.clone())
})?); .with_context(|| format!("failed to parse rpc portal: {}", cli.rpc_portal))?,
);
if let Some(external_nodes) = cli.external_node.as_ref() { if let Some(external_nodes) = cli.external_node.as_ref() {
let mut old_peers = cfg.get_peers(); let mut old_peers = cfg.get_peers();
old_peers.push(PeerConfig { old_peers.push(PeerConfig {
uri: external_nodes uri: external_nodes.parse().with_context(|| {
.parse() format!("failed to parse external node uri: {}", external_nodes)
.with_context(|| { })?,
format!("failed to parse external node uri: {}", external_nodes)
})?
}); });
cfg.set_peers(old_peers); cfg.set_peers(old_peers);
} }
@ -450,11 +453,15 @@ impl TryFrom<&Cli> for TomlConfigLoader {
let url: url::Url = vpn_portal let url: url::Url = vpn_portal
.parse() .parse()
.with_context(|| format!("failed to parse vpn portal url: {}", vpn_portal))?; .with_context(|| format!("failed to parse vpn portal url: {}", vpn_portal))?;
let host = url.host_str().ok_or_else(|| anyhow::anyhow!("vpn portal url missing host"))?; let host = url
let port = url.port().ok_or_else(|| anyhow::anyhow!("vpn portal url missing port"))?; .host_str()
let client_cidr = url.path()[1..] .ok_or_else(|| anyhow::anyhow!("vpn portal url missing host"))?;
.parse() let port = url
.with_context(|| format!("failed to parse vpn portal client cidr: {}", url.path()))?; .port()
.ok_or_else(|| anyhow::anyhow!("vpn portal url missing port"))?;
let client_cidr = url.path()[1..].parse().with_context(|| {
format!("failed to parse vpn portal client cidr: {}", url.path())
})?;
let wireguard_listen: SocketAddr = format!("{}:{}", host, port).parse().unwrap(); let wireguard_listen: SocketAddr = format!("{}:{}", host, port).parse().unwrap();
cfg.set_vpn_portal_config(VpnPortalConfig { cfg.set_vpn_portal_config(VpnPortalConfig {
wireguard_listen, wireguard_listen,
@ -464,8 +471,11 @@ impl TryFrom<&Cli> for TomlConfigLoader {
if let Some(manual_routes) = cli.manual_routes.as_ref() { if let Some(manual_routes) = cli.manual_routes.as_ref() {
let mut routes = Vec::<cidr::Ipv4Cidr>::with_capacity(manual_routes.len()); let mut routes = Vec::<cidr::Ipv4Cidr>::with_capacity(manual_routes.len());
for r in manual_routes{ for r in manual_routes {
routes.push(r.parse().with_context(|| format!("failed to parse route: {}", r))?); routes.push(
r.parse()
.with_context(|| format!("failed to parse route: {}", r))?,
);
} }
cfg.set_routes(Some(routes)); cfg.set_routes(Some(routes));
} }
@ -636,9 +646,9 @@ pub fn handle_event(mut events: EventBusSubscriber) -> tokio::task::JoinHandle<(
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
fn win_service_set_work_dir(service_name: &std::ffi::OsString) -> anyhow::Result<()> { fn win_service_set_work_dir(service_name: &std::ffi::OsString) -> anyhow::Result<()> {
use easytier::common::constants::WIN_SERVICE_WORK_DIR_REG_KEY;
use winreg::enums::*; use winreg::enums::*;
use winreg::RegKey; use winreg::RegKey;
use easytier::common::constants::WIN_SERVICE_WORK_DIR_REG_KEY;
let hklm = RegKey::predef(HKEY_LOCAL_MACHINE); let hklm = RegKey::predef(HKEY_LOCAL_MACHINE);
let key = hklm.open_subkey_with_flags(WIN_SERVICE_WORK_DIR_REG_KEY, KEY_READ)?; let key = hklm.open_subkey_with_flags(WIN_SERVICE_WORK_DIR_REG_KEY, KEY_READ)?;
@ -654,10 +664,10 @@ fn win_service_set_work_dir(service_name: &std::ffi::OsString) -> anyhow::Result
fn win_service_event_loop( fn win_service_event_loop(
stop_notify: std::sync::Arc<tokio::sync::Notify>, stop_notify: std::sync::Arc<tokio::sync::Notify>,
cli: Cli, cli: Cli,
status_handle: windows_service::service_control_handler::ServiceStatusHandle, status_handle: windows_service::service_control_handler::ServiceStatusHandle,
) { ) {
use tokio::runtime::Runtime;
use std::time::Duration; use std::time::Duration;
use tokio::runtime::Runtime;
use windows_service::service::*; use windows_service::service::*;
let normal_status = ServiceStatus { let normal_status = ServiceStatus {
@ -737,13 +747,18 @@ fn win_service_main(arg: Vec<std::ffi::OsString>) {
checkpoint: 0, checkpoint: 0,
wait_hint: Duration::default(), wait_hint: Duration::default(),
process_id: None, process_id: None,
}; };
status_handle.set_service_status(next_status).expect("set service status fail"); status_handle
.set_service_status(next_status)
.expect("set service status fail");
win_service_event_loop(stop_notify_recv, cli, status_handle); win_service_event_loop(stop_notify_recv, cli, status_handle);
} }
async fn run_main(cli: Cli) -> anyhow::Result<()> { async fn run_main(cli: Cli) -> anyhow::Result<()> {
let cfg = TomlConfigLoader::try_from(&cli)?;
init_logger(&cfg, false)?;
if cli.config_server.is_some() { if cli.config_server.is_some() {
let config_server_url_s = cli.config_server.clone().unwrap(); let config_server_url_s = cli.config_server.clone().unwrap();
let config_server_url = match url::Url::parse(&config_server_url_s) { let config_server_url = match url::Url::parse(&config_server_url_s) {
@ -778,9 +793,6 @@ async fn run_main(cli: Cli) -> anyhow::Result<()> {
return Ok(()); return Ok(());
} }
let cfg = TomlConfigLoader::try_from(&cli)?;
init_logger(&cfg, false)?;
println!("Starting easytier with config:"); println!("Starting easytier with config:");
println!("############### TOML ###############\n"); println!("############### TOML ###############\n");
println!("{}", cfg.dump()); println!("{}", cfg.dump());
@ -788,8 +800,8 @@ async fn run_main(cli: Cli) -> anyhow::Result<()> {
let mut l = launcher::NetworkInstance::new(cfg).set_fetch_node_info(false); let mut l = launcher::NetworkInstance::new(cfg).set_fetch_node_info(false);
let _t = ScopedTask::from(handle_event(l.start().unwrap())); let _t = ScopedTask::from(handle_event(l.start().unwrap()));
if let Some(e) = l.wait().await{ if let Some(e) = l.wait().await {
return Err(anyhow::anyhow!("launcher error: {}", e)); anyhow::bail!("launcher error: {}", e);
} }
Ok(()) Ok(())
} }
@ -803,22 +815,23 @@ async fn main() {
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
match windows_service::service_dispatcher::start(String::new(), ffi_service_main) { match windows_service::service_dispatcher::start(String::new(), ffi_service_main) {
Ok(_) => std::thread::park(), Ok(_) => std::thread::park(),
Err(e) => Err(e) => {
{ let should_panic = if let windows_service::Error::Winapi(ref io_error) = e {
let should_panic = if let windows_service::Error::Winapi(ref io_error) = e { io_error.raw_os_error() != Some(0x427) // ERROR_FAILED_SERVICE_CONTROLLER_CONNECT
io_error.raw_os_error() != Some(0x427) // ERROR_FAILED_SERVICE_CONTROLLER_CONNECT } else {
} else { true }; true
};
if should_panic {
panic!("SCM start an error: {}", e); if should_panic {
} panic!("SCM start an error: {}", e);
} }
}; }
};
let cli = Cli::parse(); let cli = Cli::parse();
if let Err(e) = run_main(cli).await { if let Err(e) = run_main(cli).await {
eprintln!("error: {:?}", e); eprintln!("error: {:?}", e);
std::process::exit(1); std::process::exit(1);
} }
} }