From: AsamK Date: Sat, 5 Feb 2022 15:41:22 +0000 (+0100) Subject: Implement JSON-RPC client PoC X-Git-Tag: v0.10.4~6 X-Git-Url: https://git.nmode.ca/signal-cli/commitdiff_plain/abde122a35d85f5db956d652300d7a995d460863 Implement JSON-RPC client PoC --- diff --git a/client/.gitignore b/client/.gitignore new file mode 100644 index 00000000..b83d2226 --- /dev/null +++ b/client/.gitignore @@ -0,0 +1 @@ +/target/ diff --git a/client/Cargo.lock b/client/Cargo.lock new file mode 100644 index 00000000..77afe1dd --- /dev/null +++ b/client/Cargo.lock @@ -0,0 +1,910 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "aho-corasick" +version = "0.7.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e37cfd5e7657ada45f742d6e99ca5788580b5c529dc78faf11ece6dc702656f" +dependencies = [ + "memchr", +] + +[[package]] +name = "anyhow" +version = "1.0.54" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a99269dff3bc004caa411f38845c20303f1e393ca2bd6581576fa3a7f59577d" + +[[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi", + "libc", + "winapi", +] + +[[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[package]] +name = "bstr" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba3569f383e8f1598449f1a423e72e99569137b47740b1da11ef19af3d5c3223" +dependencies = [ + "memchr", +] + +[[package]] +name = "bytes" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4872d67bab6358e59559027aa3b9157c53d9358c51423c17554809a8858e0f8" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "clap" +version = "3.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5f1fea81f183005ced9e59cdb01737ef2423956dac5a6d731b06b2ecfaa3467" +dependencies = [ + "atty", + "bitflags", + "clap_derive", + "indexmap", + "lazy_static", + "os_str_bytes", + "strsim", + "termcolor", + "textwrap", +] + +[[package]] +name = "clap_derive" +version = "3.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5fd1122e63869df2cb309f449da1ad54a7c6dfeb7c7e6ccd8e0825d9eb93bb72" +dependencies = [ + "heck", + "proc-macro-error", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "convert_case" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6245d59a3e82a7fc217c5828a6692dbc6dfb63a0c8c90495621f7b9d79704a0e" + +[[package]] +name = "derive_more" +version = "0.99.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4fb810d30a7c1953f91334de7244731fc3f3c10d7fe163338a35b9f640960321" +dependencies = [ + "convert_case", + "proc-macro2", + "quote", + "rustc_version", + "syn", +] + +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + +[[package]] +name = "futures" +version = "0.1.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a471a38ef8ed83cd6e40aa59c1ffe17db6855c18e3604d9c4ed8c08ebc28678" + +[[package]] +name = "futures" +version = "0.3.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f73fe65f54d1e12b726f517d3e2135ca3125a437b6d998caf1962961f7172d9e" +dependencies = [ + "futures-channel", + "futures-core", + "futures-executor", + "futures-io", + "futures-sink", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-channel" +version = "0.3.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3083ce4b914124575708913bca19bfe887522d6e2e6d0952943f5eac4a74010" +dependencies = [ + "futures-core", + "futures-sink", +] + +[[package]] +name = "futures-core" +version = "0.3.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c09fd04b7e4073ac7156a9539b57a484a8ea920f79c7c675d05d289ab6110d3" + +[[package]] +name = "futures-executor" +version = "0.3.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9420b90cfa29e327d0429f19be13e7ddb68fa1cccb09d65e5706b8c7a749b8a6" +dependencies = [ + "futures-core", + "futures-task", + "futures-util", + "num_cpus", +] + +[[package]] +name = "futures-io" +version = "0.3.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc4045962a5a5e935ee2fdedaa4e08284547402885ab326734432bed5d12966b" + +[[package]] +name = "futures-macro" +version = "0.3.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33c1e13800337f4d4d7a316bf45a567dbcb6ffe087f16424852d97e97a91f512" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "futures-sink" +version = "0.3.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "21163e139fa306126e6eedaf49ecdb4588f939600f0b1e770f4205ee4b7fa868" + +[[package]] +name = "futures-task" +version = "0.3.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57c66a976bf5909d801bbef33416c41372779507e7a6b3a5e25e4749c58f776a" + +[[package]] +name = "futures-util" +version = "0.3.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d8b7abd5d659d9b90c8cba917f6ec750a74e2dc23902ef9cd4cc8c8b22e6036a" +dependencies = [ + "futures 0.1.31", + "futures-channel", + "futures-core", + "futures-io", + "futures-macro", + "futures-sink", + "futures-task", + "memchr", + "pin-project-lite", + "pin-utils", + "slab", +] + +[[package]] +name = "getrandom" +version = "0.1.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8fc3cb4d91f53b50155bdcfd23f6a4c39ae1969c2ae85982b135750cccaf5fce" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "globset" +version = "0.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "10463d9ff00a2a068db14231982f5132edebad0d7660cd956a1c30292dbcbfbd" +dependencies = [ + "aho-corasick", + "bstr", + "fnv", + "log", + "regex", +] + +[[package]] +name = "hashbrown" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e" + +[[package]] +name = "heck" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2540771e65fc8cb83cd6e8a237f70c319bd5c29f78ed1084ba5d50eeac86f7f9" + +[[package]] +name = "hermit-abi" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" +dependencies = [ + "libc", +] + +[[package]] +name = "indexmap" +version = "1.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282a6247722caba404c065016bbfa522806e51714c34f5dfc3e4a3a46fcb4223" +dependencies = [ + "autocfg", + "hashbrown", +] + +[[package]] +name = "instant" +version = "0.1.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a5bbe824c507c5da5956355e86a746d82e0e1464f65d862cc5e71da70e94b2c" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "itoa" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1aab8fc367588b89dcee83ab0fd66b72b50b72fa1904d7095045ace2b0c81c35" + +[[package]] +name = "jsonrpc-client-transports" +version = "18.0.0" +source = "git+https://github.com/AsamK/jsonrpc?branch=client_subscribe_named_params#8a68f95e202943fd338fd0c14da08c2dc8f6e6a6" +dependencies = [ + "derive_more", + "futures 0.3.21", + "jsonrpc-core 18.0.0 (git+https://github.com/AsamK/jsonrpc?branch=client_subscribe_named_params)", + "jsonrpc-pubsub", + "jsonrpc-server-utils 18.0.0 (git+https://github.com/AsamK/jsonrpc?branch=client_subscribe_named_params)", + "log", + "parity-tokio-ipc", + "serde", + "serde_json", + "tokio", +] + +[[package]] +name = "jsonrpc-core" +version = "18.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "14f7f76aef2d054868398427f6c54943cf3d1caa9a7ec7d0c38d69df97a965eb" +dependencies = [ + "futures 0.3.21", + "futures-executor", + "futures-util", + "log", + "serde", + "serde_derive", + "serde_json", +] + +[[package]] +name = "jsonrpc-core" +version = "18.0.0" +source = "git+https://github.com/AsamK/jsonrpc?branch=client_subscribe_named_params#8a68f95e202943fd338fd0c14da08c2dc8f6e6a6" +dependencies = [ + "futures 0.3.21", + "futures-executor", + "futures-util", + "log", + "serde", + "serde_derive", + "serde_json", +] + +[[package]] +name = "jsonrpc-core-client" +version = "18.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b51da17abecbdab3e3d4f26b01c5ec075e88d3abe3ab3b05dc9aa69392764ec0" +dependencies = [ + "futures 0.3.21", + "jsonrpc-client-transports", +] + +[[package]] +name = "jsonrpc-derive" +version = "18.0.0" +source = "git+https://github.com/AsamK/jsonrpc?branch=client_subscribe_named_params#8a68f95e202943fd338fd0c14da08c2dc8f6e6a6" +dependencies = [ + "proc-macro-crate", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "jsonrpc-pubsub" +version = "18.0.0" +source = "git+https://github.com/AsamK/jsonrpc?branch=client_subscribe_named_params#8a68f95e202943fd338fd0c14da08c2dc8f6e6a6" +dependencies = [ + "futures 0.3.21", + "jsonrpc-core 18.0.0 (git+https://github.com/AsamK/jsonrpc?branch=client_subscribe_named_params)", + "lazy_static", + "log", + "parking_lot", + "rand", + "serde", +] + +[[package]] +name = "jsonrpc-server-utils" +version = "18.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa4fdea130485b572c39a460d50888beb00afb3e35de23ccd7fad8ff19f0e0d4" +dependencies = [ + "bytes", + "futures 0.3.21", + "globset", + "jsonrpc-core 18.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static", + "log", + "tokio", + "tokio-stream", + "tokio-util", + "unicase", +] + +[[package]] +name = "jsonrpc-server-utils" +version = "18.0.0" +source = "git+https://github.com/AsamK/jsonrpc?branch=client_subscribe_named_params#8a68f95e202943fd338fd0c14da08c2dc8f6e6a6" +dependencies = [ + "bytes", + "futures 0.3.21", + "globset", + "jsonrpc-core 18.0.0 (git+https://github.com/AsamK/jsonrpc?branch=client_subscribe_named_params)", + "lazy_static", + "log", + "tokio", + "tokio-stream", + "tokio-util", + "unicase", +] + +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + +[[package]] +name = "libc" +version = "0.2.119" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bf2e165bb3457c8e098ea76f3e3bc9db55f87aa90d52d0e6be741470916aaa4" + +[[package]] +name = "lock_api" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "88943dd7ef4a2e5a4bfa2753aaab3013e34ce2533d1996fb18ef591e315e2b3b" +dependencies = [ + "scopeguard", +] + +[[package]] +name = "log" +version = "0.4.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "51b9bbe6c47d51fc3e1a9b945965946b4c44142ab8792c50835a980d362c2710" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "memchr" +version = "2.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a" + +[[package]] +name = "mio" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba272f85fa0b41fc91872be579b3bbe0f56b792aa361a380eb669469f68dafb2" +dependencies = [ + "libc", + "log", + "miow", + "ntapi", + "winapi", +] + +[[package]] +name = "miow" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9f1c5b025cda876f66ef43a113f91ebc9f4ccef34843000e0adf6ebbab84e21" +dependencies = [ + "winapi", +] + +[[package]] +name = "ntapi" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c28774a7fd2fbb4f0babd8237ce554b73af68021b5f695a3cebd6c59bac0980f" +dependencies = [ + "winapi", +] + +[[package]] +name = "num_cpus" +version = "1.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "19e64526ebdee182341572e50e9ad03965aa510cd94427a4549448f285e957a1" +dependencies = [ + "hermit-abi", + "libc", +] + +[[package]] +name = "os_str_bytes" +version = "6.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e22443d1643a904602595ba1cd8f7d896afe56d26712531c5ff73a15b2fbf64" +dependencies = [ + "memchr", +] + +[[package]] +name = "parity-tokio-ipc" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9981e32fb75e004cc148f5fb70342f393830e0a4aa62e3cc93b50976218d42b6" +dependencies = [ + "futures 0.3.21", + "libc", + "log", + "rand", + "tokio", + "winapi", +] + +[[package]] +name = "parking_lot" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7d17b78036a60663b797adeaee46f5c9dfebb86948d1255007a1d6be0271ff99" +dependencies = [ + "instant", + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d76e8e1493bcac0d2766c42737f34458f1c8c50c0d23bcb24ea953affb273216" +dependencies = [ + "cfg-if", + "instant", + "libc", + "redox_syscall", + "smallvec", + "winapi", +] + +[[package]] +name = "pin-project-lite" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e280fbe77cc62c91527259e9442153f4688736748d24660126286329742b4c6c" + +[[package]] +name = "pin-utils" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" + +[[package]] +name = "ppv-lite86" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872" + +[[package]] +name = "proc-macro-crate" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d6ea3c4595b96363c13943497db34af4460fb474a95c43f4446ad341b8c9785" +dependencies = [ + "toml", +] + +[[package]] +name = "proc-macro-error" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" +dependencies = [ + "proc-macro-error-attr", + "proc-macro2", + "quote", + "syn", + "version_check", +] + +[[package]] +name = "proc-macro-error-attr" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" +dependencies = [ + "proc-macro2", + "quote", + "version_check", +] + +[[package]] +name = "proc-macro2" +version = "1.0.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c7342d5883fbccae1cc37a2353b09c87c9b0f3afd73f5fb9bba687a1f733b029" +dependencies = [ + "unicode-xid", +] + +[[package]] +name = "quote" +version = "1.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "864d3e96a899863136fc6e99f3d7cae289dafe43bf2c5ac19b70df7210c0a145" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rand" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03" +dependencies = [ + "getrandom", + "libc", + "rand_chacha", + "rand_core", + "rand_hc", +] + +[[package]] +name = "rand_chacha" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f4c8ed856279c9737206bf725bf36935d8666ead7aa69b52be55af369d193402" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19" +dependencies = [ + "getrandom", +] + +[[package]] +name = "rand_hc" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c" +dependencies = [ + "rand_core", +] + +[[package]] +name = "redox_syscall" +version = "0.2.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8383f39639269cde97d255a32bdb68c047337295414940c68bdd30c2e13203ff" +dependencies = [ + "bitflags", +] + +[[package]] +name = "regex" +version = "1.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d07a8629359eb56f1e2fb1652bb04212c072a87ba68546a04065d525673ac461" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.6.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b" + +[[package]] +name = "rustc_version" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bfa0f585226d2e68097d4f95d113b15b83a82e819ab25717ec0590d9584ef366" +dependencies = [ + "semver", +] + +[[package]] +name = "ryu" +version = "1.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73b4b750c782965c211b42f022f59af1fbceabdd026623714f104152f1ec149f" + +[[package]] +name = "scopeguard" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" + +[[package]] +name = "semver" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0486718e92ec9a68fbed73bb5ef687d71103b142595b406835649bebd33f72c7" + +[[package]] +name = "serde" +version = "1.0.136" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce31e24b01e1e524df96f1c2fdd054405f8d7376249a5110886fb4b658484789" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.136" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08597e7152fcd306f41838ed3e37be9eaeed2b61c42e2117266a554fab4662f9" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.79" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e8d9fa5c3b304765ce1fd9c4c8a3de2c8db365a5b91be52f186efc675681d95" +dependencies = [ + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "signal-cli-client" +version = "0.0.1" +dependencies = [ + "anyhow", + "clap", + "jsonrpc-client-transports", + "jsonrpc-core 18.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "jsonrpc-core-client", + "jsonrpc-derive", + "jsonrpc-server-utils 18.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "log", + "serde", + "serde_json", + "tokio", +] + +[[package]] +name = "slab" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9def91fd1e018fe007022791f865d0ccc9b3a0d5001e01aabb8b40e46000afb5" + +[[package]] +name = "smallvec" +version = "1.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2dd574626839106c320a323308629dcb1acfc96e32a8cba364ddc61ac23ee83" + +[[package]] +name = "socket2" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "66d72b759436ae32898a2af0a14218dbf55efde3feeb170eb623637db85ee1e0" +dependencies = [ + "libc", + "winapi", +] + +[[package]] +name = "strsim" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" + +[[package]] +name = "syn" +version = "1.0.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a65b3f4ffa0092e9887669db0eae07941f023991ab58ea44da8fe8e2d511c6b" +dependencies = [ + "proc-macro2", + "quote", + "unicode-xid", +] + +[[package]] +name = "termcolor" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2dfed899f0eb03f32ee8c6a0aabdb8a7949659e3466561fc0adf54e26d88c5f4" +dependencies = [ + "winapi-util", +] + +[[package]] +name = "textwrap" +version = "0.14.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0066c8d12af8b5acd21e00547c3797fde4e8677254a7ee429176ccebbe93dd80" + +[[package]] +name = "tokio" +version = "1.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2af73ac49756f3f7c01172e34a23e5d0216f6c32333757c2c61feb2bbff5a5ee" +dependencies = [ + "bytes", + "libc", + "memchr", + "mio", + "num_cpus", + "pin-project-lite", + "socket2", + "tokio-macros", + "winapi", +] + +[[package]] +name = "tokio-macros" +version = "1.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b557f72f448c511a979e2564e55d74e6c4432fc96ff4f6241bc6bded342643b7" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tokio-stream" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50145484efff8818b5ccd256697f36863f587da82cf8b409c53adf1e840798e3" +dependencies = [ + "futures-core", + "pin-project-lite", + "tokio", +] + +[[package]] +name = "tokio-util" +version = "0.6.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e99e1983e5d376cd8eb4b66604d2e99e79f5bd988c3055891dcd8c9e2604cc0" +dependencies = [ + "bytes", + "futures-core", + "futures-sink", + "log", + "pin-project-lite", + "tokio", +] + +[[package]] +name = "toml" +version = "0.5.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a31142970826733df8241ef35dc040ef98c679ab14d7c3e54d827099b3acecaa" +dependencies = [ + "serde", +] + +[[package]] +name = "unicase" +version = "2.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50f37be617794602aabbeee0be4f259dc1778fabe05e2d67ee8f79326d5cb4f6" +dependencies = [ + "version_check", +] + +[[package]] +name = "unicode-xid" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" + +[[package]] +name = "version_check" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" + +[[package]] +name = "wasi" +version = "0.9.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-util" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" +dependencies = [ + "winapi", +] + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/client/Cargo.toml b/client/Cargo.toml new file mode 100644 index 00000000..e14d2d74 --- /dev/null +++ b/client/Cargo.toml @@ -0,0 +1,25 @@ +[package] +name = "signal-cli-client" +version = "0.0.1" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +anyhow = "1" +clap = { version = "3", features = ["cargo", "derive"] } +jsonrpc-core = "18" +jsonrpc-core-client = "18" +jsonrpc-client-transports = { version = "18", default-features = false, features = [ + "ipc", +] } +jsonrpc-derive = "18" +jsonrpc-server-utils = "18" +log = "0.4" +serde = "1" +serde_json = "1" +tokio = { version = "1", features = ["rt", "macros", "net"] } + +[patch.crates-io] +jsonrpc-client-transports = { git = "https://github.com/AsamK/jsonrpc", branch = "client_subscribe_named_params" } +jsonrpc-derive = { git = "https://github.com/AsamK/jsonrpc", branch = "client_subscribe_named_params" } diff --git a/client/src/cli.rs b/client/src/cli.rs new file mode 100644 index 00000000..bc6a3079 --- /dev/null +++ b/client/src/cli.rs @@ -0,0 +1,340 @@ +use clap::{crate_version, ArgEnum, Parser, Subcommand}; +use std::{ffi::OsString, net::SocketAddr}; + +/// JSON-RPC client for signal-cli +#[derive(Parser, Debug)] +#[clap(rename_all = "kebab-case", version=crate_version!())] +pub struct Cli { + /// Account to use (for daemon in multi-account mode) + #[clap(short = 'a', long)] + pub account: Option, + + /// TCP host and port of signal-cli daemon + #[clap(long)] + pub json_rpc_tcp: Option>, + + /// UNIX socket address and port of signal-cli daemon + #[clap(long)] + pub json_rpc_socket: Option>, + + #[clap(arg_enum, long, default_value_t = OutputTypes::Json)] + pub output: OutputTypes, + + #[clap(long)] + pub verbose: bool, + + #[clap(subcommand)] + pub command: CliCommands, +} + +#[derive(ArgEnum, Clone, Debug)] +#[clap(rename_all = "kebab-case")] +pub enum OutputTypes { + PlainText, + Json, +} + +#[allow(clippy::large_enum_variant)] +#[derive(Subcommand, Debug)] +#[clap(rename_all = "camelCase", version=crate_version!())] +pub enum CliCommands { + AddDevice { + #[clap(long)] + uri: String, + }, + #[clap(rename_all = "kebab-case")] + Block { + recipient: Vec, + + #[clap(short = 'g', long)] + group_id: Vec, + }, + GetUserStatus { + recipient: Vec, + }, + JoinGroup { + #[clap(long)] + uri: String, + }, + Link { + #[clap(short = 'n', long)] + name: String, + }, + ListAccounts, + ListContacts, + ListDevices, + ListGroups { + #[clap(short = 'd', long)] + detailed: bool, + }, + ListIdentities { + #[clap(short = 'n', long)] + number: Option, + }, + ListStickerPacks, + QuitGroup { + #[clap(short = 'g', long = "group-id")] + group_id: String, + #[clap(long)] + delete: bool, + #[clap(long)] + admin: Vec, + }, + Receive { + #[clap(short = 't', long, default_value_t = 3.0)] + timeout: f64, + }, + Register { + #[clap(short = 'v', long)] + voice: bool, + #[clap(long)] + captcha: Option, + }, + RemoveContact { + recipient: String, + #[clap(long)] + forget: bool, + }, + RemoveDevice { + #[clap(short = 'd', long = "device-id")] + device_id: u32, + }, + RemovePin, + RemoteDelete { + #[clap(short = 't', long = "target-timestamp")] + target_timestamp: u64, + + recipient: Vec, + + #[clap(short = 'g', long = "group-id")] + group_id: Vec, + + #[clap(long = "note-to-self")] + note_to_self: bool, + }, + #[clap(rename_all = "kebab-case")] + Send { + recipient: Vec, + + #[clap(short = 'g', long)] + group_id: Vec, + + #[clap(long)] + note_to_self: bool, + + #[clap(short = 'e', long)] + end_session: bool, + + #[clap(short = 'm', long)] + message: Option, + + #[clap(short = 'a', long)] + attachment: Vec, + + #[clap(long)] + mention: Vec, + + #[clap(long)] + quote_timestamp: Option, + + #[clap(long)] + quote_author: Option, + + #[clap(long)] + quote_message: Option, + + #[clap(long)] + quote_mention: Vec, + + #[clap(long)] + sticker: Option, + }, + SendContacts, + SendReaction { + recipient: Vec, + + #[clap(short = 'g', long = "group-id")] + group_id: Vec, + + #[clap(long = "note-to-self")] + note_to_self: bool, + + #[clap(short = 'e', long)] + emoji: String, + + #[clap(short = 'a', long = "target-author")] + target_author: String, + + #[clap(short = 't', long = "target-timestamp")] + target_timestamp: u64, + + #[clap(short = 'r', long)] + remove: bool, + }, + SendReceipt { + recipient: String, + + #[clap(short = 't', long = "target-timestamp")] + target_timestamp: Vec, + + #[clap(arg_enum, long)] + r#type: ReceiptType, + }, + SendSyncRequest, + SendTyping { + recipient: Vec, + + #[clap(short = 'g', long = "group-id")] + group_id: Vec, + + #[clap(short = 's', long)] + stop: bool, + }, + SetPin { + pin: String, + }, + SubmitRateLimitChallenge { + challenge: String, + captcha: String, + }, + Trust { + recipient: String, + + #[clap(short = 'a', long = "trust-all-known-keys")] + trust_all_known_keys: bool, + + #[clap(short = 'v', long = "verified-safety-number")] + verified_safety_number: Option, + }, + #[clap(rename_all = "kebab-case")] + Unblock { + recipient: Vec, + + #[clap(short = 'g', long)] + group_id: Vec, + }, + Unregister { + #[clap(long = "delete-account")] + delete_account: bool, + }, + UpdateAccount { + #[clap(short = 'n', long = "device-name")] + device_name: Option, + }, + UpdateConfiguration { + #[clap(long = "read-receipts", parse(try_from_str))] + read_receipts: Option, + + #[clap(long = "unidentified-delivery-indicators")] + unidentified_delivery_indicators: Option, + + #[clap(long = "typing-indicators")] + typing_indicators: Option, + + #[clap(long = "link-previews")] + link_previews: Option, + }, + UpdateContact { + recipient: String, + + #[clap(short = 'e', long)] + expiration: Option, + + #[clap(short = 'n', long)] + name: Option, + }, + UpdateGroup { + #[clap(short = 'g', long = "group-id")] + group_id: Option, + + #[clap(short = 'n', long)] + name: Option, + + #[clap(short = 'd', long)] + description: Option, + + #[clap(short = 'a', long)] + avatar: Option, + + #[clap(short = 'm', long)] + member: Vec, + + #[clap(short = 'r', long = "remove-member")] + remove_member: Vec, + + #[clap(long)] + admin: Vec, + + #[clap(long = "remove-admin")] + remove_admin: Vec, + + #[clap(long = "reset-link")] + reset_link: bool, + + #[clap(arg_enum, long)] + link: Option, + + #[clap(arg_enum, long = "set-permission-add-member")] + set_permission_add_member: Option, + + #[clap(arg_enum, long = "set-permission-edit-details")] + set_permission_edit_details: Option, + + #[clap(arg_enum, long = "set-permission-send-messages")] + set_permission_send_messages: Option, + + #[clap(short = 'e', long)] + expiration: Option, + }, + UpdateProfile { + #[clap(long = "given-name")] + given_name: Option, + + #[clap(long = "family-name")] + family_name: Option, + + #[clap(long)] + about: Option, + + #[clap(long = "about-emoji")] + about_emoji: Option, + + #[clap(long)] + avatar: Option, + + #[clap(long = "remove-avatar")] + remove_avatar: bool, + }, + UploadStickerPack { + path: String, + }, + Verify { + verification_code: String, + + #[clap(short = 'p', long)] + pin: Option, + }, + Version, +} + +#[derive(ArgEnum, Clone, Debug)] +#[clap(rename_all = "kebab-case")] +pub enum ReceiptType { + Read, + Viewed, +} + +#[derive(ArgEnum, Clone, Debug)] +#[clap(rename_all = "kebab-case")] +pub enum LinkState { + Enabled, + EnabledWithApproval, + Disabled, +} + +#[derive(ArgEnum, Clone, Debug)] +#[clap(rename_all = "kebab-case")] +pub enum GroupPermission { + EveryMember, + OnlyAdmins, +} diff --git a/client/src/jsonrpc.rs b/client/src/jsonrpc.rs new file mode 100644 index 00000000..c0feddd0 --- /dev/null +++ b/client/src/jsonrpc.rs @@ -0,0 +1,288 @@ +use std::path::Path; + +use jsonrpc_client_transports::{transports::ipc, RpcError}; +use jsonrpc_core::serde::Deserialize; +use jsonrpc_derive::rpc; +use tokio::net::ToSocketAddrs; + +pub type SignalCliClient = gen_client::Client; + +#[rpc(client, params = "named")] +pub trait Rpc { + #[rpc(name = "addDevice", params = "named")] + fn add_device(&self, account: Option, uri: String) -> Result; + + #[rpc(name = "block", params = "named")] + fn block( + &self, + account: Option, + recipients: Vec, + #[allow(non_snake_case)] groupIds: Vec, + ) -> Result; + + #[rpc(name = "getUserStatus", params = "named")] + fn get_user_status(&self, account: Option, recipients: Vec) -> Result; + + #[rpc(name = "joinGroup", params = "named")] + fn join_group(&self, account: Option, uri: String) -> Result; + + #[rpc(name = "finishLink", params = "named")] + fn finish_link( + &self, + #[allow(non_snake_case)] deviceLinkUri: String, + #[allow(non_snake_case)] deviceName: String, + ) -> Result; + + #[rpc(name = "listAccounts", params = "named")] + fn list_accounts(&self) -> Result; + + #[rpc(name = "listContacts", params = "named")] + fn list_contacts(&self, account: Option) -> Result; + + #[rpc(name = "listDevices", params = "named")] + fn list_devices(&self, account: Option) -> Result; + + #[rpc(name = "listGroups", params = "named")] + fn list_groups(&self, account: Option) -> Result; + + #[rpc(name = "listIdentities", params = "named")] + fn list_identities(&self, account: Option, number: Option) -> Result; + + #[rpc(name = "listStickerPacks", params = "named")] + fn list_sticker_packs(&self, account: Option) -> Result; + + #[rpc(name = "quitGroup", params = "named")] + fn quit_group( + &self, + account: Option, + #[allow(non_snake_case)] groupId: String, + delete: bool, + admins: Vec, + ) -> Result; + + #[rpc(name = "register", params = "named")] + fn register( + &self, + account: Option, + voice: bool, + captcha: Option, + ) -> Result; + + #[rpc(name = "removeContact", params = "named")] + fn remove_contact( + &self, + account: Option, + recipient: String, + forget: bool, + ) -> Result; + + #[rpc(name = "removeDevice", params = "named")] + fn remove_device( + &self, + account: Option, + #[allow(non_snake_case)] deviceId: u32, + ) -> Result; + + #[rpc(name = "removePin", params = "named")] + fn remove_pin(&self, account: Option) -> Result; + + #[rpc(name = "remoteDelete", params = "named")] + fn remote_delete( + &self, + account: Option, + #[allow(non_snake_case)] targetTimestamp: u64, + recipients: Vec, + #[allow(non_snake_case)] groupIds: Vec, + #[allow(non_snake_case)] noteToSelf: bool, + ) -> Result; + + #[rpc(name = "send", params = "named")] + fn send( + &self, + account: Option, + recipients: Vec, + #[allow(non_snake_case)] groupIds: Vec, + #[allow(non_snake_case)] noteToSelf: bool, + #[allow(non_snake_case)] endSession: bool, + message: String, + attachments: Vec, + mentions: Vec, + #[allow(non_snake_case)] quoteTimestamp: Option, + #[allow(non_snake_case)] quoteAuthor: Option, + #[allow(non_snake_case)] quoteMessage: Option, + #[allow(non_snake_case)] quoteMention: Vec, + sticker: Option, + ) -> Result; + + #[rpc(name = "sendContacts", params = "named")] + fn send_contacts(&self, account: Option) -> Result; + + #[rpc(name = "sendReaction", params = "named")] + fn send_reaction( + &self, + account: Option, + recipients: Vec, + #[allow(non_snake_case)] groupIds: Vec, + #[allow(non_snake_case)] noteToSelf: bool, + emoji: String, + #[allow(non_snake_case)] targetAuthor: String, + #[allow(non_snake_case)] targetTimestamp: u64, + remove: bool, + ) -> Result; + + #[rpc(name = "sendReceipt", params = "named")] + fn send_receipt( + &self, + account: Option, + recipient: String, + #[allow(non_snake_case)] targetTimestamps: Vec, + r#type: String, + ) -> Result; + + #[rpc(name = "sendSyncRequest", params = "named")] + fn send_sync_request(&self, account: Option) -> Result; + + #[rpc(name = "sendTyping", params = "named")] + fn send_typing( + &self, + account: Option, + recipients: Vec, + #[allow(non_snake_case)] groupIds: Vec, + stop: bool, + ) -> Result; + + #[rpc(name = "setPin", params = "named")] + fn set_pin(&self, account: Option, pin: String) -> Result; + + #[rpc(name = "submitRateLimitChallenge", params = "named")] + fn submit_rate_limit_challenge( + &self, + account: Option, + challenge: String, + captcha: String, + ) -> Result; + + #[rpc(name = "startLink", params = "named")] + fn start_link(&self, account: Option) -> Result; + + #[rpc(name = "trust", params = "named")] + fn trust( + &self, + account: Option, + recipient: String, + #[allow(non_snake_case)] trustAllKnownKeys: bool, + #[allow(non_snake_case)] verifiedSafetyNumber: Option, + ) -> Result; + + #[rpc(name = "unblock", params = "named")] + fn unblock( + &self, + account: Option, + recipients: Vec, + #[allow(non_snake_case)] groupIds: Vec, + ) -> Result; + + #[rpc(name = "unregister", params = "named")] + fn unregister( + &self, + account: Option, + #[allow(non_snake_case)] deleteAccount: bool, + ) -> Result; + + #[rpc(name = "updateAccount", params = "named")] + fn update_account( + &self, + account: Option, + #[allow(non_snake_case)] deviceName: Option, + ) -> Result; + + #[rpc(name = "updateConfiguration", params = "named")] + fn update_configuration( + &self, + account: Option, + #[allow(non_snake_case)] readReceiptes: Option, + #[allow(non_snake_case)] unidentifiedDeliveryIndicators: Option, + #[allow(non_snake_case)] typingIndicators: Option, + #[allow(non_snake_case)] linkPreviews: Option, + ) -> Result; + + #[rpc(name = "updateContact", params = "named")] + fn update_contact( + &self, + account: Option, + recipient: String, + name: Option, + expiration: Option, + ) -> Result; + + #[rpc(name = "updateGroup", params = "named")] + fn update_group( + &self, + account: Option, + #[allow(non_snake_case)] groupId: Option, + name: Option, + description: Option, + avatar: Option, + member: Vec, + #[allow(non_snake_case)] removeMember: Vec, + admin: Vec, + #[allow(non_snake_case)] removeAdmin: Vec, + #[allow(non_snake_case)] resetLink: bool, + #[allow(non_snake_case)] link: Option, + #[allow(non_snake_case)] setPermissionAddMember: Option, + #[allow(non_snake_case)] setPermissionEditDetails: Option, + #[allow(non_snake_case)] setPermissionSendMessages: Option, + expiration: Option, + ) -> Result; + + #[rpc(name = "updateProfile", params = "named")] + fn update_profile( + &self, + account: Option, + #[allow(non_snake_case)] givenName: Option, + #[allow(non_snake_case)] familyName: Option, + about: Option, + #[allow(non_snake_case)] aboutEmoji: Option, + avatar: Option, + #[allow(non_snake_case)] removeAvatar: bool, + ) -> Result; + + #[rpc(name = "uploadStickerPack", params = "named")] + fn upload_sticker_pack(&self, account: Option, path: String) -> Result; + + #[rpc(name = "verify", params = "named")] + fn verify( + &self, + account: Option, + #[allow(non_snake_case)] verificationCode: String, + pin: Option, + ) -> Result; + + #[pubsub( + subscription = "receive", + subscribe, + name = "subscribeReceive", + params = "named" + )] + fn subscribe_receive(&self, _: Self::Metadata, _: Subscriber, account: Option); + + #[pubsub(subscription = "receive", unsubscribe, name = "unsubscribeReceive")] + fn unsubscribe_receive(&self, _: Option, _: SubscriptionId) -> Result; + + #[rpc(name = "version")] + fn version(&self) -> Result; +} + +#[derive(Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct JsonLink { + pub device_link_uri: String, +} + +pub async fn connect_tcp(tcp: impl ToSocketAddrs) -> Result { + super::tcp::connect::<_, SignalCliClient>(tcp).await +} + +pub async fn connect_unix(socket_path: impl AsRef) -> Result { + ipc::connect::<_, SignalCliClient>(socket_path).await +} diff --git a/client/src/main.rs b/client/src/main.rs new file mode 100644 index 00000000..1ad8dbcf --- /dev/null +++ b/client/src/main.rs @@ -0,0 +1,355 @@ +use clap::StructOpt; +use jsonrpc_client_transports::{RpcError, TypedSubscriptionStream}; +use jsonrpc_core::{futures_util::StreamExt, Value}; +use std::{path::PathBuf, time::Duration}; +use tokio::{select, time::sleep}; + +use crate::cli::{GroupPermission, LinkState}; + +mod cli; +#[allow(clippy::too_many_arguments)] +mod jsonrpc; +mod tcp; + +const DEFAULT_TCP: &str = "127.0.0.1:7583"; +const DEFAULT_SOCKET_SUFFIX: &str = "signal-cli/socket"; + +#[tokio::main] +async fn main() -> Result<(), anyhow::Error> { + let cli = cli::Cli::parse(); + + let client = connect(&cli) + .await + .map_err(|e| anyhow::anyhow!("Failed to connect to socket: {e}"))?; + + let result = match cli.command { + cli::CliCommands::Receive { timeout } => { + let mut stream = client + .subscribe_receive(cli.account) + .map_err(|e| anyhow::anyhow!("JSON-RPC command failed: {:?}", e))?; + + { + while let Some(v) = stream_next(timeout, &mut stream).await { + let v = v.map_err(|e| anyhow::anyhow!("JSON-RPC command failed: {:?}", e))?; + println!("{v}"); + } + } + return Ok(()); + } + cli::CliCommands::AddDevice { uri } => client.add_device(cli.account, uri).await, + cli::CliCommands::Block { + recipient, + group_id, + } => client.block(cli.account, recipient, group_id).await, + cli::CliCommands::GetUserStatus { recipient } => { + client.get_user_status(cli.account, recipient).await + } + cli::CliCommands::JoinGroup { uri } => client.join_group(cli.account, uri).await, + cli::CliCommands::Link { name } => { + let url = client + .start_link(cli.account) + .await + .map_err(|e| anyhow::anyhow!("JSON-RPC command startLink failed: {e:?}",))? + .device_link_uri; + println!("{}", url); + client.finish_link(url, name).await + } + cli::CliCommands::ListAccounts => client.list_accounts().await, + cli::CliCommands::ListContacts => client.list_contacts(cli.account).await, + cli::CliCommands::ListDevices => client.list_devices(cli.account).await, + cli::CliCommands::ListGroups { detailed: _ } => client.list_groups(cli.account).await, + cli::CliCommands::ListIdentities { number } => { + client.list_identities(cli.account, number).await + } + cli::CliCommands::ListStickerPacks => client.list_sticker_packs(cli.account).await, + cli::CliCommands::QuitGroup { + group_id, + delete, + admin, + } => { + client + .quit_group(cli.account, group_id, delete, admin) + .await + } + cli::CliCommands::Register { voice, captcha } => { + client.register(cli.account, voice, captcha).await + } + cli::CliCommands::RemoveContact { recipient, forget } => { + client.remove_contact(cli.account, recipient, forget).await + } + cli::CliCommands::RemoveDevice { device_id } => { + client.remove_device(cli.account, device_id).await + } + cli::CliCommands::RemovePin => client.remove_pin(cli.account).await, + cli::CliCommands::RemoteDelete { + target_timestamp, + recipient, + group_id, + note_to_self, + } => { + client + .remote_delete( + cli.account, + target_timestamp, + recipient, + group_id, + note_to_self, + ) + .await + } + cli::CliCommands::Send { + recipient, + group_id, + note_to_self, + end_session, + message, + attachment, + mention, + quote_timestamp, + quote_author, + quote_message, + quote_mention, + sticker, + } => { + client + .send( + cli.account, + recipient, + group_id, + note_to_self, + end_session, + message.unwrap_or_default(), + attachment, + mention, + quote_timestamp, + quote_author, + quote_message, + quote_mention, + sticker, + ) + .await + } + cli::CliCommands::SendContacts => client.send_contacts(cli.account).await, + cli::CliCommands::SendReaction { + recipient, + group_id, + note_to_self, + emoji, + target_author, + target_timestamp, + remove, + } => { + client + .send_reaction( + cli.account, + recipient, + group_id, + note_to_self, + emoji, + target_author, + target_timestamp, + remove, + ) + .await + } + cli::CliCommands::SendReceipt { + recipient, + target_timestamp, + r#type, + } => { + client + .send_receipt( + cli.account, + recipient, + target_timestamp, + match r#type { + cli::ReceiptType::Read => "read".to_owned(), + cli::ReceiptType::Viewed => "viewed".to_owned(), + }, + ) + .await + } + cli::CliCommands::SendSyncRequest => client.send_sync_request(cli.account).await, + cli::CliCommands::SendTyping { + recipient, + group_id, + stop, + } => { + client + .send_typing(cli.account, recipient, group_id, stop) + .await + } + cli::CliCommands::SetPin { pin } => client.set_pin(cli.account, pin).await, + cli::CliCommands::SubmitRateLimitChallenge { challenge, captcha } => { + client + .submit_rate_limit_challenge(cli.account, challenge, captcha) + .await + } + cli::CliCommands::Trust { + recipient, + trust_all_known_keys, + verified_safety_number, + } => { + client + .trust( + cli.account, + recipient, + trust_all_known_keys, + verified_safety_number, + ) + .await + } + cli::CliCommands::Unblock { + recipient, + group_id, + } => client.unblock(cli.account, recipient, group_id).await, + cli::CliCommands::Unregister { delete_account } => { + client.unregister(cli.account, delete_account).await + } + cli::CliCommands::UpdateAccount { device_name } => { + client.update_account(cli.account, device_name).await + } + cli::CliCommands::UpdateConfiguration { + read_receipts, + unidentified_delivery_indicators, + typing_indicators, + link_previews, + } => { + client + .update_configuration( + cli.account, + read_receipts, + unidentified_delivery_indicators, + typing_indicators, + link_previews, + ) + .await + } + cli::CliCommands::UpdateContact { + recipient, + expiration, + name, + } => { + client + .update_contact(cli.account, recipient, name, expiration) + .await + } + cli::CliCommands::UpdateGroup { + group_id, + name, + description, + avatar, + member, + remove_member, + admin, + remove_admin, + reset_link, + link, + set_permission_add_member, + set_permission_edit_details, + set_permission_send_messages, + expiration, + } => { + client + .update_group( + cli.account, + group_id, + name, + description, + avatar, + member, + remove_member, + admin, + remove_admin, + reset_link, + link.map(|link| match link { + LinkState::Enabled => "enabled".to_owned(), + LinkState::EnabledWithApproval => "enabledWithApproval".to_owned(), + LinkState::Disabled => "disabled".to_owned(), + }), + set_permission_add_member.map(|p| match p { + GroupPermission::EveryMember => "everyMember".to_owned(), + GroupPermission::OnlyAdmins => "onlyAdmins".to_owned(), + }), + set_permission_edit_details.map(|p| match p { + GroupPermission::EveryMember => "everyMember".to_owned(), + GroupPermission::OnlyAdmins => "onlyAdmins".to_owned(), + }), + set_permission_send_messages.map(|p| match p { + GroupPermission::EveryMember => "everyMember".to_owned(), + GroupPermission::OnlyAdmins => "onlyAdmins".to_owned(), + }), + expiration, + ) + .await + } + cli::CliCommands::UpdateProfile { + given_name, + family_name, + about, + about_emoji, + avatar, + remove_avatar, + } => { + client + .update_profile( + cli.account, + given_name, + family_name, + about, + about_emoji, + avatar, + remove_avatar, + ) + .await + } + cli::CliCommands::UploadStickerPack { path } => { + client.upload_sticker_pack(cli.account, path).await + } + cli::CliCommands::Verify { + verification_code, + pin, + } => client.verify(cli.account, verification_code, pin).await, + cli::CliCommands::Version => client.version().await, + }; + + result + .map(|v| println!("{v}")) + .map_err(|e| anyhow::anyhow!("JSON-RPC command failed: {e:?}",))?; + Ok(()) +} + +async fn connect(cli: &cli::Cli) -> Result { + if let Some(tcp) = cli.json_rpc_tcp { + let socket_addr = tcp.unwrap_or_else(|| DEFAULT_TCP.parse().unwrap()); + jsonrpc::connect_tcp(socket_addr).await + } else { + let socket_path = cli + .json_rpc_socket + .clone() + .unwrap_or(None) + .or_else(|| { + std::env::var_os("XDG_RUNTIME_DIR").map(|runtime_dir| { + PathBuf::from(runtime_dir) + .join(DEFAULT_SOCKET_SUFFIX) + .into() + }) + }) + .unwrap_or_else(|| ("/run".to_owned() + DEFAULT_SOCKET_SUFFIX).into()); + jsonrpc::connect_unix(socket_path).await + } +} + +async fn stream_next( + timeout: f64, + stream: &mut TypedSubscriptionStream, +) -> Option> { + if timeout < 0.0 { + stream.next().await + } else { + select! { + v = stream.next() => v, + _= sleep(Duration::from_millis((timeout * 1000.0) as u64)) => None, + } + } +} diff --git a/client/src/tcp.rs b/client/src/tcp.rs new file mode 100644 index 00000000..53650381 --- /dev/null +++ b/client/src/tcp.rs @@ -0,0 +1,29 @@ +use jsonrpc_client_transports::{transports::duplex, RpcChannel, RpcError}; +use jsonrpc_core::futures_util::{SinkExt, StreamExt, TryStreamExt}; +use jsonrpc_server_utils::{codecs::StreamCodec, tokio_util::codec::Decoder}; +use tokio::net::{TcpStream, ToSocketAddrs}; + +/// Connect to a JSON-RPC TCP server. +pub async fn connect>( + socket: S, +) -> Result { + let connection = TcpStream::connect(socket) + .await + .map_err(|e| RpcError::Other(Box::new(e)))?; + let (sink, stream) = StreamCodec::stream_incoming().framed(connection).split(); + let sink = sink.sink_map_err(|e| RpcError::Other(Box::new(e))); + let stream = stream.map_err(|e| log::error!("TCP stream error: {}", e)); + + let (client, sender) = duplex( + Box::pin(sink), + Box::pin( + stream + .take_while(|x| std::future::ready(x.is_ok())) + .map(|x| x.expect("Stream is closed upon first error.")), + ), + ); + + tokio::spawn(client); + + Ok(sender.into()) +} diff --git a/run_tests.sh b/run_tests.sh index 277739cb..341c825f 100755 --- a/run_tests.sh +++ b/run_tests.sh @@ -16,6 +16,7 @@ NUMBER_1="$1" NUMBER_2="$2" TEST_PIN_1=456test_pin_foo123 NATIVE=0 +JSON_RPC=0 PATH_TEST_CONFIG="$PWD/build/test-config" PATH_MAIN="$PATH_TEST_CONFIG/main" @@ -23,6 +24,12 @@ PATH_LINK="$PATH_TEST_CONFIG/link" if [ "$NATIVE" -eq 1 ]; then SIGNAL_CLI="$PWD/build/native/nativeCompile/signal-cli" +elif [ "$JSON_RPC" -eq 1 ]; then + (cd client && cargo build) + "$PWD/build/install/signal-cli/bin/signal-cli" --verbose --verbose --trust-new-identities=always --config="$PATH_MAIN" --service-environment="staging" daemon --socket --receive-mode=manual& + "$PWD/build/install/signal-cli/bin/signal-cli" --verbose --verbose --trust-new-identities=always --config="$PATH_LINK" --service-environment="staging" daemon --tcp --receive-mode=manual& + sleep 5 + SIGNAL_CLI="$PWD/client/target/debug/signal-cli-client" else ./gradlew installDist SIGNAL_CLI="$PWD/build/install/signal-cli/bin/signal-cli" @@ -30,16 +37,28 @@ fi run() { set -x - "$SIGNAL_CLI" --service-environment="staging" $@ + if [ "$JSON_RPC" -eq 1 ]; then + "$SIGNAL_CLI" $@ + else + "$SIGNAL_CLI" --service-environment="staging" $@ + fi set +x } run_main() { - run --config="$PATH_MAIN" $@ + if [ "$JSON_RPC" -eq 1 ]; then + run --json-rpc-socket="$XDG_RUNTIME_DIR/signal-cli/socket" $@ + else + run --config="$PATH_MAIN" $@ + fi } run_linked() { - run --config="$PATH_LINK" $@ + if [ "$JSON_RPC" -eq 1 ]; then + run --json-rpc-tcp="127.0.0.1:7583" $@ + else + run --config="$PATH_LINK" $@ + fi } register() { @@ -84,6 +103,7 @@ sleep 5 run_main listAccounts run_main --output=json listAccounts +if [ "$JSON_RPC" -eq 0 ]; then ## DBus #run_main -a "$NUMBER_1" --dbus send "$NUMBER_2" -m daemon_not_running || true #run_main daemon & @@ -127,6 +147,7 @@ exec 3<> "$FIFO_FILE" exec 3>&- wait +fi run_main -a "$NUMBER_1" setPin "$TEST_PIN_1" run_main -a "$NUMBER_2" removePin @@ -143,7 +164,7 @@ run_main -a "$NUMBER_2" send "$NUMBER_1" -m hi run_main -a "$NUMBER_1" receive run_main -a "$NUMBER_2" receive ## Groups -GROUP_ID=$(run_main -a "$NUMBER_1" updateGroup -n GRUPPE -a LICENSE -m "$NUMBER_1" | grep -oP '(?<=").+(?=")') +GROUP_ID=$(run_main -a "$NUMBER_1" --output=json updateGroup -n GRUPPE -a LICENSE -m "$NUMBER_1" | jq -r '.groupId') run_main -a "$NUMBER_1" send "$NUMBER_2" -m first run_main -a "$NUMBER_1" updateGroup -g "$GROUP_ID" -n GRUPPE_UMB -m "$NUMBER_2" --admin "$NUMBER_2" --description DESCRIPTION --link=enabled-with-approval --set-permission-add-member=only-admins --set-permission-edit-details=only-admins --set-permission-send-messages=only-admins -e 42 run_main -a "$NUMBER_1" updateGroup -g "$GROUP_ID" --remove-admin "$NUMBER_2" --reset-link --set-permission-send-messages=every-member @@ -171,15 +192,15 @@ run_main -a "$NUMBER_2" trust "$NUMBER_1" -a ## Basic send/receive for OUTPUT in "plain-text" "json"; do run_main -a "$NUMBER_1" --output="$OUTPUT" getUserStatus "$NUMBER_1" "$NUMBER_2" "+111111111" - run_main -a "$NUMBER_1" send "$NUMBER_2" -m hi - run_main -a "$NUMBER_2" send "$NUMBER_1" -m hi - run_main -a "$NUMBER_1" send -g "$GROUP_ID" -m hi -a LICENSE --mention "1:1:$NUMBER_2" - TIMESTAMP=$(uname -a | run_main -a "$NUMBER_1" send "$NUMBER_2") - run_main -a "$NUMBER_2" sendReaction "$NUMBER_1" -e 🍀 -a "$NUMBER_1" -t "$TIMESTAMP" - run_main -a "$NUMBER_1" remoteDelete "$NUMBER_2" -t "$TIMESTAMP" + run_main -a "$NUMBER_1" --output="$OUTPUT" send "$NUMBER_2" -m hi + run_main -a "$NUMBER_2" --output="$OUTPUT" send "$NUMBER_1" -m hi + run_main -a "$NUMBER_1" --output="$OUTPUT" send -g "$GROUP_ID" -m hi -a LICENSE --mention "1:1:$NUMBER_2" + TIMESTAMP=$(uname -a | run_main -a "$NUMBER_1" --output=json send "$NUMBER_2" | jq '.timestamp') + run_main -a "$NUMBER_2" --output="$OUTPUT" sendReaction "$NUMBER_1" -e 🍀 -a "$NUMBER_1" -t "$TIMESTAMP" + run_main -a "$NUMBER_1" --output="$OUTPUT" remoteDelete "$NUMBER_2" -t "$TIMESTAMP" run_main -a "$NUMBER_2" --output="$OUTPUT" receive run_main -a "$NUMBER_1" --output="$OUTPUT" receive - run_main -a "$NUMBER_1" send -e "$NUMBER_2" + run_main -a "$NUMBER_1" --output="$OUTPUT" send -e "$NUMBER_2" run_main -a "$NUMBER_2" --output="$OUTPUT" receive done @@ -194,8 +215,8 @@ run_linked -a "$NUMBER_1" sendSyncRequest run_main -a "$NUMBER_1" sendContacts for OUTPUT in "plain-text" "json"; do - run_main -a "$NUMBER_1" send "$NUMBER_2" -m hi - run_main -a "$NUMBER_2" send "$NUMBER_1" -m hi + run_main -a "$NUMBER_1" --output="$OUTPUT" send "$NUMBER_2" -m hi + run_main -a "$NUMBER_2" --output="$OUTPUT" send "$NUMBER_1" -m hi run_main -a "$NUMBER_2" --output="$OUTPUT" receive run_main -a "$NUMBER_1" --output="$OUTPUT" receive run_linked -a "$NUMBER_1" --output="$OUTPUT" receive