diff --git a/.gitignore b/.gitignore index ea8c4bf..2f2c30a 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /target +/private diff --git a/Cargo.lock b/Cargo.lock index ad86f83..d40a35f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6,6 +6,47 @@ version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ee2a4ec343196209d6594e19543ae87a39f96d5534d7174822a3ad825dd6ed7e" +[[package]] +name = "aes" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7001367fde4c768a19d1029f0a8be5abd9308e1119846d5bd9ad26297b8faf5" +dependencies = [ + "aes-soft", + "aesni", + "block-cipher", +] + +[[package]] +name = "aes-soft" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4925647ee64e5056cf231608957ce7c81e12d6d6e316b9ce1404778cc1d35fa7" +dependencies = [ + "block-cipher", + "byteorder", + "opaque-debug 0.2.3", +] + +[[package]] +name = "aesni" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d050d39b0b7688b3a3254394c3e30a9d66c41dcf9b05b0e2dbdc623f6505d264" +dependencies = [ + "block-cipher", + "opaque-debug 0.2.3", +] + +[[package]] +name = "aho-corasick" +version = "0.7.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "043164d8ba5c4c3035fec9bbee8647c0261d788f3474306f93bb65901cae0e86" +dependencies = [ + "memchr", +] + [[package]] name = "ansi_term" version = "0.11.0" @@ -37,24 +78,99 @@ dependencies = [ "winapi 0.3.9", ] +[[package]] +name = "autocfg" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d49d90015b3c36167a20fe2810c5cd875ad504b39cff3d4eae7977e6b7c1cb2" + +[[package]] +name = "autocfg" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8aac770f1885fd7e387acedd76065302551364496e46b3dd00860b2f8359b9d" + +[[package]] +name = "base64" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3441f0f7b02788e948e47f457ca01f1d7e6d92c693bc132c22b087d3141c03ff" + [[package]] name = "bitflags" version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" +[[package]] +name = "block-buffer" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4152116fd6e9dadb291ae18fc1ec3575ed6d84c29642d97890f4b4a3417297e4" +dependencies = [ + "generic-array", +] + +[[package]] +name = "block-cipher" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa136449e765dc7faa244561ccae839c394048667929af599b5d931ebe7b7f10" +dependencies = [ + "generic-array", +] + +[[package]] +name = "bumpalo" +version = "3.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2e8c087f005730276d1096a652e92a8bacee2e2472bcc9715a74d2bec38b5820" + +[[package]] +name = "byteorder" +version = "1.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08c48aae112d48ed9f069b33538ea9e3e90aa263cfa3d1c24309612b1f7472de" + [[package]] name = "bytes" version = "0.5.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0e4cec68f03f32e44924783795810fa50a7035d8c8ebe78580ad7e6c703fba38" +[[package]] +name = "cc" +version = "1.0.58" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f9a06fb2e53271d7c279ec1efea6ab691c35a2ae67ec0d91d7acec0caf13b518" + +[[package]] +name = "cfb8" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba2baac45bade12cdd992418b4b4ea40f6198c03b2de1a9eb7b059f118f445d6" +dependencies = [ + "block-cipher", + "stream-cipher", +] + [[package]] name = "cfg-if" version = "0.1.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" +[[package]] +name = "chrono" +version = "0.4.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c74d84029116787153e02106bf53e66828452a4b325cc8652b788b5967c0a0b6" +dependencies = [ + "num-integer", + "num-traits", + "time", +] + [[package]] name = "clap" version = "2.33.1" @@ -71,6 +187,28 @@ dependencies = [ "yaml-rust", ] +[[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 = "cpuid-bool" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8aebca1129a03dc6dc2b127edd729435bbc4a37e1d5f4d7513165089ceb02634" + [[package]] name = "crc32fast" version = "1.2.0" @@ -80,6 +218,30 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "digest" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3dd60d1080a57a05ab032377049e0591415d2b31afd7028356dbf3cc6dcb066" +dependencies = [ + "generic-array", +] + +[[package]] +name = "dtoa" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "134951f4028bdadb9b84baf4232681efbf277da25144b9b0ad65df75946c422b" + +[[package]] +name = "encoding_rs" +version = "0.8.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8ac63f94732332f44fe654443c46f6375d1939684c17b0afb6cb56b0456e171" +dependencies = [ + "cfg-if", +] + [[package]] name = "flate2" version = "1.0.16" @@ -92,6 +254,27 @@ dependencies = [ "miniz_oxide", ] +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + +[[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 = "fuchsia-zircon" version = "0.3.3" @@ -108,6 +291,94 @@ version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7" +[[package]] +name = "futures-channel" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f366ad74c28cca6ba456d95e6422883cfb4b252a83bed929c83abfdbbf2967d5" +dependencies = [ + "futures-core", +] + +[[package]] +name = "futures-core" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59f5fff90fd5d971f936ad674802482ba441b6f09ba5e15fd8b39145582ca399" + +[[package]] +name = "futures-sink" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f2032893cb734c7a05d85ce0cc8b8c4075278e93b24b66f9de99d6eb0fa8acc" + +[[package]] +name = "futures-task" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bdb66b5f09e22019b1ab0830f7785bcea8e7a42148683f99214f73f8ec21a626" + +[[package]] +name = "futures-util" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8764574ff08b701a084482c3c7031349104b07ac897393010494beaa18ce32c6" +dependencies = [ + "futures-core", + "futures-task", + "pin-project", + "pin-utils", +] + +[[package]] +name = "generic-array" +version = "0.14.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60fb4bb6bba52f78a471264d9a3b7d026cc0af47b22cd2cffbc0b787ca003e63" +dependencies = [ + "typenum", + "version_check", +] + +[[package]] +name = "getrandom" +version = "0.1.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7abc8dd8451921606d809ba32e95b6111925cd2906060d2dcc29c070220503eb" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "h2" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "993f9e0baeed60001cf565546b0d3dbe6a6ad23f2bd31644a133c641eccf6d53" +dependencies = [ + "bytes", + "fnv", + "futures-core", + "futures-sink", + "futures-util", + "http", + "indexmap", + "slab", + "tokio", + "tokio-util", + "tracing", +] + +[[package]] +name = "hashbrown" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34f595585f103464d8d2f6e9864682d74c1601fed5e07d62b1c9058dba8246fb" +dependencies = [ + "autocfg 1.0.0", +] + [[package]] name = "hermit-abi" version = "0.1.15" @@ -117,6 +388,91 @@ dependencies = [ "libc", ] +[[package]] +name = "http" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "28d569972648b2c512421b5f2a405ad6ac9666547189d0c5477a3f200f3e02f9" +dependencies = [ + "bytes", + "fnv", + "itoa", +] + +[[package]] +name = "http-body" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "13d5ff830006f7646652e057693569bfe0d51760c0085a071769d142a205111b" +dependencies = [ + "bytes", + "http", +] + +[[package]] +name = "httparse" +version = "1.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd179ae861f0c2e53da70d892f5f3029f9594be0c41dc5269cd371691b1dc2f9" + +[[package]] +name = "hyper" +version = "0.13.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3e68a8dd9716185d9e64ea473ea6ef63529252e3e27623295a0378a19665d5eb" +dependencies = [ + "bytes", + "futures-channel", + "futures-core", + "futures-util", + "h2", + "http", + "http-body", + "httparse", + "itoa", + "pin-project", + "socket2", + "time", + "tokio", + "tower-service", + "tracing", + "want", +] + +[[package]] +name = "hyper-tls" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d979acc56dcb5b8dddba3917601745e877576475aa046df3226eabdecef78eed" +dependencies = [ + "bytes", + "hyper", + "native-tls", + "tokio", + "tokio-tls", +] + +[[package]] +name = "idna" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "02e2673c30ee86b5b96a9cb52ad15718aa1f966f5ab9ad54a8b95d5ca33120a9" +dependencies = [ + "matches", + "unicode-bidi", + "unicode-normalization", +] + +[[package]] +name = "indexmap" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b88cd59ee5f71fea89a62248fc8f387d44400cefe05ef548466d61ced9029a7" +dependencies = [ + "autocfg 1.0.0", + "hashbrown", +] + [[package]] name = "iovec" version = "0.1.4" @@ -126,12 +482,27 @@ dependencies = [ "libc", ] +[[package]] +name = "ipnet" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47be2f14c678be2fdcab04ab1171db51b2762ce6f0a8ee87c8dd4a04ed216135" + [[package]] name = "itoa" version = "0.4.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dc6f3ad7b9d11a0c00842ff8de1b60ee58661048eb8049ed33c73594f359d7e6" +[[package]] +name = "js-sys" +version = "0.3.42" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "52732a3d3ad72c58ad2dc70624f9c17b46ecd0943b9a4f1ee37c4c18c5d983e2" +dependencies = [ + "wasm-bindgen", +] + [[package]] name = "kernel32-sys" version = "0.2.2" @@ -147,6 +518,9 @@ name = "lazy_static" version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" +dependencies = [ + "spin", +] [[package]] name = "libc" @@ -154,6 +528,12 @@ version = "0.2.73" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bd7d4bd64732af4bf3a67f367c27df8520ad7e230c5817b8ff485864d80242b9" +[[package]] +name = "libm" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c7d73b3f436185384286bd8098d17ec07c9a7d2388a6599f824d8502b529702a" + [[package]] name = "log" version = "0.4.11" @@ -163,12 +543,34 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "matches" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ffc5c5338469d4d3ea17d269fa8ea3512ad247247c30bd2df69e68309ed0a08" + [[package]] name = "memchr" version = "2.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3728d817d99e5ac407411fa471ff9800a778d88a24685968b36824eaf4bee400" +[[package]] +name = "mime" +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 = "miniz_oxide" version = "0.4.0" @@ -220,6 +622,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.34" @@ -231,6 +651,77 @@ dependencies = [ "winapi 0.3.9", ] +[[package]] +name = "num-bigint" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "090c7f9998ee0ff65aa5b723e4009f7b217707f1fb5ea551329cc4d6231fb304" +dependencies = [ + "autocfg 1.0.0", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-bigint" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7f3fc75e3697059fb1bc465e3d8cca6cf92f56854f201158b3f9c77d5a3cfa0" +dependencies = [ + "autocfg 1.0.0", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-bigint-dig" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b3d03c330f9f7a2c19e3c0b42698e48141d0809c78cd9b6219f85bd7d7e892aa" +dependencies = [ + "autocfg 0.1.7", + "byteorder", + "lazy_static", + "libm", + "num-integer", + "num-iter", + "num-traits", + "rand", + "serde", + "smallvec", + "zeroize", +] + +[[package]] +name = "num-integer" +version = "0.1.43" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d59457e662d541ba17869cf51cf177c0b5f0cbf476c66bdc90bf1edac4f875b" +dependencies = [ + "autocfg 1.0.0", + "num-traits", +] + +[[package]] +name = "num-iter" +version = "0.1.41" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a6e6b7c748f995c4c29c5f5ae0248536e04a5739927c74ec0fa564805094b9f" +dependencies = [ + "autocfg 1.0.0", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac267bcc07f48ee5f8935ab0d24f316fb722d7a1292e2913f0cc196b29ffd611" +dependencies = [ + "autocfg 1.0.0", +] + [[package]] name = "num_cpus" version = "1.13.0" @@ -241,12 +732,118 @@ dependencies = [ "libc", ] +[[package]] +name = "once_cell" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b631f7e854af39a1739f401cf34a8a013dfe09eac4fa4dba91e9768bd28168d" + +[[package]] +name = "opaque-debug" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2839e79665f131bdb5782e51f2c6c9599c133c6098982a54c794358bf432529c" + +[[package]] +name = "opaque-debug" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5" + +[[package]] +name = "openssl" +version = "0.10.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d575eff3665419f9b83678ff2815858ad9d11567e082f5ac1814baba4e2bcb4" +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.58" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a842db4709b604f0fe5d1170ae3565899be2ad3d9cbc72dedc789ac0511f78de" +dependencies = [ + "autocfg 1.0.0", + "cc", + "libc", + "pkg-config", + "vcpkg", +] + +[[package]] +name = "pem" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59698ea79df9bf77104aefd39cc3ec990cb9693fb59c3b0a70ddf2646fdffb4b" +dependencies = [ + "base64", + "once_cell", + "regex", +] + +[[package]] +name = "percent-encoding" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e" + +[[package]] +name = "pin-project" +version = "0.4.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "12e3a6cdbfe94a5e4572812a0201f8c0ed98c1c452c7b8563ce2276988ef9c17" +dependencies = [ + "pin-project-internal", +] + +[[package]] +name = "pin-project-internal" +version = "0.4.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a0ffd45cf79d88737d7cc85bfd5d2894bee1139b356e616fe85dc389c61aaf7" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "pin-project-lite" version = "0.1.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "282adbf10f2698a7a77f8e983a74b2d18176c19a7fd32a45446139ae7b02b715" +[[package]] +name = "pin-utils" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" + +[[package]] +name = "pkg-config" +version = "0.3.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d36492546b6af1463394d46f0c834346f31548646f6ba10849802c9c9a27ac33" + +[[package]] +name = "ppv-lite86" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "237a5ed80e274dbc66f86bd59c1e25edc039660be53194b5fe0a482e0f2612ea" + [[package]] name = "proc-macro2" version = "1.0.19" @@ -265,12 +862,176 @@ 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.1.57" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41cc0f7e4d5d4544e8861606a285bb08d3e70712ccc7d2b84d7c0ccfaf4b05ce" + +[[package]] +name = "regex" +version = "1.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c3780fcf44b193bc4d09f36d2a3c87b251da4a046c87795a0d35f4f927ad8e6" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", + "thread_local", +] + +[[package]] +name = "regex-syntax" +version = "0.6.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26412eb97c6b088a6997e05f69403a802a92d520de2f8e63c2b65f9e0f47c4e8" + +[[package]] +name = "remove_dir_all" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3acd125665422973a33ac9d3dd2df85edad0f4ae9b00dafb1a05e43a9f5ef8e7" +dependencies = [ + "winapi 0.3.9", +] + +[[package]] +name = "reqwest" +version = "0.10.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "12427a5577082c24419c9c417db35cfeb65962efc7675bb6b0d5f1f9d315bfe6" +dependencies = [ + "base64", + "bytes", + "encoding_rs", + "futures-core", + "futures-util", + "http", + "http-body", + "hyper", + "hyper-tls", + "ipnet", + "js-sys", + "lazy_static", + "log", + "mime", + "mime_guess", + "native-tls", + "percent-encoding", + "pin-project-lite", + "serde", + "serde_urlencoded", + "tokio", + "tokio-tls", + "url", + "wasm-bindgen", + "wasm-bindgen-futures", + "web-sys", + "winreg", +] + +[[package]] +name = "rsa" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3648b669b10afeab18972c105e284a7b953a669b0be3514c27f9b17acab2f9cd" +dependencies = [ + "byteorder", + "digest", + "lazy_static", + "num-bigint-dig", + "num-integer", + "num-iter", + "num-traits", + "pem", + "rand", + "sha2", + "simple_asn1", + "subtle", + "thiserror", + "zeroize", +] + [[package]] name = "ryu" version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e" +[[package]] +name = "schannel" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f05ba609c234e60bee0d547fe94a4c7e9da733d1c962cf6e59efa4cd9c8bc75" +dependencies = [ + "lazy_static", + "winapi 0.3.9", +] + +[[package]] +name = "security-framework" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "64808902d7d99f78eaddd2b4e2509713babc3dc3c85ad6f4c447680f3c01e535" +dependencies = [ + "bitflags", + "core-foundation", + "core-foundation-sys", + "libc", + "security-framework-sys", +] + +[[package]] +name = "security-framework-sys" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "17bf11d99252f512695eb468de5516e5cf75455521e69dfe343f3b74e4748405" +dependencies = [ + "core-foundation-sys", + "libc", +] + [[package]] name = "serde" version = "1.0.114" @@ -302,18 +1063,106 @@ dependencies = [ "serde", ] +[[package]] +name = "serde_urlencoded" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ec5d77e2d4c73717816afac02670d5c4f534ea95ed430442cad02e7a6e32c97" +dependencies = [ + "dtoa", + "itoa", + "serde", + "url", +] + +[[package]] +name = "sha-1" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "170a36ea86c864a3f16dd2687712dd6646f7019f301e57537c7f4dc9f5916770" +dependencies = [ + "block-buffer", + "cfg-if", + "cpuid-bool", + "digest", + "opaque-debug 0.3.0", +] + +[[package]] +name = "sha2" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2933378ddfeda7ea26f48c555bdad8bb446bf8a3d17832dc83e380d444cfb8c1" +dependencies = [ + "block-buffer", + "cfg-if", + "cpuid-bool", + "digest", + "opaque-debug 0.3.0", +] + +[[package]] +name = "simple_asn1" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "692ca13de57ce0613a363c8c2f1de925adebc81b04c923ac60c5488bb44abe4b" +dependencies = [ + "chrono", + "num-bigint 0.2.6", + "num-traits", +] + [[package]] name = "slab" version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c111b5bd5695e56cffe5129854aa230b39c93a305372fdbb2668ca2394eea9f8" +[[package]] +name = "smallvec" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3757cb9d89161a2f24e1cf78efa0c1fcff485d18e3f55e0aa3480824ddaa0f3f" + +[[package]] +name = "socket2" +version = "0.3.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "03088793f677dce356f3ccc2edb1b314ad191ab702a5de3faf49304f7e104918" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall", + "winapi 0.3.9", +] + +[[package]] +name = "spin" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d" + +[[package]] +name = "stream-cipher" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09f8ed9974042b8c3672ff3030a69fcc03b74c47c3d1ecb7755e8a3626011e88" +dependencies = [ + "generic-array", +] + [[package]] name = "strsim" version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" +[[package]] +name = "subtle" +version = "2.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "502d53007c02d7605a05df1c1a73ee436952781653da5d0bf57ad608f66932c1" + [[package]] name = "syn" version = "1.0.35" @@ -325,6 +1174,32 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "synstructure" +version = "0.12.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b834f2d66f734cb897113e34aaff2f1ab4719ca946f9a7358dba8f8064148701" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "unicode-xid", +] + +[[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.9", +] + [[package]] name = "textwrap" version = "0.11.0" @@ -334,15 +1209,67 @@ dependencies = [ "unicode-width", ] +[[package]] +name = "thiserror" +version = "1.0.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7dfdd070ccd8ccb78f4ad66bf1982dc37f620ef696c6b5028fe2ed83dd3d0d08" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bd80fc12f73063ac132ac92aceea36734f04a1d93c1240c6944e23a3b8841793" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "thread_local" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d40c6d1b69745a6ec6fb1ca717914848da4b44ae29d9b3080cbee91d72a69b14" +dependencies = [ + "lazy_static", +] + +[[package]] +name = "time" +version = "0.1.43" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca8a50ef2360fbd1eeb0ecd46795a87a19024eb4b53c5dc916ca1fd95fe62438" +dependencies = [ + "libc", + "winapi 0.3.9", +] + +[[package]] +name = "tinyvec" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53953d2d3a5ad81d9f844a32f14ebb121f50b650cd59d0ee2a07cf13c617efed" + [[package]] name = "tmd" version = "0.1.0" dependencies = [ + "aes", "async-trait", + "cfb8", "clap", "flate2", + "num-bigint 0.3.0", + "rand", + "reqwest", + "rsa", "serde", "serde_json", + "sha-1", "tokio", "uuid", ] @@ -354,6 +1281,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5d34ca54d84bf2b5b4d7d31e901a8464f7b60ac145a284fba25ceb801f2ddccd" dependencies = [ "bytes", + "fnv", + "futures-core", "iovec", "lazy_static", "libc", @@ -362,6 +1291,7 @@ dependencies = [ "mio-uds", "num_cpus", "pin-project-lite", + "slab", "tokio-macros", ] @@ -376,6 +1306,95 @@ dependencies = [ "syn", ] +[[package]] +name = "tokio-tls" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a70f4fcd7b3b24fb194f837560168208f669ca8cb70d0c4b862944452396343" +dependencies = [ + "native-tls", + "tokio", +] + +[[package]] +name = "tokio-util" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "be8242891f2b6cbef26a2d7e8605133c2c554cd35b3e4948ea892d6d68436499" +dependencies = [ + "bytes", + "futures-core", + "futures-sink", + "log", + "pin-project-lite", + "tokio", +] + +[[package]] +name = "tower-service" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e987b6bf443f4b5b3b6f38704195592cca41c5bb7aedd3c3693c7081f8289860" + +[[package]] +name = "tracing" +version = "0.1.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dbdf4ccd1652592b01286a5dbe1e2a77d78afaa34beadd9872a5f7396f92aaa9" +dependencies = [ + "cfg-if", + "log", + "tracing-core", +] + +[[package]] +name = "tracing-core" +version = "0.1.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94ae75f0d28ae10786f3b1895c55fe72e79928fd5ccdebb5438c75e93fec178f" +dependencies = [ + "lazy_static", +] + +[[package]] +name = "try-lock" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59547bce71d9c38b83d9c0e92b6066c4253371f15005def0c30d9657f50c7642" + +[[package]] +name = "typenum" +version = "1.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "373c8a200f9e67a0c95e62a4f52fbf80c23b4381c05a17845531982fa99e6b33" + +[[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-bidi" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49f2bd0c6468a8230e1db229cff8029217cf623c767ea5d60bfbd42729ea54d5" +dependencies = [ + "matches", +] + +[[package]] +name = "unicode-normalization" +version = "0.1.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6fb19cf769fa8c6a80a162df694621ebeb4dafb606470b2b2fce0be40a98a977" +dependencies = [ + "tinyvec", +] + [[package]] name = "unicode-width" version = "0.1.8" @@ -388,6 +1407,17 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564" +[[package]] +name = "url" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "829d4a8476c35c9bf0bbce5a3b23f4106f79728039b726d292bb93bc106787cb" +dependencies = [ + "idna", + "matches", + "percent-encoding", +] + [[package]] name = "uuid" version = "0.8.1" @@ -397,12 +1427,118 @@ dependencies = [ "serde", ] +[[package]] +name = "vcpkg" +version = "0.2.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6454029bf181f092ad1b853286f23e2c507d8e8194d01d92da4a55c274a5508c" + [[package]] name = "vec_map" version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" +[[package]] +name = "version_check" +version = "0.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5a972e5669d67ba988ce3dc826706fb0a8b01471c088cb0b6110b805cc36aed" + +[[package]] +name = "want" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ce8a968cb1cd110d136ff8b819a556d6fb6d919363c61534f6860c7eb172ba0" +dependencies = [ + "log", + "try-lock", +] + +[[package]] +name = "wasi" +version = "0.9.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" + +[[package]] +name = "wasm-bindgen" +version = "0.2.65" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f3edbcc9536ab7eababcc6d2374a0b7bfe13a2b6d562c5e07f370456b1a8f33d" +dependencies = [ + "cfg-if", + "serde", + "serde_json", + "wasm-bindgen-macro", +] + +[[package]] +name = "wasm-bindgen-backend" +version = "0.2.65" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "89ed2fb8c84bfad20ea66b26a3743f3e7ba8735a69fe7d95118c33ec8fc1244d" +dependencies = [ + "bumpalo", + "lazy_static", + "log", + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-futures" +version = "0.4.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41ad6e4e8b2b7f8c90b6e09a9b590ea15cb0d1dbe28502b5a405cd95d1981671" +dependencies = [ + "cfg-if", + "js-sys", + "wasm-bindgen", + "web-sys", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.65" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eb071268b031a64d92fc6cf691715ca5a40950694d8f683c5bb43db7c730929e" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.65" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf592c807080719d1ff2f245a687cbadb3ed28b2077ed7084b47aba8b691f2c6" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-backend", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.65" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b6c0220ded549d63860c78c38f3bcc558d1ca3f4efa74942c536ddbbb55e87" + +[[package]] +name = "web-sys" +version = "0.3.42" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8be2398f326b7ba09815d0b403095f34dd708579220d099caae89be0b32137b2" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + [[package]] name = "winapi" version = "0.2.8" @@ -437,6 +1573,15 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +[[package]] +name = "winreg" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0120db82e8a1e0b9fb3345a539c478767c0048d842860994d96113d5b667bd69" +dependencies = [ + "winapi 0.3.9", +] + [[package]] name = "ws2_32-sys" version = "0.2.1" @@ -452,3 +1597,24 @@ name = "yaml-rust" version = "0.3.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e66366e18dc58b46801afbf2ca7661a9f59cc8c5962c29892b6039b4f86fa992" + +[[package]] +name = "zeroize" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3cbac2ed2ba24cc90f5e06485ac8c7c1e5449fe8911aef4d8877218af021a5b8" +dependencies = [ + "zeroize_derive", +] + +[[package]] +name = "zeroize_derive" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "de251eec69fc7c1bc3923403d18ececb929380e016afe103da75f396704f8ca2" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "synstructure", +] diff --git a/Cargo.toml b/Cargo.toml index be1f427..c4b8551 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,13 +7,22 @@ repository = "https://github.com/jamestmartin/tmd" license = "GPL-3.0+" publish = false -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - [dependencies] async-trait = "0.1.36" clap = { version = "2.33.1", features = ["yaml"] } -flate2 = "1.0.16" serde = { version = "1.0.114", features = ["derive"] } serde_json = "1.0.56" tokio = { version = "0.2.22", features = ["io-util", "macros", "net", "tcp", "rt-threaded"] } uuid = { version = "0.8", features = ["serde"] } + +# Dependencies required for compression +flate2 = "1.0.16" + +# Dependencies required for encryption +aes = "0.4.0" +cfb8 = "0.4.0" +num-bigint = "0.3.0" +rand = "0.7.3" +reqwest = "0.10.7" +rsa = "0.3.0" +sha-1 = "0.9.1" diff --git a/src/main.rs b/src/main.rs index 4f9e895..1aebf3b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -124,7 +124,71 @@ async fn interact_login(mut con: Connection) -> io::Result<()> { } }; - eprintln!("Client set username to {}.", name); + use rand::Rng; + use rand::rngs::OsRng; + use rsa::{RSAPrivateKey, PaddingScheme}; + + use std::fs::File; + use std::io::Read; + let mut public_key = Vec::new(); + File::open("private/pub.der").expect("missing public key").read_to_end(&mut public_key)?; + + let mut private_key = Vec::new(); + File::open("private/priv.der").expect("missing private key").read_to_end(&mut private_key)?; + + let key = RSAPrivateKey::from_pkcs1(&private_key).expect("Invalid private key."); + + let mut verify_token = Vec::new(); + verify_token.resize(4, 0u8); + OsRng.fill(verify_token.as_mut_slice()); + + let server_id = ""; + + con.write(&Clientbound::EncryptionRequest(EncryptionRequest { + server_id: server_id.to_string().into_boxed_str(), + public_key: public_key.clone().into_boxed_slice(), + verify_token: verify_token.clone().into_boxed_slice(), + })).await?; + + let secret = match con.read().await? { + Serverbound::EncryptionResponse(encryption_response) => { + let token = key.decrypt(PaddingScheme::PKCS1v15Encrypt, + &encryption_response.verify_token) + .expect("Failed to decrypt verify token."); + if token.as_slice() != verify_token.as_slice() { + return mk_err("Incorrect verify token."); + } + + key.decrypt(PaddingScheme::PKCS1v15Encrypt, &encryption_response.shared_secret) + .expect("Failed to decrypt shared secret.") + }, + _ => { + return mk_err("Unexpected packet (expected Encryption Response)."); + } + }; + + con = con.set_encryption(&secret).expect("Failed to set encryption."); + + use reqwest::Client; + + let server_hash = { + let server_hash_bytes = { + use sha1::{Sha1, Digest}; + let mut hasher = Sha1::new(); + hasher.update(server_id.as_bytes()); + hasher.update(&secret); + hasher.update(&public_key); + hasher.finalize() + }; + + format!("{:x}", num_bigint::BigInt::from_signed_bytes_be(&server_hash_bytes)) + }; + + // TODO: Authentication, not just encryption. + println!("{:?}", Client::new().get("https://sessionserver.mojang.com/session/minecraft/hasJoined") + .header("Content-Type", "application/json") + .query(&[("username", name.clone()), ("serverId", server_hash.into_boxed_str())]) + .send().await.expect("Request failed.").text().await.unwrap()); con.set_compression(Some(64)).await?; diff --git a/src/net/connection.rs b/src/net/connection.rs index 57d64fe..a2247af 100644 --- a/src/net/connection.rs +++ b/src/net/connection.rs @@ -1,8 +1,9 @@ mod packet_format; +mod stream; -use crate::net::{Reader, Writer}; use crate::net::connection::packet_format::PacketFormat; use crate::net::connection::packet_format::default::DefaultPacketFormat; +use crate::net::connection::stream::Stream; use crate::net::protocol::packet_map::PacketMap; use crate::net::protocol::state::ProtocolState; use crate::net::protocol::state::handshake::Handshake; @@ -11,27 +12,29 @@ use crate::net::protocol::state::play::Play; use crate::net::protocol::state::status::Status; use std::io; use std::marker::PhantomData; +use tokio::io::BufStream; use tokio::net::TcpStream; pub struct Connection { - src: Reader, - dest: Writer, + rw: Box, fmt: Box, st: PhantomData, } impl Connection { pub async fn write(&mut self, pkt: &St::Clientbound) -> io::Result<()> { - let mut buf = Vec::new(); - pkt.write(&mut buf); + // Turn the packet into bytes. + let mut contents = Vec::new(); + pkt.write(&mut contents); - self.fmt.send(&mut self.dest, buf.as_ref()).await + // Send the packet with the appropriate header. + self.fmt.send(&mut self.rw, &contents).await } pub async fn read(&mut self) -> io::Result { use crate::net::serialize::VecPacketDeserializer; - let buf = self.fmt.recieve(&mut self.src).await?; + let buf = self.fmt.recieve(&mut self.rw).await?; St::Serverbound::read(&mut VecPacketDeserializer::new(buf.as_ref())) .map_err(|err| io::Error::new(io::ErrorKind::Other, err)) @@ -39,8 +42,7 @@ impl Connection { fn into_state(self) -> Connection { Connection { - src: self.src, - dest: self.dest, + rw: self.rw, fmt: self.fmt, st: PhantomData, } @@ -53,13 +55,8 @@ impl Connection { impl Connection { pub fn new(stream: TcpStream) -> Self { - use tokio::io::{BufReader, BufWriter}; - - let (src, dest) = stream.into_split(); - Connection { - src: BufReader::new(src), - dest: BufWriter::new(dest), + rw: Box::new(BufStream::new(stream)), fmt: Box::new(DefaultPacketFormat), st: PhantomData, } @@ -90,7 +87,7 @@ impl Connection { // Further packets will use the new compression threshold. match threshold { Some(threshold) => { - self.fmt = Box::new(CompressedPacketFormat { threshold: threshold as usize }); + self.fmt = Box::new(CompressedPacketFormat::new(threshold as usize)); }, None => { self.fmt = Box::new(DefaultPacketFormat); @@ -100,6 +97,22 @@ impl Connection { Ok(()) } + /// WARNING: This function is not idempontent. + /// Calling it twice will result in the underlying stream getting encrypted twice. + pub fn set_encryption(self, secret: &[u8]) -> Result { + use cfb8::Cfb8; + use cfb8::stream_cipher::NewStreamCipher; + use crate::net::connection::stream::encrypted::EncryptedStream; + + let cipher: Cfb8 = Cfb8::new_var(secret, secret).map_err(|err| err.to_string())?; + + Ok(Connection { + rw: Box::new(EncryptedStream::new(self.rw, cipher)), + fmt: self.fmt, + st: PhantomData, + }) + } + pub fn into_play(self) -> Connection { self.into_state() } diff --git a/src/net/connection/packet_format.rs b/src/net/connection/packet_format.rs index 6a3124a..dd92608 100644 --- a/src/net/connection/packet_format.rs +++ b/src/net/connection/packet_format.rs @@ -2,31 +2,33 @@ pub mod compressed; pub mod default; use async_trait::async_trait; -use crate::net::{Reader, Writer}; use std::io; -use tokio::io::AsyncReadExt; +use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite}; + +pub type Reader = dyn AsyncRead + Unpin + Send; +pub type Writer = dyn AsyncWrite + Unpin + Send; #[async_trait] pub trait PacketFormat: Send + Sync { - async fn send(&self, dest: &mut Writer, data: &[u8]) -> io::Result<()>; async fn recieve(&self, src: &mut Reader) -> io::Result>; + async fn send(&self, dest: &mut Writer, data: &[u8]) -> io::Result<()>; } -pub const MAX_CLIENT_PACKET_SIZE: usize = 32767; +/// A completely arbitrary limitation on the maximum size of a recieved packet. +pub const MAX_PACKET_SIZE: usize = 35565; pub async fn read_varint(src: &mut Reader) -> io::Result<(usize, i32)> { - let mut length = 1; + let mut num_read: usize = 0; let mut acc = 0; - while length <= 5 { + while num_read < 5 { let byte = src.read_u8().await?; - acc |= (byte & 0b01111111) as i32; + acc |= ((byte & 0b01111111) as i32) << num_read * 7; + + num_read += 1; if byte & 0b10000000 == 0 { - return Ok((length, acc)); + return Ok((num_read, acc)); } - - acc = acc << 7; - length += 1; } Err(io::Error::new(io::ErrorKind::Other, "VarInt was too long.".to_string())) diff --git a/src/net/connection/packet_format/compressed.rs b/src/net/connection/packet_format/compressed.rs index 9961661..300b85e 100644 --- a/src/net/connection/packet_format/compressed.rs +++ b/src/net/connection/packet_format/compressed.rs @@ -1,69 +1,44 @@ use async_trait::async_trait; -use crate::net::{Reader, Writer}; -use crate::net::connection::packet_format::{PacketFormat, MAX_CLIENT_PACKET_SIZE, read_varint}; +use crate::net::connection::packet_format:: + {PacketFormat, Reader, Writer, MAX_PACKET_SIZE, read_varint}; use std::boxed::Box; use std::io; -use tokio::io::AsyncReadExt; pub struct CompressedPacketFormat { - pub threshold: usize, + threshold: usize, } +impl CompressedPacketFormat { + pub fn new(threshold: usize) -> Self { + Self { + threshold: threshold, + } + } +} + +// A compressed header is in this format: +// +// packet_length: VarInt +// uncompressed_length: VarInt +// data: [u8] +// +// The packet length is the size of the entire packet in bytes, +// including the uncompressed length. +// The uncompressed length is the size of the uncompressed data in bytes, +// or if it is zero, indicates that the data is not compressed. +// This is followed by the data, either compressed or uncompressed. + #[async_trait] impl PacketFormat for CompressedPacketFormat { - async fn send(&self, dest: &mut Writer, uncompressed_data: &[u8]) -> io::Result<()> { - use crate::net::serialize::{PacketSerializer, VarInt}; - - // If the length of the uncompressed packet is less than the threshold, - // then we do not compress the packet and set the data_length field to 0. - // Otherwise, data_length is set to the length of the uncompressed packet. - let will_compress = uncompressed_data.len() >= self.threshold; - - let data_length = if will_compress { uncompressed_data.len() } else { 0 }; - let mut data_length_buf = Vec::with_capacity(5); - data_length_buf.write(VarInt(data_length as i32)); - - let mut compression_buf; - let data = if will_compress { - use flate2::{Compress, Compression, FlushCompress}; - - // 1024 is just an arbitrary amount of extra space reserved - // in case the output data ends up larger than the input data - // (e.g. due to the zlib header). - // FIXME: Further research to figure out the exact maximum capacity necessary. - // Perhaps you only need space for the header and the data itself can't get bigger? - // And what is the limit to how much bigger the data will get? - // Currently I don't actually know for a fact that this won't ever drop data. - compression_buf = Vec::with_capacity(1024 + uncompressed_data.len()); - Compress::new(Compression::best(), true) - .compress_vec(uncompressed_data, &mut compression_buf, FlushCompress::Finish) - .map_err(|e| io::Error::new(io::ErrorKind::Other, e.to_string()))?; - compression_buf.as_slice() - } else { - uncompressed_data - }; - - let mut packet_length_buf = Vec::with_capacity(5); - packet_length_buf.write(VarInt((data_length_buf.len() + data.len()) as i32)); - - { - use tokio::io::AsyncWriteExt; - - dest.write(packet_length_buf.as_slice()).await?; - dest.write(data_length_buf.as_slice()).await?; - dest.write(data).await?; - dest.flush().await?; - } - - Ok(()) - } - async fn recieve(&self, src: &mut Reader) -> io::Result> { + use tokio::io::AsyncReadExt; + + // First we read in the packet and uncompressed data lengths. let (_, packet_length) = read_varint(src).await?; if packet_length < 0 { return Err(io::Error::new(io::ErrorKind::Other, "Packet length was negative.")); } - if packet_length > MAX_CLIENT_PACKET_SIZE as i32 { + if packet_length > MAX_PACKET_SIZE as i32 { return Err(io::Error::new(io::ErrorKind::Other, "Packet was too long.")); } let packet_length = packet_length as usize; @@ -72,30 +47,88 @@ impl PacketFormat for CompressedPacketFormat { if data_length < 0 { return Err(io::Error::new(io::ErrorKind::Other, "Data length was negative.")); } - if data_length > MAX_CLIENT_PACKET_SIZE as i32 { + if data_length > MAX_PACKET_SIZE as i32 { return Err(io::Error::new(io::ErrorKind::Other, "Data was too long.")); } let data_length = data_length as usize; - let mut buf = Vec::with_capacity(packet_length); - buf.resize(packet_length, 0); - src.read_exact(buf.as_mut_slice()).await?; + // Now we recieve the remainder of the packet's data. + let mut data = Vec::with_capacity(packet_length - data_length_size); + data.resize(packet_length, 0); + src.read_exact(data.as_mut_slice()).await?; - let decompressed_buf = if data_length != 0 { - use flate2::{Decompress, FlushDecompress}; + // If the data was not compressed, we simply return it. + if data_length == 0 { + return Ok(data.into_boxed_slice()); + } - let mut decompressed_buf = Vec::with_capacity(data_length); - decompressed_buf.resize(data_length, 0); - Decompress::new(true) - .decompress(&buf, decompressed_buf.as_mut_slice(), FlushDecompress::Finish) + // Otherwise, we decompress it. + let mut decompressed = Vec::new(); + decompressed.resize(data_length, 0); + + use flate2::{Decompress, FlushDecompress}; + Decompress::new(true) + .decompress(&data, decompressed.as_mut_slice(), FlushDecompress::Finish) + .map_err(|e| io::Error::new(io::ErrorKind::Other, e.to_string()))?; + + Ok(decompressed.into_boxed_slice()) + } + + async fn send(&self, dest: &mut Writer, data: &[u8]) -> io::Result<()> { + use crate::net::serialize::{PacketSerializer, VarInt}; + + // If the length of the uncompressed data exceeds the threshold, + // then we will compress this packet. + if data.len() >= self.threshold { + // Now we compress the data. + use flate2::{Compress, FlushCompress}; + + // 1024 is just an arbitrary amount of extra space reserved + // in case the output data ends up larger than the input data + // (e.g. due to the zlib header). + // FIXME: Further research to figure out the exact maximum capacity necessary. + // Perhaps you only need space for the header and the data itself can't get bigger? + // And what is the limit to how much bigger the data will get? + // Currently I don't actually know for a fact that this won't ever drop data. + let mut compressed = Vec::with_capacity(1024 + data.len()); + Compress::new(flate2::Compression::best(), true) + .compress_vec(data, &mut compressed, FlushCompress::Finish) .map_err(|e| io::Error::new(io::ErrorKind::Other, e.to_string()))?; - decompressed_buf - } else { - let mut decompressed_buf = Vec::with_capacity(packet_length - data_length_size); - decompressed_buf.copy_from_slice(&buf[data_length_size..]); - decompressed_buf - }; - Ok(decompressed_buf.into_boxed_slice()) + // Since the packet is compressed, + // data_length will be the length of the uncompressed data. + let mut data_length_buf = Vec::with_capacity(5); + data_length_buf.write(VarInt(data.len() as i32)); + + let mut packet_length_buf = Vec::with_capacity(5); + packet_length_buf.write(VarInt((data_length_buf.len() + compressed.len()) as i32)); + + { + // I have to keep this import in a block so that + // it won't conflict with PacketSerialize::write. + use tokio::io::AsyncWriteExt; + dest.write(packet_length_buf.as_slice()).await?; + dest.write(data_length_buf.as_slice()).await?; + dest.write(compressed.as_slice()).await?; + dest.flush().await?; + Ok(()) + } + } else { + // Since the packet is uncompressed, + // the packet length is just the length of the data plus the data_length, + // which will just be 0x00 (1 byte long) because the data isn't compressed. + let mut packet_length_buf = Vec::with_capacity(5); + packet_length_buf.write(VarInt(data.len() as i32 + 1)); + + { + use tokio::io::AsyncWriteExt; + + dest.write(packet_length_buf.as_slice()).await?; + dest.write_u8(0x00).await?; + dest.write(data).await?; + dest.flush().await?; + Ok(()) + } + } } } diff --git a/src/net/connection/packet_format/default.rs b/src/net/connection/packet_format/default.rs index 133bd69..1163c87 100644 --- a/src/net/connection/packet_format/default.rs +++ b/src/net/connection/packet_format/default.rs @@ -1,14 +1,29 @@ use async_trait::async_trait; -use crate::net::{Reader, Writer}; -use crate::net::connection::packet_format::{PacketFormat, MAX_CLIENT_PACKET_SIZE, read_varint}; +use crate::net::connection::packet_format:: + {PacketFormat, Reader, Writer, MAX_PACKET_SIZE, read_varint}; use std::boxed::Box; use std::io; -use tokio::io::AsyncReadExt; pub struct DefaultPacketFormat; #[async_trait] impl PacketFormat for DefaultPacketFormat { + async fn recieve(&self, src: &mut Reader) -> io::Result> { + use tokio::io::AsyncReadExt; + + let (_, length) = read_varint(src).await?; + if length > MAX_PACKET_SIZE as i32 { + return Err(io::Error::new(io::ErrorKind::Other, "Packet was too long.".to_string())); + } + let length = length as usize; + + let mut buf = Vec::with_capacity(length); + buf.resize(length, 0); + src.read_exact(buf.as_mut_slice()).await?; + + Ok(buf.into_boxed_slice()) + } + async fn send(&self, dest: &mut Writer, data: &[u8]) -> io::Result<()> { use crate::net::serialize::{PacketSerializer, VarInt}; @@ -25,18 +40,4 @@ impl PacketFormat for DefaultPacketFormat { Ok(()) } - - async fn recieve(&self, src: &mut Reader) -> io::Result> { - let (_, length) = read_varint(src).await?; - if length > MAX_CLIENT_PACKET_SIZE as i32 { - return Err(io::Error::new(io::ErrorKind::Other, "Packet was too long.".to_string())); - } - let length = length as usize; - - let mut buf = Vec::with_capacity(length); - buf.resize(length, 0); - src.read_exact(buf.as_mut_slice()).await?; - - Ok(buf.into_boxed_slice()) - } } diff --git a/src/net/connection/stream.rs b/src/net/connection/stream.rs new file mode 100644 index 0000000..0248e98 --- /dev/null +++ b/src/net/connection/stream.rs @@ -0,0 +1,6 @@ +pub mod encrypted; + +use tokio::io::{AsyncRead, AsyncWrite}; + +pub trait Stream: AsyncRead + AsyncWrite + Send + Unpin { } +impl Stream for S { } diff --git a/src/net/connection/stream/encrypted.rs b/src/net/connection/stream/encrypted.rs new file mode 100644 index 0000000..5cefb62 --- /dev/null +++ b/src/net/connection/stream/encrypted.rs @@ -0,0 +1,88 @@ +use aes::Aes128; +use cfb8::Cfb8; +use cfb8::stream_cipher::StreamCipher; +use crate::net::connection::stream::Stream; +use std::pin::Pin; +use std::task::Poll; +use std::task::Context; +use tokio::io::{AsyncRead, AsyncWrite, Result}; + +pub struct EncryptedStream { + rw: Box, + cipher: Cfb8, + write_buf: Vec, +} + +impl EncryptedStream { + pub fn new(rw: Box, cipher: Cfb8) -> Self { + Self { + rw: rw, + cipher: cipher, + write_buf: Vec::new(), + } + } + + fn flush(&mut self, cx: &mut Context) -> Poll> { + // We don't know when the internal writer will be ready, + // so we have to coax it into returning "pending" and scheduling an interrupt for us. + // Either that, or we finish writing our buffer and flush. + loop { + if self.write_buf.len() == 0 { + break; + } + + match Pin::new(&mut self.rw).poll_write(cx, &self.write_buf) { + Poll::Ready(Ok(length)) => { + let mut new_buf = Vec::new(); + new_buf.copy_from_slice(&self.write_buf[length..]); + self.write_buf = new_buf; + }, + other => return other.map(|x| x.map(|_| ())) + } + } + + Pin::new(&mut self.rw).poll_flush(cx) + } +} + +impl AsyncRead for EncryptedStream { + fn poll_read(self: Pin<&mut Self>, cx: &mut Context, buf: &mut [u8]) -> Poll> { + let me = Pin::into_inner(self); + + match Pin::new(&mut me.rw).poll_read(cx, buf) { + Poll::Ready(Ok(bytes)) => { + me.cipher.decrypt(&mut buf[..bytes]); + Poll::Ready(Ok(bytes)) + }, + other => other + } + } +} + +impl AsyncWrite for EncryptedStream { + fn poll_write(self: Pin<&mut Self>, _cx: &mut Context, buf: &[u8]) -> Poll> { + let me = Pin::into_inner(self); + + let index = me.write_buf.len(); + // Copy data to our write buffer and then encrypt it. + me.write_buf.extend_from_slice(buf); + me.cipher.encrypt(&mut me.write_buf[index..]); + + Poll::Ready(Ok(buf.len())) + } + + fn poll_flush(self: Pin<&mut Self>, cx: &mut Context) -> Poll> { + Pin::into_inner(self).flush(cx) + } + + fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context) -> Poll> { + let me = Pin::into_inner(self); + + match me.flush(cx) { + Poll::Ready(Ok(())) => { + Pin::new(&mut me.rw).poll_shutdown(cx) + }, + other => other + } + } +} diff --git a/src/net/mod.rs b/src/net/mod.rs index a03c315..fca7f30 100644 --- a/src/net/mod.rs +++ b/src/net/mod.rs @@ -2,9 +2,3 @@ pub mod chat; pub mod connection; pub mod protocol; pub mod serialize; - -use tokio::io::{BufReader, BufWriter}; -use tokio::net::tcp::{OwnedReadHalf, OwnedWriteHalf}; - -pub type Reader = BufReader; -pub type Writer = BufWriter; diff --git a/src/net/serialize.rs b/src/net/serialize.rs index 95ff336..85447f4 100644 --- a/src/net/serialize.rs +++ b/src/net/serialize.rs @@ -142,13 +142,15 @@ macro_rules! impl_varnum { impl PacketReadable for $name { fn read(deser: &mut impl PacketDeserializer) -> Result { - let mut length = 1; + let mut num_read: usize = 0; let mut acc = 0; - while length <= $length { + while num_read < $length { // If the highest bit is set, there are further bytes to be read; // the rest of the bits are the actual bits of the number. let read = deser.read::()?; - acc |= (read & 0b01111111) as $wraps; + acc |= ((read & 0b01111111) as $wraps) << num_read * 7; + + num_read += 1; if (read & 0b10000000) == 0 { // There are no more bytes. @@ -157,7 +159,6 @@ macro_rules! impl_varnum { // Make space for the rest of the bits. acc = acc << 7; - length += 1; } Err(format!("VarNum was more than {} bytes.", $length))