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