Skip to main content

iroh_ssh/service/
mod.rs

1#[cfg(target_os = "linux")]
2mod linux;
3#[cfg(target_os = "linux")]
4use crate::service::linux::LinuxService;
5
6#[cfg(target_os = "windows")]
7mod windows;
8#[cfg(target_os = "windows")]
9pub(crate) use crate::service::windows::WindowsService;
10
11#[cfg(target_os = "windows")]
12pub async fn run_service(
13    ssh_port: u16,
14    key_dir: Option<std::path::PathBuf>,
15    relay_url: Vec<String>,
16    extra_relay_url: Vec<String>,
17) -> anyhow::Result<()> {
18    WindowsService::run_service(ServiceParams {
19        ssh_port,
20        key_dir: crate::api::abs_key_dir(key_dir),
21        relay_url,
22        extra_relay_url,
23    })
24    .await
25}
26
27#[cfg(not(target_os = "windows"))]
28pub async fn run_service(
29    _ssh_port: u16,
30    _key_dir: Option<std::path::PathBuf>,
31    _relay_url: Vec<String>,
32    _extra_relay_url: Vec<String>,
33) -> anyhow::Result<()> {
34    anyhow::bail!("service run is only supported on windows");
35}
36
37#[derive(Debug, Clone)]
38pub struct ServiceParams {
39    pub ssh_port: u16,
40    pub key_dir: Option<std::path::PathBuf>,
41    pub relay_url: Vec<String>,
42    pub extra_relay_url: Vec<String>,
43}
44
45pub trait Service {
46    fn install(
47        service_params: ServiceParams,
48    ) -> impl std::future::Future<Output = anyhow::Result<()>> + Send;
49    fn info() -> impl std::future::Future<Output = anyhow::Result<()>> + Send;
50    fn uninstall() -> impl std::future::Future<Output = anyhow::Result<()>> + Send;
51}
52
53pub async fn install_service(_service_params: ServiceParams) -> anyhow::Result<()> {
54    match std::env::consts::OS {
55        #[cfg(target_os = "linux")]
56        "linux" => LinuxService::install(_service_params).await,
57        #[cfg(target_os = "windows")]
58        "windows" => WindowsService::install(_service_params).await,
59        _ => anyhow::bail!("service mode is only supported on linux and windows"),
60    }
61}
62
63pub async fn uninstall_service() -> anyhow::Result<()> {
64    match std::env::consts::OS {
65        #[cfg(target_os = "linux")]
66        "linux" => LinuxService::uninstall().await,
67        #[cfg(target_os = "windows")]
68        "windows" => WindowsService::uninstall().await,
69        _ => anyhow::bail!("service mode is only supported on linux and windows"),
70    }
71}