feat(server): protocol v2 with signed handshake, secure voice relay, media broadcast and channel authorization
Revoke voice access on channel delete, kick and permission change, and acknowledge the sender's own chat messages.
This commit is contained in:
parent
bc5157046e
commit
3e7f5b22dc
44 changed files with 2825 additions and 386 deletions
|
|
@ -3,7 +3,7 @@ use ed25519_dalek::SigningKey;
|
|||
use serde::{Deserialize, Serialize};
|
||||
use std::path::Path;
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[derive(Clone, Serialize, Deserialize)]
|
||||
struct StoredIdentity {
|
||||
pubkey_hex: String,
|
||||
privkey_hex: String,
|
||||
|
|
@ -15,6 +15,10 @@ pub struct ServerIdentity {
|
|||
}
|
||||
|
||||
impl ServerIdentity {
|
||||
pub fn sign(&self, message: &[u8]) -> Vec<u8> {
|
||||
use ed25519_dalek::Signer;
|
||||
self.signing_key.sign(message).to_bytes().to_vec()
|
||||
}
|
||||
pub fn pubkey_hex(&self) -> String {
|
||||
hex::encode(self.signing_key.verifying_key().to_bytes())
|
||||
}
|
||||
|
|
@ -22,7 +26,16 @@ impl ServerIdentity {
|
|||
pub fn load_or_generate(data_dir: &Path) -> Result<Self> {
|
||||
let path = data_dir.join("server_identity.json");
|
||||
|
||||
if path.exists() {
|
||||
if std::fs::symlink_metadata(&path).is_ok() {
|
||||
anyhow::ensure!(
|
||||
std::fs::symlink_metadata(&path)?.file_type().is_file(),
|
||||
"server identity must be a regular file"
|
||||
);
|
||||
#[cfg(unix)]
|
||||
{
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o600))?;
|
||||
}
|
||||
let text = std::fs::read_to_string(&path)?;
|
||||
let stored: StoredIdentity = serde_json::from_str(&text)?;
|
||||
let signing_key = SigningKey::from_bytes(
|
||||
|
|
@ -50,8 +63,67 @@ impl ServerIdentity {
|
|||
};
|
||||
|
||||
std::fs::create_dir_all(data_dir)?;
|
||||
std::fs::write(&path, serde_json::to_string_pretty(&stored)?)?;
|
||||
let temporary = data_dir.join(format!(".server-identity-{}.tmp", uuid::Uuid::new_v4()));
|
||||
let mut options = std::fs::OpenOptions::new();
|
||||
options.write(true).create_new(true);
|
||||
#[cfg(unix)]
|
||||
{
|
||||
use std::os::unix::fs::OpenOptionsExt;
|
||||
options.mode(0o600);
|
||||
}
|
||||
let result = (|| -> Result<()> {
|
||||
use std::io::Write;
|
||||
let mut file = options.open(&temporary)?;
|
||||
file.write_all(serde_json::to_string_pretty(&stored)?.as_bytes())?;
|
||||
file.sync_all()?;
|
||||
std::fs::hard_link(&temporary, &path)?;
|
||||
#[cfg(unix)]
|
||||
std::fs::File::open(data_dir)?.sync_all()?;
|
||||
Ok(())
|
||||
})();
|
||||
let _ = std::fs::remove_file(&temporary);
|
||||
if let Err(error) = result {
|
||||
if error
|
||||
.downcast_ref::<std::io::Error>()
|
||||
.is_some_and(|e| e.kind() == std::io::ErrorKind::AlreadyExists)
|
||||
{
|
||||
return Self::load_or_generate(data_dir);
|
||||
}
|
||||
return Err(error);
|
||||
}
|
||||
|
||||
Ok(Self { signing_key })
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn gateway_identity_is_private_stable_and_never_replaced() {
|
||||
let dir =
|
||||
std::env::temp_dir().join(format!("vnox-private-identity-{}", uuid::Uuid::new_v4()));
|
||||
let first = ServerIdentity::load_or_generate(&dir).unwrap();
|
||||
let second = ServerIdentity::load_or_generate(&dir).unwrap();
|
||||
assert_eq!(first.pubkey_hex(), second.pubkey_hex());
|
||||
#[cfg(unix)]
|
||||
{
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
assert_eq!(
|
||||
std::fs::metadata(dir.join("server_identity.json"))
|
||||
.unwrap()
|
||||
.permissions()
|
||||
.mode()
|
||||
& 0o777,
|
||||
0o600
|
||||
);
|
||||
}
|
||||
std::fs::write(dir.join("server_identity.json"), "corrupt").unwrap();
|
||||
assert!(ServerIdentity::load_or_generate(&dir).is_err());
|
||||
assert_eq!(
|
||||
std::fs::read_to_string(dir.join("server_identity.json")).unwrap(),
|
||||
"corrupt"
|
||||
);
|
||||
std::fs::remove_dir_all(dir).unwrap();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue