From 80ddf80f17c57d2f29d13788e8b5424d4b5598a8 Mon Sep 17 00:00:00 2001 From: timokoesters Date: Sun, 19 Apr 2020 14:14:47 +0200 Subject: [PATCH 01/12] work --- Cargo.lock | 366 +++++++++++++++++++++++++++++++++++++------ Cargo.toml | 19 +-- src/client_server.rs | 192 +++++++++++++++-------- src/data.rs | 62 +++++++- src/database.rs | 12 +- src/main.rs | 8 +- src/pdu.rs | 13 +- src/server_server.rs | 25 +++ src/stateres.rs | 59 +++++++ src/utils.rs | 17 +- 10 files changed, 632 insertions(+), 141 deletions(-) create mode 100644 src/server_server.rs create mode 100644 src/stateres.rs diff --git a/Cargo.lock b/Cargo.lock index d6066556..9d7eaf21 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -29,9 +29,9 @@ checksum = "cff77d8686867eceff3105329d4698d96c2391c176d5d03adc90c7389162b5b8" [[package]] name = "async-trait" -version = "0.1.29" +version = "0.1.30" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bab5c215748dc1ad11a145359b1067107ae0f8ca5e99844fa64067ed5bf198e3" +checksum = "da71fef07bc806586090247e971229289f64c210a278ee5ae419314eb386b31d" dependencies = [ "proc-macro2 1.0.10", "quote 1.0.3", @@ -148,6 +148,7 @@ dependencies = [ "log", "pretty_env_logger", "rand", + "reqwest", "rocket", "ruma-api", "ruma-client-api", @@ -179,6 +180,22 @@ dependencies = [ "time", ] +[[package]] +name = "core-foundation" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57d24c7a13c43e870e37c1556b74555437870a04514f7685f5b354e090567171" +dependencies = [ + "core-foundation-sys", + "libc", +] + +[[package]] +name = "core-foundation-sys" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b3a71ab494c0b5b860bdc8407ae08978052417070c2ced38573a9157ad75b8ac" + [[package]] name = "crc32fast" version = "1.2.0" @@ -271,6 +288,15 @@ version = "0.4.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4358a9e11b9a09cf52383b451b49a169e8d797b68aa02301ff586d70d9661ea3" +[[package]] +name = "encoding_rs" +version = "0.8.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd8d03faa7fe0c1431609dfad7bbe827af30f82e1e2ae6f7ee4fca6bd764bc28" +dependencies = [ + "cfg-if", +] + [[package]] name = "env_logger" version = "0.7.1" @@ -290,6 +316,21 @@ version = "1.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2fad85553e09a6f881f739c29f0b00b0f01357c743266d478b68951ce23285f3" +[[package]] +name = "foreign-types" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6f339eb8adc052cd2ca78910fda869aefa38d22d5cb648e6485e4d3fc06f3b1" +dependencies = [ + "foreign-types-shared", +] + +[[package]] +name = "foreign-types-shared" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b" + [[package]] name = "fs2" version = "0.4.3" @@ -457,9 +498,9 @@ dependencies = [ [[package]] name = "hermit-abi" -version = "0.1.10" +version = "0.1.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "725cf19794cf90aa94e65050cb4191ff5d8fa87a498383774c47b332e3af952e" +checksum = "8a0d737e0f947a1864e93d33fdef4af8445a00d1ed8dc0c8ddb73139ea6abf15" dependencies = [ "libc", ] @@ -502,9 +543,9 @@ dependencies = [ [[package]] name = "hyper" -version = "0.13.4" +version = "0.13.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ed6081100e960d9d74734659ffc9cc91daf1c0fc7aceb8eaa94ee1a3f5046f2e" +checksum = "96816e1d921eca64d208a85aab4f7798455a8e34229ee5a88c935bdee1b78b14" dependencies = [ "bytes", "futures-channel", @@ -516,6 +557,7 @@ dependencies = [ "httparse", "itoa", "log", + "net2", "pin-project", "time", "tokio", @@ -523,6 +565,19 @@ dependencies = [ "want", ] +[[package]] +name = "hyper-tls" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3adcd308402b9553630734e9c36b77a7e48b3821251ca2493e8cd596763aafaa" +dependencies = [ + "bytes", + "hyper", + "native-tls", + "tokio", + "tokio-tls", +] + [[package]] name = "idna" version = "0.2.0" @@ -594,15 +649,15 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "libc" -version = "0.2.68" +version = "0.2.69" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dea0c0405123bba743ee3f91f49b1c7cfb684eef0da0a50110f758ccf24cdff0" +checksum = "99e85c08494b21a9054e7fe1374a732aeadaff3980b6990b94bfd3a70f690005" [[package]] name = "lock_api" -version = "0.3.3" +version = "0.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "79b2de95ecb4691949fea4716ca53cdbcfccb2c612e19644a8bad05edcf9f47b" +checksum = "c4da24a77a3d8a6d4862d95f72e6fdb9c09a643ecdb402d754004a557f2bec75" dependencies = [ "scopeguard", ] @@ -649,6 +704,16 @@ version = "0.3.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2a60c7ce501c71e03a9c9c0d35b861413ae925bd979cc7a4e30d060069aaac8d" +[[package]] +name = "mime_guess" +version = "2.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2684d4c2e97d99848d30b324b00c8fcc7e5c897b7cbb5819b09e7c90e8baf212" +dependencies = [ + "mime", + "unicase", +] + [[package]] name = "mio" version = "0.6.21" @@ -691,6 +756,24 @@ dependencies = [ "ws2_32-sys", ] +[[package]] +name = "native-tls" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b0d88c06fe90d5ee94048ba40409ef1d9315d86f6f38c2efdaad4fb50c58b2d" +dependencies = [ + "lazy_static", + "libc", + "log", + "openssl", + "openssl-probe", + "openssl-sys", + "schannel", + "security-framework", + "security-framework-sys", + "tempfile", +] + [[package]] name = "net2" version = "0.2.33" @@ -704,19 +787,52 @@ dependencies = [ [[package]] name = "num_cpus" -version = "1.12.0" +version = "1.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "46203554f085ff89c235cd12f7075f3233af9b11ed7c9e16dfe2560d03313ce6" +checksum = "05499f3756671c15885fee9034446956fff3f243d6077b91e5767df161f766b3" dependencies = [ "hermit-abi", "libc", ] [[package]] -name = "parking_lot" -version = "0.10.0" +name = "openssl" +version = "0.10.29" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "92e98c49ab0b7ce5b222f2cc9193fc4efe11c6d0bd4f648e374684a6857b1cfc" +checksum = "cee6d85f4cb4c4f59a6a85d5b68a233d280c82e29e822913b9c8b129fbf20bdd" +dependencies = [ + "bitflags", + "cfg-if", + "foreign-types", + "lazy_static", + "libc", + "openssl-sys", +] + +[[package]] +name = "openssl-probe" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77af24da69f9d9341038eba93a073b1fdaaa1b788221b00a69bce9e762cb32de" + +[[package]] +name = "openssl-sys" +version = "0.9.55" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7717097d810a0f2e2323f9e5d11e71608355e24828410b55b9d4f18aa5f9a5d8" +dependencies = [ + "autocfg", + "cc", + "libc", + "pkg-config", + "vcpkg", +] + +[[package]] +name = "parking_lot" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3a704eb390aafdc107b0e392f56a82b668e3a71366993b5340f5833fd62505e" dependencies = [ "lock_api", "parking_lot_core", @@ -724,9 +840,9 @@ dependencies = [ [[package]] name = "parking_lot_core" -version = "0.7.0" +version = "0.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7582838484df45743c8434fbff785e8edf260c28748353d44bc0da32e0ceabf1" +checksum = "0e136c1904604defe99ce5fd71a28d473fa60a12255d511aa78a9ddf11237aeb" dependencies = [ "cfg-if", "cloudabi", @@ -772,18 +888,18 @@ checksum = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e" [[package]] name = "pin-project" -version = "0.4.8" +version = "0.4.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7804a463a8d9572f13453c516a5faea534a2403d7ced2f0c7e100eeff072772c" +checksum = "6f6a7f5eee6292c559c793430c55c00aea9d3b3d1905e855806ca4d7253426a2" dependencies = [ "pin-project-internal", ] [[package]] name = "pin-project-internal" -version = "0.4.8" +version = "0.4.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "385322a45f2ecf3410c68d2a549a4a2685e8051d0f278e39743ff4e451cb9b3f" +checksum = "8988430ce790d8682672117bc06dda364c0be32d3abd738234f19f3240bad99a" dependencies = [ "proc-macro2 1.0.10", "quote 1.0.3", @@ -802,6 +918,12 @@ version = "0.1.0-alpha.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5894c618ce612a3fa23881b152b608bafb8c56cfc22f434a3ba3120b40f7b587" +[[package]] +name = "pkg-config" +version = "0.3.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05da548ad6865900e60eaba7f589cc0783590a92e940c26953ff81ddbab2d677" + [[package]] name = "ppv-lite86" version = "0.2.6" @@ -932,9 +1054,9 @@ dependencies = [ [[package]] name = "regex" -version = "1.3.6" +version = "1.3.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7f6946991529684867e47d86474e3a6d0c0ab9b82d5821e314b1ede31fa3a4b3" +checksum = "a6020f034922e3194c711b82a627453881bc4682166cabb07134a10c26ba7692" dependencies = [ "aho-corasick", "memchr", @@ -948,6 +1070,50 @@ version = "0.6.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7fe5bd57d1d7414c6b5ed48563a2c855d995ff777729dcd91c369ec7fea395ae" +[[package]] +name = "remove_dir_all" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4a83fa3702a688b9359eccba92d153ac33fd2e8462f9e0e3fdf155239ea7792e" +dependencies = [ + "winapi 0.3.8", +] + +[[package]] +name = "reqwest" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "02b81e49ddec5109a9dcfc5f2a317ff53377c915e9ae9d4f2fb50914b85614e2" +dependencies = [ + "base64 0.11.0", + "bytes", + "encoding_rs", + "futures-core", + "futures-util", + "http", + "http-body", + "hyper", + "hyper-tls", + "js-sys", + "lazy_static", + "log", + "mime", + "mime_guess", + "native-tls", + "percent-encoding 2.1.0", + "pin-project-lite", + "serde", + "serde_urlencoded", + "time", + "tokio", + "tokio-tls", + "url", + "wasm-bindgen", + "wasm-bindgen-futures", + "web-sys", + "winreg", +] + [[package]] name = "ring" version = "0.16.12" @@ -1024,9 +1190,9 @@ dependencies = [ [[package]] name = "ruma-api" -version = "0.15.0" +version = "0.16.0-rc.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "120f0cd8625b842423ef3a63cabb8c309ca35a02de87cc4b377fb2cdd43f1fe5" +checksum = "7769e934360383f91d68a2b9132610d02436ef3272cbdd46de239c9025198a36" dependencies = [ "http", "percent-encoding 2.1.0", @@ -1036,14 +1202,13 @@ dependencies = [ "serde_json", "serde_urlencoded", "strum", - "url", ] [[package]] name = "ruma-api-macros" -version = "0.12.0" +version = "0.16.0-rc.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bfc523efc9c1ba7033ff17888551c1d378e12eae087cfbe4fcee938ff516759e" +checksum = "0f8dad3311d0bee6d43da684ba03f9a84cddd39f9b3e7e88cab5d726419ec22e" dependencies = [ "proc-macro2 1.0.10", "quote 1.0.3", @@ -1052,14 +1217,15 @@ dependencies = [ [[package]] name = "ruma-client-api" -version = "0.7.2" -source = "git+https://github.com/ruma/ruma-client-api.git?branch=uiaa-error-type#a7136c06285864dadcc0b0c6371d181002727c55" +version = "0.8.0-rc.2" +source = "git+https://github.com/ruma/ruma-client-api.git#1b7863dc36e6e043ae365791cc719dc190abd660" dependencies = [ "http", "js_int", "ruma-api", "ruma-events", "ruma-identifiers", + "ruma-serde", "serde", "serde_json", "strum", @@ -1068,22 +1234,23 @@ dependencies = [ [[package]] name = "ruma-events" -version = "0.18.0" +version = "0.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "80e34bfc20462f18d7f0beb6f1863db62d29438f2dcf390b625e9b20696cb2b3" +checksum = "844b5d38397c945395c7a1eaf89d55714c3d22983b870085a1a67d51fb6611cf" dependencies = [ "js_int", "ruma-events-macros", "ruma-identifiers", + "ruma-serde", "serde", "serde_json", ] [[package]] name = "ruma-events-macros" -version = "0.3.0" +version = "0.19.0-final" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ff95b6b4480c570db471b490b35ad70add5470651654e75faf0b97052b4f29e1" +checksum = "5477046b734fde45dd7913dbc8d7b260af3b1c31ea2bc329bd2f0b44e37368be" dependencies = [ "proc-macro2 1.0.10", "quote 1.0.3", @@ -1093,10 +1260,10 @@ dependencies = [ [[package]] name = "ruma-federation-api" version = "0.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e2a73a23c4d9243be91e101e1942f4d9cd913ef5156d756bafdfe2409ee23d72" +source = "git+https://github.com/ruma/ruma-federation-api.git#5448c650f0a583382152d0f43f2dcf720d495390" dependencies = [ "js_int", + "ruma-api", "ruma-events", "ruma-identifiers", "serde", @@ -1105,19 +1272,28 @@ dependencies = [ [[package]] name = "ruma-identifiers" -version = "0.14.1" +version = "0.15.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "07e442c700a3b33fc4dd4a1c4b463ebdd252d2c2db31b83da6bb3009307039b9" +checksum = "63db5545f38077ea141fb112df070773e6ab9b7025174d732f56c6b37525ccc0" dependencies = [ - "rand", "serde", - "url", +] + +[[package]] +name = "ruma-serde" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09901d608958f63618546957134dd4242d2ca07a885a28f794ad4574a937c22c" +dependencies = [ + "js_int", + "serde", + "serde_json", ] [[package]] name = "ruma-signatures" version = "0.6.0-dev.1" -source = "git+https://github.com/ruma/ruma-signatures.git#9947e94cb28daea456904197f7cd754a8e48797a" +source = "git+https://github.com/ruma/ruma-signatures.git#c3f8399c268695464730afd6077c7ce50155b8d5" dependencies = [ "base64 0.12.0", "ring", @@ -1168,6 +1344,16 @@ version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "535622e6be132bccd223f4bb2b8ac8d53cda3c7a6394944d3b2b33fb974f9d76" +[[package]] +name = "schannel" +version = "0.1.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "039c25b130bd8c1321ee2d7de7fde2659fa9c2744e4bb29711cfc852ea53cd19" +dependencies = [ + "lazy_static", + "winapi 0.3.8", +] + [[package]] name = "scopeguard" version = "1.1.0" @@ -1184,6 +1370,29 @@ dependencies = [ "untrusted", ] +[[package]] +name = "security-framework" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "572dfa3a0785509e7a44b5b4bebcf94d41ba34e9ed9eb9df722545c3b3c4144a" +dependencies = [ + "bitflags", + "core-foundation", + "core-foundation-sys", + "libc", + "security-framework-sys", +] + +[[package]] +name = "security-framework-sys" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ddb15a5fec93b7021b8a9e96009c5d8d51c15673569f7c0f6b7204e5b7b404f" +dependencies = [ + "core-foundation-sys", + "libc", +] + [[package]] name = "serde" version = "1.0.106" @@ -1261,9 +1470,9 @@ dependencies = [ [[package]] name = "smallvec" -version = "1.2.0" +version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5c2fb2ec9bcd216a5b0d0ccf31ab17b5ed1d627960edff65bbe95d3ce221cefc" +checksum = "05720e22615919e4734f6a99ceae50d00226c3c5aca406e102ebc33298214e0a" [[package]] name = "spin" @@ -1320,6 +1529,20 @@ dependencies = [ "unicode-xid 0.2.0", ] +[[package]] +name = "tempfile" +version = "3.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a6e24d9338a0a5be79593e2fa15a648add6138caa803e2d5bc782c371732ca9" +dependencies = [ + "cfg-if", + "libc", + "rand", + "redox_syscall", + "remove_dir_all", + "winapi 0.3.8", +] + [[package]] name = "termcolor" version = "1.1.0" @@ -1340,20 +1563,19 @@ dependencies = [ [[package]] name = "time" -version = "0.1.42" +version = "0.1.43" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "db8dcfca086c1143c9270ac42a2bbd8a7ee477b78ac8e45b19abfb0cbede4b6f" +checksum = "ca8a50ef2360fbd1eeb0ecd46795a87a19024eb4b53c5dc916ca1fd95fe62438" dependencies = [ "libc", - "redox_syscall", "winapi 0.3.8", ] [[package]] name = "tokio" -version = "0.2.16" +version = "0.2.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ee5a0dd887e37d37390c13ff8ac830f992307fe30a1fff0ab8427af67211ba28" +checksum = "34ef16d072d2b6dc8b4a56c70f5c5ced1a37752116f8e7c1e80c659aa7cb6713" dependencies = [ "bytes", "fnv", @@ -1395,6 +1617,16 @@ dependencies = [ "webpki", ] +[[package]] +name = "tokio-tls" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7bde02a3a5291395f59b06ec6945a3077602fac2b07eeeaf0dee2122f3619828" +dependencies = [ + "native-tls", + "tokio", +] + [[package]] name = "tokio-util" version = "0.3.1" @@ -1430,6 +1662,15 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e604eb7b43c06650e854be16a2a03155743d3752dd1c943f6829e26b7a36e382" +[[package]] +name = "unicase" +version = "2.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50f37be617794602aabbeee0be4f259dc1778fabe05e2d67ee8f79326d5cb4f6" +dependencies = [ + "version_check 0.9.1", +] + [[package]] name = "unicode-bidi" version = "0.3.4" @@ -1484,6 +1725,12 @@ dependencies = [ "serde", ] +[[package]] +name = "vcpkg" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3fc439f2794e98976c88a2a2dafce96b930fe8010b0a256b3c2199a773933168" + [[package]] name = "version_check" version = "0.1.5" @@ -1519,6 +1766,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2cc57ce05287f8376e998cbddfb4c8cb43b84a7ec55cf4551d7c00eef317a47f" dependencies = [ "cfg-if", + "serde", + "serde_json", "wasm-bindgen-macro", ] @@ -1537,6 +1786,18 @@ dependencies = [ "wasm-bindgen-shared", ] +[[package]] +name = "wasm-bindgen-futures" +version = "0.4.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7add542ea1ac7fdaa9dc25e031a6af33b7d63376292bd24140c637d00d1c312a" +dependencies = [ + "cfg-if", + "js-sys", + "wasm-bindgen", + "web-sys", +] + [[package]] name = "wasm-bindgen-macro" version = "0.2.60" @@ -1629,6 +1890,15 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +[[package]] +name = "winreg" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b2986deb581c4fe11b621998a5e53361efe6b48a151178d0cd9eeffa4dc6acc9" +dependencies = [ + "winapi 0.3.8", +] + [[package]] name = "ws2_32-sys" version = "0.2.1" diff --git a/Cargo.toml b/Cargo.toml index 3b8da6c6..5cf5412f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,19 +13,20 @@ edition = "2018" [dependencies] rocket = { git = "https://github.com/SergioBenitez/Rocket.git", branch = "async", features = ["tls"] } http = "0.2.1" -ruma-client-api = { git = "https://github.com/ruma/ruma-client-api.git", branch = "uiaa-error-type" } +ruma-client-api = { git = "https://github.com/ruma/ruma-client-api.git" } +ruma-identifiers = "0.15.1" +ruma-api = "0.16.0-rc.1" +ruma-events = "0.19.0" +ruma-signatures = { git = "https://github.com/ruma/ruma-signatures.git" } +ruma-federation-api = { git = "https://github.com/ruma/ruma-federation-api.git" } pretty_env_logger = "0.4.0" log = "0.4.8" sled = "0.31.0" directories = "2.0.2" -ruma-identifiers = "0.14.1" -ruma-api = "0.15.0" -ruma-events = "0.18.0" js_int = "0.1.4" -serde_json = "1.0.50" -ruma-signatures = { git = "https://github.com/ruma/ruma-signatures.git" } -ruma-federation-api = "0.0.1" +serde_json = "1.0.51" serde = "1.0.106" -tokio = { version = "0.2.16", features = ["macros"] } #rt-threaded +tokio = { version = "0.2.18", features = ["macros"] } rand = "0.7.3" -rust-argon2 = "0.8.2" \ No newline at end of file +rust-argon2 = "0.8.2" +reqwest = "0.10.4" diff --git a/src/client_server.rs b/src/client_server.rs index 2548d257..a0191423 100644 --- a/src/client_server.rs +++ b/src/client_server.rs @@ -5,16 +5,16 @@ use rocket::{get, options, post, put, State}; use ruma_client_api::{ error::{Error, ErrorKind}, r0::{ - account::{ - register, AuthenticationFlow, UserInteractiveAuthenticationInfo, - UserInteractiveAuthenticationResponse, - }, + account::register, alias::get_alias, + capabilities::get_capabilities, config::{get_global_account_data, set_global_account_data}, directory::{self, get_public_rooms_filtered}, filter::{self, create_filter, get_filter}, keys::{get_keys, upload_keys}, - membership::{invite_user, join_room_by_id, join_room_by_id_or_alias}, + membership::{ + get_member_events, invite_user, join_room_by_id, join_room_by_id_or_alias, leave_room, + }, message::create_message_event, presence::set_presence, profile::{ @@ -28,15 +28,20 @@ use ruma_client_api::{ sync::sync_events, thirdparty::get_protocols, typing::create_typing_event, + uiaa::{AuthFlow, UiaaInfo, UiaaResponse}, user_directory::search_users, }, unversioned::get_supported_versions, }; use ruma_events::{collections::only::Event as EduEvent, EventType}; -use ruma_identifiers::{RoomId, RoomIdOrAliasId, UserId}; +use ruma_identifiers::{RoomId, UserId}; use serde_json::json; -use std::{collections::HashMap, convert::TryInto, path::PathBuf, time::Duration}; -use argon2::{Config, Variant}; +use std::{ + collections::{BTreeMap, HashMap}, + convert::{TryFrom, TryInto}, + path::PathBuf, + time::{Duration, SystemTime}, +}; const GUEST_NAME_LENGTH: usize = 10; const DEVICE_ID_LENGTH: usize = 10; @@ -46,8 +51,8 @@ const TOKEN_LENGTH: usize = 256; #[get("/_matrix/client/versions")] pub fn get_supported_versions_route() -> MatrixResult { MatrixResult(Ok(get_supported_versions::Response { - versions: vec!["r0.6.0".to_owned()], - unstable_features: HashMap::new(), + versions: vec!["r0.5.0".to_owned(), "r0.6.0".to_owned()], + unstable_features: BTreeMap::new(), })) } @@ -55,18 +60,17 @@ pub fn get_supported_versions_route() -> MatrixResult, body: Ruma, -) -> MatrixResult { +) -> MatrixResult { if body.auth.is_none() { - return MatrixResult(Err(UserInteractiveAuthenticationResponse::AuthResponse( - UserInteractiveAuthenticationInfo { - flows: vec![AuthenticationFlow { - stages: vec!["m.login.dummy".to_owned()], - }], - completed: vec![], - params: json!({}), - session: Some(utils::random_string(SESSION_ID_LENGTH)), - }, - ))); + return MatrixResult(Err(UiaaResponse::AuthResponse(UiaaInfo { + flows: vec![AuthFlow { + stages: vec!["m.login.dummy".to_owned()], + }], + completed: vec![], + params: json!({}), + session: Some(utils::random_string(SESSION_ID_LENGTH)), + auth_error: None, + }))); } // Validate user id @@ -81,13 +85,11 @@ pub fn register_route( { Err(_) => { debug!("Username invalid"); - return MatrixResult(Err(UserInteractiveAuthenticationResponse::MatrixError( - Error { - kind: ErrorKind::InvalidUsername, - message: "Username was invalid.".to_owned(), - status_code: http::StatusCode::BAD_REQUEST, - }, - ))); + return MatrixResult(Err(UiaaResponse::MatrixError(Error { + kind: ErrorKind::InvalidUsername, + message: "Username was invalid.".to_owned(), + status_code: http::StatusCode::BAD_REQUEST, + }))); } Ok(user_id) => user_id, }; @@ -95,13 +97,11 @@ pub fn register_route( // Check if username is creative enough if data.user_exists(&user_id) { debug!("ID already taken"); - return MatrixResult(Err(UserInteractiveAuthenticationResponse::MatrixError( - Error { - kind: ErrorKind::UserInUse, - message: "Desired user ID is already taken.".to_owned(), - status_code: http::StatusCode::BAD_REQUEST, - }, - ))); + return MatrixResult(Err(UiaaResponse::MatrixError(Error { + kind: ErrorKind::UserInUse, + message: "Desired user ID is already taken.".to_owned(), + status_code: http::StatusCode::BAD_REQUEST, + }))); } let password = body.password.clone().unwrap_or_default(); @@ -110,13 +110,11 @@ pub fn register_route( // Create user data.user_add(&user_id, &hash); } else { - return MatrixResult(Err(UserInteractiveAuthenticationResponse::MatrixError( - Error { - kind: ErrorKind::InvalidParam, - message: "Password did not meet requirements".to_owned(), - status_code: http::StatusCode::BAD_REQUEST, - }, - ))); + return MatrixResult(Err(UiaaResponse::MatrixError(Error { + kind: ErrorKind::InvalidParam, + message: "Password did not met requirements".to_owned(), + status_code: http::StatusCode::BAD_REQUEST, + }))); } // Generate new device id if the user didn't specify one @@ -158,8 +156,8 @@ pub fn login_route(data: State, body: Ruma) -> MatrixResul } if let Ok(user_id) = (*username).try_into() { if let Some(hash) = data.password_hash_get(&user_id) { - let hash_matches = argon2::verify_encoded(&hash, password.as_bytes()) - .unwrap_or(false); + let hash_matches = + argon2::verify_encoded(&hash, password.as_bytes()).unwrap_or(false); if hash_matches { // Success! @@ -219,11 +217,25 @@ pub fn login_route(data: State, body: Ruma) -> MatrixResul })) } +#[get("/_matrix/client/r0/capabilities", data = "")] +pub fn get_capabilities_route( + body: Ruma, +) -> MatrixResult { + // TODO + MatrixResult(Ok(get_capabilities::Response { + capabilities: get_capabilities::Capabilities { + change_password: None, + room_versions: None, + custom_capabilities: BTreeMap::new(), + }, + })) +} + #[get("/_matrix/client/r0/pushrules")] pub fn get_pushrules_all_route() -> MatrixResult { // TODO MatrixResult(Ok(get_pushrules_all::Response { - global: HashMap::new(), + global: BTreeMap::new(), })) } @@ -447,8 +459,8 @@ pub fn set_presence_route( pub fn get_keys_route(body: Ruma) -> MatrixResult { // TODO MatrixResult(Ok(get_keys::Response { - failures: HashMap::new(), - device_keys: HashMap::new(), + failures: BTreeMap::new(), + device_keys: BTreeMap::new(), })) } @@ -459,7 +471,7 @@ pub fn upload_keys_route( ) -> MatrixResult { // TODO MatrixResult(Ok(upload_keys::Response { - one_time_key_counts: HashMap::new(), + one_time_key_counts: BTreeMap::new(), })) } @@ -472,14 +484,14 @@ pub fn set_read_marker_route( let user_id = body.user_id.clone().expect("user is authenticated"); // TODO: Fully read if let Some(event) = &body.read_receipt { - let mut user_receipts = HashMap::new(); + let mut user_receipts = BTreeMap::new(); user_receipts.insert( user_id.clone(), ruma_events::receipt::Receipt { - ts: Some(utils::millis_since_unix_epoch().try_into().unwrap()), + ts: Some(SystemTime::now()), }, ); - let mut receipt_content = HashMap::new(); + let mut receipt_content = BTreeMap::new(); receipt_content.insert( event.clone(), ruma_events::receipt::Receipts { @@ -537,7 +549,7 @@ pub fn create_room_route( body: Ruma, ) -> MatrixResult { // TODO: check if room is unique - let room_id = RoomId::new(data.hostname()).expect("host is valid"); + let room_id = RoomId::try_from(data.hostname()).expect("host is valid"); let user_id = body.user_id.clone().expect("user is authenticated"); data.pdu_append( @@ -589,8 +601,6 @@ pub fn create_room_route( ); } - dbg!(&*body); - data.room_join(&room_id, &user_id); for user in &body.invite { @@ -651,8 +661,8 @@ pub fn join_room_by_id_or_alias_route( body: Ruma, _room_id_or_alias: String, ) -> MatrixResult { - let room_id = match &body.room_id_or_alias { - RoomIdOrAliasId::RoomAliasId(alias) => match alias.alias() { + let room_id = if body.room_id_or_alias.is_room_alias_id() { + match body.room_id_or_alias.as_ref() { "#room:localhost" => "!xclkjvdlfj:localhost".try_into().unwrap(), _ => { debug!("Room not found."); @@ -662,9 +672,9 @@ pub fn join_room_by_id_or_alias_route( status_code: http::StatusCode::NOT_FOUND, })); } - }, - - RoomIdOrAliasId::RoomId(id) => id.clone(), + } + } else { + body.room_id_or_alias.try_into().unwrap() }; if data.room_join( @@ -681,6 +691,17 @@ pub fn join_room_by_id_or_alias_route( } } +#[post("/_matrix/client/r0/rooms/<_room_id>/leave", data = "")] +pub fn leave_room_route( + data: State, + body: Ruma, + _room_id: String, +) -> MatrixResult { + let user_id = body.user_id.clone().expect("user is authenticated"); + data.room_leave(&user_id, &body.room_id, &user_id); + MatrixResult(Ok(leave_room::Response)) +} + #[post("/_matrix/client/r0/rooms/<_room_id>/invite", data = "")] pub fn invite_user_route( data: State, @@ -714,7 +735,7 @@ pub fn get_public_rooms_filtered_route( .map(|room_id| { let state = data.room_state(&room_id); directory::PublicRoomsChunk { - aliases: None, + aliases: Vec::new(), canonical_alias: None, name: state .get(&(EventType::RoomName, "".to_owned())) @@ -763,13 +784,22 @@ pub fn search_users_route( })) } +#[get("/_matrix/client/r0/rooms/<_room_id>/members", data = "")] +pub fn get_member_events_route( + body: Ruma, + _room_id: String, +) -> MatrixResult { + // TODO + MatrixResult(Ok(get_member_events::Response { chunk: Vec::new() })) +} + #[get("/_matrix/client/r0/thirdparty/protocols", data = "")] pub fn get_protocols_route( body: Ruma, ) -> MatrixResult { // TODO MatrixResult(Ok(get_protocols::Response { - protocols: HashMap::new(), + protocols: BTreeMap::new(), })) } @@ -851,7 +881,7 @@ pub fn sync_route( std::thread::sleep(Duration::from_millis(200)); let next_batch = data.last_pdu_index().to_string(); - let mut joined_rooms = HashMap::new(); + let mut joined_rooms = BTreeMap::new(); let joined_roomids = data.rooms_joined(body.user_id.as_ref().expect("user is authenticated")); let since = body .since @@ -860,7 +890,11 @@ pub fn sync_route( .unwrap_or(0); for room_id in joined_roomids { let pdus = data.pdus_since(&room_id, since); - let room_events = pdus.into_iter().map(|pdu| pdu.to_room_event()).collect(); + let room_events = pdus + .into_iter() + .map(|pdu| pdu.to_room_event()) + .filter_map(|e| e) + .collect(); let mut edus = data.roomlatests_since(&room_id, since); edus.extend_from_slice(&data.roomactives_in(&room_id)); @@ -888,7 +922,33 @@ pub fn sync_route( ); } - let mut invited_rooms = HashMap::new(); + let mut left_rooms = BTreeMap::new(); + let left_roomids = data.rooms_left(body.user_id.as_ref().expect("user is authenticated")); + for room_id in left_roomids { + let pdus = data.pdus_since(&room_id, since); + let room_events = pdus + .into_iter() + .map(|pdu| pdu.to_room_event()) + .filter_map(|e| e) + .collect(); + let mut edus = data.roomlatests_since(&room_id, since); + edus.extend_from_slice(&data.roomactives_in(&room_id)); + + left_rooms.insert( + room_id.clone().try_into().unwrap(), + sync_events::LeftRoom { + account_data: sync_events::AccountData { events: Vec::new() }, + timeline: sync_events::Timeline { + limited: Some(false), + prev_batch: Some("".to_owned()), + events: room_events, + }, + state: sync_events::State { events: Vec::new() }, + }, + ); + } + + let mut invited_rooms = BTreeMap::new(); for room_id in data.rooms_invited(body.user_id.as_ref().expect("user is authenticated")) { let events = data .pdus_since(&room_id, since) @@ -907,7 +967,7 @@ pub fn sync_route( MatrixResult(Ok(sync_events::Response { next_batch, rooms: sync_events::Rooms { - leave: Default::default(), + leave: left_rooms, join: joined_rooms, invite: invited_rooms, }, @@ -945,4 +1005,4 @@ pub fn options_route(_segments: PathBuf) -> MatrixResult Self { + let db = Database::load_or_create(hostname); Self { hostname: hostname.to_owned(), - db: Database::load_or_create(hostname), + reqwest_client: reqwest::Client::new(), + db, } } @@ -27,6 +30,15 @@ impl Data { &self.hostname } + /// Get the hostname of the server. + pub fn reqwest_client(&self) -> &reqwest::Client { + &self.reqwest_client + } + + pub fn keypair(&self) -> &ruma_signatures::Ed25519KeyPair { + &self.db.keypair + } + /// Check if a user has an account by looking for an assigned password. pub fn user_exists(&self, user_id: &UserId) -> bool { self.db @@ -183,6 +195,10 @@ impl Data { user_id.to_string().as_bytes(), room_id.to_string().as_bytes(), ); + self.db.userid_leftroomids.remove_value( + user_id.to_string().as_bytes(), + room_id.to_string().as_bytes().into(), + ); self.pdu_append( room_id.clone(), @@ -277,6 +293,33 @@ impl Data { hashmap } + pub fn room_leave(&self, sender: &UserId, room_id: &RoomId, user_id: &UserId) { + self.pdu_append( + room_id.clone(), + sender.clone(), + EventType::RoomMember, + json!({"membership": "leave"}), + None, + Some(user_id.to_string()), + ); + self.db.userid_inviteroomids.remove_value( + user_id.to_string().as_bytes(), + room_id.to_string().as_bytes().into(), + ); + self.db.userid_roomids.remove_value( + user_id.to_string().as_bytes(), + room_id.to_string().as_bytes().into(), + ); + self.db.roomid_userids.remove_value( + room_id.to_string().as_bytes(), + user_id.to_string().as_bytes().into(), + ); + self.db.userid_leftroomids.add( + user_id.to_string().as_bytes(), + room_id.to_string().as_bytes().into(), + ); + } + pub fn room_invite(&self, sender: &UserId, room_id: &RoomId, user_id: &UserId) { self.pdu_append( room_id.clone(), @@ -287,9 +330,13 @@ impl Data { Some(user_id.to_string()), ); self.db.userid_inviteroomids.add( - &user_id.to_string().as_bytes(), + user_id.to_string().as_bytes(), room_id.to_string().as_bytes().into(), ); + self.db.roomid_userids.add( + room_id.to_string().as_bytes(), + user_id.to_string().as_bytes().into(), + ); } pub fn rooms_invited(&self, user_id: &UserId) -> Vec { @@ -301,6 +348,15 @@ impl Data { .collect() } + pub fn rooms_left(&self, user_id: &UserId) -> Vec { + self.db + .userid_leftroomids + .get_iter(&user_id.to_string().as_bytes()) + .values() + .map(|key| RoomId::try_from(&*utils::string_from_bytes(&key.unwrap())).unwrap()) + .collect() + } + pub fn pdu_get(&self, event_id: &EventId) -> Option { self.db .eventid_pduid @@ -434,7 +490,7 @@ impl Data { if let Some(state_key) = pdu.state_key { let mut key = room_id.to_string().as_bytes().to_vec(); key.push(0xff); - key.extend_from_slice(dbg!(pdu.kind.to_string().as_bytes())); + key.extend_from_slice(pdu.kind.to_string().as_bytes()); key.push(0xff); key.extend_from_slice(state_key.to_string().as_bytes()); self.db.roomstateid_pdu.insert(key, &*pdu_json).unwrap(); diff --git a/src/database.rs b/src/database.rs index 041a215b..bc328473 100644 --- a/src/database.rs +++ b/src/database.rs @@ -30,7 +30,7 @@ impl MultiValue { pub fn remove_value(&self, id: &[u8], value: &[u8]) { if let Some(key) = self .get_iter(id) - .find(|t| t.as_ref().unwrap().1 == value) + .find(|t| &t.as_ref().unwrap().1 == value) .map(|t| t.unwrap().0) { self.0.remove(key).unwrap(); @@ -74,11 +74,13 @@ pub struct Database { pub roomid_userids: MultiValue, pub userid_roomids: MultiValue, pub userid_inviteroomids: MultiValue, + pub userid_leftroomids: MultiValue, // EDUs: pub roomlatestid_roomlatest: sled::Tree, // Read Receipts, RoomLatestId = RoomId + Since + UserId TODO: Types pub roomactiveid_roomactive: sled::Tree, // Typing, RoomActiveId = TimeoutTime + Since pub globalallid_globalall: sled::Tree, // ToDevice, GlobalAllId = UserId + Since pub globallatestid_globallatest: sled::Tree, // Presence, GlobalLatestId = Since + Type + UserId + pub keypair: ruma_signatures::Ed25519KeyPair, _db: sled::Db, } @@ -116,10 +118,18 @@ impl Database { roomid_userids: MultiValue(db.open_tree("roomid_userids").unwrap()), userid_roomids: MultiValue(db.open_tree("userid_roomids").unwrap()), userid_inviteroomids: MultiValue(db.open_tree("userid_inviteroomids").unwrap()), + userid_leftroomids: MultiValue(db.open_tree("userid_leftroomids").unwrap()), roomlatestid_roomlatest: db.open_tree("roomlatestid_roomlatest").unwrap(), roomactiveid_roomactive: db.open_tree("roomactiveid_roomactive").unwrap(), globalallid_globalall: db.open_tree("globalallid_globalall").unwrap(), globallatestid_globallatest: db.open_tree("globallatestid_globallatest").unwrap(), + keypair: ruma_signatures::Ed25519KeyPair::new( + &*db.update_and_fetch("keypair", utils::generate_keypair) + .unwrap() + .unwrap(), + "0.0.0".to_owned(), + ) + .unwrap(), _db: db, } } diff --git a/src/main.rs b/src/main.rs index f79a0b4a..f26ed913 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,7 @@ mod data; mod database; mod pdu; mod ruma_wrapper; +mod server_server; mod utils; #[cfg(test)] @@ -26,6 +27,7 @@ fn setup_rocket(data: Data) -> rocket::Rocket { client_server::register_route, client_server::get_login_route, client_server::login_route, + client_server::get_capabilities_route, client_server::get_pushrules_all_route, client_server::get_filter_route, client_server::create_filter_route, @@ -45,9 +47,11 @@ fn setup_rocket(data: Data) -> rocket::Rocket { client_server::get_alias_route, client_server::join_room_by_id_route, client_server::join_room_by_id_or_alias_route, + client_server::leave_room_route, client_server::invite_user_route, client_server::get_public_rooms_filtered_route, client_server::search_users_route, + client_server::get_member_events_route, client_server::get_protocols_route, client_server::create_message_event_route, client_server::create_state_event_for_key_route, @@ -64,12 +68,12 @@ fn setup_rocket(data: Data) -> rocket::Rocket { fn main() { // Log info by default if let Err(_) = std::env::var("RUST_LOG") { - std::env::set_var("RUST_LOG", "matrixserver=debug,info"); + std::env::set_var("RUST_LOG", "warn"); } pretty_env_logger::init(); let data = Data::load_or_create("matrixtesting.koesters.xyz"); - data.debug(); + //data.debug(); setup_rocket(data).launch().unwrap(); } diff --git a/src/pdu.rs b/src/pdu.rs index 47f94ac5..3f15aa21 100644 --- a/src/pdu.rs +++ b/src/pdu.rs @@ -31,15 +31,18 @@ pub struct PduEvent { } impl PduEvent { - pub fn to_room_event(&self) -> RoomEvent { + // TODO: This shouldn't be an option + pub fn to_room_event(&self) -> Option { // Can only fail in rare circumstances that won't ever happen here, see // https://docs.rs/serde_json/1.0.50/serde_json/fn.to_string.html let json = serde_json::to_string(&self).unwrap(); // EventResult's deserialize implementation always returns `Ok(...)` - serde_json::from_str::>(&json) - .unwrap() - .into_result() - .unwrap() + Some( + serde_json::from_str::>(&json) + .unwrap() + .into_result() + .ok()?, + ) } pub fn to_stripped_state_event(&self) -> Option { diff --git a/src/server_server.rs b/src/server_server.rs new file mode 100644 index 00000000..57f76cee --- /dev/null +++ b/src/server_server.rs @@ -0,0 +1,25 @@ +use std::convert::TryInto; + +pub fn send_request>>>( + data: &crate::Data, + method: http::Method, + uri: String, + destination: String, + request: T, +) where + T::Error: std::fmt::Debug, +{ + let mut http_request: http::Request<_> = request.try_into().unwrap(); + let request_json = serde_json::to_value(http_request.body()).unwrap(); + + let request_map = request_json.as_object_mut().unwrap(); + + request_map.insert("method".to_owned(), method.to_string().into()); + request_map.insert("uri".to_owned(), uri.to_string().into()); + //TODO: request_map.insert("origin".to_owned(), data.origin().to_string().into()); + request_map.insert("destination".to_owned(), destination.to_string().into()); + + ruma_signatures::sign_json(data.hostname(), data.keypair(), &mut request_json).unwrap(); + let signature = request_json["signatures"]; + data.reqwest_client().execute(http_request.into()); +} diff --git a/src/stateres.rs b/src/stateres.rs new file mode 100644 index 00000000..ee47099a --- /dev/null +++ b/src/stateres.rs @@ -0,0 +1,59 @@ +use std::collections::HashMap; + +fn stateres(state_a: HashMap, state_b: HashMap) { + let mut unconflicted = todo!("state at fork event"); + + let mut conflicted: HashMap = state_a + .iter() + .filter(|(key_a, value_a)| match state_b.remove(key_a) { + Some(value_b) if value_a == value_b => unconflicted.insert(key_a, value_a), + _ => false, + }) + .collect(); + + // We removed unconflicted from state_b, now we can easily insert all events that are only in fork b + conflicted.extend(state_b); + + let partial_state = unconflicted.clone(); + + let full_conflicted = conflicted.clone(); // TODO: auth events + + let output_rev = Vec::new(); + let event_map = HashMap::new(); + let incoming_edges = HashMap::new(); + + for event in full_conflicted { + event_map.insert(event.event_id, event); + incoming_edges.insert(event.event_id, 0); + } + + for e in conflicted_control_events { + for a in e.auth_events { + incoming_edges[a.event_id] += 1; + } + } + + while incoming_edges.len() > 0 { + let mut count_0 = incoming_edges + .iter() + .filter(|(_, c)| c == 0) + .collect::>(); + + count_0.sort_by(|(x, _), (y, _)| { + x.power_level + .cmp(&a.power_level) + .then_with(|| x.origin_server.ts.cmp(&y.origin_server_ts)) + .then_with(|| x.event_id.cmp(&y.event_id)) + }); + + for (id, count) in count_0 { + output_rev.push(event_map[id]); + + for auth_event in event_map[id].auth_events { + incoming_edges[auth_event.event_id] -= 1; + } + + incoming_edges.remove(id); + } + } +} diff --git a/src/utils.rs b/src/utils.rs index 3b3ed929..a3600360 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,9 +1,9 @@ +use argon2::{Config, Variant}; use rand::prelude::*; use std::{ convert::TryInto, time::{SystemTime, UNIX_EPOCH}, }; -use argon2::{Config, Variant}; pub fn millis_since_unix_epoch() -> u64 { SystemTime::now() @@ -25,6 +25,13 @@ pub fn increment(old: Option<&[u8]>) -> Option> { Some(number.to_be_bytes().to_vec()) } +pub fn generate_keypair(old: Option<&[u8]>) -> Option> { + Some( + old.map(|s| s.to_vec()) + .unwrap_or_else(|| ruma_signatures::Ed25519KeyPair::generate().unwrap()), + ) +} + pub fn u64_from_bytes(bytes: &[u8]) -> u64 { let array: [u8; 8] = bytes.try_into().expect("bytes are valid u64"); u64::from_be_bytes(array) @@ -49,9 +56,5 @@ pub fn calculate_hash(password: &str) -> Result { }; let salt = random_string(32); - argon2::hash_encoded( - password.as_bytes(), - salt.as_bytes(), - &hashing_config, - ) -} \ No newline at end of file + argon2::hash_encoded(password.as_bytes(), salt.as_bytes(), &hashing_config) +} From 9b79798e56c772fe338815d97e7d423e4e920806 Mon Sep 17 00:00:00 2001 From: timokoesters Date: Mon, 6 Apr 2020 14:53:34 +0200 Subject: [PATCH 02/12] start work on signing --- src/data.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/data.rs b/src/data.rs index 2aa003a5..08e5e59a 100644 --- a/src/data.rs +++ b/src/data.rs @@ -457,6 +457,9 @@ impl Data { )) .expect("ruma's reference hashes are correct"); + let mut pdu_json = serde_json::to_value(pdu).unwrap(); + ruma_signatures::hash_and_sign_event(self.hostname(), self.keypair(), &mut pdu_json); + self.pdu_leaves_replace(&room_id, &pdu.event_id); // The new value will need a new index. We store the last used index in 'n' @@ -478,9 +481,10 @@ impl Data { pdu_id.push(0xff); // Add delimiter so we don't find rooms starting with the same id pdu_id.extend_from_slice(&index.to_be_bytes()); - let pdu_json = serde_json::to_string(&pdu).unwrap(); - - self.db.pduid_pdu.insert(&pdu_id, &*pdu_json).unwrap(); + self.db + .pduid_pdu + .insert(&pdu_id, &*serde_json::to_string(&pdu_json).unwrap()) + .unwrap(); self.db .eventid_pduid From b0d9ccdb2db31e685e05b828669bec2a00b6b589 Mon Sep 17 00:00:00 2001 From: timokoesters Date: Wed, 22 Apr 2020 11:53:06 +0200 Subject: [PATCH 03/12] Signing, basis for federation --- Cargo.lock | 1 - Cargo.toml | 2 +- src/client_server.rs | 28 ++++++++++++-- src/data.rs | 9 +++-- src/database.rs | 2 +- src/server_server.rs | 88 ++++++++++++++++++++++++++++++++++++++------ src/test.rs | 38 +++++++++++++------ src/utils.rs | 4 +- 8 files changed, 137 insertions(+), 35 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9d7eaf21..5e6dc73e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1260,7 +1260,6 @@ dependencies = [ [[package]] name = "ruma-federation-api" version = "0.0.1" -source = "git+https://github.com/ruma/ruma-federation-api.git#5448c650f0a583382152d0f43f2dcf720d495390" dependencies = [ "js_int", "ruma-api", diff --git a/Cargo.toml b/Cargo.toml index 5cf5412f..2a3a0b63 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,7 +18,7 @@ ruma-identifiers = "0.15.1" ruma-api = "0.16.0-rc.1" ruma-events = "0.19.0" ruma-signatures = { git = "https://github.com/ruma/ruma-signatures.git" } -ruma-federation-api = { git = "https://github.com/ruma/ruma-federation-api.git" } +ruma-federation-api = { path = "../ruma-federation-api" } pretty_env_logger = "0.4.0" log = "0.4.8" sled = "0.31.0" diff --git a/src/client_server.rs b/src/client_server.rs index a0191423..c97ba80c 100644 --- a/src/client_server.rs +++ b/src/client_server.rs @@ -1,4 +1,4 @@ -use crate::{utils, Data, MatrixResult, Ruma}; +use crate::{server_server, utils, Data, MatrixResult, Ruma}; use log::debug; use rocket::{get, options, post, put, State}; @@ -674,7 +674,8 @@ pub fn join_room_by_id_or_alias_route( } } } else { - body.room_id_or_alias.try_into().unwrap() + todo!(); + //body.room_id_or_alias.try_into().unwrap() }; if data.room_join( @@ -725,8 +726,8 @@ pub fn invite_user_route( } #[post("/_matrix/client/r0/publicRooms", data = "")] -pub fn get_public_rooms_filtered_route( - data: State, +pub async fn get_public_rooms_filtered_route( + data: State<'_, Data>, body: Ruma, ) -> MatrixResult { let mut chunk = data @@ -752,6 +753,25 @@ pub fn get_public_rooms_filtered_route( }) .collect::>(); + chunk.extend_from_slice( + &server_server::send_request( + &data, + "https://matrix.org".to_owned(), + ruma_federation_api::v1::get_public_rooms::Request { + limit: None, + since: None, + include_all_networks: None, + third_party_instance_id: None, + }, + ) + .await + .unwrap() + .chunk + .into_iter() + .map(|c| serde_json::from_str(&serde_json::to_string(dbg!(&c)).unwrap()).unwrap()) + .collect::>(), + ); + chunk.sort_by(|l, r| r.num_joined_members.cmp(&l.num_joined_members)); let total_room_count_estimate = (chunk.len() as u32).into(); diff --git a/src/data.rs b/src/data.rs index 08e5e59a..8b24c9da 100644 --- a/src/data.rs +++ b/src/data.rs @@ -457,7 +457,7 @@ impl Data { )) .expect("ruma's reference hashes are correct"); - let mut pdu_json = serde_json::to_value(pdu).unwrap(); + let mut pdu_json = serde_json::to_value(&pdu).unwrap(); ruma_signatures::hash_and_sign_event(self.hostname(), self.keypair(), &mut pdu_json); self.pdu_leaves_replace(&room_id, &pdu.event_id); @@ -483,7 +483,7 @@ impl Data { self.db .pduid_pdu - .insert(&pdu_id, &*serde_json::to_string(&pdu_json).unwrap()) + .insert(&pdu_id, &*pdu_json.to_string()) .unwrap(); self.db @@ -497,7 +497,10 @@ impl Data { key.extend_from_slice(pdu.kind.to_string().as_bytes()); key.push(0xff); key.extend_from_slice(state_key.to_string().as_bytes()); - self.db.roomstateid_pdu.insert(key, &*pdu_json).unwrap(); + self.db + .roomstateid_pdu + .insert(key, &*pdu_json.to_string()) + .unwrap(); } pdu.event_id diff --git a/src/database.rs b/src/database.rs index bc328473..73406c12 100644 --- a/src/database.rs +++ b/src/database.rs @@ -127,7 +127,7 @@ impl Database { &*db.update_and_fetch("keypair", utils::generate_keypair) .unwrap() .unwrap(), - "0.0.0".to_owned(), + "key1".to_owned(), ) .unwrap(), _db: db, diff --git a/src/server_server.rs b/src/server_server.rs index 57f76cee..8f355c0e 100644 --- a/src/server_server.rs +++ b/src/server_server.rs @@ -1,25 +1,89 @@ -use std::convert::TryInto; +use log::error; +use http::header::{HeaderValue, AUTHORIZATION}; +use ruma_api::{ + error::{FromHttpRequestError, FromHttpResponseError}, + Endpoint, Outgoing, +}; +use std::convert::{TryFrom, TryInto}; -pub fn send_request>>>( +pub async fn send_request( data: &crate::Data, - method: http::Method, - uri: String, destination: String, request: T, -) where +) -> Option<::Incoming> +where + // We need to duplicate Endpoint's where clauses because the compiler is not smart enough yet. + // See https://github.com/rust-lang/rust/issues/54149 + ::Incoming: TryFrom>, Error = FromHttpRequestError>, + ::Incoming: TryFrom< + http::Response>, + Error = FromHttpResponseError<::ResponseError>, + >, T::Error: std::fmt::Debug, { let mut http_request: http::Request<_> = request.try_into().unwrap(); - let request_json = serde_json::to_value(http_request.body()).unwrap(); + let uri = destination.clone() + T::METADATA.path; + *http_request.uri_mut() = uri.parse().unwrap(); + + let body = http_request.body(); + let mut request_json = if !body.is_empty() { + serde_json::to_value(http_request.body()).unwrap() + } else { + serde_json::Map::new().into() + }; let request_map = request_json.as_object_mut().unwrap(); - request_map.insert("method".to_owned(), method.to_string().into()); - request_map.insert("uri".to_owned(), uri.to_string().into()); - //TODO: request_map.insert("origin".to_owned(), data.origin().to_string().into()); + request_map.insert("method".to_owned(), T::METADATA.method.to_string().into()); + request_map.insert("uri".to_owned(), uri.into()); + request_map.insert("origin".to_owned(), data.hostname().into()); request_map.insert("destination".to_owned(), destination.to_string().into()); - ruma_signatures::sign_json(data.hostname(), data.keypair(), &mut request_json).unwrap(); - let signature = request_json["signatures"]; - data.reqwest_client().execute(http_request.into()); + ruma_signatures::sign_json(data.hostname(), data.keypair(), dbg!(&mut request_json)).unwrap(); + let signatures = request_json["signatures"] + .as_object() + .unwrap() + .values() + .next() + .unwrap() + .as_object() + .unwrap() + .iter() + .map(|(k, v)| (k, v.as_str().unwrap())); + + for s in signatures { + http_request.headers_mut().insert(AUTHORIZATION, HeaderValue::from_str(dbg!(&format!("X-Matrix origin={},key=\"{}\",sig=\"{}\"", data.hostname(), s.0, s.1))).unwrap()); + } + + let reqwest_response = data + .reqwest_client() + .execute(dbg!(http_request.into())) + .await; + + // Because reqwest::Response -> http::Response is complicated: + match reqwest_response { + Ok(mut reqwest_response) => { + let status = reqwest_response.status(); + let mut http_response = http::Response::builder().status(status); + let headers = http_response.headers_mut().unwrap(); + + for (k, v) in reqwest_response.headers_mut().drain() { + if let Some(key) = k { + headers.insert(key, v); + } + } + + let body = reqwest_response + .bytes() + .await + .unwrap() + .into_iter() + .collect(); + Some(::Incoming::try_from(dbg!(http_response.body(body).unwrap())).ok().unwrap()) + } + Err(e) => { + println!("ERROR: {}", e); + None + } + } } diff --git a/src/test.rs b/src/test.rs index c3cefb16..87564365 100644 --- a/src/test.rs +++ b/src/test.rs @@ -1,8 +1,7 @@ use super::*; -use rocket::{local::Client, http::Status}; -use serde_json::Value; -use serde_json::json; +use rocket::{http::Status, local::Client}; use ruma_client_api::error::ErrorKind; +use serde_json::{json, Value}; use std::time::Duration; fn setup_client() -> Client { @@ -19,7 +18,8 @@ async fn register_login() { let mut response = client .post("/_matrix/client/r0/register?kind=user") .body(registration_init()) - .dispatch().await; + .dispatch() + .await; let body = serde_json::from_str::(&response.body_string().await.unwrap()).unwrap(); assert_eq!(response.status().code, 401); @@ -33,14 +33,16 @@ async fn login_after_register_correct_password() { let mut response = client .post("/_matrix/client/r0/register?kind=user") .body(registration_init()) - .dispatch().await; + .dispatch() + .await; let body = serde_json::from_str::(&response.body_string().await.unwrap()).unwrap(); let session = body["session"].clone(); let response = client .post("/_matrix/client/r0/register?kind=user") .body(registration(session.as_str().unwrap())) - .dispatch().await; + .dispatch() + .await; assert_eq!(response.status().code, 200); let login_response = client @@ -57,14 +59,16 @@ async fn login_after_register_incorrect_password() { let mut response = client .post("/_matrix/client/r0/register?kind=user") .body(registration_init()) - .dispatch().await; + .dispatch() + .await; let body = serde_json::from_str::(&response.body_string().await.unwrap()).unwrap(); let session = body["session"].clone(); let response = client .post("/_matrix/client/r0/register?kind=user") .body(registration(session.as_str().unwrap())) - .dispatch().await; + .dispatch() + .await; assert_eq!(response.status().code, 200); let mut login_response = client @@ -73,7 +77,15 @@ async fn login_after_register_incorrect_password() { .dispatch() .await; let body = serde_json::from_str::(&login_response.body_string().await.unwrap()).unwrap(); - assert_eq!(body.as_object().unwrap().get("errcode").unwrap().as_str().unwrap(), "M_FORBIDDEN"); + assert_eq!( + body.as_object() + .unwrap() + .get("errcode") + .unwrap() + .as_str() + .unwrap(), + "M_FORBIDDEN" + ); assert_eq!(login_response.status().code, 403); } @@ -98,7 +110,8 @@ fn registration(session: &str) -> String { "device_id": "GHTYAJCE", "initial_device_display_name": "Jungle Phone", "inhibit_login": false - }).to_string() + }) + .to_string() } fn login_with_password(password: &str) -> String { @@ -110,5 +123,6 @@ fn login_with_password(password: &str) -> String { }, "password": password, "initial_device_display_name": "Jungle Phone" - }).to_string() -} \ No newline at end of file + }) + .to_string() +} diff --git a/src/utils.rs b/src/utils.rs index a3600360..07a99f1b 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -27,8 +27,10 @@ pub fn increment(old: Option<&[u8]>) -> Option> { pub fn generate_keypair(old: Option<&[u8]>) -> Option> { Some( + /* old.map(|s| s.to_vec()) - .unwrap_or_else(|| ruma_signatures::Ed25519KeyPair::generate().unwrap()), + .unwrap_or_else(|| */ + ruma_signatures::Ed25519KeyPair::generate().unwrap(), ) } From 1af6dd984ab021d1a7eb253111e1773fc1b6c370 Mon Sep 17 00:00:00 2001 From: timokoesters Date: Wed, 22 Apr 2020 20:55:11 +0200 Subject: [PATCH 04/12] More work on federation --- Cargo.lock | 3 +- Cargo.toml | 3 +- src/client_server.rs | 2 +- src/main.rs | 6 +++- src/server_server.rs | 86 +++++++++++++++++++++++++++++++++++++++++--- 5 files changed, 91 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5e6dc73e..df7e88f6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -142,6 +142,7 @@ dependencies = [ name = "conduit" version = "0.1.0" dependencies = [ + "base64 0.12.0", "directories", "http", "js_int", @@ -1265,6 +1266,7 @@ dependencies = [ "ruma-api", "ruma-events", "ruma-identifiers", + "ruma-serde", "serde", "serde_json", ] @@ -1292,7 +1294,6 @@ dependencies = [ [[package]] name = "ruma-signatures" version = "0.6.0-dev.1" -source = "git+https://github.com/ruma/ruma-signatures.git#c3f8399c268695464730afd6077c7ce50155b8d5" dependencies = [ "base64 0.12.0", "ring", diff --git a/Cargo.toml b/Cargo.toml index 2a3a0b63..afe23f79 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,7 +17,7 @@ ruma-client-api = { git = "https://github.com/ruma/ruma-client-api.git" } ruma-identifiers = "0.15.1" ruma-api = "0.16.0-rc.1" ruma-events = "0.19.0" -ruma-signatures = { git = "https://github.com/ruma/ruma-signatures.git" } +ruma-signatures = { path = "../ruma-signatures" } ruma-federation-api = { path = "../ruma-federation-api" } pretty_env_logger = "0.4.0" log = "0.4.8" @@ -30,3 +30,4 @@ tokio = { version = "0.2.18", features = ["macros"] } rand = "0.7.3" rust-argon2 = "0.8.2" reqwest = "0.10.4" +base64 = "0.12.0" diff --git a/src/client_server.rs b/src/client_server.rs index c97ba80c..a2cc5754 100644 --- a/src/client_server.rs +++ b/src/client_server.rs @@ -756,7 +756,7 @@ pub async fn get_public_rooms_filtered_route( chunk.extend_from_slice( &server_server::send_request( &data, - "https://matrix.org".to_owned(), + "https://matrix.koesters.xyz".to_owned(), ruma_federation_api::v1::get_public_rooms::Request { limit: None, since: None, diff --git a/src/main.rs b/src/main.rs index f26ed913..0794fcfe 100644 --- a/src/main.rs +++ b/src/main.rs @@ -60,6 +60,10 @@ fn setup_rocket(data: Data) -> rocket::Rocket { client_server::turn_server_route, client_server::publicised_groups_route, client_server::options_route, + server_server::well_known_server, + server_server::get_server_version, + server_server::get_server_keys, + server_server::get_server_keys_deprecated, ], ) .manage(data) @@ -72,7 +76,7 @@ fn main() { } pretty_env_logger::init(); - let data = Data::load_or_create("matrixtesting.koesters.xyz"); + let data = Data::load_or_create("matrixtesting.koesters.xyz:14004"); //data.debug(); setup_rocket(data).launch().unwrap(); diff --git a/src/server_server.rs b/src/server_server.rs index 8f355c0e..cbdcb6ad 100644 --- a/src/server_server.rs +++ b/src/server_server.rs @@ -1,10 +1,20 @@ -use log::error; +use crate::{utils, Data, MatrixResult, Ruma}; use http::header::{HeaderValue, AUTHORIZATION}; +use log::error; +use rocket::{get, options, post, put, response::content::Json, State}; use ruma_api::{ error::{FromHttpRequestError, FromHttpResponseError}, Endpoint, Outgoing, }; -use std::convert::{TryFrom, TryInto}; +use ruma_client_api::error::{Error, ErrorKind}; +use ruma_federation_api::{v1::get_server_version, v2::get_server_keys}; +use serde_json::json; +use std::{ + collections::{BTreeMap, HashMap}, + convert::{TryFrom, TryInto}, + path::PathBuf, + time::{Duration, SystemTime}, +}; pub async fn send_request( data: &crate::Data, @@ -39,7 +49,7 @@ where request_map.insert("origin".to_owned(), data.hostname().into()); request_map.insert("destination".to_owned(), destination.to_string().into()); - ruma_signatures::sign_json(data.hostname(), data.keypair(), dbg!(&mut request_json)).unwrap(); + ruma_signatures::sign_json(data.hostname(), data.keypair(), &mut request_json).unwrap(); let signatures = request_json["signatures"] .as_object() .unwrap() @@ -52,7 +62,16 @@ where .map(|(k, v)| (k, v.as_str().unwrap())); for s in signatures { - http_request.headers_mut().insert(AUTHORIZATION, HeaderValue::from_str(dbg!(&format!("X-Matrix origin={},key=\"{}\",sig=\"{}\"", data.hostname(), s.0, s.1))).unwrap()); + http_request.headers_mut().insert( + AUTHORIZATION, + HeaderValue::from_str(&format!( + "X-Matrix origin={},key=\"{}\",sig=\"{}\"", + data.hostname(), + s.0, + s.1 + )) + .unwrap(), + ); } let reqwest_response = data @@ -79,7 +98,13 @@ where .unwrap() .into_iter() .collect(); - Some(::Incoming::try_from(dbg!(http_response.body(body).unwrap())).ok().unwrap()) + Some( + ::Incoming::try_from( + dbg!(http_response.body(body)).unwrap(), + ) + .ok() + .unwrap(), + ) } Err(e) => { println!("ERROR: {}", e); @@ -87,3 +112,54 @@ where } } } + +#[get("/.well-known/matrix/server")] +pub fn well_known_server(data: State) -> Json { + rocket::response::content::Json( + json!({ "m.server": "matrixtesting.koesters.xyz:14004"}).to_string(), + ) +} + +#[get("/_matrix/federation/v1/version")] +pub fn get_server_version(data: State) -> MatrixResult { + MatrixResult(Ok(get_server_version::Response { + server: get_server_version::Server { + name: Some("Conduit".to_owned()), + version: Some(env!("CARGO_PKG_VERSION").to_owned()), + }, + })) +} + +#[get("/_matrix/key/v2/server", data = "")] +pub fn get_server_keys(data: State, body: Ruma) -> Json { + let mut verify_keys = BTreeMap::new(); + verify_keys.insert( + format!("ed25519:{}", data.keypair().version()), + get_server_keys::VerifyKey { + key: base64::encode_config(data.keypair().public_key(), base64::STANDARD_NO_PAD), + }, + ); + let mut response = serde_json::from_slice( + http::Response::try_from(get_server_keys::Response { + server_name: data.hostname().to_owned(), + verify_keys, + old_verify_keys: BTreeMap::new(), + signatures: BTreeMap::new(), + valid_until_ts: SystemTime::now() + Duration::from_secs(60 * 60 * 24), + }) + .unwrap() + .body(), + ) + .unwrap(); + ruma_signatures::sign_json(data.hostname(), data.keypair(), &mut response).unwrap(); + Json(dbg!(response.to_string())) +} + +#[get("/_matrix/key/v2/server/<_key_id>", data = "")] +pub fn get_server_keys_deprecated( + data: State, + body: Ruma, + _key_id: String, +) -> Json { + get_server_keys(data, body) +} From 873d191569985737ccc815dbb031bbbc18e7e15e Mon Sep 17 00:00:00 2001 From: timokoesters Date: Wed, 22 Apr 2020 21:14:40 +0200 Subject: [PATCH 05/12] fix: http body as content when signing --- src/client_server.rs | 2 +- src/server_server.rs | 24 +++++++++++++----------- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/client_server.rs b/src/client_server.rs index a2cc5754..dd609f2b 100644 --- a/src/client_server.rs +++ b/src/client_server.rs @@ -756,7 +756,7 @@ pub async fn get_public_rooms_filtered_route( chunk.extend_from_slice( &server_server::send_request( &data, - "https://matrix.koesters.xyz".to_owned(), + "matrix.koesters.xyz".to_owned(), ruma_federation_api::v1::get_public_rooms::Request { limit: None, since: None, diff --git a/src/server_server.rs b/src/server_server.rs index cbdcb6ad..f0fdf655 100644 --- a/src/server_server.rs +++ b/src/server_server.rs @@ -32,24 +32,26 @@ where T::Error: std::fmt::Debug, { let mut http_request: http::Request<_> = request.try_into().unwrap(); - let uri = destination.clone() + T::METADATA.path; - *http_request.uri_mut() = uri.parse().unwrap(); + + *http_request.uri_mut() = ("https://".to_owned() + &destination.clone() + T::METADATA.path).parse().unwrap(); - let body = http_request.body(); - let mut request_json = if !body.is_empty() { - serde_json::to_value(http_request.body()).unwrap() - } else { - serde_json::Map::new().into() + let mut request_map = serde_json::Map::new(); + + if !http_request.body().is_empty() { + request_map.insert("content".to_owned(), + serde_json::to_value(http_request.body()).unwrap()); }; - let request_map = request_json.as_object_mut().unwrap(); - request_map.insert("method".to_owned(), T::METADATA.method.to_string().into()); - request_map.insert("uri".to_owned(), uri.into()); + request_map.insert("uri".to_owned(), T::METADATA.path.into()); request_map.insert("origin".to_owned(), data.hostname().into()); request_map.insert("destination".to_owned(), destination.to_string().into()); + //request_map.insert("signatures".to_owned(), json!({})); + + let mut request_json = request_map.into(); + ruma_signatures::sign_json(data.hostname(), data.keypair(), dbg!(&mut request_json)).unwrap(); + dbg!(&request_json); - ruma_signatures::sign_json(data.hostname(), data.keypair(), &mut request_json).unwrap(); let signatures = request_json["signatures"] .as_object() .unwrap() From 120b6f4b9542bc46c6335b3f36c8875aa352a228 Mon Sep 17 00:00:00 2001 From: timokoesters Date: Thu, 23 Apr 2020 14:27:50 +0200 Subject: [PATCH 06/12] Bump dependencies and fixes --- Cargo.lock | 51 +++++++++++++++++++++++--------------------- Cargo.toml | 10 ++++----- src/client_server.rs | 8 +++---- src/data.rs | 23 +++++++++++--------- src/pdu.rs | 23 ++++++-------------- src/ruma_wrapper.rs | 23 ++++++-------------- src/server_server.rs | 19 +++++------------ 7 files changed, 67 insertions(+), 90 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index df7e88f6..0b659c33 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -11,9 +11,9 @@ dependencies = [ [[package]] name = "arc-swap" -version = "0.4.5" +version = "0.4.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d663a8e9a99154b5fb793032533f6328da35e23aac63d5c152279aa8ba356825" +checksum = "b585a98a234c46fc563103e9278c9391fde1f4e6850334da895d27edb9580f62" [[package]] name = "arrayref" @@ -119,9 +119,9 @@ checksum = "130aac562c0dd69c56b3b1cc8ffd2e17be31d0b6c25b61c96b76231aa23e39e1" [[package]] name = "cc" -version = "1.0.50" +version = "1.0.52" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "95e28fa049fda1c330bcf9d723be7663a899c4679724b34c81e9f5a326aab8cd" +checksum = "c3d87b23d6a92cd03af510a5ade527033f6aa6fa92161e2d5863a907d4c5e31d" [[package]] name = "cfg-if" @@ -841,9 +841,9 @@ dependencies = [ [[package]] name = "parking_lot_core" -version = "0.7.1" +version = "0.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0e136c1904604defe99ce5fd71a28d473fa60a12255d511aa78a9ddf11237aeb" +checksum = "d58c7c768d4ba344e3e8d72518ac13e259d7c7ade24167003b8488e10b6740a3" dependencies = [ "cfg-if", "cloudabi", @@ -915,9 +915,9 @@ checksum = "237844750cfbb86f67afe27eee600dfbbcb6188d734139b534cbfbf4f96792ae" [[package]] name = "pin-utils" -version = "0.1.0-alpha.4" +version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5894c618ce612a3fa23881b152b608bafb8c56cfc22f434a3ba3120b40f7b587" +checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" [[package]] name = "pkg-config" @@ -1191,9 +1191,9 @@ dependencies = [ [[package]] name = "ruma-api" -version = "0.16.0-rc.1" +version = "0.16.0-rc.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7769e934360383f91d68a2b9132610d02436ef3272cbdd46de239c9025198a36" +checksum = "c296a951625ccc8c04d5188f1791d1628503c8614073a05833af9fed18b029c1" dependencies = [ "http", "percent-encoding 2.1.0", @@ -1207,9 +1207,9 @@ dependencies = [ [[package]] name = "ruma-api-macros" -version = "0.16.0-rc.1" +version = "0.16.0-rc.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0f8dad3311d0bee6d43da684ba03f9a84cddd39f9b3e7e88cab5d726419ec22e" +checksum = "0f6b02a6a860a96e3c2081c8aea88b37b2918b53e539856b73aadde1908b65ad" dependencies = [ "proc-macro2 1.0.10", "quote 1.0.3", @@ -1218,8 +1218,8 @@ dependencies = [ [[package]] name = "ruma-client-api" -version = "0.8.0-rc.2" -source = "git+https://github.com/ruma/ruma-client-api.git#1b7863dc36e6e043ae365791cc719dc190abd660" +version = "0.8.0-rc.5" +source = "git+https://github.com/ruma/ruma-client-api.git#473cf2386178781688f12c538ee6419869d14a46" dependencies = [ "http", "js_int", @@ -1235,9 +1235,9 @@ dependencies = [ [[package]] name = "ruma-events" -version = "0.19.0" +version = "0.21.0-beta.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "844b5d38397c945395c7a1eaf89d55714c3d22983b870085a1a67d51fb6611cf" +checksum = "d4802476bbe517f2ac6cb7b1cf4869d54586c10e86e2ddc00806cafa32a96553" dependencies = [ "js_int", "ruma-events-macros", @@ -1249,9 +1249,9 @@ dependencies = [ [[package]] name = "ruma-events-macros" -version = "0.19.0-final" +version = "0.21.0-beta.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5477046b734fde45dd7913dbc8d7b260af3b1c31ea2bc329bd2f0b44e37368be" +checksum = "abd3cfe96c9887fe2eebfa2e5e7d3a4afff02c374874d4e718f46dab5fd3320d" dependencies = [ "proc-macro2 1.0.10", "quote 1.0.3", @@ -1261,6 +1261,7 @@ dependencies = [ [[package]] name = "ruma-federation-api" version = "0.0.1" +source = "git+https://git.koesters.xyz/timo/ruma-federation-api.git#6f1c5a6a714d6be2f420f3832d31e214fe4fb229" dependencies = [ "js_int", "ruma-api", @@ -1273,10 +1274,11 @@ dependencies = [ [[package]] name = "ruma-identifiers" -version = "0.15.1" +version = "0.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "63db5545f38077ea141fb112df070773e6ab9b7025174d732f56c6b37525ccc0" +checksum = "6ee548c5dbb5a92a93bb435fd3b66484cd19b0f37450c9b93677338cbe9550d2" dependencies = [ + "rand", "serde", ] @@ -1294,6 +1296,7 @@ dependencies = [ [[package]] name = "ruma-signatures" version = "0.6.0-dev.1" +source = "git+https://git.koesters.xyz/timo/ruma-signatures.git#e2cd0927c14133b8d49a8cd9fbd96f8af5c60f07" dependencies = [ "base64 0.12.0", "ring", @@ -1340,9 +1343,9 @@ dependencies = [ [[package]] name = "ryu" -version = "1.0.3" +version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "535622e6be132bccd223f4bb2b8ac8d53cda3c7a6394944d3b2b33fb974f9d76" +checksum = "ed3d612bc64430efeb3f7ee6ef26d590dce0c43249217bddc62112540c7941e1" [[package]] name = "schannel" @@ -1877,9 +1880,9 @@ checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" [[package]] name = "winapi-util" -version = "0.1.4" +version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fa515c5163a99cc82bab70fd3bfdd36d827be85de63737b40fcef2ce084a436e" +checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" dependencies = [ "winapi 0.3.8", ] diff --git a/Cargo.toml b/Cargo.toml index afe23f79..9ae2a0cf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,11 +14,11 @@ edition = "2018" rocket = { git = "https://github.com/SergioBenitez/Rocket.git", branch = "async", features = ["tls"] } http = "0.2.1" ruma-client-api = { git = "https://github.com/ruma/ruma-client-api.git" } -ruma-identifiers = "0.15.1" -ruma-api = "0.16.0-rc.1" -ruma-events = "0.19.0" -ruma-signatures = { path = "../ruma-signatures" } -ruma-federation-api = { path = "../ruma-federation-api" } +ruma-identifiers = { version = "0.16.0", features = ["rand"] } +ruma-api = "0.16.0-rc.3" +ruma-events = "0.21.0-beta.1" +ruma-signatures = { git = "https://git.koesters.xyz/timo/ruma-signatures.git" } +ruma-federation-api = { git = "https://git.koesters.xyz/timo/ruma-federation-api.git" } pretty_env_logger = "0.4.0" log = "0.4.8" sled = "0.31.0" diff --git a/src/client_server.rs b/src/client_server.rs index dd609f2b..2c88daac 100644 --- a/src/client_server.rs +++ b/src/client_server.rs @@ -549,7 +549,7 @@ pub fn create_room_route( body: Ruma, ) -> MatrixResult { // TODO: check if room is unique - let room_id = RoomId::try_from(data.hostname()).expect("host is valid"); + let room_id = RoomId::new(data.hostname()).expect("host is valid"); let user_id = body.user_id.clone().expect("user is authenticated"); data.pdu_append( @@ -756,7 +756,7 @@ pub async fn get_public_rooms_filtered_route( chunk.extend_from_slice( &server_server::send_request( &data, - "matrix.koesters.xyz".to_owned(), + "koesters.xyz".to_owned(), ruma_federation_api::v1::get_public_rooms::Request { limit: None, since: None, @@ -913,7 +913,6 @@ pub fn sync_route( let room_events = pdus .into_iter() .map(|pdu| pdu.to_room_event()) - .filter_map(|e| e) .collect(); let mut edus = data.roomlatests_since(&room_id, since); edus.extend_from_slice(&data.roomactives_in(&room_id)); @@ -949,7 +948,6 @@ pub fn sync_route( let room_events = pdus .into_iter() .map(|pdu| pdu.to_room_event()) - .filter_map(|e| e) .collect(); let mut edus = data.roomlatests_since(&room_id, since); edus.extend_from_slice(&data.roomactives_in(&room_id)); @@ -973,7 +971,7 @@ pub fn sync_route( let events = data .pdus_since(&room_id, since) .into_iter() - .filter_map(|pdu| pdu.to_stripped_state_event()) + .map(|pdu| pdu.to_stripped_state_event()) .collect(); invited_rooms.insert( diff --git a/src/data.rs b/src/data.rs index 8b24c9da..864536f6 100644 --- a/src/data.rs +++ b/src/data.rs @@ -1,5 +1,5 @@ use crate::{utils, Database, PduEvent}; -use ruma_events::{collections::only::Event as EduEvent, EventResult, EventType}; +use ruma_events::{collections::only::Event as EduEvent, EventJson, EventType}; use ruma_federation_api::RoomV3Pdu; use ruma_identifiers::{EventId, RoomId, UserId}; use serde_json::json; @@ -604,7 +604,7 @@ impl Data { } /// Returns a vector of the most recent read_receipts in a room that happened after the event with id `since`. - pub fn roomlatests_since(&self, room_id: &RoomId, since: u64) -> Vec { + pub fn roomlatests_since(&self, room_id: &RoomId, since: u64) -> Vec> { let mut room_latests = Vec::new(); let mut prefix = room_id.to_string().as_bytes().to_vec(); @@ -617,10 +617,11 @@ impl Data { if key.starts_with(&prefix) { current = key.to_vec(); room_latests.push( - serde_json::from_slice::>(&value) + serde_json::from_slice::>(&value) .expect("room_latest in db is valid") - .into_result() - .expect("room_latest in db is valid"), + .deserialize() + .expect("room_latest in db is valid") + .into(), ); } else { break; @@ -691,7 +692,7 @@ impl Data { } /// Returns a vector of the most recent read_receipts in a room that happened after the event with id `since`. - pub fn roomactives_in(&self, room_id: &RoomId) -> Vec { + pub fn roomactives_in(&self, room_id: &RoomId) -> Vec> { let mut room_actives = Vec::new(); let mut prefix = room_id.to_string().as_bytes().to_vec(); @@ -704,10 +705,11 @@ impl Data { if key.starts_with(&prefix) { current = key.to_vec(); room_actives.push( - serde_json::from_slice::>(&value) + serde_json::from_slice::>(&value) .expect("room_active in db is valid") - .into_result() - .expect("room_active in db is valid"), + .deserialize() + .expect("room_active in db is valid") + .into(), ); } else { break; @@ -720,7 +722,8 @@ impl Data { user_ids: Vec::new(), }, room_id: None, // None because it can be inferred - })]; + }) + .into()]; } else { room_actives } diff --git a/src/pdu.rs b/src/pdu.rs index 3f15aa21..b6aa45df 100644 --- a/src/pdu.rs +++ b/src/pdu.rs @@ -1,6 +1,6 @@ use js_int::UInt; use ruma_events::{ - collections::all::RoomEvent, stripped::AnyStrippedStateEvent, EventResult, EventType, + collections::all::RoomEvent, stripped::AnyStrippedStateEvent, EventJson, EventType, }; use ruma_federation_api::EventHash; use ruma_identifiers::{EventId, RoomId, UserId}; @@ -31,29 +31,20 @@ pub struct PduEvent { } impl PduEvent { - // TODO: This shouldn't be an option - pub fn to_room_event(&self) -> Option { + pub fn to_room_event(&self) -> EventJson { // Can only fail in rare circumstances that won't ever happen here, see // https://docs.rs/serde_json/1.0.50/serde_json/fn.to_string.html let json = serde_json::to_string(&self).unwrap(); - // EventResult's deserialize implementation always returns `Ok(...)` - Some( - serde_json::from_str::>(&json) - .unwrap() - .into_result() - .ok()?, - ) + // EventJson's deserialize implementation always returns `Ok(...)` + serde_json::from_str::>(&json).unwrap() } - pub fn to_stripped_state_event(&self) -> Option { + pub fn to_stripped_state_event(&self) -> EventJson { // Can only fail in rare circumstances that won't ever happen here, see // https://docs.rs/serde_json/1.0.50/serde_json/fn.to_string.html let json = serde_json::to_string(&self).unwrap(); - // EventResult's deserialize implementation always returns `Ok(...)` - serde_json::from_str::>(&json) - .unwrap() - .into_result() - .ok() + // EventJson's deserialize implementation always returns `Ok(...)` + serde_json::from_str::>(&json).unwrap() } } diff --git a/src/ruma_wrapper.rs b/src/ruma_wrapper.rs index 81b7ea8e..81294a45 100644 --- a/src/ruma_wrapper.rs +++ b/src/ruma_wrapper.rs @@ -7,12 +7,11 @@ use rocket::{ Request, State, }; use ruma_api::{ - error::{FromHttpRequestError, FromHttpResponseError}, - Endpoint, Outgoing, + Endpoint }; use ruma_identifiers::UserId; use std::{ - convert::{TryFrom, TryInto}, + convert::{TryInto}, io::Cursor, ops::Deref, }; @@ -22,21 +21,13 @@ const MESSAGE_LIMIT: u64 = 65535; /// This struct converts rocket requests into ruma structs by converting them into http requests /// first. -pub struct Ruma { - body: T::Incoming, +pub struct Ruma { + body: T, pub user_id: Option, pub json_body: serde_json::Value, } impl<'a, T: Endpoint> FromData<'a> for Ruma -where - // We need to duplicate Endpoint's where clauses because the compiler is not smart enough yet. - // See https://github.com/rust-lang/rust/issues/54149 - ::Incoming: TryFrom>, Error = FromHttpRequestError>, - ::Incoming: TryFrom< - http::Response>, - Error = FromHttpResponseError<::ResponseError>, - >, { type Error = (); // TODO: Better error handling type Owned = Data; @@ -95,7 +86,7 @@ where let http_request = http_request.body(body.clone()).unwrap(); log::info!("{:?}", http_request); - match T::Incoming::try_from(http_request) { + match T::try_from(http_request) { Ok(t) => Success(Ruma { body: t, user_id, @@ -115,8 +106,8 @@ where } } -impl Deref for Ruma { - type Target = T::Incoming; +impl Deref for Ruma { + type Target = T; fn deref(&self) -> &Self::Target { &self.body diff --git a/src/server_server.rs b/src/server_server.rs index f0fdf655..13e1a3fd 100644 --- a/src/server_server.rs +++ b/src/server_server.rs @@ -4,7 +4,7 @@ use log::error; use rocket::{get, options, post, put, response::content::Json, State}; use ruma_api::{ error::{FromHttpRequestError, FromHttpResponseError}, - Endpoint, Outgoing, + Endpoint, }; use ruma_client_api::error::{Error, ErrorKind}; use ruma_federation_api::{v1::get_server_version, v2::get_server_keys}; @@ -20,20 +20,11 @@ pub async fn send_request( data: &crate::Data, destination: String, request: T, -) -> Option<::Incoming> -where - // We need to duplicate Endpoint's where clauses because the compiler is not smart enough yet. - // See https://github.com/rust-lang/rust/issues/54149 - ::Incoming: TryFrom>, Error = FromHttpRequestError>, - ::Incoming: TryFrom< - http::Response>, - Error = FromHttpResponseError<::ResponseError>, - >, - T::Error: std::fmt::Debug, +) -> Option { let mut http_request: http::Request<_> = request.try_into().unwrap(); - *http_request.uri_mut() = ("https://".to_owned() + &destination.clone() + T::METADATA.path).parse().unwrap(); + *http_request.uri_mut() = format!("https://{}:8448{}", &destination.clone(), T::METADATA.path).parse().unwrap(); let mut request_map = serde_json::Map::new(); @@ -50,7 +41,7 @@ where let mut request_json = request_map.into(); ruma_signatures::sign_json(data.hostname(), data.keypair(), dbg!(&mut request_json)).unwrap(); - dbg!(&request_json); + println!("{}", &request_json); let signatures = request_json["signatures"] .as_object() @@ -101,7 +92,7 @@ where .into_iter() .collect(); Some( - ::Incoming::try_from( + ::try_from( dbg!(http_response.body(body)).unwrap(), ) .ok() From 720cc0cffce3363dee2aacbccc6fd53b76ac8397 Mon Sep 17 00:00:00 2001 From: timokoesters Date: Sat, 25 Apr 2020 11:47:32 +0200 Subject: [PATCH 07/12] feat: federated room directory --- src/client_server.rs | 22 ++++++--------- src/data.rs | 3 +- src/ruma_wrapper.rs | 13 ++------- src/server_server.rs | 67 +++++++++++++++++++------------------------- src/utils.rs | 4 +-- 5 files changed, 43 insertions(+), 66 deletions(-) diff --git a/src/client_server.rs b/src/client_server.rs index 2c88daac..dac5e47d 100644 --- a/src/client_server.rs +++ b/src/client_server.rs @@ -37,8 +37,8 @@ use ruma_events::{collections::only::Event as EduEvent, EventType}; use ruma_identifiers::{RoomId, UserId}; use serde_json::json; use std::{ - collections::{BTreeMap, HashMap}, - convert::{TryFrom, TryInto}, + collections::BTreeMap, + convert::TryInto, path::PathBuf, time::{Duration, SystemTime}, }; @@ -753,10 +753,12 @@ pub async fn get_public_rooms_filtered_route( }) .collect::>(); + chunk.sort_by(|l, r| r.num_joined_members.cmp(&l.num_joined_members)); + chunk.extend_from_slice( &server_server::send_request( &data, - "koesters.xyz".to_owned(), + "chat.privacytools.io".to_owned(), ruma_federation_api::v1::get_public_rooms::Request { limit: None, since: None, @@ -768,12 +770,10 @@ pub async fn get_public_rooms_filtered_route( .unwrap() .chunk .into_iter() - .map(|c| serde_json::from_str(&serde_json::to_string(dbg!(&c)).unwrap()).unwrap()) + .map(|c| serde_json::from_str(&serde_json::to_string(&c).unwrap()).unwrap()) .collect::>(), ); - chunk.sort_by(|l, r| r.num_joined_members.cmp(&l.num_joined_members)); - let total_room_count_estimate = (chunk.len() as u32).into(); MatrixResult(Ok(get_public_rooms_filtered::Response { @@ -910,10 +910,7 @@ pub fn sync_route( .unwrap_or(0); for room_id in joined_roomids { let pdus = data.pdus_since(&room_id, since); - let room_events = pdus - .into_iter() - .map(|pdu| pdu.to_room_event()) - .collect(); + let room_events = pdus.into_iter().map(|pdu| pdu.to_room_event()).collect(); let mut edus = data.roomlatests_since(&room_id, since); edus.extend_from_slice(&data.roomactives_in(&room_id)); @@ -945,10 +942,7 @@ pub fn sync_route( let left_roomids = data.rooms_left(body.user_id.as_ref().expect("user is authenticated")); for room_id in left_roomids { let pdus = data.pdus_since(&room_id, since); - let room_events = pdus - .into_iter() - .map(|pdu| pdu.to_room_event()) - .collect(); + let room_events = pdus.into_iter().map(|pdu| pdu.to_room_event()).collect(); let mut edus = data.roomlatests_since(&room_id, since); edus.extend_from_slice(&data.roomactives_in(&room_id)); diff --git a/src/data.rs b/src/data.rs index 864536f6..8096a081 100644 --- a/src/data.rs +++ b/src/data.rs @@ -458,7 +458,8 @@ impl Data { .expect("ruma's reference hashes are correct"); let mut pdu_json = serde_json::to_value(&pdu).unwrap(); - ruma_signatures::hash_and_sign_event(self.hostname(), self.keypair(), &mut pdu_json); + ruma_signatures::hash_and_sign_event(self.hostname(), self.keypair(), &mut pdu_json) + .unwrap(); self.pdu_leaves_replace(&room_id, &pdu.event_id); diff --git a/src/ruma_wrapper.rs b/src/ruma_wrapper.rs index 81294a45..1d3328b7 100644 --- a/src/ruma_wrapper.rs +++ b/src/ruma_wrapper.rs @@ -6,15 +6,9 @@ use rocket::{ Outcome::*, Request, State, }; -use ruma_api::{ - Endpoint -}; +use ruma_api::Endpoint; use ruma_identifiers::UserId; -use std::{ - convert::{TryInto}, - io::Cursor, - ops::Deref, -}; +use std::{convert::TryInto, io::Cursor, ops::Deref}; use tokio::io::AsyncReadExt; const MESSAGE_LIMIT: u64 = 65535; @@ -27,8 +21,7 @@ pub struct Ruma { pub json_body: serde_json::Value, } -impl<'a, T: Endpoint> FromData<'a> for Ruma -{ +impl<'a, T: Endpoint> FromData<'a> for Ruma { type Error = (); // TODO: Better error handling type Owned = Data; type Borrowed = Self::Owned; diff --git a/src/server_server.rs b/src/server_server.rs index 13e1a3fd..a62f23b7 100644 --- a/src/server_server.rs +++ b/src/server_server.rs @@ -1,18 +1,14 @@ -use crate::{utils, Data, MatrixResult, Ruma}; +use crate::{Data, MatrixResult}; use http::header::{HeaderValue, AUTHORIZATION}; use log::error; -use rocket::{get, options, post, put, response::content::Json, State}; -use ruma_api::{ - error::{FromHttpRequestError, FromHttpResponseError}, - Endpoint, -}; -use ruma_client_api::error::{Error, ErrorKind}; +use rocket::{get, post, put, response::content::Json, State}; +use ruma_api::Endpoint; +use ruma_client_api::error::Error; use ruma_federation_api::{v1::get_server_version, v2::get_server_keys}; use serde_json::json; use std::{ - collections::{BTreeMap, HashMap}, - convert::{TryFrom, TryInto}, - path::PathBuf, + collections::BTreeMap, + convert::TryFrom, time::{Duration, SystemTime}, }; @@ -20,28 +16,29 @@ pub async fn send_request( data: &crate::Data, destination: String, request: T, -) -> Option -{ +) -> Option { let mut http_request: http::Request<_> = request.try_into().unwrap(); - - *http_request.uri_mut() = format!("https://{}:8448{}", &destination.clone(), T::METADATA.path).parse().unwrap(); + + *http_request.uri_mut() = format!("https://{}:8448{}", &destination.clone(), T::METADATA.path) + .parse() + .unwrap(); let mut request_map = serde_json::Map::new(); if !http_request.body().is_empty() { - request_map.insert("content".to_owned(), - serde_json::to_value(http_request.body()).unwrap()); + request_map.insert( + "content".to_owned(), + serde_json::to_value(http_request.body()).unwrap(), + ); }; request_map.insert("method".to_owned(), T::METADATA.method.to_string().into()); request_map.insert("uri".to_owned(), T::METADATA.path.into()); request_map.insert("origin".to_owned(), data.hostname().into()); - request_map.insert("destination".to_owned(), destination.to_string().into()); - //request_map.insert("signatures".to_owned(), json!({})); + request_map.insert("destination".to_owned(), "privacytools.io".into()); let mut request_json = request_map.into(); - ruma_signatures::sign_json(data.hostname(), data.keypair(), dbg!(&mut request_json)).unwrap(); - println!("{}", &request_json); + ruma_signatures::sign_json(data.hostname(), data.keypair(), &mut request_json).unwrap(); let signatures = request_json["signatures"] .as_object() @@ -67,10 +64,7 @@ pub async fn send_request( ); } - let reqwest_response = data - .reqwest_client() - .execute(dbg!(http_request.into())) - .await; + let reqwest_response = data.reqwest_client().execute(http_request.into()).await; // Because reqwest::Response -> http::Response is complicated: match reqwest_response { @@ -92,15 +86,13 @@ pub async fn send_request( .into_iter() .collect(); Some( - ::try_from( - dbg!(http_response.body(body)).unwrap(), - ) - .ok() - .unwrap(), + ::try_from(http_response.body(body).unwrap()) + .ok() + .unwrap(), ) } Err(e) => { - println!("ERROR: {}", e); + error!("{}", e); None } } @@ -114,7 +106,7 @@ pub fn well_known_server(data: State) -> Json { } #[get("/_matrix/federation/v1/version")] -pub fn get_server_version(data: State) -> MatrixResult { +pub fn get_server_version() -> MatrixResult { MatrixResult(Ok(get_server_version::Response { server: get_server_version::Server { name: Some("Conduit".to_owned()), @@ -123,8 +115,8 @@ pub fn get_server_version(data: State) -> MatrixResult, body: Ruma) -> Json { +#[get("/_matrix/key/v2/server")] +pub fn get_server_keys(data: State) -> Json { let mut verify_keys = BTreeMap::new(); verify_keys.insert( format!("ed25519:{}", data.keypair().version()), @@ -138,21 +130,20 @@ pub fn get_server_keys(data: State, body: Ruma) verify_keys, old_verify_keys: BTreeMap::new(), signatures: BTreeMap::new(), - valid_until_ts: SystemTime::now() + Duration::from_secs(60 * 60 * 24), + valid_until_ts: SystemTime::now() + Duration::from_secs(60 * 2), }) .unwrap() .body(), ) .unwrap(); ruma_signatures::sign_json(data.hostname(), data.keypair(), &mut response).unwrap(); - Json(dbg!(response.to_string())) + Json(response.to_string()) } -#[get("/_matrix/key/v2/server/<_key_id>", data = "")] +#[get("/_matrix/key/v2/server/<_key_id>")] pub fn get_server_keys_deprecated( data: State, - body: Ruma, _key_id: String, ) -> Json { - get_server_keys(data, body) + get_server_keys(data) } diff --git a/src/utils.rs b/src/utils.rs index 07a99f1b..a3600360 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -27,10 +27,8 @@ pub fn increment(old: Option<&[u8]>) -> Option> { pub fn generate_keypair(old: Option<&[u8]>) -> Option> { Some( - /* old.map(|s| s.to_vec()) - .unwrap_or_else(|| */ - ruma_signatures::Ed25519KeyPair::generate().unwrap(), + .unwrap_or_else(|| ruma_signatures::Ed25519KeyPair::generate().unwrap()), ) } From 4cc0a070929bb714429a9df3ee501d84fce65a9d Mon Sep 17 00:00:00 2001 From: timokoesters Date: Sun, 26 Apr 2020 22:39:15 +0200 Subject: [PATCH 08/12] feat: user renaming --- src/client_server.rs | 67 ++++++++++++++++++++++++-------------------- src/data.rs | 37 +++++++++++++++++++++--- src/main.rs | 2 +- src/ruma_wrapper.rs | 8 ++++++ src/server_server.rs | 26 +++++++++++++---- 5 files changed, 98 insertions(+), 42 deletions(-) diff --git a/src/client_server.rs b/src/client_server.rs index dac5e47d..873d3ec4 100644 --- a/src/client_server.rs +++ b/src/client_server.rs @@ -38,7 +38,7 @@ use ruma_identifiers::{RoomId, UserId}; use serde_json::json; use std::{ collections::BTreeMap, - convert::TryInto, + convert::{TryFrom, TryInto}, path::PathBuf, time::{Duration, SystemTime}, }; @@ -325,8 +325,7 @@ pub fn set_displayname_route( if displayname == "" { data.displayname_remove(&user_id); } else { - data.displayname_set(&user_id, body.displayname.clone()); - // TODO send a new m.room.member join event with the updated displayname + data.displayname_set(&user_id, displayname.clone()); // TODO send a new m.presence event with the updated displayname } } @@ -610,26 +609,34 @@ pub fn create_room_route( MatrixResult(Ok(create_room::Response { room_id })) } -#[get("/_matrix/client/r0/directory/room/")] -pub fn get_alias_route(room_alias: String) -> MatrixResult { +#[get("/_matrix/client/r0/directory/room/<_room_alias>", data = "")] +pub fn get_alias_route( + data: State, + body: Ruma, + _room_alias: String, +) -> MatrixResult { // TODO - let room_id = match &*room_alias { - "#room:localhost" => "!xclkjvdlfj:localhost", - _ => { - debug!("Room not found."); - return MatrixResult(Err(Error { - kind: ErrorKind::NotFound, - message: "Room not found.".to_owned(), - status_code: http::StatusCode::NOT_FOUND, - })); + let room_id = if body.room_alias.server_name() == data.hostname() { + match body.room_alias.alias() { + "conduit" => "!lgOCCXQKtXOAPlAlG5:conduit.rs", + _ => { + debug!("Room alias not found."); + return MatrixResult(Err(Error { + kind: ErrorKind::NotFound, + message: "Room not found.".to_owned(), + status_code: http::StatusCode::NOT_FOUND, + })); + } } + } else { + todo!("ask remote server"); } .try_into() .unwrap(); MatrixResult(Ok(get_alias::Response { room_id, - servers: vec!["localhost".to_owned()], + servers: vec!["conduit.rs".to_owned()], })) } @@ -661,21 +668,19 @@ pub fn join_room_by_id_or_alias_route( body: Ruma, _room_id_or_alias: String, ) -> MatrixResult { - let room_id = if body.room_id_or_alias.is_room_alias_id() { - match body.room_id_or_alias.as_ref() { - "#room:localhost" => "!xclkjvdlfj:localhost".try_into().unwrap(), - _ => { - debug!("Room not found."); - return MatrixResult(Err(Error { - kind: ErrorKind::NotFound, - message: "Room not found.".to_owned(), - status_code: http::StatusCode::NOT_FOUND, - })); - } + let room_id = match RoomId::try_from(body.room_id_or_alias.clone()) { + Ok(room_id) => room_id, + Err(room_alias) => if room_alias.server_name() == data.hostname() { + return MatrixResult(Err(Error { + kind: ErrorKind::NotFound, + message: "Room alias not found.".to_owned(), + status_code: http::StatusCode::NOT_FOUND, + })); + } else { + // Ask creator server of the room to join TODO ask someone else when not available + //server_server::send_request(data, destination, request) + todo!(); } - } else { - todo!(); - //body.room_id_or_alias.try_into().unwrap() }; if data.room_join( @@ -758,9 +763,9 @@ pub async fn get_public_rooms_filtered_route( chunk.extend_from_slice( &server_server::send_request( &data, - "chat.privacytools.io".to_owned(), + "privacytools.io".to_owned(), ruma_federation_api::v1::get_public_rooms::Request { - limit: None, + limit: Some(20_u32.into()), since: None, include_all_networks: None, third_party_instance_id: None, diff --git a/src/data.rs b/src/data.rs index 8096a081..88facca3 100644 --- a/src/data.rs +++ b/src/data.rs @@ -91,11 +91,21 @@ impl Data { } /// Set a new displayname. - pub fn displayname_set(&self, user_id: &UserId, displayname: Option) { + pub fn displayname_set(&self, user_id: &UserId, displayname: String) { self.db .userid_displayname - .insert(user_id.to_string(), &*displayname.unwrap_or_default()) + .insert(user_id.to_string(), &*displayname) .unwrap(); + for room_id in self.rooms_joined(user_id) { + self.pdu_append( + room_id.clone(), + user_id.clone(), + EventType::RoomMember, + json!({"membership": "join", "displayname": displayname}), + None, + Some(user_id.to_string()), + ); + } } /// Get a the displayname of a user. @@ -200,11 +210,19 @@ impl Data { room_id.to_string().as_bytes().into(), ); + let mut content = json!({"membership": "join"}); + if let Some(displayname) = self.displayname_get(user_id) { + content + .as_object_mut() + .unwrap() + .insert("displayname".to_owned(), displayname.into()); + } + self.pdu_append( room_id.clone(), user_id.clone(), EventType::RoomMember, - json!({"membership": "join"}), + content, None, Some(user_id.to_string()), ); @@ -429,6 +447,17 @@ impl Data { .unwrap_or(0_u64) + 1; + let mut unsigned = unsigned.unwrap_or_default(); + // TODO: Optimize this to not load the whole room state? + if let Some(state_key) = &state_key { + if let Some(prev_pdu) = self + .room_state(&room_id) + .get(&(event_type.clone(), state_key.clone())) + { + unsigned.insert("prev_content".to_owned(), prev_pdu.content.clone()); + } + } + let mut pdu = PduEvent { event_id: EventId::try_from("$thiswillbefilledinlater").unwrap(), room_id: room_id.clone(), @@ -442,7 +471,7 @@ impl Data { depth: depth.try_into().unwrap(), auth_events: Vec::new(), redacts: None, - unsigned: unsigned.unwrap_or_default(), + unsigned, hashes: ruma_federation_api::EventHash { sha256: "aaa".to_owned(), }, diff --git a/src/main.rs b/src/main.rs index 0794fcfe..324e23fa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -76,7 +76,7 @@ fn main() { } pretty_env_logger::init(); - let data = Data::load_or_create("matrixtesting.koesters.xyz:14004"); + let data = Data::load_or_create("conduit.rs"); //data.debug(); setup_rocket(data).launch().unwrap(); diff --git a/src/ruma_wrapper.rs b/src/ruma_wrapper.rs index 1d3328b7..eec9d84b 100644 --- a/src/ruma_wrapper.rs +++ b/src/ruma_wrapper.rs @@ -164,3 +164,11 @@ where } } } + +impl Deref for MatrixResult { + type Target = Result; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} diff --git a/src/server_server.rs b/src/server_server.rs index a62f23b7..93c3c22d 100644 --- a/src/server_server.rs +++ b/src/server_server.rs @@ -12,6 +12,22 @@ use std::{ time::{Duration, SystemTime}, }; +pub async fn request_well_known(data: &crate::Data, destination: &str) -> Option { + let body: serde_json::Value = serde_json::from_str(&data + .reqwest_client() + .get(&format!( + "https://{}/.well-known/matrix/server", + destination + )) + .send() + .await + .ok()? + .text() + .await + .ok()?).ok()?; + Some(body.get("m.server")?.as_str()?.to_owned()) +} + pub async fn send_request( data: &crate::Data, destination: String, @@ -19,7 +35,8 @@ pub async fn send_request( ) -> Option { let mut http_request: http::Request<_> = request.try_into().unwrap(); - *http_request.uri_mut() = format!("https://{}:8448{}", &destination.clone(), T::METADATA.path) + let actual_destination = "https://".to_owned() + &request_well_known(data, &destination).await.unwrap_or(destination.clone() + ":8448"); + *http_request.uri_mut() = (actual_destination + T::METADATA.path) .parse() .unwrap(); @@ -35,7 +52,7 @@ pub async fn send_request( request_map.insert("method".to_owned(), T::METADATA.method.to_string().into()); request_map.insert("uri".to_owned(), T::METADATA.path.into()); request_map.insert("origin".to_owned(), data.hostname().into()); - request_map.insert("destination".to_owned(), "privacytools.io".into()); + request_map.insert("destination".to_owned(), destination.into()); let mut request_json = request_map.into(); ruma_signatures::sign_json(data.hostname(), data.keypair(), &mut request_json).unwrap(); @@ -141,9 +158,6 @@ pub fn get_server_keys(data: State) -> Json { } #[get("/_matrix/key/v2/server/<_key_id>")] -pub fn get_server_keys_deprecated( - data: State, - _key_id: String, -) -> Json { +pub fn get_server_keys_deprecated(data: State, _key_id: String) -> Json { get_server_keys(data) } From 23cb550d00ca41eb0447e3e84b5e9e2ef1f88047 Mon Sep 17 00:00:00 2001 From: timokoesters Date: Tue, 28 Apr 2020 19:56:34 +0200 Subject: [PATCH 09/12] forget rooms, load history --- src/client_server.rs | 59 ++++++++++++++++++++++++++++++++++++++------ src/data.rs | 39 +++++++++++++++++++++++++++++ src/main.rs | 2 ++ 3 files changed, 92 insertions(+), 8 deletions(-) diff --git a/src/client_server.rs b/src/client_server.rs index 873d3ec4..80bf61ce 100644 --- a/src/client_server.rs +++ b/src/client_server.rs @@ -13,9 +13,9 @@ use ruma_client_api::{ filter::{self, create_filter, get_filter}, keys::{get_keys, upload_keys}, membership::{ - get_member_events, invite_user, join_room_by_id, join_room_by_id_or_alias, leave_room, + get_member_events, invite_user, join_room_by_id, join_room_by_id_or_alias, forget_room, leave_room, }, - message::create_message_event, + message::{get_message_events, create_message_event}, presence::set_presence, profile::{ get_avatar_url, get_display_name, get_profile, set_avatar_url, set_display_name, @@ -222,10 +222,13 @@ pub fn get_capabilities_route( body: Ruma, ) -> MatrixResult { // TODO + //let mut available = BTreeMap::new(); + //available.insert("5".to_owned(), get_capabilities::RoomVersionStability::Unstable); + MatrixResult(Ok(get_capabilities::Response { capabilities: get_capabilities::Capabilities { change_password: None, - room_versions: None, + room_versions: None, //Some(get_capabilities::RoomVersionsCapability { default: "5".to_owned(), available }), custom_capabilities: BTreeMap::new(), }, })) @@ -708,6 +711,17 @@ pub fn leave_room_route( MatrixResult(Ok(leave_room::Response)) } +#[post("/_matrix/client/r0/rooms/<_room_id>/forget", data = "")] +pub fn forget_room_route( + data: State, + body: Ruma, + _room_id: String, +) -> MatrixResult { + let user_id = body.user_id.clone().expect("user is authenticated"); + data.room_forget(&body.room_id, &user_id); + MatrixResult(Ok(forget_room::Response)) +} + #[post("/_matrix/client/r0/rooms/<_room_id>/invite", data = "")] pub fn invite_user_route( data: State, @@ -903,7 +917,7 @@ pub fn sync_route( data: State, body: Ruma, ) -> MatrixResult { - std::thread::sleep(Duration::from_millis(200)); + std::thread::sleep(Duration::from_millis(300)); let next_batch = data.last_pdu_index().to_string(); let mut joined_rooms = BTreeMap::new(); @@ -915,7 +929,8 @@ pub fn sync_route( .unwrap_or(0); for room_id in joined_roomids { let pdus = data.pdus_since(&room_id, since); - let room_events = pdus.into_iter().map(|pdu| pdu.to_room_event()).collect(); + let room_events = pdus.into_iter().map(|pdu| pdu.to_room_event()).collect::>(); + let is_first_pdu = data.room_pdu_first(&room_id, since); let mut edus = data.roomlatests_since(&room_id, since); edus.extend_from_slice(&data.roomactives_in(&room_id)); @@ -933,8 +948,8 @@ pub fn sync_route( notification_count: None, }, timeline: sync_events::Timeline { - limited: Some(false), - prev_batch: Some("".to_owned()), + limited: None, + prev_batch: Some(since.to_string()), events: room_events, }, state: sync_events::State { events: Vec::new() }, @@ -957,7 +972,7 @@ pub fn sync_route( account_data: sync_events::AccountData { events: Vec::new() }, timeline: sync_events::Timeline { limited: Some(false), - prev_batch: Some("".to_owned()), + prev_batch: Some(next_batch.clone()), events: room_events, }, state: sync_events::State { events: Vec::new() }, @@ -995,6 +1010,34 @@ pub fn sync_route( })) } +#[get("/_matrix/client/r0/rooms/<_room_id>/messages", data = "")] +pub fn get_message_events_route( + data: State, + body: Ruma, + _room_id: String) -> MatrixResult { + if let get_message_events::Direction::Forward = body.dir {todo!();} + + if let Ok(from) = body + .from + .clone() + .parse() { + let pdus = data.pdus_until(&body.room_id, from); + let room_events = pdus.into_iter().map(|pdu| pdu.to_room_event()).collect::>(); + MatrixResult(Ok(get_message_events::Response { + start: body.from.clone(), + end: "".to_owned(), + chunk: room_events, + + })) + } else { + MatrixResult(Err(Error { + kind: ErrorKind::NotFound, + message: "Invalid from.".to_owned(), + status_code: http::StatusCode::BAD_REQUEST, + })) + } +} + #[get("/_matrix/client/r0/voip/turnServer")] pub fn turn_server_route() -> MatrixResult { // TODO diff --git a/src/data.rs b/src/data.rs index 88facca3..6e09d073 100644 --- a/src/data.rs +++ b/src/data.rs @@ -338,6 +338,13 @@ impl Data { ); } + pub fn room_forget(&self, room_id: &RoomId, user_id: &UserId) { + self.db.userid_leftroomids.remove_value( + user_id.to_string().as_bytes(), + room_id.to_string().as_bytes().into(), + ); + } + pub fn room_invite(&self, sender: &UserId, room_id: &RoomId, user_id: &UserId) { self.pdu_append( room_id.clone(), @@ -375,6 +382,15 @@ impl Data { .collect() } + pub fn room_pdu_first(&self, room_id: &RoomId, pdu_index: u64) -> bool { + let mut pdu_id = vec![b'd']; + pdu_id.extend_from_slice(room_id.to_string().as_bytes()); + pdu_id.push(0xff); + pdu_id.extend_from_slice(&pdu_index.to_be_bytes()); + + self.db.pduid_pdu.get_lt(&pdu_id).unwrap().is_none() + } + pub fn pdu_get(&self, event_id: &EventId) -> Option { self.db .eventid_pduid @@ -577,6 +593,29 @@ impl Data { pdus } + pub fn pdus_until(&self, room_id: &RoomId, until: u64) -> Vec { + let mut pdus = Vec::new(); + + // Create the first part of the full pdu id + let mut prefix = vec![b'd']; + prefix.extend_from_slice(room_id.to_string().as_bytes()); + prefix.push(0xff); // Add delimiter so we don't find rooms starting with the same id + + let mut current = prefix.clone(); + current.extend_from_slice(&until.to_be_bytes()); + + while let Some((key, value)) = self.db.pduid_pdu.get_lt(¤t).unwrap() { + if key.starts_with(&prefix) { + current = key.to_vec(); + pdus.push(serde_json::from_slice(&value).expect("pdu in db is valid")); + } else { + break; + } + } + + pdus + } + pub fn roomlatest_update(&self, user_id: &UserId, room_id: &RoomId, event: EduEvent) { let mut prefix = room_id.to_string().as_bytes().to_vec(); prefix.push(0xff); diff --git a/src/main.rs b/src/main.rs index 324e23fa..13998023 100644 --- a/src/main.rs +++ b/src/main.rs @@ -48,6 +48,7 @@ fn setup_rocket(data: Data) -> rocket::Rocket { client_server::join_room_by_id_route, client_server::join_room_by_id_or_alias_route, client_server::leave_room_route, + client_server::forget_room_route, client_server::invite_user_route, client_server::get_public_rooms_filtered_route, client_server::search_users_route, @@ -57,6 +58,7 @@ fn setup_rocket(data: Data) -> rocket::Rocket { client_server::create_state_event_for_key_route, client_server::create_state_event_for_empty_key_route, client_server::sync_route, + client_server::get_message_events_route, client_server::turn_server_route, client_server::publicised_groups_route, client_server::options_route, From 1cdf30f38cab3bc0051fa1550ec93db837caf889 Mon Sep 17 00:00:00 2001 From: timokoesters Date: Wed, 29 Apr 2020 08:48:56 +0200 Subject: [PATCH 10/12] Add hostname to Rocket.toml config --- Rocket.toml | 3 ++- src/main.rs | 16 +++++++++------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/Rocket.toml b/Rocket.toml index 99c136d7..5db4a3d6 100644 --- a/Rocket.toml +++ b/Rocket.toml @@ -1,6 +1,7 @@ [global] -address = "0.0.0.0" +hostname = "conduit.rs" port = 14004 +address = "0.0.0.0" [global.tls] certs = "/etc/letsencrypt/live/matrixtesting.koesters.xyz/fullchain.pem" diff --git a/src/main.rs b/src/main.rs index 13998023..2f0a2f07 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,9 +16,9 @@ pub use database::Database; pub use pdu::PduEvent; pub use ruma_wrapper::{MatrixResult, Ruma}; -use rocket::routes; +use rocket::{fairing::AdHoc, routes}; -fn setup_rocket(data: Data) -> rocket::Rocket { +fn setup_rocket() -> rocket::Rocket { rocket::ignite() .mount( "/", @@ -68,7 +68,12 @@ fn setup_rocket(data: Data) -> rocket::Rocket { server_server::get_server_keys_deprecated, ], ) - .manage(data) + .attach(AdHoc::on_attach("Config", |rocket| { + let hostname = rocket.config().get_str("hostname").unwrap_or("localhost"); + let data = Data::load_or_create(&hostname); + + Ok(rocket.manage(data)) + })) } fn main() { @@ -78,8 +83,5 @@ fn main() { } pretty_env_logger::init(); - let data = Data::load_or_create("conduit.rs"); - //data.debug(); - - setup_rocket(data).launch().unwrap(); + setup_rocket().launch().unwrap(); } From fd1aea7e3630a4f9016612e57ecc19fcf8ed86d3 Mon Sep 17 00:00:00 2001 From: timokoesters Date: Tue, 28 Apr 2020 20:03:14 +0200 Subject: [PATCH 11/12] Update dependencies --- Cargo.lock | 64 ++++++++++++++++++++++---------------------- Cargo.toml | 10 +++---- src/client_server.rs | 9 +++---- src/server_server.rs | 4 +-- 4 files changed, 43 insertions(+), 44 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0b659c33..d205cd24 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -35,7 +35,7 @@ checksum = "da71fef07bc806586090247e971229289f64c210a278ee5ae419314eb386b31d" dependencies = [ "proc-macro2 1.0.10", "quote 1.0.3", - "syn 1.0.17", + "syn 1.0.18", ] [[package]] @@ -258,7 +258,7 @@ dependencies = [ "bitflags", "proc-macro2 1.0.10", "quote 1.0.3", - "syn 1.0.17", + "syn 1.0.18", ] [[package]] @@ -415,7 +415,7 @@ dependencies = [ "proc-macro-hack", "proc-macro2 1.0.10", "quote 1.0.3", - "syn 1.0.17", + "syn 1.0.18", ] [[package]] @@ -499,9 +499,9 @@ dependencies = [ [[package]] name = "hermit-abi" -version = "0.1.11" +version = "0.1.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8a0d737e0f947a1864e93d33fdef4af8445a00d1ed8dc0c8ddb73139ea6abf15" +checksum = "61565ff7aaace3525556587bd2dc31d4a07071957be715e63ce7b1eccf51a8f4" dependencies = [ "libc", ] @@ -625,9 +625,9 @@ dependencies = [ [[package]] name = "js_int" -version = "0.1.4" +version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f97dc0d13f3bf0369f00504ad806499490045d6f93524a6ead4081c380703a2f" +checksum = "77ab7bb370a788ad675863e035fd9bfa56a66a030a16a88ab80aeb6b18cbdf31" dependencies = [ "serde", ] @@ -904,7 +904,7 @@ checksum = "8988430ce790d8682672117bc06dda364c0be32d3abd738234f19f3240bad99a" dependencies = [ "proc-macro2 1.0.10", "quote 1.0.3", - "syn 1.0.17", + "syn 1.0.18", ] [[package]] @@ -1213,13 +1213,13 @@ checksum = "0f6b02a6a860a96e3c2081c8aea88b37b2918b53e539856b73aadde1908b65ad" dependencies = [ "proc-macro2 1.0.10", "quote 1.0.3", - "syn 1.0.17", + "syn 1.0.18", ] [[package]] name = "ruma-client-api" version = "0.8.0-rc.5" -source = "git+https://github.com/ruma/ruma-client-api.git#473cf2386178781688f12c538ee6419869d14a46" +source = "git+https://github.com/ruma/ruma-client-api.git#5a26c387646e17ba076e478d1e7b896b7e47137d" dependencies = [ "http", "js_int", @@ -1255,13 +1255,13 @@ checksum = "abd3cfe96c9887fe2eebfa2e5e7d3a4afff02c374874d4e718f46dab5fd3320d" dependencies = [ "proc-macro2 1.0.10", "quote 1.0.3", - "syn 1.0.17", + "syn 1.0.18", ] [[package]] name = "ruma-federation-api" version = "0.0.1" -source = "git+https://git.koesters.xyz/timo/ruma-federation-api.git#6f1c5a6a714d6be2f420f3832d31e214fe4fb229" +source = "git+https://github.com/ruma/ruma-federation-api.git#263f2ffc75be6542bd68161e446adf588505fb56" dependencies = [ "js_int", "ruma-api", @@ -1296,7 +1296,7 @@ dependencies = [ [[package]] name = "ruma-signatures" version = "0.6.0-dev.1" -source = "git+https://git.koesters.xyz/timo/ruma-signatures.git#e2cd0927c14133b8d49a8cd9fbd96f8af5c60f07" +source = "git+https://github.com/ruma/ruma-signatures.git#1ca545cba8dfd43e0fc8e3c18e1311fb73390a97" dependencies = [ "base64 0.12.0", "ring", @@ -1375,9 +1375,9 @@ dependencies = [ [[package]] name = "security-framework" -version = "0.4.2" +version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "572dfa3a0785509e7a44b5b4bebcf94d41ba34e9ed9eb9df722545c3b3c4144a" +checksum = "3f331b9025654145cd425b9ded0caf8f5ae0df80d418b326e2dc1c3dc5eb0620" dependencies = [ "bitflags", "core-foundation", @@ -1388,9 +1388,9 @@ dependencies = [ [[package]] name = "security-framework-sys" -version = "0.4.2" +version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ddb15a5fec93b7021b8a9e96009c5d8d51c15673569f7c0f6b7204e5b7b404f" +checksum = "17bf11d99252f512695eb468de5516e5cf75455521e69dfe343f3b74e4748405" dependencies = [ "core-foundation-sys", "libc", @@ -1413,14 +1413,14 @@ checksum = "9e549e3abf4fb8621bd1609f11dfc9f5e50320802273b12f3811a67e6716ea6c" dependencies = [ "proc-macro2 1.0.10", "quote 1.0.3", - "syn 1.0.17", + "syn 1.0.18", ] [[package]] name = "serde_json" -version = "1.0.51" +version = "1.0.52" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "da07b57ee2623368351e9a0488bb0b261322a15a6e0ae53e243cbdc0f4208da9" +checksum = "a7894c8ed05b7a3a279aeb79025fdec1d3158080b75b98a08faf2806bb799edd" dependencies = [ "itoa", "ryu", @@ -1473,9 +1473,9 @@ dependencies = [ [[package]] name = "smallvec" -version = "1.3.0" +version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "05720e22615919e4734f6a99ceae50d00226c3c5aca406e102ebc33298214e0a" +checksum = "c7cb5678e1615754284ec264d9bb5b4c27d2018577fd90ac0ceb578591ed5ee4" [[package]] name = "spin" @@ -1507,7 +1507,7 @@ dependencies = [ "heck", "proc-macro2 1.0.10", "quote 1.0.3", - "syn 1.0.17", + "syn 1.0.18", ] [[package]] @@ -1523,9 +1523,9 @@ dependencies = [ [[package]] name = "syn" -version = "1.0.17" +version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0df0eb663f387145cab623dea85b09c2c5b4b0aef44e945d928e682fce71bb03" +checksum = "410a7488c0a728c7ceb4ad59b9567eb4053d02e8cc7f5c0e0eeeb39518369213" dependencies = [ "proc-macro2 1.0.10", "quote 1.0.3", @@ -1576,9 +1576,9 @@ dependencies = [ [[package]] name = "tokio" -version = "0.2.18" +version = "0.2.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "34ef16d072d2b6dc8b4a56c70f5c5ced1a37752116f8e7c1e80c659aa7cb6713" +checksum = "7d9c43f1bb96970e153bcbae39a65e249ccb942bd9d36dbdf086024920417c9c" dependencies = [ "bytes", "fnv", @@ -1605,7 +1605,7 @@ checksum = "f0c3acc6aa564495a0f2e1d59fab677cd7f81a19994cfc7f3ad0e64301560389" dependencies = [ "proc-macro2 1.0.10", "quote 1.0.3", - "syn 1.0.17", + "syn 1.0.18", ] [[package]] @@ -1712,9 +1712,9 @@ checksum = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c" [[package]] name = "untrusted" -version = "0.7.0" +version = "0.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "60369ef7a31de49bcb3f6ca728d4ba7300d9a1658f94c727d4cab8c8d9f4aece" +checksum = "a156c684c91ea7d62626509bce3cb4e1d9ed5c4d978f7b4352658f96a4c26b4a" [[package]] name = "url" @@ -1785,7 +1785,7 @@ dependencies = [ "log", "proc-macro2 1.0.10", "quote 1.0.3", - "syn 1.0.17", + "syn 1.0.18", "wasm-bindgen-shared", ] @@ -1819,7 +1819,7 @@ checksum = "d68a5b36eef1be7868f668632863292e37739656a80fc4b9acec7b0bd35a4931" dependencies = [ "proc-macro2 1.0.10", "quote 1.0.3", - "syn 1.0.17", + "syn 1.0.18", "wasm-bindgen-backend", "wasm-bindgen-shared", ] diff --git a/Cargo.toml b/Cargo.toml index 9ae2a0cf..5ceb72f5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,16 +17,16 @@ ruma-client-api = { git = "https://github.com/ruma/ruma-client-api.git" } ruma-identifiers = { version = "0.16.0", features = ["rand"] } ruma-api = "0.16.0-rc.3" ruma-events = "0.21.0-beta.1" -ruma-signatures = { git = "https://git.koesters.xyz/timo/ruma-signatures.git" } -ruma-federation-api = { git = "https://git.koesters.xyz/timo/ruma-federation-api.git" } +ruma-signatures = { git = "https://github.com/ruma/ruma-signatures.git" } +ruma-federation-api = { git = "https://github.com/ruma/ruma-federation-api.git" } pretty_env_logger = "0.4.0" log = "0.4.8" sled = "0.31.0" directories = "2.0.2" -js_int = "0.1.4" -serde_json = "1.0.51" +js_int = "0.1.5" +serde_json = "1.0.52" serde = "1.0.106" -tokio = { version = "0.2.18", features = ["macros"] } +tokio = { version = "0.2.19", features = ["macros"] } rand = "0.7.3" rust-argon2 = "0.8.2" reqwest = "0.10.4" diff --git a/src/client_server.rs b/src/client_server.rs index 80bf61ce..cc6a1631 100644 --- a/src/client_server.rs +++ b/src/client_server.rs @@ -781,8 +781,7 @@ pub async fn get_public_rooms_filtered_route( ruma_federation_api::v1::get_public_rooms::Request { limit: Some(20_u32.into()), since: None, - include_all_networks: None, - third_party_instance_id: None, + room_network: ruma_federation_api::v1::get_public_rooms::RoomNetwork::Matrix, }, ) .await @@ -1024,10 +1023,10 @@ pub fn get_message_events_route( let pdus = data.pdus_until(&body.room_id, from); let room_events = pdus.into_iter().map(|pdu| pdu.to_room_event()).collect::>(); MatrixResult(Ok(get_message_events::Response { - start: body.from.clone(), - end: "".to_owned(), + start: Some(body.from.clone()), + end: None, chunk: room_events, - + state: Vec::new(), })) } else { MatrixResult(Err(Error { diff --git a/src/server_server.rs b/src/server_server.rs index 93c3c22d..da0b8a05 100644 --- a/src/server_server.rs +++ b/src/server_server.rs @@ -125,10 +125,10 @@ pub fn well_known_server(data: State) -> Json { #[get("/_matrix/federation/v1/version")] pub fn get_server_version() -> MatrixResult { MatrixResult(Ok(get_server_version::Response { - server: get_server_version::Server { + server: Some(get_server_version::Server { name: Some("Conduit".to_owned()), version: Some(env!("CARGO_PKG_VERSION").to_owned()), - }, + }), })) } From c769283953343d23a1b8811b713de13f60e8c028 Mon Sep 17 00:00:00 2001 From: timokoesters Date: Wed, 29 Apr 2020 10:08:55 +0200 Subject: [PATCH 12/12] Update readme --- README.md | 12 +++++++----- src/client_server.rs | 2 +- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 557c121c..4c9baf5b 100644 --- a/README.md +++ b/README.md @@ -20,17 +20,19 @@ A fast Matrix homeserver that's optimized for smaller, personal servers, instead - [x] Create room messages - [x] Sync room messages - [x] Join rooms, lookup room ids -- [x] Basic Riot web support -- [x] Riot room discovery -- [x] Riot read receipts +- [x] Riot web support +- [x] Room discovery +- [x] Read receipts - [x] Typing indications - [x] Invites, user search - [x] Password hashing +- [ ] Basic federation +- [ ] State resolution +- [ ] Permission system +- [ ] Notifications (push rules) - [ ] Riot presence - [ ] Proper room creation - [ ] Riot E2EE -- [ ] Basic federation -- [ ] State resolution #### How can I contribute? diff --git a/src/client_server.rs b/src/client_server.rs index cc6a1631..01032082 100644 --- a/src/client_server.rs +++ b/src/client_server.rs @@ -1058,7 +1058,7 @@ pub fn publicised_groups_route() -> MatrixResult } #[options("/<_segments..>")] -pub fn options_route(_segments: PathBuf) -> MatrixResult { +pub fn options_route(_segments: rocket::http::uri::Segments) -> MatrixResult { MatrixResult(Err(Error { kind: ErrorKind::NotFound, message: "This is the options route.".to_owned(),