From 72cdf7d79e498b62acf3973a62b87af40632fa59 Mon Sep 17 00:00:00 2001 From: somehybrid Date: Sat, 27 Jan 2024 19:46:41 +0700 Subject: [PATCH 1/8] Encrypted messages --- protolok/src/lib.rs | 3 -- protolok/src/message.rs | 73 ----------------------------------------- 2 files changed, 76 deletions(-) delete mode 100644 protolok/src/message.rs diff --git a/protolok/src/lib.rs b/protolok/src/lib.rs index 42b7dc5..8f23a65 100644 --- a/protolok/src/lib.rs +++ b/protolok/src/lib.rs @@ -1,12 +1,9 @@ pub mod channel; -pub mod crypto; pub mod ids; -pub mod message; pub mod place; pub mod user; pub use channel::*; pub use ids::*; -pub use message::*; pub use place::*; pub use user::*; diff --git a/protolok/src/message.rs b/protolok/src/message.rs deleted file mode 100644 index c542722..0000000 --- a/protolok/src/message.rs +++ /dev/null @@ -1,73 +0,0 @@ -use rsa::{Pkcs1v15Encrypt, PublicKey, RsaPrivateKey, RsaPublicKey}; - -use crate::{ - ids::{make_id, Object, ServerMeta}, - user::UserHandle, -}; - -#[derive(Clone, Debug, PartialEq, Eq)] -pub struct UnencryptedContent { - pub hash: String, - pub text: String, -} -#[derive(Clone, Debug, PartialEq, Eq)] -pub struct EncryptedContent { - pub hash: String, - pub enc_text: Vec, -} - -#[derive(Clone, Debug, PartialEq, Eq)] -pub enum MessageContent { - Encrypted(EncryptedContent), - Unencrypted(UnencryptedContent), -} -impl MessageContent { - pub fn is_encrypted(&self) -> bool { - matches!(self, &MessageContent::Encrypted { .. }) - } - - pub fn decrypt(&self, key: &RsaPrivateKey) -> Result { - match self { - MessageContent::Encrypted(EncryptedContent { hash, enc_text }) => { - key.decrypt(Pkcs1v15Encrypt, enc_text).map(|x| { - Self::Unencrypted(UnencryptedContent { - hash: hash.to_owned(), - text: String::from_utf8(x).unwrap_or_else(|_| "INVALID UTF8".to_owned()), - }) - }) - } - MessageContent::Unencrypted(x) => Ok(Self::Unencrypted(x.clone())), - } - } -} - -#[derive(Clone, Debug, PartialEq, Eq)] -pub struct Message { - pub id: u64, - pub from: Option, - pub content: MessageContent, -} - -pub fn encrypt( - unencrypted: UnencryptedContent, - key: &RsaPublicKey, -) -> Result { - Ok(MessageContent::Encrypted(EncryptedContent { - hash: unencrypted.hash, - enc_text: key.encrypt( - &mut rand::thread_rng(), - Pkcs1v15Encrypt, - unencrypted.text.as_bytes(), - )?, - })) -} - -impl Object for Message { - fn initialize(&mut self, meta: &ServerMeta) { - self.id = make_id(meta); - } - - fn get_id(&self) -> u64 { - self.id - } -} From 348d4787716464224c2d12480983a9f38cd2f1fd Mon Sep 17 00:00:00 2001 From: somehybrid Date: Sat, 27 Jan 2024 21:06:35 +0700 Subject: [PATCH 2/8] Add signed prekeys --- Cargo.lock | 300 +++++++++++++++++++++++++++++++------ loki-server/src/dm.rs | 5 + loki-server/src/main.rs | 2 + loki-shared/Cargo.toml | 4 + loki-shared/src/lib.rs | 17 +-- loki-shared/src/message.rs | 113 ++++++++++++++ loki-shared/src/prekey.rs | 24 +++ 7 files changed, 403 insertions(+), 62 deletions(-) create mode 100644 loki-server/src/dm.rs create mode 100644 loki-shared/src/message.rs create mode 100644 loki-shared/src/prekey.rs diff --git a/Cargo.lock b/Cargo.lock index ecb6599..c47d8df 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,16 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "aead" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d122413f284cf2d62fb1b7db97e02edb8cda96d769b16e443a4f6195e35662b0" +dependencies = [ + "crypto-common", + "generic-array", +] + [[package]] name = "autocfg" version = "1.1.0" @@ -41,6 +51,41 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "chacha20" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3613f74bd2eac03dad61bd53dbe620703d4371614fe0bc3b9f04dd36fe4e818" +dependencies = [ + "cfg-if", + "cipher", + "cpufeatures", +] + +[[package]] +name = "chacha20poly1305" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "10cd79432192d1c0f4e1a0fef9527696cc039165d729fb41b3f4f4f354c2dc35" +dependencies = [ + "aead", + "chacha20", + "cipher", + "poly1305", + "zeroize", +] + +[[package]] +name = "cipher" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "773f3b9af64447d2ce9850330c473515014aa235e6a783b02db81ff39e4a3dad" +dependencies = [ + "crypto-common", + "inout", + "zeroize", +] + [[package]] name = "const-oid" version = "0.9.2" @@ -63,9 +108,38 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" dependencies = [ "generic-array", + "rand_core", "typenum", ] +[[package]] +name = "curve25519-dalek" +version = "4.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e89b8c6a2e4b1f45971ad09761aafb85514a84744b67a95e32c3cc1352d1f65c" +dependencies = [ + "cfg-if", + "cpufeatures", + "curve25519-dalek-derive", + "digest", + "fiat-crypto", + "platforms", + "rustc_version", + "subtle", + "zeroize", +] + +[[package]] +name = "curve25519-dalek-derive" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f46882e17999c6cc590af592290432be3bce0428cb0d5f8b6715e4dc7b383eb3" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "der" version = "0.6.1" @@ -73,18 +147,17 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f1a467a65c5e759bce6e65eaf91cc29f466cdc57cb65777bd646872a8a1fd4de" dependencies = [ "const-oid", - "pem-rfc7468 0.6.0", + "pem-rfc7468", "zeroize", ] [[package]] name = "der" -version = "0.7.6" +version = "0.7.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "56acb310e15652100da43d130af8d97b509e95af61aab1c5a7939ef24337ee17" +checksum = "fffa369a668c8af7dbf8b5e56c9f744fbd399949ed171606040001947de40b1c" dependencies = [ "const-oid", - "pem-rfc7468 0.7.0", "zeroize", ] @@ -99,6 +172,36 @@ dependencies = [ "crypto-common", ] +[[package]] +name = "ed25519" +version = "2.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "115531babc129696a58c64a4fef0a8bf9e9698629fb97e9e40767d235cfbcd53" +dependencies = [ + "pkcs8 0.10.2", + "signature", +] + +[[package]] +name = "ed25519-dalek" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f628eaec48bfd21b865dc2950cfa014450c01d2fa2b69a86c2fd5844ec523c0" +dependencies = [ + "curve25519-dalek", + "ed25519", + "serde", + "sha2", + "subtle", + "zeroize", +] + +[[package]] +name = "fiat-crypto" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "27573eac26f4dd11e2b1916c3fe1baa56407c83c71a773a8ba17ec0bca03b6b7" + [[package]] name = "generic-array" version = "0.14.7" @@ -120,6 +223,15 @@ dependencies = [ "wasi", ] +[[package]] +name = "inout" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a0c10553d664a4d0bcff9f4215d0aac67a639cc68ef660840afe309b807bc9f5" +dependencies = [ + "generic-array", +] + [[package]] name = "lazy_static" version = "1.4.0" @@ -156,12 +268,17 @@ version = "0.1.0" dependencies = [ "loki-shared", "protolok", - "rsa 0.9.2", ] [[package]] name = "loki-shared" version = "0.1.0" +dependencies = [ + "chacha20poly1305", + "ed25519-dalek", + "rand", + "x25519-dalek", +] [[package]] name = "lokui" @@ -216,19 +333,16 @@ dependencies = [ ] [[package]] -name = "pem-rfc7468" -version = "0.6.0" +name = "opaque-debug" +version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "24d159833a9105500e0398934e205e0773f0b27529557134ecfc51c27646adac" -dependencies = [ - "base64ct", -] +checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5" [[package]] name = "pem-rfc7468" -version = "0.7.0" +version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "88b39c9bfcfc231068454382784bb460aae594343fb030d46e9f50a645418412" +checksum = "24d159833a9105500e0398934e205e0773f0b27529557134ecfc51c27646adac" dependencies = [ "base64ct", ] @@ -245,17 +359,6 @@ dependencies = [ "zeroize", ] -[[package]] -name = "pkcs1" -version = "0.7.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c8ffb9f10fa047879315e6625af03c164b16962a5368d724ed16323b68ace47f" -dependencies = [ - "der 0.7.6", - "pkcs8 0.10.2", - "spki 0.7.2", -] - [[package]] name = "pkcs8" version = "0.9.0" @@ -272,8 +375,25 @@ version = "0.10.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f950b2377845cebe5cf8b5165cb3cc1a5e0fa5cfa3e1f7f55707d8fd82e0a7b7" dependencies = [ - "der 0.7.6", - "spki 0.7.2", + "der 0.7.8", + "spki 0.7.3", +] + +[[package]] +name = "platforms" +version = "3.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "626dec3cac7cc0e1577a2ec3fc496277ec2baa084bebad95bb6fdbfae235f84c" + +[[package]] +name = "poly1305" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8159bd90725d2df49889a078b54f4f79e87f1f8a8444194cdca81d38f5393abf" +dependencies = [ + "cpufeatures", + "opaque-debug", + "universal-hash", ] [[package]] @@ -282,16 +402,34 @@ version = "0.2.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" +[[package]] +name = "proc-macro2" +version = "1.0.78" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2422ad645d89c99f8f3e6b88a9fdeca7fabeac836b1002371c4367c8f984aae" +dependencies = [ + "unicode-ident", +] + [[package]] name = "protolok" version = "0.1.0" dependencies = [ "base16ct", "rand", - "rsa 0.8.2", + "rsa", "sha2", ] +[[package]] +name = "quote" +version = "1.0.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "291ec9ab5efd934aaf503a6466c5d5251535d108ee747472c3977cc5acc868ef" +dependencies = [ + "proc-macro2", +] + [[package]] name = "rand" version = "0.8.5" @@ -334,7 +472,7 @@ dependencies = [ "num-integer", "num-iter", "num-traits", - "pkcs1 0.4.1", + "pkcs1", "pkcs8 0.9.0", "rand_core", "signature", @@ -343,25 +481,38 @@ dependencies = [ ] [[package]] -name = "rsa" -version = "0.9.2" +name = "rustc_version" +version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6ab43bb47d23c1a631b4b680199a45255dce26fa9ab2fa902581f624ff13e6a8" +checksum = "bfa0f585226d2e68097d4f95d113b15b83a82e819ab25717ec0590d9584ef366" dependencies = [ - "byteorder", - "const-oid", - "digest", - "num-bigint-dig", - "num-integer", - "num-iter", - "num-traits", - "pkcs1 0.7.5", - "pkcs8 0.10.2", - "rand_core", - "signature", - "spki 0.7.2", - "subtle", - "zeroize", + "semver", +] + +[[package]] +name = "semver" +version = "1.0.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b97ed7a9823b74f99c7742f5336af7be5ecd3eeafcb1507d1fa93347b1d589b0" + +[[package]] +name = "serde" +version = "1.0.196" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "870026e60fa08c69f064aa766c10f10b1d62db9ccd4d0abb206472bee0ce3b32" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.196" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33c85360c95e7d137454dc81d9a4ed2b8efd8fbe19cee57357b32b9771fccb67" +dependencies = [ + "proc-macro2", + "quote", + "syn", ] [[package]] @@ -409,12 +560,12 @@ dependencies = [ [[package]] name = "spki" -version = "0.7.2" +version = "0.7.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9d1e996ef02c474957d681f1b05213dfb0abab947b446a62d37770b23500184a" +checksum = "d91ed6c858b01f942cd56b37a94b3e0a1798290327d1236e4d9cf4eaca44d29d" dependencies = [ "base64ct", - "der 0.7.6", + "der 0.7.8", ] [[package]] @@ -423,12 +574,39 @@ version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "81cdd64d312baedb58e21336b31bc043b77e01cc99033ce76ef539f78e965ebc" +[[package]] +name = "syn" +version = "2.0.48" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0f3531638e407dfc0814761abb7c00a5b54992b849452a0646b7f65c9f770f3f" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + [[package]] name = "typenum" version = "1.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "497961ef93d974e23eb6f433eb5fe1b7930b659f06d12dec6fc44a8f554c0bba" +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" + +[[package]] +name = "universal-hash" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc1de2c688dc15305988b563c3854064043356019f97a4b46276fe734c4f07ea" +dependencies = [ + "crypto-common", + "subtle", +] + [[package]] name = "version_check" version = "0.9.4" @@ -441,8 +619,34 @@ version = "0.11.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" +[[package]] +name = "x25519-dalek" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fb66477291e7e8d2b0ff1bcb900bf29489a9692816d79874bea351e7a8b6de96" +dependencies = [ + "curve25519-dalek", + "rand_core", + "serde", + "zeroize", +] + [[package]] name = "zeroize" version = "1.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2a0956f1ba7c7909bfb66c2e9e4124ab6f6482560f6628b5aaeba39207c9aad9" +dependencies = [ + "zeroize_derive", +] + +[[package]] +name = "zeroize_derive" +version = "1.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce36e65b0d2999d2aafac989fb249189a141aee1f53c612c1f37d72631959f69" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] diff --git a/loki-server/src/dm.rs b/loki-server/src/dm.rs new file mode 100644 index 0000000..e5f2dfe --- /dev/null +++ b/loki-server/src/dm.rs @@ -0,0 +1,5 @@ +use loki_shared::Message; + +pub struct DmChannel { + mailbox: Vec +} diff --git a/loki-server/src/main.rs b/loki-server/src/main.rs index a30eb95..2b09740 100644 --- a/loki-server/src/main.rs +++ b/loki-server/src/main.rs @@ -1,3 +1,5 @@ +mod dm; + fn main() { println!("Hello, world!"); } diff --git a/loki-shared/Cargo.toml b/loki-shared/Cargo.toml index 7c73d38..d1fdf51 100644 --- a/loki-shared/Cargo.toml +++ b/loki-shared/Cargo.toml @@ -6,3 +6,7 @@ version = "0.1.0" edition = "2021" [dependencies] +chacha20poly1305 = "0.10" +ed25519-dalek = "2.1" +x25519-dalek = { version="2.0", features=["reusable_secrets"] } +rand = "0.8" diff --git a/loki-shared/src/lib.rs b/loki-shared/src/lib.rs index 06d268d..b425b7b 100644 --- a/loki-shared/src/lib.rs +++ b/loki-shared/src/lib.rs @@ -1,14 +1,3 @@ -pub fn add(left: usize, right: usize) -> usize { - left + right -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn it_works() { - let result = add(2, 2); - assert_eq!(result, 4); - } -} +pub mod prekey; +pub mod message; +pub use message::{Message, InvalidKey}; diff --git a/loki-shared/src/message.rs b/loki-shared/src/message.rs new file mode 100644 index 0000000..7c3163a --- /dev/null +++ b/loki-shared/src/message.rs @@ -0,0 +1,113 @@ +use chacha20poly1305::{ + aead::{rand_core::RngCore, Aead, KeyInit, OsRng}, + Key, XChaCha20Poly1305, XNonce, +}; +use std::error::Error; +use std::fmt; +use std::time::{SystemTime, UNIX_EPOCH}; + +#[derive(Clone, Copy, PartialEq)] +pub struct InvalidKey; + +impl Eq for InvalidKey {} + +impl fmt::Display for InvalidKey { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "Invalid key") + } +} + +impl fmt::Debug for InvalidKey { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "Invalid key") + } +} + +impl Error for InvalidKey {} + +type Cipher = XChaCha20Poly1305; + +pub struct Message { + data: Vec, + nonce: Option>, + encrypted: bool, +} + +impl Message { + pub fn encrypt(key: &[u8], data: String, raw_nonce: Option<&[u8]>) -> Message { + let nonce = if raw_nonce.is_none() { + let timestamp = SystemTime::now() + .duration_since(UNIX_EPOCH) + .expect("Time went backwards") + .as_nanos() + .to_le_bytes(); + + let mut random = [0u8; 8]; + OsRng.fill_bytes(&mut random); + let x: Vec = [timestamp.as_ref(), random.as_ref()].concat(); + XNonce::from_iter(x) + } else { + XNonce::from_iter(raw_nonce.unwrap().to_vec()) + }; + + let cipher = Cipher::new(Key::from_slice(key)); + + Message { + data: cipher.encrypt(&nonce, data.as_bytes()).unwrap(), // *hopefully* shouldn't panic + nonce: Some(nonce.to_vec()), + encrypted: true, + } + } + + pub fn new(data: String) -> Message { + Message { + data: data.as_bytes().to_vec(), + nonce: None, + encrypted: false, + } + } + + pub fn deserialize(msg: Vec) -> Message { + if msg.last().unwrap() == &0u8 { + Message { + data: msg[..msg.len() - 2].to_vec(), + nonce: None, + encrypted: false, + } + } else { + Message { + data: msg[24..msg.len() - 2].to_vec(), + nonce: Some(msg[0..24].to_vec()), + encrypted: true, + } + } + } + + pub fn serialize(&self) -> Vec { + if !self.encrypted { + [self.data.as_ref(), [0].as_ref()].concat().to_vec() + } else { + [ + self.nonce.clone().unwrap().as_ref(), + self.data.as_ref(), + [1].as_ref(), + ] + .concat() + .to_vec() + } + } + + pub fn retrieve(&self, key: &[u8]) -> Result, InvalidKey> { + if !self.encrypted { + Ok(self.data.clone()) + } else { + match Cipher::new(Key::from_slice(key)).decrypt( + XNonce::from_slice(&self.nonce.clone().unwrap()), + self.data.as_ref(), + ) { + Ok(x) => Ok(x), + Err(_) => Err(InvalidKey), + } + } + } +} diff --git a/loki-shared/src/prekey.rs b/loki-shared/src/prekey.rs new file mode 100644 index 0000000..4440dc7 --- /dev/null +++ b/loki-shared/src/prekey.rs @@ -0,0 +1,24 @@ +use ed25519_dalek::{Signature, Signer, SigningKey}; +use x25519_dalek::{EphemeralSecret, PublicKey, ReusableSecret}; + +pub struct PublicSignedPrekey { + pubkey: PublicKey, + signature: Signature, +} + +impl PublicSignedPrekey { + pub(crate) fn from(pubkey: PublicKey, signing_key: SigningKey) -> PublicSignedPrekey { + let signature = signing_key.sign(&pubkey.to_bytes()); + + PublicSignedPrekey { pubkey, signature } + } + + pub fn serialize(&self) -> [u8; 96] { + let mut output = [0u8; 96]; + let (left, right) = output.split_at_mut(64); + left.copy_from_slice(&self.pubkey.to_bytes()); + right.copy_from_slice(&self.signature.to_bytes()); + + output + } +} From 123df2970eebde4551e07c1430f3772e8af7acf6 Mon Sep 17 00:00:00 2001 From: somehybrid Date: Sun, 28 Jan 2024 13:49:01 +0700 Subject: [PATCH 3/8] Rewrote user struct --- Cargo.lock | 180 +---------- loki-keyserver/Cargo.toml | 10 + loki-keyserver/LICENSE.md | 660 ++++++++++++++++++++++++++++++++++++++ protolok/Cargo.toml | 2 - protolok/src/crypto.rs | 49 --- protolok/src/ids.rs | 153 --------- protolok/src/lib.rs | 2 - protolok/src/user.rs | 73 +++-- 8 files changed, 717 insertions(+), 412 deletions(-) create mode 100644 loki-keyserver/Cargo.toml create mode 100644 loki-keyserver/LICENSE.md delete mode 100644 protolok/src/crypto.rs delete mode 100644 protolok/src/ids.rs diff --git a/Cargo.lock b/Cargo.lock index c47d8df..3ed3f54 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -12,18 +12,6 @@ dependencies = [ "generic-array", ] -[[package]] -name = "autocfg" -version = "1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" - -[[package]] -name = "base16ct" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4c7f02d4ea65f2c1853089ffd8d2787bdbc63de2f0d29dedbcf8ccdfa0ccd4cf" - [[package]] name = "base64ct" version = "1.6.0" @@ -39,12 +27,6 @@ dependencies = [ "generic-array", ] -[[package]] -name = "byteorder" -version = "1.4.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" - [[package]] name = "cfg-if" version = "1.0.0" @@ -140,17 +122,6 @@ dependencies = [ "syn", ] -[[package]] -name = "der" -version = "0.6.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f1a467a65c5e759bce6e65eaf91cc29f466cdc57cb65777bd646872a8a1fd4de" -dependencies = [ - "const-oid", - "pem-rfc7468", - "zeroize", -] - [[package]] name = "der" version = "0.7.8" @@ -168,7 +139,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8168378f4e5023e7218c89c891c0fd8ecdb5e5e4f18cb78f38cf245dd021e76f" dependencies = [ "block-buffer", - "const-oid", "crypto-common", ] @@ -178,7 +148,7 @@ version = "2.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "115531babc129696a58c64a4fef0a8bf9e9698629fb97e9e40767d235cfbcd53" dependencies = [ - "pkcs8 0.10.2", + "pkcs8", "signature", ] @@ -232,27 +202,12 @@ dependencies = [ "generic-array", ] -[[package]] -name = "lazy_static" -version = "1.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" -dependencies = [ - "spin", -] - [[package]] name = "libc" version = "0.2.141" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3304a64d199bb964be99741b7a14d26972741915b3649639149b2479bb46f4b5" -[[package]] -name = "libm" -version = "0.2.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "348108ab3fba42ec82ff6e9564fc4ca0247bdccdc68dd8af9764bbc79c3c8ffb" - [[package]] name = "loki-client" version = "0.1.0" @@ -284,99 +239,20 @@ dependencies = [ name = "lokui" version = "0.1.0" -[[package]] -name = "num-bigint-dig" -version = "0.8.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2399c9463abc5f909349d8aa9ba080e0b88b3ce2885389b60b993f39b1a56905" -dependencies = [ - "byteorder", - "lazy_static", - "libm", - "num-integer", - "num-iter", - "num-traits", - "rand", - "smallvec", - "zeroize", -] - -[[package]] -name = "num-integer" -version = "0.1.45" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9" -dependencies = [ - "autocfg", - "num-traits", -] - -[[package]] -name = "num-iter" -version = "0.1.43" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7d03e6c028c5dc5cac6e2dec0efda81fc887605bb3d884578bb6d6bf7514e252" -dependencies = [ - "autocfg", - "num-integer", - "num-traits", -] - -[[package]] -name = "num-traits" -version = "0.2.15" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd" -dependencies = [ - "autocfg", - "libm", -] - [[package]] name = "opaque-debug" version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5" -[[package]] -name = "pem-rfc7468" -version = "0.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "24d159833a9105500e0398934e205e0773f0b27529557134ecfc51c27646adac" -dependencies = [ - "base64ct", -] - -[[package]] -name = "pkcs1" -version = "0.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eff33bdbdfc54cc98a2eca766ebdec3e1b8fb7387523d5c9c9a2891da856f719" -dependencies = [ - "der 0.6.1", - "pkcs8 0.9.0", - "spki 0.6.0", - "zeroize", -] - -[[package]] -name = "pkcs8" -version = "0.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9eca2c590a5f85da82668fa685c09ce2888b9430e83299debf1f34b65fd4a4ba" -dependencies = [ - "der 0.6.1", - "spki 0.6.0", -] - [[package]] name = "pkcs8" version = "0.10.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f950b2377845cebe5cf8b5165cb3cc1a5e0fa5cfa3e1f7f55707d8fd82e0a7b7" dependencies = [ - "der 0.7.8", - "spki 0.7.3", + "der", + "spki", ] [[package]] @@ -415,9 +291,7 @@ dependencies = [ name = "protolok" version = "0.1.0" dependencies = [ - "base16ct", "rand", - "rsa", "sha2", ] @@ -460,26 +334,6 @@ dependencies = [ "getrandom", ] -[[package]] -name = "rsa" -version = "0.8.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "55a77d189da1fee555ad95b7e50e7457d91c0e089ec68ca69ad2989413bbdab4" -dependencies = [ - "byteorder", - "digest", - "num-bigint-dig", - "num-integer", - "num-iter", - "num-traits", - "pkcs1", - "pkcs8 0.9.0", - "rand_core", - "signature", - "subtle", - "zeroize", -] - [[package]] name = "rustc_version" version = "0.4.0" @@ -531,32 +385,6 @@ name = "signature" version = "2.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5e1788eed21689f9cf370582dfc467ef36ed9c707f073528ddafa8d83e3b8500" -dependencies = [ - "digest", - "rand_core", -] - -[[package]] -name = "smallvec" -version = "1.10.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a507befe795404456341dfab10cef66ead4c041f62b8b11bbb92bffe5d0953e0" - -[[package]] -name = "spin" -version = "0.5.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d" - -[[package]] -name = "spki" -version = "0.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "67cf02bbac7a337dc36e4f5a693db6c21e7863f45070f7064577eb4367a3212b" -dependencies = [ - "base64ct", - "der 0.6.1", -] [[package]] name = "spki" @@ -565,7 +393,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d91ed6c858b01f942cd56b37a94b3e0a1798290327d1236e4d9cf4eaca44d29d" dependencies = [ "base64ct", - "der 0.7.8", + "der", ] [[package]] diff --git a/loki-keyserver/Cargo.toml b/loki-keyserver/Cargo.toml new file mode 100644 index 0000000..03c957c --- /dev/null +++ b/loki-keyserver/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "loki-keyserver" +description = "Federated keyserver for Loki" +authors = ["The Loki Authors"] +version = "0.1.0" +edition = "2021" + +[dependencies] +loki-shared = { path = "../loki-shared" } +protolok = { path = "../protolok" } diff --git a/loki-keyserver/LICENSE.md b/loki-keyserver/LICENSE.md new file mode 100644 index 0000000..e066202 --- /dev/null +++ b/loki-keyserver/LICENSE.md @@ -0,0 +1,660 @@ +### GNU AFFERO GENERAL PUBLIC LICENSE + +Version 3, 19 November 2007 + +Copyright (C) 2007 Free Software Foundation, Inc. + + +Everyone is permitted to copy and distribute verbatim copies of this +license document, but changing it is not allowed. + +### Preamble + +The GNU Affero General Public License is a free, copyleft license for +software and other kinds of works, specifically designed to ensure +cooperation with the community in the case of network server software. + +The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +our General Public Licenses are intended to guarantee your freedom to +share and change all versions of a program--to make sure it remains +free software for all its users. + +When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +them if you wish), that you receive source code or can get it if you +want it, that you can change the software or use pieces of it in new +free programs, and that you know you can do these things. + +Developers that use our General Public Licenses protect your rights +with two steps: (1) assert copyright on the software, and (2) offer +you this License which gives you legal permission to copy, distribute +and/or modify the software. + +A secondary benefit of defending all users' freedom is that +improvements made in alternate versions of the program, if they +receive widespread use, become available for other developers to +incorporate. Many developers of free software are heartened and +encouraged by the resulting cooperation. However, in the case of +software used on network servers, this result may fail to come about. +The GNU General Public License permits making a modified version and +letting the public access it on a server without ever releasing its +source code to the public. + +The GNU Affero General Public License is designed specifically to +ensure that, in such cases, the modified source code becomes available +to the community. It requires the operator of a network server to +provide the source code of the modified version running there to the +users of that server. Therefore, public use of a modified version, on +a publicly accessible server, gives the public access to the source +code of the modified version. + +An older license, called the Affero General Public License and +published by Affero, was designed to accomplish similar goals. This is +a different license, not a version of the Affero GPL, but Affero has +released a new version of the Affero GPL which permits relicensing +under this license. + +The precise terms and conditions for copying, distribution and +modification follow. + +### TERMS AND CONDITIONS + +#### 0. Definitions. + +"This License" refers to version 3 of the GNU Affero General Public +License. + +"Copyright" also means copyright-like laws that apply to other kinds +of works, such as semiconductor masks. + +"The Program" refers to any copyrightable work licensed under this +License. Each licensee is addressed as "you". "Licensees" and +"recipients" may be individuals or organizations. + +To "modify" a work means to copy from or adapt all or part of the work +in a fashion requiring copyright permission, other than the making of +an exact copy. The resulting work is called a "modified version" of +the earlier work or a work "based on" the earlier work. + +A "covered work" means either the unmodified Program or a work based +on the Program. + +To "propagate" a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification), making available to the +public, and in some countries other activities as well. + +To "convey" a work means any kind of propagation that enables other +parties to make or receive copies. Mere interaction with a user +through a computer network, with no transfer of a copy, is not +conveying. + +An interactive user interface displays "Appropriate Legal Notices" to +the extent that it includes a convenient and prominently visible +feature that (1) displays an appropriate copyright notice, and (2) +tells the user that there is no warranty for the work (except to the +extent that warranties are provided), that licensees may convey the +work under this License, and how to view a copy of this License. If +the interface presents a list of user commands or options, such as a +menu, a prominent item in the list meets this criterion. + +#### 1. Source Code. + +The "source code" for a work means the preferred form of the work for +making modifications to it. "Object code" means any non-source form of +a work. + +A "Standard Interface" means an interface that either is an official +standard defined by a recognized standards body, or, in the case of +interfaces specified for a particular programming language, one that +is widely used among developers working in that language. + +The "System Libraries" of an executable work include anything, other +than the work as a whole, that (a) is included in the normal form of +packaging a Major Component, but which is not part of that Major +Component, and (b) serves only to enable use of the work with that +Major Component, or to implement a Standard Interface for which an +implementation is available to the public in source code form. A +"Major Component", in this context, means a major essential component +(kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to +produce the work, or an object code interpreter used to run it. + +The "Corresponding Source" for a work in object code form means all +the source code needed to generate, install, and (for an executable +work) run the object code and to modify the work, including scripts to +control those activities. However, it does not include the work's +System Libraries, or general-purpose tools or generally available free +programs which are used unmodified in performing those activities but +which are not part of the work. For example, Corresponding Source +includes interface definition files associated with source files for +the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, +such as by intimate data communication or control flow between those +subprograms and other parts of the work. + +The Corresponding Source need not include anything that users can +regenerate automatically from other parts of the Corresponding Source. + +The Corresponding Source for a work in source code form is that same +work. + +#### 2. Basic Permissions. + +All rights granted under this License are granted for the term of +copyright on the Program, and are irrevocable provided the stated +conditions are met. This License explicitly affirms your unlimited +permission to run the unmodified Program. The output from running a +covered work is covered by this License only if the output, given its +content, constitutes a covered work. This License acknowledges your +rights of fair use or other equivalent, as provided by copyright law. + +You may make, run and propagate covered works that you do not convey, +without conditions so long as your license otherwise remains in force. +You may convey covered works to others for the sole purpose of having +them make modifications exclusively for you, or provide you with +facilities for running those works, provided that you comply with the +terms of this License in conveying all material for which you do not +control copyright. Those thus making or running the covered works for +you must do so exclusively on your behalf, under your direction and +control, on terms that prohibit them from making any copies of your +copyrighted material outside their relationship with you. + +Conveying under any other circumstances is permitted solely under the +conditions stated below. Sublicensing is not allowed; section 10 makes +it unnecessary. + +#### 3. Protecting Users' Legal Rights From Anti-Circumvention Law. + +No covered work shall be deemed part of an effective technological +measure under any applicable law fulfilling obligations under article +11 of the WIPO copyright treaty adopted on 20 December 1996, or +similar laws prohibiting or restricting circumvention of such +measures. + +When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such +circumvention is effected by exercising rights under this License with +respect to the covered work, and you disclaim any intention to limit +operation or modification of the work as a means of enforcing, against +the work's users, your or third parties' legal rights to forbid +circumvention of technological measures. + +#### 4. Conveying Verbatim Copies. + +You may convey verbatim copies of the Program's source code as you +receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice; +keep intact all notices stating that this License and any +non-permissive terms added in accord with section 7 apply to the code; +keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + +You may charge any price or no price for each copy that you convey, +and you may offer support or warranty protection for a fee. + +#### 5. Conveying Modified Source Versions. + +You may convey a work based on the Program, or the modifications to +produce it from the Program, in the form of source code under the +terms of section 4, provided that you also meet all of these +conditions: + +- a) The work must carry prominent notices stating that you modified + it, and giving a relevant date. +- b) The work must carry prominent notices stating that it is + released under this License and any conditions added under + section 7. This requirement modifies the requirement in section 4 + to "keep intact all notices". +- c) You must license the entire work, as a whole, under this + License to anyone who comes into possession of a copy. This + License will therefore apply, along with any applicable section 7 + additional terms, to the whole of the work, and all its parts, + regardless of how they are packaged. This License gives no + permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. +- d) If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive + interfaces that do not display Appropriate Legal Notices, your + work need not make them do so. + +A compilation of a covered work with other separate and independent +works, which are not by their nature extensions of the covered work, +and which are not combined with it such as to form a larger program, +in or on a volume of a storage or distribution medium, is called an +"aggregate" if the compilation and its resulting copyright are not +used to limit the access or legal rights of the compilation's users +beyond what the individual works permit. Inclusion of a covered work +in an aggregate does not cause this License to apply to the other +parts of the aggregate. + +#### 6. Conveying Non-Source Forms. + +You may convey a covered work in object code form under the terms of +sections 4 and 5, provided that you also convey the machine-readable +Corresponding Source under the terms of this License, in one of these +ways: + +- a) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the + Corresponding Source fixed on a durable physical medium + customarily used for software interchange. +- b) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a + written offer, valid for at least three years and valid for as + long as you offer spare parts or customer support for that product + model, to give anyone who possesses the object code either (1) a + copy of the Corresponding Source for all the software in the + product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no + more than your reasonable cost of physically performing this + conveying of source, or (2) access to copy the Corresponding + Source from a network server at no charge. +- c) Convey individual copies of the object code with a copy of the + written offer to provide the Corresponding Source. This + alternative is allowed only occasionally and noncommercially, and + only if you received the object code with such an offer, in accord + with subsection 6b. +- d) Convey the object code by offering access from a designated + place (gratis or for a charge), and offer equivalent access to the + Corresponding Source in the same way through the same place at no + further charge. You need not require recipients to copy the + Corresponding Source along with the object code. If the place to + copy the object code is a network server, the Corresponding Source + may be on a different server (operated by you or a third party) + that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the + Corresponding Source, you remain obligated to ensure that it is + available for as long as needed to satisfy these requirements. +- e) Convey the object code using peer-to-peer transmission, + provided you inform other peers where the object code and + Corresponding Source of the work are being offered to the general + public at no charge under subsection 6d. + +A separable portion of the object code, whose source code is excluded +from the Corresponding Source as a System Library, need not be +included in conveying the object code work. + +A "User Product" is either (1) a "consumer product", which means any +tangible personal property which is normally used for personal, +family, or household purposes, or (2) anything designed or sold for +incorporation into a dwelling. In determining whether a product is a +consumer product, doubtful cases shall be resolved in favor of +coverage. For a particular product received by a particular user, +"normally used" refers to a typical or common use of that class of +product, regardless of the status of the particular user or of the way +in which the particular user actually uses, or expects or is expected +to use, the product. A product is a consumer product regardless of +whether the product has substantial commercial, industrial or +non-consumer uses, unless such uses represent the only significant +mode of use of the product. + +"Installation Information" for a User Product means any methods, +procedures, authorization keys, or other information required to +install and execute modified versions of a covered work in that User +Product from a modified version of its Corresponding Source. The +information must suffice to ensure that the continued functioning of +the modified object code is in no case prevented or interfered with +solely because modification has been made. + +If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as +part of a transaction in which the right of possession and use of the +User Product is transferred to the recipient in perpetuity or for a +fixed term (regardless of how the transaction is characterized), the +Corresponding Source conveyed under this section must be accompanied +by the Installation Information. But this requirement does not apply +if neither you nor any third party retains the ability to install +modified object code on the User Product (for example, the work has +been installed in ROM). + +The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or +updates for a work that has been modified or installed by the +recipient, or for the User Product in which it has been modified or +installed. Access to a network may be denied when the modification +itself materially and adversely affects the operation of the network +or violates the rules and protocols for communication across the +network. + +Corresponding Source conveyed, and Installation Information provided, +in accord with this section must be in a format that is publicly +documented (and with an implementation available to the public in +source code form), and must require no special password or key for +unpacking, reading or copying. + +#### 7. Additional Terms. + +"Additional permissions" are terms that supplement the terms of this +License by making exceptions from one or more of its conditions. +Additional permissions that are applicable to the entire Program shall +be treated as though they were included in this License, to the extent +that they are valid under applicable law. If additional permissions +apply only to part of the Program, that part may be used separately +under those permissions, but the entire Program remains governed by +this License without regard to the additional permissions. + +When you convey a copy of a covered work, you may at your option +remove any additional permissions from that copy, or from any part of +it. (Additional permissions may be written to require their own +removal in certain cases when you modify the work.) You may place +additional permissions on material, added by you to a covered work, +for which you have or can give appropriate copyright permission. + +Notwithstanding any other provision of this License, for material you +add to a covered work, you may (if authorized by the copyright holders +of that material) supplement the terms of this License with terms: + +- a) Disclaiming warranty or limiting liability differently from the + terms of sections 15 and 16 of this License; or +- b) Requiring preservation of specified reasonable legal notices or + author attributions in that material or in the Appropriate Legal + Notices displayed by works containing it; or +- c) Prohibiting misrepresentation of the origin of that material, + or requiring that modified versions of such material be marked in + reasonable ways as different from the original version; or +- d) Limiting the use for publicity purposes of names of licensors + or authors of the material; or +- e) Declining to grant rights under trademark law for use of some + trade names, trademarks, or service marks; or +- f) Requiring indemnification of licensors and authors of that + material by anyone who conveys the material (or modified versions + of it) with contractual assumptions of liability to the recipient, + for any liability that these contractual assumptions directly + impose on those licensors and authors. + +All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is +governed by this License along with a term that is a further +restriction, you may remove that term. If a license document contains +a further restriction but permits relicensing or conveying under this +License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does +not survive such relicensing or conveying. + +If you add terms to a covered work in accord with this section, you +must place, in the relevant source files, a statement of the +additional terms that apply to those files, or a notice indicating +where to find the applicable terms. + +Additional terms, permissive or non-permissive, may be stated in the +form of a separately written license, or stated as exceptions; the +above requirements apply either way. + +#### 8. Termination. + +You may not propagate or modify a covered work except as expressly +provided under this License. Any attempt otherwise to propagate or +modify it is void, and will automatically terminate your rights under +this License (including any patent licenses granted under the third +paragraph of section 11). + +However, if you cease all violation of this License, then your license +from a particular copyright holder is reinstated (a) provisionally, +unless and until the copyright holder explicitly and finally +terminates your license, and (b) permanently, if the copyright holder +fails to notify you of the violation by some reasonable means prior to +60 days after the cessation. + +Moreover, your license from a particular copyright holder is +reinstated permanently if the copyright holder notifies you of the +violation by some reasonable means, this is the first time you have +received notice of violation of this License (for any work) from that +copyright holder, and you cure the violation prior to 30 days after +your receipt of the notice. + +Termination of your rights under this section does not terminate the +licenses of parties who have received copies or rights from you under +this License. If your rights have been terminated and not permanently +reinstated, you do not qualify to receive new licenses for the same +material under section 10. + +#### 9. Acceptance Not Required for Having Copies. + +You are not required to accept this License in order to receive or run +a copy of the Program. Ancillary propagation of a covered work +occurring solely as a consequence of using peer-to-peer transmission +to receive a copy likewise does not require acceptance. However, +nothing other than this License grants you permission to propagate or +modify any covered work. These actions infringe copyright if you do +not accept this License. Therefore, by modifying or propagating a +covered work, you indicate your acceptance of this License to do so. + +#### 10. Automatic Licensing of Downstream Recipients. + +Each time you convey a covered work, the recipient automatically +receives a license from the original licensors, to run, modify and +propagate that work, subject to this License. You are not responsible +for enforcing compliance by third parties with this License. + +An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered +work results from an entity transaction, each party to that +transaction who receives a copy of the work also receives whatever +licenses to the work the party's predecessor in interest had or could +give under the previous paragraph, plus a right to possession of the +Corresponding Source of the work from the predecessor in interest, if +the predecessor has it or can get it with reasonable efforts. + +You may not impose any further restrictions on the exercise of the +rights granted or affirmed under this License. For example, you may +not impose a license fee, royalty, or other charge for exercise of +rights granted under this License, and you may not initiate litigation +(including a cross-claim or counterclaim in a lawsuit) alleging that +any patent claim is infringed by making, using, selling, offering for +sale, or importing the Program or any portion of it. + +#### 11. Patents. + +A "contributor" is a copyright holder who authorizes use under this +License of the Program or a work on which the Program is based. The +work thus licensed is called the contributor's "contributor version". + +A contributor's "essential patent claims" are all patent claims owned +or controlled by the contributor, whether already acquired or +hereafter acquired, that would be infringed by some manner, permitted +by this License, of making, using, or selling its contributor version, +but do not include claims that would be infringed only as a +consequence of further modification of the contributor version. For +purposes of this definition, "control" includes the right to grant +patent sublicenses in a manner consistent with the requirements of +this License. + +Each contributor grants you a non-exclusive, worldwide, royalty-free +patent license under the contributor's essential patent claims, to +make, use, sell, offer for sale, import and otherwise run, modify and +propagate the contents of its contributor version. + +In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent +(such as an express permission to practice a patent or covenant not to +sue for patent infringement). To "grant" such a patent license to a +party means to make such an agreement or commitment not to enforce a +patent against the party. + +If you convey a covered work, knowingly relying on a patent license, +and the Corresponding Source of the work is not available for anyone +to copy, free of charge and under the terms of this License, through a +publicly available network server or other readily accessible means, +then you must either (1) cause the Corresponding Source to be so +available, or (2) arrange to deprive yourself of the benefit of the +patent license for this particular work, or (3) arrange, in a manner +consistent with the requirements of this License, to extend the patent +license to downstream recipients. "Knowingly relying" means you have +actual knowledge that, but for the patent license, your conveying the +covered work in a country, or your recipient's use of the covered work +in a country, would infringe one or more identifiable patents in that +country that you have reason to believe are valid. + +If, pursuant to or in connection with a single transaction or +arrangement, you convey, or propagate by procuring conveyance of, a +covered work, and grant a patent license to some of the parties +receiving the covered work authorizing them to use, propagate, modify +or convey a specific copy of the covered work, then the patent license +you grant is automatically extended to all recipients of the covered +work and works based on it. + +A patent license is "discriminatory" if it does not include within the +scope of its coverage, prohibits the exercise of, or is conditioned on +the non-exercise of one or more of the rights that are specifically +granted under this License. You may not convey a covered work if you +are a party to an arrangement with a third party that is in the +business of distributing software, under which you make payment to the +third party based on the extent of your activity of conveying the +work, and under which the third party grants, to any of the parties +who would receive the covered work from you, a discriminatory patent +license (a) in connection with copies of the covered work conveyed by +you (or copies made from those copies), or (b) primarily for and in +connection with specific products or compilations that contain the +covered work, unless you entered into that arrangement, or that patent +license was granted, prior to 28 March 2007. + +Nothing in this License shall be construed as excluding or limiting +any implied license or other defenses to infringement that may +otherwise be available to you under applicable patent law. + +#### 12. No Surrender of Others' Freedom. + +If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot convey a +covered work so as to satisfy simultaneously your obligations under +this License and any other pertinent obligations, then as a +consequence you may not convey it at all. For example, if you agree to +terms that obligate you to collect a royalty for further conveying +from those to whom you convey the Program, the only way you could +satisfy both those terms and this License would be to refrain entirely +from conveying the Program. + +#### 13. Remote Network Interaction; Use with the GNU General Public License. + +Notwithstanding any other provision of this License, if you modify the +Program, your modified version must prominently offer all users +interacting with it remotely through a computer network (if your +version supports such interaction) an opportunity to receive the +Corresponding Source of your version by providing access to the +Corresponding Source from a network server at no charge, through some +standard or customary means of facilitating copying of software. This +Corresponding Source shall include the Corresponding Source for any +work covered by version 3 of the GNU General Public License that is +incorporated pursuant to the following paragraph. + +Notwithstanding any other provision of this License, you have +permission to link or combine any covered work with a work licensed +under version 3 of the GNU General Public License into a single +combined work, and to convey the resulting work. The terms of this +License will continue to apply to the part which is the covered work, +but the work with which it is combined will remain governed by version +3 of the GNU General Public License. + +#### 14. Revised Versions of this License. + +The Free Software Foundation may publish revised and/or new versions +of the GNU Affero General Public License from time to time. Such new +versions will be similar in spirit to the present version, but may +differ in detail to address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies that a certain numbered version of the GNU Affero General +Public License "or any later version" applies to it, you have the +option of following the terms and conditions either of that numbered +version or of any later version published by the Free Software +Foundation. If the Program does not specify a version number of the +GNU Affero General Public License, you may choose any version ever +published by the Free Software Foundation. + +If the Program specifies that a proxy can decide which future versions +of the GNU Affero General Public License can be used, that proxy's +public statement of acceptance of a version permanently authorizes you +to choose that version for the Program. + +Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + +#### 15. Disclaimer of Warranty. + +THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT +WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND +PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE +DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR +CORRECTION. + +#### 16. Limitation of Liability. + +IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR +CONVEYS THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES +ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT +NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR +LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM +TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER +PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. + +#### 17. Interpretation of Sections 15 and 16. + +If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. + +END OF TERMS AND CONDITIONS + +### How to Apply These Terms to Your New Programs + +If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these +terms. + +To do so, attach the following notices to the program. It is safest to +attach them to the start of each source file to most effectively state +the exclusion of warranty; and each file should have at least the +"copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as + published by the Free Software Foundation, either version 3 of the + License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . + +Also add information on how to contact you by electronic and paper +mail. + +If your software can interact with users remotely through a computer +network, you should also make sure that it provides a way for users to +get its source. For example, if your program is a web application, its +interface could display a "Source" link that leads users to an archive +of the code. There are many ways you could offer source, and different +solutions will be better for different programs; see section 13 for +the specific requirements. + +You should also get your employer (if you work as a programmer) or +school, if any, to sign a "copyright disclaimer" for the program, if +necessary. For more information on this, and how to apply and follow +the GNU AGPL, see . \ No newline at end of file diff --git a/protolok/Cargo.toml b/protolok/Cargo.toml index 022943f..d636370 100644 --- a/protolok/Cargo.toml +++ b/protolok/Cargo.toml @@ -6,7 +6,5 @@ version = "0.1.0" edition = "2021" [dependencies] -base16ct = "0.2" rand = "0.8.5" -rsa = "0.8" sha2 = "0.10" diff --git a/protolok/src/crypto.rs b/protolok/src/crypto.rs deleted file mode 100644 index 257ee62..0000000 --- a/protolok/src/crypto.rs +++ /dev/null @@ -1,49 +0,0 @@ -use std::collections::HashMap; - -use rand::{CryptoRng, RngCore}; -use rsa::{Pkcs1v15Encrypt, PublicKey, RsaPrivateKey}; - -use crate::ids::{make_id, Object, ServerMeta}; - -pub struct ClientKeypair { - pub(crate) rsa_pair: RsaPrivateKey, - pub rng: R, - id: u64, -} - -impl Object for ClientKeypair { - fn initialize(&mut self, meta: &ServerMeta) { - self.id = make_id(meta); - } - - fn get_id(&self) -> u64 { - self.id - } -} - -trait Decrypt { - fn decrypt_content(&self, data: Vec) -> Option>; -} - -trait Encrypt { - fn encrypt_content(&mut self, data: Vec) -> Option>; -} - -impl Decrypt for ClientKeypair { - fn decrypt_content(&self, data: Vec) -> Option> { - self.rsa_pair.decrypt(Pkcs1v15Encrypt, &data).ok() - } -} - -impl Encrypt for ClientKeypair { - fn encrypt_content(&mut self, data: Vec) -> Option> { - self.rsa_pair - .encrypt(&mut self.rng, Pkcs1v15Encrypt, &data) - .ok() - } -} - -pub struct ClientCrypto { - pub keystore: HashMap>, - pub channel_keys: HashMap, -} diff --git a/protolok/src/ids.rs b/protolok/src/ids.rs deleted file mode 100644 index 3029a86..0000000 --- a/protolok/src/ids.rs +++ /dev/null @@ -1,153 +0,0 @@ -use std::{ - collections::HashMap, - sync::{Mutex, RwLock}, - thread::{self, ThreadId}, - time::{Duration, SystemTime}, -}; - -/// 2023-01-01 00:00:00.000 GMT -const JAN_1_2023: u64 = 1672531200000; - -pub struct Count { - pub count: u64, - pub time_ms: u64, -} - -pub struct Timekeeper { - last_invoked: SystemTime, - current_count: u16, -} - -impl Timekeeper { - pub fn new() -> Self { - Self { - last_invoked: SystemTime::now(), - current_count: 0, - } - } -} - -impl Default for Timekeeper { - fn default() -> Self { - Self::new() - } -} - -impl Iterator for Timekeeper { - type Item = Count; - - /// Never returns None. - fn next(&mut self) -> Option { - let cnt = self.current_count; - self.current_count += 1; - let time = SystemTime::now(); - if time - .duration_since(self.last_invoked) - .expect("time should not go backwards!!!!!") - .as_millis() > 0 - { - self.current_count = 0; - } - Some(Count { - count: cnt as u64, - time_ms: time - .duration_since(SystemTime::UNIX_EPOCH + Duration::from_millis(JAN_1_2023)) - .expect("unix epoch should be minimum") - .as_millis() as u64, - }) - } -} - -pub struct ServerMeta { - pub server_domain: &'static str, - pub worker_id: RwLock>, - pub timekeeper: RwLock>>, - pub next_tid: Mutex, -} - -impl ServerMeta { - pub fn new(server_domain: &'static str) -> Self { - Self { - server_domain, - worker_id: RwLock::new(HashMap::new()), - timekeeper: RwLock::new(HashMap::new()), - next_tid: Mutex::new(0), - } - } - - fn next_tid(&self) -> u16 { - let mut n = self.next_tid.lock().expect("poisoned mutex"); - let tid: u16 = *n; - *n += 1; - tid - } - - pub fn get_id(&self) -> u16 { - let key = thread::current().id(); - let tid = self.next_tid(); - if self.worker_id.read().unwrap().get(&key).is_none() { - self.worker_id.write().unwrap().insert(key, tid); - } - *self.worker_id.read().unwrap().get(&key).unwrap() // SAFETY: Checked above - } - - pub fn count(&self) -> Count { - let key = thread::current().id(); - if self.timekeeper.read().unwrap().get(&key).is_none() { - self.timekeeper - .write() - .unwrap() - .insert(key, Mutex::new(Timekeeper::new())); - } - self.timekeeper - .read() - .unwrap() - .get(&key) - .unwrap() // SAFETY: checked above - .lock() - .unwrap() - .next() - .unwrap() // SAFETY: Never None. - } -} - -const fn bit_mask(amount: u64) -> u64 { - let mut m = 0; - let mut i = 0; - while i < amount { - m <<= 1; - m += 1; - i += 1; - } - m -} - -pub fn make_id(meta: &ServerMeta) -> u64 { - let count = meta.count(); - ((count.time_ms & bit_mask(42)) << (10 + 12)) - + ((meta.get_id() as u64 & bit_mask(10)) << 10) - + (count.count & bit_mask(12)) -} - -pub fn id_to_parts(id: u64) -> (u64, u16, u16) { - let count = id & bit_mask(12); - let worker = (id >> 12) & bit_mask(10); - let time = (id >> (12 + 10)) & bit_mask(42); - (time, worker as u16, count as u16) -} - -pub fn to_unix_time(time: u64) -> u128 { - time as u128 + JAN_1_2023 as u128 -} - -pub trait Object { - fn initialize(&mut self, meta: &ServerMeta); - fn get_id(&self) -> u64; - fn id_to_parts(&self) -> (u64, u16, u16) { - id_to_parts(self.get_id()) - } - fn get_time(&self) -> SystemTime { - SystemTime::UNIX_EPOCH - + Duration::from_millis(to_unix_time(id_to_parts(self.get_id()).0) as u64) - } -} diff --git a/protolok/src/lib.rs b/protolok/src/lib.rs index 8f23a65..21f62fc 100644 --- a/protolok/src/lib.rs +++ b/protolok/src/lib.rs @@ -1,9 +1,7 @@ pub mod channel; -pub mod ids; pub mod place; pub mod user; pub use channel::*; -pub use ids::*; pub use place::*; pub use user::*; diff --git a/protolok/src/user.rs b/protolok/src/user.rs index c1482e6..c17230a 100644 --- a/protolok/src/user.rs +++ b/protolok/src/user.rs @@ -1,44 +1,57 @@ -use crate::ids::{make_id, Object, ServerMeta}; +use std::collections::hash_map::DefaultHasher; +use std::hash::Hasher; +use std::time::{Duration, SystemTime, UNIX_EPOCH}; + +const EPOCH: u64 = 1672531200000; + +fn generate_id(name: String, home: String, bot: bool) -> u64 { + let mut hash = DefaultHasher::new(); + hash.write(name.as_bytes()); + hash.write(home.as_bytes()); + + let mut id = hash.finish(); + + let mut time: u64 = SystemTime::now() + .duration_since(UNIX_EPOCH + Duration::from_millis(EPOCH)) + .expect("Time went backwards") + .as_millis() as u64; + + time |= bot as u64; + + id >>= 32; + id <<= 32; + id |= time; + + id +} #[derive(Clone, Debug, PartialEq, Eq)] -pub struct UserHandle { +pub struct User { pub name: String, pub home: String, pub bot: bool, pub id: u64, } -impl Object for UserHandle { - fn initialize(&mut self, meta: &ServerMeta) { - self.id = make_id(meta); - } - - fn get_id(&self) -> u64 { - self.id +impl User { + pub fn new(name: String, home: String, bot: bool) -> User { + let id = generate_id(name, home, bot); + User { + name, + home, + bot, + id, + } } } #[cfg(test)] mod tests { - use std::sync::OnceLock; - - use crate::ids::{id_to_parts, to_unix_time, Object, ServerMeta}; - - use super::UserHandle; - - #[test] - fn init_test() { - static META: OnceLock = OnceLock::new(); - META.get_or_init(|| ServerMeta::new("test.lokichat.xyz")); - let mut handle = UserHandle { - name: "TudbuT".to_owned(), - home: "test.lokichat.xyz".to_owned(), - bot: false, - id: 0, - }; - handle.initialize(META.get().unwrap()); - dbg!(handle.id); - dbg!(id_to_parts(handle.id)); - dbg!(to_unix_time(id_to_parts(handle.id).0)); - } + #[test] + fn test_unique() { + let user1 = User::new("tudbut", "loki.chat", false); + let user2 = User::new("tudbut", "loki.chat", true); + + assert_ne!(user1, user2); + } } From 833f70ca4a569609e79a74d3ff56d00d58593385 Mon Sep 17 00:00:00 2001 From: somehybrid Date: Sun, 28 Jan 2024 14:19:35 +0700 Subject: [PATCH 4/8] Refactor and bugfixes --- Cargo.lock | 9 +++------ loki-shared/Cargo.toml | 4 ---- loki-shared/src/lib.rs | 3 --- protolok/Cargo.toml | 3 +++ protolok/src/channel.rs | 4 ++-- protolok/src/lib.rs | 4 ++++ {loki-shared => protolok}/src/message.rs | 0 {loki-shared => protolok}/src/prekey.rs | 0 protolok/src/user.rs | 22 ++++++++++++---------- 9 files changed, 24 insertions(+), 25 deletions(-) rename {loki-shared => protolok}/src/message.rs (100%) rename {loki-shared => protolok}/src/prekey.rs (100%) diff --git a/Cargo.lock b/Cargo.lock index 3ed3f54..2fea748 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -228,12 +228,6 @@ dependencies = [ [[package]] name = "loki-shared" version = "0.1.0" -dependencies = [ - "chacha20poly1305", - "ed25519-dalek", - "rand", - "x25519-dalek", -] [[package]] name = "lokui" @@ -291,8 +285,11 @@ dependencies = [ name = "protolok" version = "0.1.0" dependencies = [ + "chacha20poly1305", + "ed25519-dalek", "rand", "sha2", + "x25519-dalek", ] [[package]] diff --git a/loki-shared/Cargo.toml b/loki-shared/Cargo.toml index d1fdf51..7c73d38 100644 --- a/loki-shared/Cargo.toml +++ b/loki-shared/Cargo.toml @@ -6,7 +6,3 @@ version = "0.1.0" edition = "2021" [dependencies] -chacha20poly1305 = "0.10" -ed25519-dalek = "2.1" -x25519-dalek = { version="2.0", features=["reusable_secrets"] } -rand = "0.8" diff --git a/loki-shared/src/lib.rs b/loki-shared/src/lib.rs index b425b7b..e69de29 100644 --- a/loki-shared/src/lib.rs +++ b/loki-shared/src/lib.rs @@ -1,3 +0,0 @@ -pub mod prekey; -pub mod message; -pub use message::{Message, InvalidKey}; diff --git a/protolok/Cargo.toml b/protolok/Cargo.toml index d636370..51e1945 100644 --- a/protolok/Cargo.toml +++ b/protolok/Cargo.toml @@ -8,3 +8,6 @@ edition = "2021" [dependencies] rand = "0.8.5" sha2 = "0.10" +chacha20poly1305 = "0.10" +ed25519-dalek = "2.1" +x25519-dalek = { version="2.0", features=["reusable_secrets"] } diff --git a/protolok/src/channel.rs b/protolok/src/channel.rs index e843716..fd079b5 100644 --- a/protolok/src/channel.rs +++ b/protolok/src/channel.rs @@ -1,4 +1,4 @@ -use crate::message::{Message, MessageContent}; +use crate::message::Message; pub trait Channel { fn fetch_newest_messages( @@ -6,5 +6,5 @@ pub trait Channel { amount: u32, begin_at_id: Option, ) -> Result, ErrorType>; - fn send_message(&self, message: MessageContent, ctx: Ctx) -> Result; + fn send_message(&self, message: Message, ctx: Ctx) -> Result<(), ErrorType>; } diff --git a/protolok/src/lib.rs b/protolok/src/lib.rs index 21f62fc..c7fb4e2 100644 --- a/protolok/src/lib.rs +++ b/protolok/src/lib.rs @@ -1,7 +1,11 @@ pub mod channel; pub mod place; pub mod user; +pub mod prekey; +pub mod message; pub use channel::*; pub use place::*; pub use user::*; +pub use message::*; +pub use prekey::*; diff --git a/loki-shared/src/message.rs b/protolok/src/message.rs similarity index 100% rename from loki-shared/src/message.rs rename to protolok/src/message.rs diff --git a/loki-shared/src/prekey.rs b/protolok/src/prekey.rs similarity index 100% rename from loki-shared/src/prekey.rs rename to protolok/src/prekey.rs diff --git a/protolok/src/user.rs b/protolok/src/user.rs index c17230a..c697f2e 100644 --- a/protolok/src/user.rs +++ b/protolok/src/user.rs @@ -4,7 +4,7 @@ use std::time::{Duration, SystemTime, UNIX_EPOCH}; const EPOCH: u64 = 1672531200000; -fn generate_id(name: String, home: String, bot: bool) -> u64 { +fn generate_id(name: &String, home: &String, bot: bool) -> u64 { let mut hash = DefaultHasher::new(); hash.write(name.as_bytes()); hash.write(home.as_bytes()); @@ -16,7 +16,7 @@ fn generate_id(name: String, home: String, bot: bool) -> u64 { .expect("Time went backwards") .as_millis() as u64; - time |= bot as u64; + time |= bot as u64; id >>= 32; id <<= 32; @@ -35,7 +35,7 @@ pub struct User { impl User { pub fn new(name: String, home: String, bot: bool) -> User { - let id = generate_id(name, home, bot); + let id = generate_id(&name, &home, bot); User { name, home, @@ -47,11 +47,13 @@ impl User { #[cfg(test)] mod tests { - #[test] - fn test_unique() { - let user1 = User::new("tudbut", "loki.chat", false); - let user2 = User::new("tudbut", "loki.chat", true); - - assert_ne!(user1, user2); - } + use super::User; + + #[test] + fn test_unique() { + let user1 = User::new("tudbut".to_owned(), "loki.chat".to_owned(), false); + let user2 = User::new("tudbut".to_owned(), "loki.chat".to_owned(), true); + + assert_ne!(user1, user2); + } } From b0430731827afb7d88a42e7febbb42c6feb29f1c Mon Sep 17 00:00:00 2001 From: somehybrid Date: Wed, 31 Jan 2024 09:55:33 +0700 Subject: [PATCH 5/8] bee movie --- protolok/src/channel.rs | 2 +- protolok/src/id.rs | 66 ++++++++++++++++++ protolok/src/lib.rs | 4 +- protolok/src/message.rs | 149 +++++++++++++--------------------------- protolok/src/prekey.rs | 24 ------- protolok/src/user.rs | 111 +++++++++++++++++++++--------- 6 files changed, 197 insertions(+), 159 deletions(-) create mode 100644 protolok/src/id.rs delete mode 100644 protolok/src/prekey.rs diff --git a/protolok/src/channel.rs b/protolok/src/channel.rs index fd079b5..265524f 100644 --- a/protolok/src/channel.rs +++ b/protolok/src/channel.rs @@ -1,4 +1,4 @@ -use crate::message::Message; +use crate::Message; pub trait Channel { fn fetch_newest_messages( diff --git a/protolok/src/id.rs b/protolok/src/id.rs new file mode 100644 index 0000000..9dbb14b --- /dev/null +++ b/protolok/src/id.rs @@ -0,0 +1,66 @@ +use std::time::{Duration, SystemTime, UNIX_EPOCH}; + +const EPOCH: Duration = Duration::from_millis(1672531200000); + +const fn mask(amount: u64) -> u64 { + (1 << amount) - 1 +} + +pub struct Snowflake { + last_evoked: u64, + count: u16, + worker_id: u16, +} + +impl Snowflake { + pub fn new(worker_id: u16) -> Snowflake { + Snowflake { + last_evoked: 0, + count: 0, + worker_id, + } + } + + pub fn generate(&mut self) -> u64 { + let timestamp = SystemTime::now() + .duration_since(UNIX_EPOCH + EPOCH) + .expect("Time went backwards") + .as_millis() as u64 + & mask(42); + + if self.last_evoked > timestamp { + self.count = 0; + self.last_evoked = timestamp; + } + + let id = timestamp << 22 + | (self.worker_id as u64 & mask(10)) << 12 + | (self.count as u64 & mask(12)); + + self.count += 1; + + id + } +} + +pub trait Object { + fn get_id(&self) -> u64; + fn creation_time(&self) -> SystemTime { + UNIX_EPOCH + EPOCH + Duration::from_millis(self.get_id() >> 22) + } + + fn serialize(&self) -> Vec; + fn deserialize(data: &[u8]) -> Self; +} + +#[cfg(test)] +mod tests { + use super::Snowflake; + + #[test] + fn test_snowflake_count() { + let mut snowflake = Snowflake::new(0); + + assert_ne!(snowflake.generate(), snowflake.generate()); + } +} diff --git a/protolok/src/lib.rs b/protolok/src/lib.rs index c7fb4e2..f0240f3 100644 --- a/protolok/src/lib.rs +++ b/protolok/src/lib.rs @@ -1,11 +1,11 @@ pub mod channel; pub mod place; pub mod user; -pub mod prekey; pub mod message; +pub mod id; pub use channel::*; pub use place::*; pub use user::*; pub use message::*; -pub use prekey::*; +pub use id::*; diff --git a/protolok/src/message.rs b/protolok/src/message.rs index 7c3163a..a30dc9a 100644 --- a/protolok/src/message.rs +++ b/protolok/src/message.rs @@ -1,113 +1,62 @@ -use chacha20poly1305::{ - aead::{rand_core::RngCore, Aead, KeyInit, OsRng}, - Key, XChaCha20Poly1305, XNonce, -}; -use std::error::Error; -use std::fmt; -use std::time::{SystemTime, UNIX_EPOCH}; - -#[derive(Clone, Copy, PartialEq)] -pub struct InvalidKey; - -impl Eq for InvalidKey {} - -impl fmt::Display for InvalidKey { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "Invalid key") - } -} - -impl fmt::Debug for InvalidKey { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "Invalid key") - } -} - -impl Error for InvalidKey {} - -type Cipher = XChaCha20Poly1305; +use crate::{Object, User}; +#[derive(Clone, PartialEq, Eq, Debug)] pub struct Message { data: Vec, - nonce: Option>, - encrypted: bool, + author: User, + id: u64, } -impl Message { - pub fn encrypt(key: &[u8], data: String, raw_nonce: Option<&[u8]>) -> Message { - let nonce = if raw_nonce.is_none() { - let timestamp = SystemTime::now() - .duration_since(UNIX_EPOCH) - .expect("Time went backwards") - .as_nanos() - .to_le_bytes(); - - let mut random = [0u8; 8]; - OsRng.fill_bytes(&mut random); - let x: Vec = [timestamp.as_ref(), random.as_ref()].concat(); - XNonce::from_iter(x) - } else { - XNonce::from_iter(raw_nonce.unwrap().to_vec()) - }; - - let cipher = Cipher::new(Key::from_slice(key)); - - Message { - data: cipher.encrypt(&nonce, data.as_bytes()).unwrap(), // *hopefully* shouldn't panic - nonce: Some(nonce.to_vec()), - encrypted: true, - } +impl Object for Message { + fn get_id(&self) -> u64 { + self.id } - pub fn new(data: String) -> Message { - Message { - data: data.as_bytes().to_vec(), - nonce: None, - encrypted: false, - } + fn serialize(&self) -> Vec { + [(self.data.len() as u32).to_le_bytes().to_vec(), self.data.clone(), self.id.to_le_bytes().to_vec(), self.author.serialize()].concat() } - pub fn deserialize(msg: Vec) -> Message { - if msg.last().unwrap() == &0u8 { - Message { - data: msg[..msg.len() - 2].to_vec(), - nonce: None, - encrypted: false, - } - } else { - Message { - data: msg[24..msg.len() - 2].to_vec(), - nonce: Some(msg[0..24].to_vec()), - encrypted: true, - } - } - } + fn deserialize(data: &[u8]) -> Self { + let message_len = u32::from_le_bytes(data[0..4].try_into().unwrap()) as usize; + let message = data[4..message_len + 4].to_vec(); - pub fn serialize(&self) -> Vec { - if !self.encrypted { - [self.data.as_ref(), [0].as_ref()].concat().to_vec() - } else { - [ - self.nonce.clone().unwrap().as_ref(), - self.data.as_ref(), - [1].as_ref(), - ] - .concat() - .to_vec() - } - } + let id = u64::from_le_bytes(data[message_len + 4..message_len + 4 + 8].try_into().unwrap()); + + let author = User::deserialize(&data[message_len + 4 + 8..]); - pub fn retrieve(&self, key: &[u8]) -> Result, InvalidKey> { - if !self.encrypted { - Ok(self.data.clone()) - } else { - match Cipher::new(Key::from_slice(key)).decrypt( - XNonce::from_slice(&self.nonce.clone().unwrap()), - self.data.as_ref(), - ) { - Ok(x) => Ok(x), - Err(_) => Err(InvalidKey), - } - } + Message { + data: message, + author, + id, + } } } + +#[cfg(test)] +mod tests { + use crate::Object; + use crate::User; + + use super::Message; + + #[test] + fn test_serialize() { + let user1 = User::from( + "tudbut".to_owned(), + "test.lokichat.xyz".to_owned(), + false, + 0, + ); + + let message = Message { + data: Vec::new(), + author: user1, + id: 0, + }; + + let serialized = message.serialize(); + let deserialized = Message::deserialize(&serialized); + + assert_eq!(deserialized, message) + } +} diff --git a/protolok/src/prekey.rs b/protolok/src/prekey.rs deleted file mode 100644 index 4440dc7..0000000 --- a/protolok/src/prekey.rs +++ /dev/null @@ -1,24 +0,0 @@ -use ed25519_dalek::{Signature, Signer, SigningKey}; -use x25519_dalek::{EphemeralSecret, PublicKey, ReusableSecret}; - -pub struct PublicSignedPrekey { - pubkey: PublicKey, - signature: Signature, -} - -impl PublicSignedPrekey { - pub(crate) fn from(pubkey: PublicKey, signing_key: SigningKey) -> PublicSignedPrekey { - let signature = signing_key.sign(&pubkey.to_bytes()); - - PublicSignedPrekey { pubkey, signature } - } - - pub fn serialize(&self) -> [u8; 96] { - let mut output = [0u8; 96]; - let (left, right) = output.split_at_mut(64); - left.copy_from_slice(&self.pubkey.to_bytes()); - right.copy_from_slice(&self.signature.to_bytes()); - - output - } -} diff --git a/protolok/src/user.rs b/protolok/src/user.rs index c697f2e..c48e8c9 100644 --- a/protolok/src/user.rs +++ b/protolok/src/user.rs @@ -1,29 +1,4 @@ -use std::collections::hash_map::DefaultHasher; -use std::hash::Hasher; -use std::time::{Duration, SystemTime, UNIX_EPOCH}; - -const EPOCH: u64 = 1672531200000; - -fn generate_id(name: &String, home: &String, bot: bool) -> u64 { - let mut hash = DefaultHasher::new(); - hash.write(name.as_bytes()); - hash.write(home.as_bytes()); - - let mut id = hash.finish(); - - let mut time: u64 = SystemTime::now() - .duration_since(UNIX_EPOCH + Duration::from_millis(EPOCH)) - .expect("Time went backwards") - .as_millis() as u64; - - time |= bot as u64; - - id >>= 32; - id <<= 32; - id |= time; - - id -} +use crate::Object; #[derive(Clone, Debug, PartialEq, Eq)] pub struct User { @@ -34,8 +9,7 @@ pub struct User { } impl User { - pub fn new(name: String, home: String, bot: bool) -> User { - let id = generate_id(&name, &home, bot); + pub fn from(name: String, home: String, bot: bool, id: u64) -> User { User { name, home, @@ -45,15 +19,88 @@ impl User { } } +impl Object for User { + fn get_id(&self) -> u64 { + self.id + } + + fn serialize(&self) -> Vec { + [ + &(self.name.len() as u16).to_le_bytes(), + self.name.as_bytes(), + &(self.home.len() as u16).to_le_bytes(), + self.home.as_bytes(), + &[self.bot as u8], + &self.id.to_le_bytes(), + ] + .concat() + } + + fn deserialize(data: &[u8]) -> Self { + let username_len = u16::from_le_bytes(data[0..2].try_into().unwrap()) as usize; + + let mut start = 2; + + let username = String::from_utf8(data[start..start + username_len].to_vec()).unwrap(); + + start += username_len; + + let home_len = u16::from_le_bytes( + data[start..start + 2].try_into().unwrap() + ) as usize; + + start += 2; + + let home = + String::from_utf8(data[start..start + home_len].to_vec()) + .unwrap(); + + start += home_len; + + let bot = data[start] != 0; + + let id = u64::from_le_bytes(data[start + 1..].try_into().unwrap()); + + User::from(username, home, bot, id) + } +} + #[cfg(test)] mod tests { + use crate::Object; + use super::User; #[test] - fn test_unique() { - let user1 = User::new("tudbut".to_owned(), "loki.chat".to_owned(), false); - let user2 = User::new("tudbut".to_owned(), "loki.chat".to_owned(), true); + fn test_equivalent() { + let user1 = User::from( + "tudbut".to_owned(), + "test.lokichat.xyz".to_owned(), + false, + 0, + ); + let user2 = User::from( + "tudbut".to_owned(), + "test.lokichat.xyz".to_owned(), + false, + 0, + ); + + assert_eq!(user1, user2); + } + + #[test] + fn test_serialize() { + let user1 = User::from( + "tudbut".to_owned(), + "test.lokichat.xyz".to_owned(), + false, + 0, + ); + let serialized = user1.serialize(); + + let user2 = User::deserialize(&serialized); - assert_ne!(user1, user2); + assert_eq!(user1, user2); } } From 009ae05eff217c9c2fff4b9c6023bc259840e9b8 Mon Sep 17 00:00:00 2001 From: somehybrid Date: Wed, 31 Jan 2024 17:06:24 +0700 Subject: [PATCH 6/8] Removals --- Cargo.lock | 244 ++++++++++++++ loki-keyserver/Cargo.toml | 10 - loki-keyserver/LICENSE.md | 660 -------------------------------------- loki-server/Cargo.toml | 1 + loki-server/src/dm.rs | 5 - loki-server/src/main.rs | 6 +- protolok/Cargo.toml | 2 +- 7 files changed, 250 insertions(+), 678 deletions(-) delete mode 100644 loki-keyserver/Cargo.toml delete mode 100644 loki-keyserver/LICENSE.md delete mode 100644 loki-server/src/dm.rs diff --git a/Cargo.lock b/Cargo.lock index 2fea748..5d07843 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -12,12 +12,24 @@ dependencies = [ "generic-array", ] +[[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + [[package]] name = "base64ct" version = "1.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8c3c1a368f70d6cf7302d78f8f7093da241fb8e8807c05cc9e51a125895a6d5b" +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + [[package]] name = "block-buffer" version = "0.10.4" @@ -27,6 +39,12 @@ dependencies = [ "generic-array", ] +[[package]] +name = "bytes" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2bd12c1caf447e69cd4528f47f94d203fd2582878ecb9e9465484c4148a8223" + [[package]] name = "cfg-if" version = "1.0.0" @@ -160,6 +178,7 @@ checksum = "1f628eaec48bfd21b865dc2950cfa014450c01d2fa2b69a86c2fd5844ec523c0" dependencies = [ "curve25519-dalek", "ed25519", + "rand_core", "serde", "sha2", "subtle", @@ -193,6 +212,12 @@ dependencies = [ "wasi", ] +[[package]] +name = "hermit-abi" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d3d0e0f38255e7fa3cf31335b3a56f05febd18025f4db5ef7a0cfb4f8da651f" + [[package]] name = "inout" version = "0.1.3" @@ -208,6 +233,16 @@ version = "0.2.141" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3304a64d199bb964be99741b7a14d26972741915b3649639149b2479bb46f4b5" +[[package]] +name = "lock_api" +version = "0.4.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c168f8615b12bc01f9c17e2eb0cc07dcae1940121185446edc3744920e8ef45" +dependencies = [ + "autocfg", + "scopeguard", +] + [[package]] name = "loki-client" version = "0.1.0" @@ -223,6 +258,7 @@ version = "0.1.0" dependencies = [ "loki-shared", "protolok", + "tokio", ] [[package]] @@ -233,12 +269,62 @@ version = "0.1.0" name = "lokui" version = "0.1.0" +[[package]] +name = "mio" +version = "0.8.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "927a765cd3fc26206e66b296465fa9d3e5ab003e651c1b3c060e7956d96b19d2" +dependencies = [ + "libc", + "wasi", + "windows-sys", +] + +[[package]] +name = "num_cpus" +version = "1.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4161fcb6d602d4d2081af7c3a45852d875a03dd337a6bfdd6e06407b61342a43" +dependencies = [ + "hermit-abi", + "libc", +] + [[package]] name = "opaque-debug" version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5" +[[package]] +name = "parking_lot" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3742b2c103b9f06bc9fff0a37ff4912935851bee6d36f3c02bcc755bcfec228f" +dependencies = [ + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.9.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c42a9226546d68acdd9c0a280d17ce19bfe27a46bf68784e4066115788d008e" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall", + "smallvec", + "windows-targets", +] + +[[package]] +name = "pin-project-lite" +version = "0.2.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8afb450f006bf6385ca15ef45d71d2288452bc3683ce2e2cacc0d18e4be60b58" + [[package]] name = "pkcs8" version = "0.10.2" @@ -331,6 +417,15 @@ dependencies = [ "getrandom", ] +[[package]] +name = "redox_syscall" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4722d768eff46b75989dd134e5c353f0d6296e5aaa3132e776cbdb56be7731aa" +dependencies = [ + "bitflags", +] + [[package]] name = "rustc_version" version = "0.4.0" @@ -340,6 +435,12 @@ dependencies = [ "semver", ] +[[package]] +name = "scopeguard" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" + [[package]] name = "semver" version = "1.0.21" @@ -377,12 +478,37 @@ dependencies = [ "digest", ] +[[package]] +name = "signal-hook-registry" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d8229b473baa5980ac72ef434c4415e70c4b5e71b423043adb4ba059f89c99a1" +dependencies = [ + "libc", +] + [[package]] name = "signature" version = "2.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5e1788eed21689f9cf370582dfc467ef36ed9c707f073528ddafa8d83e3b8500" +[[package]] +name = "smallvec" +version = "1.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6ecd384b10a64542d77071bd64bd7b231f4ed5940fba55e98c3de13824cf3d7" + +[[package]] +name = "socket2" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "64a4a911eed85daf18834cfaa86a79b7d266ff93ff5ba14005426219480ed662" +dependencies = [ + "libc", + "winapi", +] + [[package]] name = "spki" version = "0.7.3" @@ -410,6 +536,36 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "tokio" +version = "1.28.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94d7b1cfd2aa4011f2de74c2c4c63665e27a71006b0a192dcd2710272e73dfa2" +dependencies = [ + "autocfg", + "bytes", + "libc", + "mio", + "num_cpus", + "parking_lot", + "pin-project-lite", + "signal-hook-registry", + "socket2", + "tokio-macros", + "windows-sys", +] + +[[package]] +name = "tokio-macros" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "630bdcf245f78637c13ec01ffae6187cca34625e8c63150d424b59e55af2675e" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "typenum" version = "1.16.0" @@ -444,6 +600,94 @@ version = "0.11.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] +name = "windows-sys" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" + +[[package]] +name = "windows_i686_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" + +[[package]] +name = "windows_i686_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" + [[package]] name = "x25519-dalek" version = "2.0.0" diff --git a/loki-keyserver/Cargo.toml b/loki-keyserver/Cargo.toml deleted file mode 100644 index 03c957c..0000000 --- a/loki-keyserver/Cargo.toml +++ /dev/null @@ -1,10 +0,0 @@ -[package] -name = "loki-keyserver" -description = "Federated keyserver for Loki" -authors = ["The Loki Authors"] -version = "0.1.0" -edition = "2021" - -[dependencies] -loki-shared = { path = "../loki-shared" } -protolok = { path = "../protolok" } diff --git a/loki-keyserver/LICENSE.md b/loki-keyserver/LICENSE.md deleted file mode 100644 index e066202..0000000 --- a/loki-keyserver/LICENSE.md +++ /dev/null @@ -1,660 +0,0 @@ -### GNU AFFERO GENERAL PUBLIC LICENSE - -Version 3, 19 November 2007 - -Copyright (C) 2007 Free Software Foundation, Inc. - - -Everyone is permitted to copy and distribute verbatim copies of this -license document, but changing it is not allowed. - -### Preamble - -The GNU Affero General Public License is a free, copyleft license for -software and other kinds of works, specifically designed to ensure -cooperation with the community in the case of network server software. - -The licenses for most software and other practical works are designed -to take away your freedom to share and change the works. By contrast, -our General Public Licenses are intended to guarantee your freedom to -share and change all versions of a program--to make sure it remains -free software for all its users. - -When we speak of free software, we are referring to freedom, not -price. Our General Public Licenses are designed to make sure that you -have the freedom to distribute copies of free software (and charge for -them if you wish), that you receive source code or can get it if you -want it, that you can change the software or use pieces of it in new -free programs, and that you know you can do these things. - -Developers that use our General Public Licenses protect your rights -with two steps: (1) assert copyright on the software, and (2) offer -you this License which gives you legal permission to copy, distribute -and/or modify the software. - -A secondary benefit of defending all users' freedom is that -improvements made in alternate versions of the program, if they -receive widespread use, become available for other developers to -incorporate. Many developers of free software are heartened and -encouraged by the resulting cooperation. However, in the case of -software used on network servers, this result may fail to come about. -The GNU General Public License permits making a modified version and -letting the public access it on a server without ever releasing its -source code to the public. - -The GNU Affero General Public License is designed specifically to -ensure that, in such cases, the modified source code becomes available -to the community. It requires the operator of a network server to -provide the source code of the modified version running there to the -users of that server. Therefore, public use of a modified version, on -a publicly accessible server, gives the public access to the source -code of the modified version. - -An older license, called the Affero General Public License and -published by Affero, was designed to accomplish similar goals. This is -a different license, not a version of the Affero GPL, but Affero has -released a new version of the Affero GPL which permits relicensing -under this license. - -The precise terms and conditions for copying, distribution and -modification follow. - -### TERMS AND CONDITIONS - -#### 0. Definitions. - -"This License" refers to version 3 of the GNU Affero General Public -License. - -"Copyright" also means copyright-like laws that apply to other kinds -of works, such as semiconductor masks. - -"The Program" refers to any copyrightable work licensed under this -License. Each licensee is addressed as "you". "Licensees" and -"recipients" may be individuals or organizations. - -To "modify" a work means to copy from or adapt all or part of the work -in a fashion requiring copyright permission, other than the making of -an exact copy. The resulting work is called a "modified version" of -the earlier work or a work "based on" the earlier work. - -A "covered work" means either the unmodified Program or a work based -on the Program. - -To "propagate" a work means to do anything with it that, without -permission, would make you directly or secondarily liable for -infringement under applicable copyright law, except executing it on a -computer or modifying a private copy. Propagation includes copying, -distribution (with or without modification), making available to the -public, and in some countries other activities as well. - -To "convey" a work means any kind of propagation that enables other -parties to make or receive copies. Mere interaction with a user -through a computer network, with no transfer of a copy, is not -conveying. - -An interactive user interface displays "Appropriate Legal Notices" to -the extent that it includes a convenient and prominently visible -feature that (1) displays an appropriate copyright notice, and (2) -tells the user that there is no warranty for the work (except to the -extent that warranties are provided), that licensees may convey the -work under this License, and how to view a copy of this License. If -the interface presents a list of user commands or options, such as a -menu, a prominent item in the list meets this criterion. - -#### 1. Source Code. - -The "source code" for a work means the preferred form of the work for -making modifications to it. "Object code" means any non-source form of -a work. - -A "Standard Interface" means an interface that either is an official -standard defined by a recognized standards body, or, in the case of -interfaces specified for a particular programming language, one that -is widely used among developers working in that language. - -The "System Libraries" of an executable work include anything, other -than the work as a whole, that (a) is included in the normal form of -packaging a Major Component, but which is not part of that Major -Component, and (b) serves only to enable use of the work with that -Major Component, or to implement a Standard Interface for which an -implementation is available to the public in source code form. A -"Major Component", in this context, means a major essential component -(kernel, window system, and so on) of the specific operating system -(if any) on which the executable work runs, or a compiler used to -produce the work, or an object code interpreter used to run it. - -The "Corresponding Source" for a work in object code form means all -the source code needed to generate, install, and (for an executable -work) run the object code and to modify the work, including scripts to -control those activities. However, it does not include the work's -System Libraries, or general-purpose tools or generally available free -programs which are used unmodified in performing those activities but -which are not part of the work. For example, Corresponding Source -includes interface definition files associated with source files for -the work, and the source code for shared libraries and dynamically -linked subprograms that the work is specifically designed to require, -such as by intimate data communication or control flow between those -subprograms and other parts of the work. - -The Corresponding Source need not include anything that users can -regenerate automatically from other parts of the Corresponding Source. - -The Corresponding Source for a work in source code form is that same -work. - -#### 2. Basic Permissions. - -All rights granted under this License are granted for the term of -copyright on the Program, and are irrevocable provided the stated -conditions are met. This License explicitly affirms your unlimited -permission to run the unmodified Program. The output from running a -covered work is covered by this License only if the output, given its -content, constitutes a covered work. This License acknowledges your -rights of fair use or other equivalent, as provided by copyright law. - -You may make, run and propagate covered works that you do not convey, -without conditions so long as your license otherwise remains in force. -You may convey covered works to others for the sole purpose of having -them make modifications exclusively for you, or provide you with -facilities for running those works, provided that you comply with the -terms of this License in conveying all material for which you do not -control copyright. Those thus making or running the covered works for -you must do so exclusively on your behalf, under your direction and -control, on terms that prohibit them from making any copies of your -copyrighted material outside their relationship with you. - -Conveying under any other circumstances is permitted solely under the -conditions stated below. Sublicensing is not allowed; section 10 makes -it unnecessary. - -#### 3. Protecting Users' Legal Rights From Anti-Circumvention Law. - -No covered work shall be deemed part of an effective technological -measure under any applicable law fulfilling obligations under article -11 of the WIPO copyright treaty adopted on 20 December 1996, or -similar laws prohibiting or restricting circumvention of such -measures. - -When you convey a covered work, you waive any legal power to forbid -circumvention of technological measures to the extent such -circumvention is effected by exercising rights under this License with -respect to the covered work, and you disclaim any intention to limit -operation or modification of the work as a means of enforcing, against -the work's users, your or third parties' legal rights to forbid -circumvention of technological measures. - -#### 4. Conveying Verbatim Copies. - -You may convey verbatim copies of the Program's source code as you -receive it, in any medium, provided that you conspicuously and -appropriately publish on each copy an appropriate copyright notice; -keep intact all notices stating that this License and any -non-permissive terms added in accord with section 7 apply to the code; -keep intact all notices of the absence of any warranty; and give all -recipients a copy of this License along with the Program. - -You may charge any price or no price for each copy that you convey, -and you may offer support or warranty protection for a fee. - -#### 5. Conveying Modified Source Versions. - -You may convey a work based on the Program, or the modifications to -produce it from the Program, in the form of source code under the -terms of section 4, provided that you also meet all of these -conditions: - -- a) The work must carry prominent notices stating that you modified - it, and giving a relevant date. -- b) The work must carry prominent notices stating that it is - released under this License and any conditions added under - section 7. This requirement modifies the requirement in section 4 - to "keep intact all notices". -- c) You must license the entire work, as a whole, under this - License to anyone who comes into possession of a copy. This - License will therefore apply, along with any applicable section 7 - additional terms, to the whole of the work, and all its parts, - regardless of how they are packaged. This License gives no - permission to license the work in any other way, but it does not - invalidate such permission if you have separately received it. -- d) If the work has interactive user interfaces, each must display - Appropriate Legal Notices; however, if the Program has interactive - interfaces that do not display Appropriate Legal Notices, your - work need not make them do so. - -A compilation of a covered work with other separate and independent -works, which are not by their nature extensions of the covered work, -and which are not combined with it such as to form a larger program, -in or on a volume of a storage or distribution medium, is called an -"aggregate" if the compilation and its resulting copyright are not -used to limit the access or legal rights of the compilation's users -beyond what the individual works permit. Inclusion of a covered work -in an aggregate does not cause this License to apply to the other -parts of the aggregate. - -#### 6. Conveying Non-Source Forms. - -You may convey a covered work in object code form under the terms of -sections 4 and 5, provided that you also convey the machine-readable -Corresponding Source under the terms of this License, in one of these -ways: - -- a) Convey the object code in, or embodied in, a physical product - (including a physical distribution medium), accompanied by the - Corresponding Source fixed on a durable physical medium - customarily used for software interchange. -- b) Convey the object code in, or embodied in, a physical product - (including a physical distribution medium), accompanied by a - written offer, valid for at least three years and valid for as - long as you offer spare parts or customer support for that product - model, to give anyone who possesses the object code either (1) a - copy of the Corresponding Source for all the software in the - product that is covered by this License, on a durable physical - medium customarily used for software interchange, for a price no - more than your reasonable cost of physically performing this - conveying of source, or (2) access to copy the Corresponding - Source from a network server at no charge. -- c) Convey individual copies of the object code with a copy of the - written offer to provide the Corresponding Source. This - alternative is allowed only occasionally and noncommercially, and - only if you received the object code with such an offer, in accord - with subsection 6b. -- d) Convey the object code by offering access from a designated - place (gratis or for a charge), and offer equivalent access to the - Corresponding Source in the same way through the same place at no - further charge. You need not require recipients to copy the - Corresponding Source along with the object code. If the place to - copy the object code is a network server, the Corresponding Source - may be on a different server (operated by you or a third party) - that supports equivalent copying facilities, provided you maintain - clear directions next to the object code saying where to find the - Corresponding Source. Regardless of what server hosts the - Corresponding Source, you remain obligated to ensure that it is - available for as long as needed to satisfy these requirements. -- e) Convey the object code using peer-to-peer transmission, - provided you inform other peers where the object code and - Corresponding Source of the work are being offered to the general - public at no charge under subsection 6d. - -A separable portion of the object code, whose source code is excluded -from the Corresponding Source as a System Library, need not be -included in conveying the object code work. - -A "User Product" is either (1) a "consumer product", which means any -tangible personal property which is normally used for personal, -family, or household purposes, or (2) anything designed or sold for -incorporation into a dwelling. In determining whether a product is a -consumer product, doubtful cases shall be resolved in favor of -coverage. For a particular product received by a particular user, -"normally used" refers to a typical or common use of that class of -product, regardless of the status of the particular user or of the way -in which the particular user actually uses, or expects or is expected -to use, the product. A product is a consumer product regardless of -whether the product has substantial commercial, industrial or -non-consumer uses, unless such uses represent the only significant -mode of use of the product. - -"Installation Information" for a User Product means any methods, -procedures, authorization keys, or other information required to -install and execute modified versions of a covered work in that User -Product from a modified version of its Corresponding Source. The -information must suffice to ensure that the continued functioning of -the modified object code is in no case prevented or interfered with -solely because modification has been made. - -If you convey an object code work under this section in, or with, or -specifically for use in, a User Product, and the conveying occurs as -part of a transaction in which the right of possession and use of the -User Product is transferred to the recipient in perpetuity or for a -fixed term (regardless of how the transaction is characterized), the -Corresponding Source conveyed under this section must be accompanied -by the Installation Information. But this requirement does not apply -if neither you nor any third party retains the ability to install -modified object code on the User Product (for example, the work has -been installed in ROM). - -The requirement to provide Installation Information does not include a -requirement to continue to provide support service, warranty, or -updates for a work that has been modified or installed by the -recipient, or for the User Product in which it has been modified or -installed. Access to a network may be denied when the modification -itself materially and adversely affects the operation of the network -or violates the rules and protocols for communication across the -network. - -Corresponding Source conveyed, and Installation Information provided, -in accord with this section must be in a format that is publicly -documented (and with an implementation available to the public in -source code form), and must require no special password or key for -unpacking, reading or copying. - -#### 7. Additional Terms. - -"Additional permissions" are terms that supplement the terms of this -License by making exceptions from one or more of its conditions. -Additional permissions that are applicable to the entire Program shall -be treated as though they were included in this License, to the extent -that they are valid under applicable law. If additional permissions -apply only to part of the Program, that part may be used separately -under those permissions, but the entire Program remains governed by -this License without regard to the additional permissions. - -When you convey a copy of a covered work, you may at your option -remove any additional permissions from that copy, or from any part of -it. (Additional permissions may be written to require their own -removal in certain cases when you modify the work.) You may place -additional permissions on material, added by you to a covered work, -for which you have or can give appropriate copyright permission. - -Notwithstanding any other provision of this License, for material you -add to a covered work, you may (if authorized by the copyright holders -of that material) supplement the terms of this License with terms: - -- a) Disclaiming warranty or limiting liability differently from the - terms of sections 15 and 16 of this License; or -- b) Requiring preservation of specified reasonable legal notices or - author attributions in that material or in the Appropriate Legal - Notices displayed by works containing it; or -- c) Prohibiting misrepresentation of the origin of that material, - or requiring that modified versions of such material be marked in - reasonable ways as different from the original version; or -- d) Limiting the use for publicity purposes of names of licensors - or authors of the material; or -- e) Declining to grant rights under trademark law for use of some - trade names, trademarks, or service marks; or -- f) Requiring indemnification of licensors and authors of that - material by anyone who conveys the material (or modified versions - of it) with contractual assumptions of liability to the recipient, - for any liability that these contractual assumptions directly - impose on those licensors and authors. - -All other non-permissive additional terms are considered "further -restrictions" within the meaning of section 10. If the Program as you -received it, or any part of it, contains a notice stating that it is -governed by this License along with a term that is a further -restriction, you may remove that term. If a license document contains -a further restriction but permits relicensing or conveying under this -License, you may add to a covered work material governed by the terms -of that license document, provided that the further restriction does -not survive such relicensing or conveying. - -If you add terms to a covered work in accord with this section, you -must place, in the relevant source files, a statement of the -additional terms that apply to those files, or a notice indicating -where to find the applicable terms. - -Additional terms, permissive or non-permissive, may be stated in the -form of a separately written license, or stated as exceptions; the -above requirements apply either way. - -#### 8. Termination. - -You may not propagate or modify a covered work except as expressly -provided under this License. Any attempt otherwise to propagate or -modify it is void, and will automatically terminate your rights under -this License (including any patent licenses granted under the third -paragraph of section 11). - -However, if you cease all violation of this License, then your license -from a particular copyright holder is reinstated (a) provisionally, -unless and until the copyright holder explicitly and finally -terminates your license, and (b) permanently, if the copyright holder -fails to notify you of the violation by some reasonable means prior to -60 days after the cessation. - -Moreover, your license from a particular copyright holder is -reinstated permanently if the copyright holder notifies you of the -violation by some reasonable means, this is the first time you have -received notice of violation of this License (for any work) from that -copyright holder, and you cure the violation prior to 30 days after -your receipt of the notice. - -Termination of your rights under this section does not terminate the -licenses of parties who have received copies or rights from you under -this License. If your rights have been terminated and not permanently -reinstated, you do not qualify to receive new licenses for the same -material under section 10. - -#### 9. Acceptance Not Required for Having Copies. - -You are not required to accept this License in order to receive or run -a copy of the Program. Ancillary propagation of a covered work -occurring solely as a consequence of using peer-to-peer transmission -to receive a copy likewise does not require acceptance. However, -nothing other than this License grants you permission to propagate or -modify any covered work. These actions infringe copyright if you do -not accept this License. Therefore, by modifying or propagating a -covered work, you indicate your acceptance of this License to do so. - -#### 10. Automatic Licensing of Downstream Recipients. - -Each time you convey a covered work, the recipient automatically -receives a license from the original licensors, to run, modify and -propagate that work, subject to this License. You are not responsible -for enforcing compliance by third parties with this License. - -An "entity transaction" is a transaction transferring control of an -organization, or substantially all assets of one, or subdividing an -organization, or merging organizations. If propagation of a covered -work results from an entity transaction, each party to that -transaction who receives a copy of the work also receives whatever -licenses to the work the party's predecessor in interest had or could -give under the previous paragraph, plus a right to possession of the -Corresponding Source of the work from the predecessor in interest, if -the predecessor has it or can get it with reasonable efforts. - -You may not impose any further restrictions on the exercise of the -rights granted or affirmed under this License. For example, you may -not impose a license fee, royalty, or other charge for exercise of -rights granted under this License, and you may not initiate litigation -(including a cross-claim or counterclaim in a lawsuit) alleging that -any patent claim is infringed by making, using, selling, offering for -sale, or importing the Program or any portion of it. - -#### 11. Patents. - -A "contributor" is a copyright holder who authorizes use under this -License of the Program or a work on which the Program is based. The -work thus licensed is called the contributor's "contributor version". - -A contributor's "essential patent claims" are all patent claims owned -or controlled by the contributor, whether already acquired or -hereafter acquired, that would be infringed by some manner, permitted -by this License, of making, using, or selling its contributor version, -but do not include claims that would be infringed only as a -consequence of further modification of the contributor version. For -purposes of this definition, "control" includes the right to grant -patent sublicenses in a manner consistent with the requirements of -this License. - -Each contributor grants you a non-exclusive, worldwide, royalty-free -patent license under the contributor's essential patent claims, to -make, use, sell, offer for sale, import and otherwise run, modify and -propagate the contents of its contributor version. - -In the following three paragraphs, a "patent license" is any express -agreement or commitment, however denominated, not to enforce a patent -(such as an express permission to practice a patent or covenant not to -sue for patent infringement). To "grant" such a patent license to a -party means to make such an agreement or commitment not to enforce a -patent against the party. - -If you convey a covered work, knowingly relying on a patent license, -and the Corresponding Source of the work is not available for anyone -to copy, free of charge and under the terms of this License, through a -publicly available network server or other readily accessible means, -then you must either (1) cause the Corresponding Source to be so -available, or (2) arrange to deprive yourself of the benefit of the -patent license for this particular work, or (3) arrange, in a manner -consistent with the requirements of this License, to extend the patent -license to downstream recipients. "Knowingly relying" means you have -actual knowledge that, but for the patent license, your conveying the -covered work in a country, or your recipient's use of the covered work -in a country, would infringe one or more identifiable patents in that -country that you have reason to believe are valid. - -If, pursuant to or in connection with a single transaction or -arrangement, you convey, or propagate by procuring conveyance of, a -covered work, and grant a patent license to some of the parties -receiving the covered work authorizing them to use, propagate, modify -or convey a specific copy of the covered work, then the patent license -you grant is automatically extended to all recipients of the covered -work and works based on it. - -A patent license is "discriminatory" if it does not include within the -scope of its coverage, prohibits the exercise of, or is conditioned on -the non-exercise of one or more of the rights that are specifically -granted under this License. You may not convey a covered work if you -are a party to an arrangement with a third party that is in the -business of distributing software, under which you make payment to the -third party based on the extent of your activity of conveying the -work, and under which the third party grants, to any of the parties -who would receive the covered work from you, a discriminatory patent -license (a) in connection with copies of the covered work conveyed by -you (or copies made from those copies), or (b) primarily for and in -connection with specific products or compilations that contain the -covered work, unless you entered into that arrangement, or that patent -license was granted, prior to 28 March 2007. - -Nothing in this License shall be construed as excluding or limiting -any implied license or other defenses to infringement that may -otherwise be available to you under applicable patent law. - -#### 12. No Surrender of Others' Freedom. - -If conditions are imposed on you (whether by court order, agreement or -otherwise) that contradict the conditions of this License, they do not -excuse you from the conditions of this License. If you cannot convey a -covered work so as to satisfy simultaneously your obligations under -this License and any other pertinent obligations, then as a -consequence you may not convey it at all. For example, if you agree to -terms that obligate you to collect a royalty for further conveying -from those to whom you convey the Program, the only way you could -satisfy both those terms and this License would be to refrain entirely -from conveying the Program. - -#### 13. Remote Network Interaction; Use with the GNU General Public License. - -Notwithstanding any other provision of this License, if you modify the -Program, your modified version must prominently offer all users -interacting with it remotely through a computer network (if your -version supports such interaction) an opportunity to receive the -Corresponding Source of your version by providing access to the -Corresponding Source from a network server at no charge, through some -standard or customary means of facilitating copying of software. This -Corresponding Source shall include the Corresponding Source for any -work covered by version 3 of the GNU General Public License that is -incorporated pursuant to the following paragraph. - -Notwithstanding any other provision of this License, you have -permission to link or combine any covered work with a work licensed -under version 3 of the GNU General Public License into a single -combined work, and to convey the resulting work. The terms of this -License will continue to apply to the part which is the covered work, -but the work with which it is combined will remain governed by version -3 of the GNU General Public License. - -#### 14. Revised Versions of this License. - -The Free Software Foundation may publish revised and/or new versions -of the GNU Affero General Public License from time to time. Such new -versions will be similar in spirit to the present version, but may -differ in detail to address new problems or concerns. - -Each version is given a distinguishing version number. If the Program -specifies that a certain numbered version of the GNU Affero General -Public License "or any later version" applies to it, you have the -option of following the terms and conditions either of that numbered -version or of any later version published by the Free Software -Foundation. If the Program does not specify a version number of the -GNU Affero General Public License, you may choose any version ever -published by the Free Software Foundation. - -If the Program specifies that a proxy can decide which future versions -of the GNU Affero General Public License can be used, that proxy's -public statement of acceptance of a version permanently authorizes you -to choose that version for the Program. - -Later license versions may give you additional or different -permissions. However, no additional obligations are imposed on any -author or copyright holder as a result of your choosing to follow a -later version. - -#### 15. Disclaimer of Warranty. - -THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY -APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT -HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT -WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT -LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND -PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE -DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR -CORRECTION. - -#### 16. Limitation of Liability. - -IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING -WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR -CONVEYS THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, -INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES -ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT -NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR -LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM -TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER -PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. - -#### 17. Interpretation of Sections 15 and 16. - -If the disclaimer of warranty and limitation of liability provided -above cannot be given local legal effect according to their terms, -reviewing courts shall apply local law that most closely approximates -an absolute waiver of all civil liability in connection with the -Program, unless a warranty or assumption of liability accompanies a -copy of the Program in return for a fee. - -END OF TERMS AND CONDITIONS - -### How to Apply These Terms to Your New Programs - -If you develop a new program, and you want it to be of the greatest -possible use to the public, the best way to achieve this is to make it -free software which everyone can redistribute and change under these -terms. - -To do so, attach the following notices to the program. It is safest to -attach them to the start of each source file to most effectively state -the exclusion of warranty; and each file should have at least the -"copyright" line and a pointer to where the full notice is found. - - - Copyright (C) - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Affero General Public License as - published by the Free Software Foundation, either version 3 of the - License, or (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program. If not, see . - -Also add information on how to contact you by electronic and paper -mail. - -If your software can interact with users remotely through a computer -network, you should also make sure that it provides a way for users to -get its source. For example, if your program is a web application, its -interface could display a "Source" link that leads users to an archive -of the code. There are many ways you could offer source, and different -solutions will be better for different programs; see section 13 for -the specific requirements. - -You should also get your employer (if you work as a programmer) or -school, if any, to sign a "copyright disclaimer" for the program, if -necessary. For more information on this, and how to apply and follow -the GNU AGPL, see . \ No newline at end of file diff --git a/loki-server/Cargo.toml b/loki-server/Cargo.toml index be40a56..aefc871 100644 --- a/loki-server/Cargo.toml +++ b/loki-server/Cargo.toml @@ -8,3 +8,4 @@ edition = "2021" [dependencies] loki-shared = { path = "../loki-shared" } protolok = { path = "../protolok" } +tokio = { version = "1", features = ["full"] } diff --git a/loki-server/src/dm.rs b/loki-server/src/dm.rs deleted file mode 100644 index e5f2dfe..0000000 --- a/loki-server/src/dm.rs +++ /dev/null @@ -1,5 +0,0 @@ -use loki_shared::Message; - -pub struct DmChannel { - mailbox: Vec -} diff --git a/loki-server/src/main.rs b/loki-server/src/main.rs index 2b09740..077d060 100644 --- a/loki-server/src/main.rs +++ b/loki-server/src/main.rs @@ -1,5 +1,7 @@ -mod dm; +use tokio::io::{AsyncRead, AsyncWrite}; +use tokio::net::TcpListener; -fn main() { +#[tokio::main] +async fn main() { println!("Hello, world!"); } diff --git a/protolok/Cargo.toml b/protolok/Cargo.toml index 51e1945..95d92c5 100644 --- a/protolok/Cargo.toml +++ b/protolok/Cargo.toml @@ -9,5 +9,5 @@ edition = "2021" rand = "0.8.5" sha2 = "0.10" chacha20poly1305 = "0.10" -ed25519-dalek = "2.1" +ed25519-dalek = { version="2.1", features=["rand_core"] } x25519-dalek = { version="2.0", features=["reusable_secrets"] } From d06ad473ad3df2cac25454c004aa13cde76e772d Mon Sep 17 00:00:00 2001 From: somehybrid Date: Thu, 1 Feb 2024 07:43:27 +0700 Subject: [PATCH 7/8] Snowflake refactoring --- protolok/src/id.rs | 44 ++++++++++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/protolok/src/id.rs b/protolok/src/id.rs index 9dbb14b..078c29b 100644 --- a/protolok/src/id.rs +++ b/protolok/src/id.rs @@ -1,4 +1,7 @@ -use std::time::{Duration, SystemTime, UNIX_EPOCH}; +use std::{ + sync::Mutex, + time::{Duration, SystemTime, UNIX_EPOCH}, +}; const EPOCH: Duration = Duration::from_millis(1672531200000); @@ -6,38 +9,36 @@ const fn mask(amount: u64) -> u64 { (1 << amount) - 1 } -pub struct Snowflake { +pub struct SnowflakeGenerator { last_evoked: u64, - count: u16, - worker_id: u16, + count: Mutex, } -impl Snowflake { - pub fn new(worker_id: u16) -> Snowflake { - Snowflake { +impl SnowflakeGenerator { + pub fn new() -> SnowflakeGenerator { + SnowflakeGenerator { last_evoked: 0, - count: 0, - worker_id, + count: Mutex::new(0), } } - pub fn generate(&mut self) -> u64 { + pub fn generate(&mut self, worker_id: u16) -> u64 { let timestamp = SystemTime::now() .duration_since(UNIX_EPOCH + EPOCH) .expect("Time went backwards") .as_millis() as u64 & mask(42); + let mut count = self.count.lock().expect("Poisoned mutex"); + if self.last_evoked > timestamp { - self.count = 0; + *count = 0; self.last_evoked = timestamp; } - let id = timestamp << 22 - | (self.worker_id as u64 & mask(10)) << 12 - | (self.count as u64 & mask(12)); + let id = timestamp << 22 | (worker_id as u64 & mask(10)) << 12 | (*count as u64 & mask(12)); - self.count += 1; + *count += 1; id } @@ -55,12 +56,19 @@ pub trait Object { #[cfg(test)] mod tests { - use super::Snowflake; + use super::SnowflakeGenerator; #[test] fn test_snowflake_count() { - let mut snowflake = Snowflake::new(0); + let mut snowflake = SnowflakeGenerator::new(); - assert_ne!(snowflake.generate(), snowflake.generate()); + assert_ne!(snowflake.generate(0), snowflake.generate(0)); } + + #[test] + fn test_snowflake_worker() { + let mut snowflake = SnowflakeGenerator::new(); + + assert_ne!(snowflake.generate(1), snowflake.generate(0)); + } } From 2337da1ff196b142fe279922c1c4d968671d0c4f Mon Sep 17 00:00:00 2001 From: somehybrid Date: Sun, 4 Feb 2024 10:55:04 +0700 Subject: [PATCH 8/8] More protolok --- protolok/src/instance.rs | 6 ++++++ protolok/src/lib.rs | 2 ++ 2 files changed, 8 insertions(+) create mode 100644 protolok/src/instance.rs diff --git a/protolok/src/instance.rs b/protolok/src/instance.rs new file mode 100644 index 0000000..9fa4076 --- /dev/null +++ b/protolok/src/instance.rs @@ -0,0 +1,6 @@ +use crate::Object; + +pub trait Instance { + /// Gets an object from its Snowflake ID + fn from_object(&self, id: u64) -> impl Object; +} diff --git a/protolok/src/lib.rs b/protolok/src/lib.rs index f0240f3..3cddd87 100644 --- a/protolok/src/lib.rs +++ b/protolok/src/lib.rs @@ -1,4 +1,5 @@ pub mod channel; +pub mod instance; pub mod place; pub mod user; pub mod message; @@ -9,3 +10,4 @@ pub use place::*; pub use user::*; pub use message::*; pub use id::*; +pub use instance::*;