1use std::path::PathBuf;
2
3#[derive(Clone, Debug)]
4pub struct CertPaths {
5 pub cert: PathBuf,
6 pub chain: PathBuf,
7 pub fullchain: PathBuf,
8 pub privkey: PathBuf,
9}
10
11pub fn get_cert_paths(
12 email: &str,
13 hostname: &str,
14) -> Result<CertPaths, Box<dyn std::error::Error>> {
15 use std::process::Command;
16
17 let exitstatus = Command::new("apt").args(["install", "-y", "certbot"]).spawn()?.wait()?;
18 if !exitstatus.success() {
19 return Err(String::from("apt install certbot failed").into());
20 };
21
22 let exitstatus = Command::new("certbot")
23 .args(["-n", "certonly", "--standalone", "--agree-tos", "--email", email, "-d", hostname])
24 .spawn()?
25 .wait()?;
26 if !exitstatus.success() {
27 return Err(String::from("certbot failed").into());
28 };
29
30 let ca_dir = PathBuf::from("/etc/letsencrypt/live/").join(hostname);
31 let cert_paths = CertPaths {
32 cert: ca_dir.join("cert.pem"),
33 chain: ca_dir.join("chain.pem"),
34 fullchain: ca_dir.join("fullchain.pem"),
35 privkey: ca_dir.join("privkey.pem"),
36 };
37
38 Ok(cert_paths)
39}
40
41#[cfg(test)]
42mod tests {
43 #[test]
44 fn get_cert() {
45 use super::*;
46
47 dbg!(get_cert_paths("trevyn-git@protonmail.com", "test3.turbonet.to").unwrap());
48 }
49}