make mkcert read the configuration file
This commit is contained in:
parent
c4ba5f7ade
commit
c77836dcb0
|
@ -1 +1,3 @@
|
|||
/target
|
||||
*.crt
|
||||
*.key
|
||||
|
|
10
config.toml
10
config.toml
|
@ -1,6 +1,6 @@
|
|||
[server]
|
||||
daemonize = true
|
||||
endpoint = "10.177.1.7:9092"
|
||||
endpoint = "127.0.0.1:9092"
|
||||
server_name = "alphamethyl.barr0w.net"
|
||||
user = "nobody"
|
||||
group = "daemon"
|
||||
|
@ -8,9 +8,13 @@ stdout = "/tmp/sleepyserver"
|
|||
stderr = "/tmp/sleepyserver.err"
|
||||
|
||||
[client]
|
||||
endpoint = "10.177.1.7:9092"
|
||||
endpoint = "alphamethyl.barr0w.net:9092"
|
||||
server_name = "alphamethyl.barr0w.net"
|
||||
|
||||
[tls]
|
||||
cert_file = "tls.crt"
|
||||
key_file = "tls.key"
|
||||
|
||||
[interface]
|
||||
address = "192.168.255.1"
|
||||
netmask = "255.255.255.252"
|
||||
netmask = "255.255.255.255"
|
||||
|
|
|
@ -2,25 +2,45 @@ use rcgen::generate_simple_self_signed;
|
|||
use clap::Parser;
|
||||
use std::fs::File;
|
||||
use std::io::Write;
|
||||
use sleepytunny::config::Configuration;
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
#[command(name = "mkcert", author = "Ren Kararou <ren@kararou.space>", version = "0.1a")]
|
||||
#[command(name = "mkcert", author = "Ren Kararou <ren@kararou.space>", version = "0.1b")]
|
||||
#[command(about = "makes simple self-signed certificates", long_about = None)]
|
||||
struct Args {
|
||||
#[arg(short, long)]
|
||||
name: Option<Vec<String>>,
|
||||
basename: String,
|
||||
#[arg(short, long)]
|
||||
config: Option<String>,
|
||||
}
|
||||
|
||||
fn main() -> anyhow::Result<()> {
|
||||
let args = Args::parse();
|
||||
let name = args.name.unwrap_or(vec![String::from("localhost")]);
|
||||
let mut cfile = File::create(format!("{}.crt", &args.basename))?;
|
||||
let mut kfile = File::create(format!("{}.key", &args.basename))?;
|
||||
let cert = generate_simple_self_signed(name)?;
|
||||
|
||||
cfile.write(cert.serialize_pem()?.as_bytes())?;
|
||||
kfile.write(cert.serialize_private_key_pem().as_bytes())?;
|
||||
|
||||
Ok(())
|
||||
let config = match args.config {
|
||||
Some(c) => Configuration::load_config(&c)?,
|
||||
None => Configuration::load_config("config.toml")?,
|
||||
};
|
||||
match &config.tls {
|
||||
Some(t) => {
|
||||
let server_names: Option<String> = match config.server() {
|
||||
Ok(s) => Some(s.server_name.clone().unwrap_or(String::from("localhost"))),
|
||||
Err(_) => None,
|
||||
};
|
||||
// Lets vectorize it.
|
||||
let name = match server_names {
|
||||
Some(s) => vec![s],
|
||||
None => args.name.unwrap_or(vec![String::from("localhost")]),
|
||||
};
|
||||
let mut cfile = File::create(&t.cert_file)?;
|
||||
let mut kfile = File::create(&t.key_file)?;
|
||||
let cert = generate_simple_self_signed(name)?;
|
||||
cfile.write(cert.serialize_pem()?.as_bytes())?;
|
||||
kfile.write(cert.serialize_private_key_pem().as_bytes())?;
|
||||
Ok(())
|
||||
}
|
||||
None => {
|
||||
eprintln!("No tls configuration specified in config.toml");
|
||||
panic!();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -100,6 +100,39 @@ impl Client {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
pub struct TLS {
|
||||
pub cert_file: String,
|
||||
#[serde(skip_serializing)]
|
||||
pub cert: Option<String>, // Eventually maybe an actual cert object
|
||||
pub key_file: String,
|
||||
#[serde(skip_serializing)]
|
||||
pub key: Option<String>, // Eventually maybe an actual key object
|
||||
}
|
||||
impl TLS {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
cert_file: String::from("/etc/sleepytunny/tls.crt"),
|
||||
key_file: String::from("/etc/sleepytunny/tls.key"),
|
||||
cert: None,
|
||||
key: None,
|
||||
}
|
||||
}
|
||||
pub fn load(&mut self) -> anyhow::Result<()> {
|
||||
let k = fs::read_to_string(&self.key_file)?;
|
||||
let c = fs::read_to_string(&self.cert_file)?;
|
||||
self.cert = Some(c);
|
||||
self.key = Some(k);
|
||||
Ok(())
|
||||
}
|
||||
pub fn cert(&self) -> anyhow::Result<String> {
|
||||
Ok(self.cert.clone().unwrap())
|
||||
}
|
||||
pub fn key(&self) -> anyhow::Result<String> {
|
||||
Ok(self.key.clone().unwrap())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
pub struct Interface {
|
||||
pub address: Ipv4Addr,
|
||||
|
@ -120,6 +153,8 @@ pub struct Configuration {
|
|||
pub server: Option<Server>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub client: Option<Client>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub tls: Option<TLS>,
|
||||
pub interface: Interface,
|
||||
}
|
||||
impl Configuration {
|
||||
|
@ -127,6 +162,7 @@ impl Configuration {
|
|||
Self {
|
||||
server: Some(Server::new()),
|
||||
client: Some(Client::new()),
|
||||
tls: Some(TLS::new()),
|
||||
interface: Interface::new(),
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue