| 1 |
use std::cell::RefCell; |
| 2 |
use std::fs; |
| 3 |
use std::path::Path; |
| 4 |
|
| 5 |
use async_process::{Child, Command, Stdio}; |
| 6 |
use nix::sys::signal::{self, Signal}; |
| 7 |
use nix::unistd::Pid; |
| 8 |
|
| 9 |
use crate::path; |
| 10 |
use crate::settings::{settings_manager, Key}; |
| 11 |
|
| 12 |
#[derive(Default)] |
| 13 |
pub struct Daemon { |
| 14 |
child: RefCell<Option<Child>>, |
| 15 |
} |
| 16 |
|
| 17 |
impl Daemon { |
| 18 |
pub async fn start(&self) -> anyhow::Result<()> { |
| 19 |
debug!("Start transmission daemon..."); |
| 20 |
self.ensure_rpc_enabled(); |
| 21 |
|
| 22 |
if self.child.borrow().is_some() { |
| 23 |
warn!("Daemon is already running!"); |
| 24 |
return Ok(()); |
| 25 |
} |
| 26 |
|
| 27 |
// Allow all addresses ("*") if remote access is enabled, |
| 28 |
// and not just localhost |
| 29 |
let remote_access = if settings_manager::boolean(Key::RemoteAccess) { |
| 30 |
"*" |
| 31 |
} else { |
| 32 |
"127.0.0.1,::1" |
| 33 |
}; |
| 34 |
|
| 35 |
let args = [ |
| 36 |
"-f", |
| 37 |
"--no-auth", |
| 38 |
"--config-dir", |
| 39 |
path::CONFIG.to_str().unwrap(), |
| 40 |
"--port", |
| 41 |
"9091", |
| 42 |
"--rpc-bind-address", |
| 43 |
"0.0.0.0", |
| 44 |
"--allowed", |
| 45 |
remote_access, |
| 46 |
]; |
| 47 |
debug!("Daemon args: {:#?}", args); |
| 48 |
let child = Command::new("transmission-daemon") |
| 49 |
.args(args) |
| 50 |
.stdout(Stdio::piped()) |
| 51 |
.spawn()?; |
| 52 |
|
| 53 |
*self.child.borrow_mut() = Some(child); |
| 54 |
Ok(()) |
| 55 |
} |
| 56 |
|
| 57 |
pub async fn stop(&self) -> anyhow::Result<()> { |
| 58 |
debug!("Stop transmission daemon..."); |
| 59 |
let child = { self.child.try_borrow_mut()?.take() }; |
| 60 |
|
| 61 |
match child { |
| 62 |
Some(mut child) => { |
| 63 |
let pid = Pid::from_raw(child.id().try_into().unwrap()); |
| 64 |
signal::kill(pid, Signal::SIGTERM)?; |
| 65 |
|
| 66 |
let status = child.status().await?; |
| 67 |
debug!("Daemon stopped, status code: {}", status); |
| 68 |
} |
| 69 |
None => warn!("Daemon is not running, unable to stop it"), |
| 70 |
} |
| 71 |
|
| 72 |
Ok(()) |
| 73 |
} |
| 74 |
|
| 75 |
pub fn is_running(&self) -> bool { |
| 76 |
self.child.borrow().is_some() |
| 77 |
} |
| 78 |
|
| 79 |
// Fragments-v1 (old Vala edition) had rpc disabled by default. |
| 80 |
// Fragments-v2 (Rust rewrite) needs RPC enabled to communicate with |
| 81 |
// transmission-daemon |
| 82 |
fn ensure_rpc_enabled(&self) { |
| 83 |
let config_path = Path::new(&*path::CONFIG).join("settings.json"); |
| 84 |
|
| 85 |
match fs::read_to_string(&config_path) { |
| 86 |
Ok(data) => { |
| 87 |
if data.contains("\"rpc-enabled\": false") { |
| 88 |
info!("RPC is disabled in settings.json. Enabling... "); |
| 89 |
let updated = data.replace("\"rpc-enabled\": false", "\"rpc-enabled\": true"); |
| 90 |
|
| 91 |
// Write updated config file |
| 92 |
if let Err(err) = fs::write(&config_path, updated) { |
| 93 |
warn!( |
| 94 |
"Cannot enable rpc, Fragments will probably not work: {}", |
| 95 |
err.to_string() |
| 96 |
) |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
Err(err) => warn!("Can't ensure that rpc is enabled: {}", err.to_string()), |
| 101 |
}; |
| 102 |
} |
| 103 |
} |