add service worker settings

This commit is contained in:
naskya 2024-07-01 20:28:47 +09:00
parent f7175d685f
commit 268603256b
Signed by: naskya
GPG key ID: 712D413B3A9FED5C
7 changed files with 50 additions and 8 deletions

View file

@ -4,6 +4,7 @@
use crate::{
command::config::{read_file_as_string, ReadError},
config::{client, server, Revision, CLIENT_CONFIG_PATH, SERVER_CONFIG_PATH},
util::vapid::{generate_vapid_keys, VapidError},
};
use color_print::cprintln;
use sqlx::{postgres::PgConnectOptions, ConnectOptions};
@ -29,6 +30,8 @@ pub(crate) enum Error {
InvalidConfig(&'static str),
#[error("failed to parse server URL")]
InvalidUrl(#[from] url::ParseError),
#[error(transparent)]
Vapid(#[from] VapidError),
}
pub(super) async fn run(base_dir: &Path) -> Result<(), Error> {
@ -158,7 +161,6 @@ struct Meta {
smtp_port: Option<i32>,
smtp_user: Option<String>,
smtp_pass: Option<String>,
enable_service_worker: bool,
sw_public_key: Option<String>,
sw_private_key: Option<String>,
pinned_users: Vec<String>,
@ -309,6 +311,20 @@ fn create_new_server_config(
url => Some(url.to_owned()),
};
let service_worker = match (meta.sw_public_key.as_ref(), meta.sw_private_key.as_ref()) {
(Some(pubkey), Some(privkey)) => server::ServiceWorker {
public_key: pubkey.to_owned(),
private_key: privkey.to_owned(),
},
_ => {
let keypair = generate_vapid_keys()?;
server::ServiceWorker {
public_key: keypair.public_key,
private_key: keypair.private_key,
}
}
};
let id: Option<server::Id> = if let Some(id) = default_yml.get("cuid") {
let id = id.as_hash().ok_or(Error::InvalidConfig("cuid"))?;
Some(server::Id {
@ -571,6 +587,7 @@ fn create_new_server_config(
},
},
id,
service_worker,
security: todo!(),
file,
};

View file

@ -1,5 +1,6 @@
mod vapid;
use crate::util::vapid::VapidError;
use clap::Subcommand;
#[derive(Subcommand)]
@ -12,7 +13,7 @@ pub(crate) enum Commands {
#[derive(thiserror::Error, Debug)]
pub(crate) enum GenerateError {
#[error(transparent)]
Validate(#[from] vapid::VapidError),
Vapid(#[from] VapidError),
}
pub(super) fn run(command: Commands) -> Result<(), GenerateError> {

View file

@ -1,15 +1,13 @@
#[derive(thiserror::Error, Debug)]
#[error("failed to generate a Vapid key ({0})")]
pub struct VapidError(String);
use crate::util::vapid::{generate_vapid_keys, VapidError};
pub(super) fn run() -> Result<(), VapidError> {
let keypair = vapid::Key::generate().map_err(|err| VapidError(err.to_string()))?;
let keypair = generate_vapid_keys()?;
println!("public key:");
println!("{}", keypair.to_public_raw());
println!("{}", keypair.public_key);
println!("");
println!("private key:");
println!("{}", keypair.to_private_raw());
println!("{}", keypair.private_key);
Ok(())
}

View file

@ -24,6 +24,8 @@ pub struct Config {
#[validate(nested)]
pub cache_server: CacheServer,
#[validate(nested)]
pub service_worker: ServiceWorker,
#[validate(nested)]
pub id: Option<Id>,
#[validate(nested)]
pub file: Option<File>,
@ -136,6 +138,12 @@ pub struct CacheServer {
pub prefix: Option<String>,
}
#[derive(Deserialize, Serialize, Validate, Debug, Clone)]
pub struct ServiceWorker {
pub public_key: String,
pub private_key: String,
}
#[derive(Deserialize, Serialize, Validate, Debug, Clone)]
pub struct Id {
#[validate(range(min = 16, max = 24))]

View file

@ -1,5 +1,6 @@
mod command;
mod config;
mod util;
use color_print::cprintln;
use std::process::ExitCode;

1
fishctl/src/util/mod.rs Normal file
View file

@ -0,0 +1 @@
pub(crate) mod vapid;

16
fishctl/src/util/vapid.rs Normal file
View file

@ -0,0 +1,16 @@
#[derive(thiserror::Error, Debug)]
#[error("failed to generate a Vapid key ({0})")]
pub struct VapidError(String);
pub(crate) struct VapidKey {
pub(crate) public_key: String,
pub(crate) private_key: String,
}
pub(crate) fn generate_vapid_keys() -> Result<VapidKey, VapidError> {
let keypair = vapid::Key::generate().map_err(|err| VapidError(err.to_string()))?;
Ok(VapidKey {
public_key: keypair.to_public_raw(),
private_key: keypair.to_private_raw(),
})
}