From 28fee8eee750585ff039df8886f25ebd1375f917 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Sun, 28 Jun 2020 22:23:48 -0400 Subject: [PATCH 01/48] initial commit --- .gitignore | 2 + Cargo.lock | 173 ++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 19 ++++++ README.md | 6 ++ src/main.rs | 6 ++ 5 files changed, 206 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 README.md create mode 100644 src/main.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000..42f4a1a64a --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +target/ +*~ diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000000..2560e14ee8 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,173 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "aho-corasick" +version = "0.7.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "043164d8ba5c4c3035fec9bbee8647c0261d788f3474306f93bb65901cae0e86" +dependencies = [ + "memchr", +] + +[[package]] +name = "anyhow" +version = "1.0.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85bb70cc08ec97ca5450e6eba421deeea5f172c0fc61f78b5357b2a8e8be195f" + +[[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 = "cfg-if" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" + +[[package]] +name = "env_logger" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44533bbbb3bb3c1fa17d9f2e4e38bbbaf8396ba82193c4cb1b6445d711445d36" +dependencies = [ + "atty", + "humantime", + "log", + "regex", + "termcolor", +] + +[[package]] +name = "hermit-abi" +version = "0.1.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9586eedd4ce6b3c498bc3b4dd92fc9f11166aa908a914071953768066c67909" +dependencies = [ + "libc", +] + +[[package]] +name = "humantime" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df004cfca50ef23c36850aaaa59ad52cc70d0e90243c3c7737a4dd32dc7a3c4f" +dependencies = [ + "quick-error", +] + +[[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.71" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9457b06509d27052635f90d6466700c65095fdf75409b3fbdd903e988b886f49" + +[[package]] +name = "log" +version = "0.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "14b6052be84e6b71ab17edffc2eeabf5c2c3ae1fdb464aae35ac50c67a44e1f7" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "memchr" +version = "2.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3728d817d99e5ac407411fa471ff9800a778d88a24685968b36824eaf4bee400" + +[[package]] +name = "quick-error" +version = "1.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0" + +[[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 = "termcolor" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb6bfa289a4d7c5766392812c0a1f4c1ba45afa1ad47803c11e1f407d846d75f" +dependencies = [ + "winapi-util", +] + +[[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 = "voder-proxy" +version = "0.0.1" +dependencies = [ + "anyhow", + "env_logger", + "log", +] + +[[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/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000000..12a6e6b239 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,19 @@ +[package] +name = "voder-proxy" +edition = "2018" +version = "0.0.1" +authors = [ + "Brandon Thomas ", + "Brandon Thomas ", +] +publish = false + +[[bin]] +name = "voder-proxy" +path = "src/main.rs" + +[dependencies] +anyhow = "1.0.31" +env_logger = "0.7.1" +log = "0.4.8" + diff --git a/README.md b/README.md new file mode 100644 index 0000000000..9d22da4084 --- /dev/null +++ b/README.md @@ -0,0 +1,6 @@ +voder-proxy +=========== + +Intercept requests to `voder/tts_service` and determine the appropriate +backend to send them to. This acts as a load balancer of sorts. + diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000000..b3e94e4fd9 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,6 @@ + + +fn main() { + println!("WIP"); +} + From 97e94d9e56e615ed7e39c1915e7f86b2068bdbcd Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Sun, 28 Jun 2020 23:44:16 -0400 Subject: [PATCH 02/48] This proxy works! --- .gitignore | 3 +- Cargo.lock | 618 ++++++++++++++++++++++++++++++++++++++++++++- Cargo.toml | 6 +- src/main.rs | 57 ++++- src/middlewares.rs | 66 +++++ 5 files changed, 744 insertions(+), 6 deletions(-) create mode 100644 src/middlewares.rs diff --git a/.gitignore b/.gitignore index 42f4a1a64a..a6eaf47f59 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ -target/ *~ +.idea/ +target/ diff --git a/Cargo.lock b/Cargo.lock index 2560e14ee8..d2a4e5ac3d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -23,7 +23,36 @@ checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" dependencies = [ "hermit-abi", "libc", - "winapi", + "winapi 0.3.9", +] + +[[package]] +name = "autocfg" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8aac770f1885fd7e387acedd76065302551364496e46b3dd00860b2f8359b9d" + +[[package]] +name = "bitflags" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" + +[[package]] +name = "byteorder" +version = "1.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08c48aae112d48ed9f069b33538ea9e3e90aa263cfa3d1c24309612b1f7472de" + +[[package]] +name = "bytes" +version = "0.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "206fdffcfa2df7cbe15601ef46c813fce0965eb3286db6b56c583b814b51c81c" +dependencies = [ + "byteorder", + "either", + "iovec", ] [[package]] @@ -32,6 +61,69 @@ version = "0.1.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" +[[package]] +name = "cloudabi" +version = "0.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f" +dependencies = [ + "bitflags", +] + +[[package]] +name = "crossbeam-deque" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f02af974daeee82218205558e51ec8768b48cf524bd01d550abe5573a608285" +dependencies = [ + "crossbeam-epoch", + "crossbeam-utils", + "maybe-uninit", +] + +[[package]] +name = "crossbeam-epoch" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "058ed274caafc1f60c4997b5fc07bf7dc7cca454af7c6e81edffe5f33f70dace" +dependencies = [ + "autocfg", + "cfg-if", + "crossbeam-utils", + "lazy_static", + "maybe-uninit", + "memoffset", + "scopeguard", +] + +[[package]] +name = "crossbeam-queue" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "774ba60a54c213d409d5353bda12d49cd68d14e45036a285234c8d6f91f92570" +dependencies = [ + "cfg-if", + "crossbeam-utils", + "maybe-uninit", +] + +[[package]] +name = "crossbeam-utils" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3c7c73a2d1e9fc0886a08b93e98eb643461230d5f1925e4036204d5f2e261a8" +dependencies = [ + "autocfg", + "cfg-if", + "lazy_static", +] + +[[package]] +name = "either" +version = "1.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb1f6b1ce1c140482ea30ddd3335fc0024ac7ee112895426e0a629a6c20adfe3" + [[package]] name = "env_logger" version = "0.7.1" @@ -45,6 +137,62 @@ dependencies = [ "termcolor", ] +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + +[[package]] +name = "fuchsia-zircon" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2e9763c69ebaae630ba35f74888db465e49e259ba1bc0eda7d06f4a067615d82" +dependencies = [ + "bitflags", + "fuchsia-zircon-sys", +] + +[[package]] +name = "fuchsia-zircon-sys" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7" + +[[package]] +name = "futures" +version = "0.1.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b980f2816d6ee8673b6517b52cb0e808a180efc92e5c19d02cdda79066703ef" + +[[package]] +name = "futures-cpupool" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ab90cde24b3319636588d0c35fe03b1333857621051837ed769faefb4c2162e4" +dependencies = [ + "futures", + "num_cpus", +] + +[[package]] +name = "h2" +version = "0.1.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a5b34c246847f938a410a03c5458c7fee2274436675e76d8b903c08efc29c462" +dependencies = [ + "byteorder", + "bytes", + "fnv", + "futures", + "http", + "indexmap", + "log", + "slab", + "string", + "tokio-io", +] + [[package]] name = "hermit-abi" version = "0.1.14" @@ -54,6 +202,35 @@ dependencies = [ "libc", ] +[[package]] +name = "http" +version = "0.1.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d6ccf5ede3a895d8856620237b2f02972c1bbc78d2965ad7fe8838d4a0ed41f0" +dependencies = [ + "bytes", + "fnv", + "itoa", +] + +[[package]] +name = "http-body" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6741c859c1b2463a423a1dbce98d418e6c3c3fc720fb0d45528657320920292d" +dependencies = [ + "bytes", + "futures", + "http", + "tokio-buf", +] + +[[package]] +name = "httparse" +version = "1.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd179ae861f0c2e53da70d892f5f3029f9594be0c41dc5269cd371691b1dc2f9" + [[package]] name = "humantime" version = "1.3.0" @@ -63,6 +240,82 @@ dependencies = [ "quick-error", ] +[[package]] +name = "hyper" +version = "0.12.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9dbe6ed1438e1f8ad955a4701e9a944938e9519f6888d12d8558b645e247d5f6" +dependencies = [ + "bytes", + "futures", + "futures-cpupool", + "h2", + "http", + "http-body", + "httparse", + "iovec", + "itoa", + "log", + "net2", + "rustc_version", + "time", + "tokio", + "tokio-buf", + "tokio-executor", + "tokio-io", + "tokio-reactor", + "tokio-tcp", + "tokio-threadpool", + "tokio-timer", + "want", +] + +[[package]] +name = "hyper-reverse-proxy" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "474f19c624c13005d914fc4a73aa2a2921b0371f5f7b748146f5130685492ce8" +dependencies = [ + "futures", + "hyper", + "lazy_static", + "unicase", +] + +[[package]] +name = "indexmap" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c398b2b113b55809ceb9ee3e753fcbac793f1956663f3c36549c1346015c2afe" +dependencies = [ + "autocfg", +] + +[[package]] +name = "iovec" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b2b3ea6ff95e175473f8ffe6a7eb7c00d054240321b84c57051175fe3c1e075e" +dependencies = [ + "libc", +] + +[[package]] +name = "itoa" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc6f3ad7b9d11a0c00842ff8de1b60ee58661048eb8049ed33c73594f359d7e6" + +[[package]] +name = "kernel32-sys" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d" +dependencies = [ + "winapi 0.2.8", + "winapi-build", +] + [[package]] name = "lazy_static" version = "1.4.0" @@ -75,6 +328,15 @@ version = "0.2.71" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9457b06509d27052635f90d6466700c65095fdf75409b3fbdd903e988b886f49" +[[package]] +name = "lock_api" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4da24a77a3d8a6d4862d95f72e6fdb9c09a643ecdb402d754004a557f2bec75" +dependencies = [ + "scopeguard", +] + [[package]] name = "log" version = "0.4.8" @@ -84,18 +346,117 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "maybe-uninit" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60302e4db3a61da70c0cb7991976248362f30319e88850c487b9b95bbf059e00" + [[package]] name = "memchr" version = "2.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3728d817d99e5ac407411fa471ff9800a778d88a24685968b36824eaf4bee400" +[[package]] +name = "memoffset" +version = "0.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b4fc2c02a7e374099d4ee95a193111f72d2110197fe200272371758f6c3643d8" +dependencies = [ + "autocfg", +] + +[[package]] +name = "mio" +version = "0.6.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fce347092656428bc8eaf6201042cb551b8d67855af7374542a92a0fbfcac430" +dependencies = [ + "cfg-if", + "fuchsia-zircon", + "fuchsia-zircon-sys", + "iovec", + "kernel32-sys", + "libc", + "log", + "miow", + "net2", + "slab", + "winapi 0.2.8", +] + +[[package]] +name = "miow" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8c1f2f3b1cf331de6896aabf6e9d55dca90356cc9960cca7eaaf408a355ae919" +dependencies = [ + "kernel32-sys", + "net2", + "winapi 0.2.8", + "ws2_32-sys", +] + +[[package]] +name = "net2" +version = "0.2.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2ba7c918ac76704fb42afcbbb43891e72731f3dcca3bef2a19786297baf14af7" +dependencies = [ + "cfg-if", + "libc", + "winapi 0.3.9", +] + +[[package]] +name = "num_cpus" +version = "1.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05499f3756671c15885fee9034446956fff3f243d6077b91e5767df161f766b3" +dependencies = [ + "hermit-abi", + "libc", +] + +[[package]] +name = "parking_lot" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f842b1982eb6c2fe34036a4fbfb06dd185a3f5c8edfaacdf7d1ea10b07de6252" +dependencies = [ + "lock_api", + "parking_lot_core", + "rustc_version", +] + +[[package]] +name = "parking_lot_core" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b876b1b9e7ac6e1a74a6da34d25c42e17e8862aa409cbbbdcfc8d86c6f3bc62b" +dependencies = [ + "cfg-if", + "cloudabi", + "libc", + "redox_syscall", + "rustc_version", + "smallvec", + "winapi 0.3.9", +] + [[package]] name = "quick-error" version = "1.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0" +[[package]] +name = "redox_syscall" +version = "0.1.56" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2439c63f3f6139d1b57529d16bc3b8bb855230c8efcc5d3a896c8bea7c3b1e84" + [[package]] name = "regex" version = "1.3.9" @@ -114,6 +475,60 @@ version = "0.6.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "26412eb97c6b088a6997e05f69403a802a92d520de2f8e63c2b65f9e0f47c4e8" +[[package]] +name = "rustc_version" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a" +dependencies = [ + "semver", +] + +[[package]] +name = "scopeguard" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" + +[[package]] +name = "semver" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403" +dependencies = [ + "semver-parser", +] + +[[package]] +name = "semver-parser" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" + +[[package]] +name = "slab" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c111b5bd5695e56cffe5129854aa230b39c93a305372fdbb2668ca2394eea9f8" + +[[package]] +name = "smallvec" +version = "0.6.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7b0758c52e15a8b5e3691eae6cc559f08eee9406e548a4477ba4e67770a82b6" +dependencies = [ + "maybe-uninit", +] + +[[package]] +name = "string" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d24114bfcceb867ca7f71a0d3fe45d45619ec47a6fbfa98cb14e14250bfa5d6d" +dependencies = [ + "bytes", +] + [[package]] name = "termcolor" version = "1.1.0" @@ -132,15 +547,198 @@ 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 = "tokio" +version = "0.1.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a09c0b5bb588872ab2f09afa13ee6e9dac11e10a0ec9e8e3ba39a5a5d530af6" +dependencies = [ + "bytes", + "futures", + "mio", + "num_cpus", + "tokio-current-thread", + "tokio-executor", + "tokio-io", + "tokio-reactor", + "tokio-threadpool", + "tokio-timer", +] + +[[package]] +name = "tokio-buf" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8fb220f46c53859a4b7ec083e41dec9778ff0b1851c0942b211edb89e0ccdc46" +dependencies = [ + "bytes", + "either", + "futures", +] + +[[package]] +name = "tokio-current-thread" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1de0e32a83f131e002238d7ccde18211c0a5397f60cbfffcb112868c2e0e20e" +dependencies = [ + "futures", + "tokio-executor", +] + +[[package]] +name = "tokio-executor" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fb2d1b8f4548dbf5e1f7818512e9c406860678f29c300cdf0ebac72d1a3a1671" +dependencies = [ + "crossbeam-utils", + "futures", +] + +[[package]] +name = "tokio-io" +version = "0.1.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57fc868aae093479e3131e3d165c93b1c7474109d13c90ec0dda2a1bbfff0674" +dependencies = [ + "bytes", + "futures", + "log", +] + +[[package]] +name = "tokio-reactor" +version = "0.1.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09bc590ec4ba8ba87652da2068d150dcada2cfa2e07faae270a5e0409aa51351" +dependencies = [ + "crossbeam-utils", + "futures", + "lazy_static", + "log", + "mio", + "num_cpus", + "parking_lot", + "slab", + "tokio-executor", + "tokio-io", + "tokio-sync", +] + +[[package]] +name = "tokio-sync" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "edfe50152bc8164fcc456dab7891fa9bf8beaf01c5ee7e1dd43a397c3cf87dee" +dependencies = [ + "fnv", + "futures", +] + +[[package]] +name = "tokio-tcp" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "98df18ed66e3b72e742f185882a9e201892407957e45fbff8da17ae7a7c51f72" +dependencies = [ + "bytes", + "futures", + "iovec", + "mio", + "tokio-io", + "tokio-reactor", +] + +[[package]] +name = "tokio-threadpool" +version = "0.1.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df720b6581784c118f0eb4310796b12b1d242a7eb95f716a8367855325c25f89" +dependencies = [ + "crossbeam-deque", + "crossbeam-queue", + "crossbeam-utils", + "futures", + "lazy_static", + "log", + "num_cpus", + "slab", + "tokio-executor", +] + +[[package]] +name = "tokio-timer" +version = "0.2.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93044f2d313c95ff1cb7809ce9a7a05735b012288a888b62d4434fd58c94f296" +dependencies = [ + "crossbeam-utils", + "futures", + "slab", + "tokio-executor", +] + +[[package]] +name = "try-lock" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e604eb7b43c06650e854be16a2a03155743d3752dd1c943f6829e26b7a36e382" + +[[package]] +name = "unicase" +version = "2.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50f37be617794602aabbeee0be4f259dc1778fabe05e2d67ee8f79326d5cb4f6" +dependencies = [ + "version_check", +] + +[[package]] +name = "version_check" +version = "0.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5a972e5669d67ba988ce3dc826706fb0a8b01471c088cb0b6110b805cc36aed" + [[package]] name = "voder-proxy" version = "0.0.1" dependencies = [ "anyhow", "env_logger", + "futures", + "hyper", + "hyper-reverse-proxy", "log", ] +[[package]] +name = "want" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6395efa4784b027708f7451087e647ec73cc74f5d9bc2e418404248d679a230" +dependencies = [ + "futures", + "log", + "try-lock", +] + +[[package]] +name = "winapi" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a" + [[package]] name = "winapi" version = "0.3.9" @@ -151,6 +749,12 @@ dependencies = [ "winapi-x86_64-pc-windows-gnu", ] +[[package]] +name = "winapi-build" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc" + [[package]] name = "winapi-i686-pc-windows-gnu" version = "0.4.0" @@ -163,7 +767,7 @@ version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" dependencies = [ - "winapi", + "winapi 0.3.9", ] [[package]] @@ -171,3 +775,13 @@ name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] +name = "ws2_32-sys" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d59cefebd0c892fa2dd6de581e937301d8552cb44489cdff035c6187cb63fa5e" +dependencies = [ + "winapi 0.2.8", + "winapi-build", +] diff --git a/Cargo.toml b/Cargo.toml index 12a6e6b239..cc200bae84 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,5 +15,9 @@ path = "src/main.rs" [dependencies] anyhow = "1.0.31" env_logger = "0.7.1" +futures = "0.1" +#hyper = "0.13.6" +hyper = "0.12" +hyper-reverse-proxy = "0.4" log = "0.4.8" - +#simple_proxy = "1.3.1" diff --git a/src/main.rs b/src/main.rs index b3e94e4fd9..36af190fbe 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,59 @@ +#[macro_use] extern crate log; -fn main() { - println!("WIP"); +/*#[derive(Deserialize)] +pub struct SpeakRequest { + /// Slug for the speaker + speaker: String, + /// Raw text to be spoken + text: String, +}*/ + +use hyper::server::conn::AddrStream; +use hyper::{Body, Request, Response, Server}; +use hyper::service::{service_fn, make_service_fn}; +use futures::future::{self, Future}; + +type BoxFut = Box, Error=hyper::Error> + Send>; + +fn debug_request(req: Request) -> BoxFut { + let body_str = format!("{:?}", req); + let response = Response::new(Body::from(body_str)); + Box::new(future::ok(response)) } +fn main() { + + // This is our socket address... + let addr = ([127, 0, 0, 1], 13900).into(); + + // A `Service` is needed for every connection. + let make_svc = make_service_fn(|socket: &AddrStream| { + let remote_addr = socket.remote_addr(); + service_fn(move |req: Request| { // returns BoxFut + + if req.uri().path().starts_with("/target/first") { + + // will forward requests to port 13901 + return hyper_reverse_proxy::call(remote_addr.ip(), "http://127.0.0.1:12345", req) + + } else if req.uri().path().starts_with("/target/second") { + + // will forward requests to port 13902 + return hyper_reverse_proxy::call(remote_addr.ip(), "http://127.0.0.1:3000", req) + + } else { + debug_request(req) + } + }) + }); + + let server = Server::bind(&addr) + .serve(make_svc) + .map_err(|e| eprintln!("server error: {}", e)); + + println!("Running server on {:?}", addr); + + // Run this server for... forever! + hyper::rt::run(server); +} \ No newline at end of file diff --git a/src/middlewares.rs b/src/middlewares.rs new file mode 100644 index 0000000000..fc51944362 --- /dev/null +++ b/src/middlewares.rs @@ -0,0 +1,66 @@ +//use chrono::{DateTime, Utc}; +//use serde_json; + +use hyper::{Body, Request, Response}; +use simple_proxy::proxy::error::MiddlewareError; +use simple_proxy::proxy::middleware::MiddlewareResult::Next; +use simple_proxy::proxy::middleware::{Middleware, MiddlewareResult}; +use simple_proxy::proxy::service::{ServiceContext, State}; + +#[derive(Clone, Default)] +pub struct Logger; + +/// # Panics +/// May panic if the request state has not been initialized in `before_request`. +/// e.g If a middleware responded early before the logger in `before_request`. +impl Middleware for Logger { + fn name() -> String { + String::from("Logger") + } + + fn before_request( + &mut self, + req: &mut Request, + context: &ServiceContext, + state: &State, + ) -> Result { + info!( + "[{}] Starting a {} request to {}", + &context.req_id.to_string()[..6], + req.method(), + req.uri() + ); + //let now = serde_json::to_string(&Utc::now()).expect("[Logger] Cannot serialize DateTime"); + self.set_state(context.req_id, state, "foo".to_string())?; + Ok(Next) + } + + fn after_request( + &mut self, + _res: Option<&mut Response>, + context: &ServiceContext, + state: &State, + ) -> Result { + let start_time = self.get_state(context.req_id, state)?; + match start_time { + Some(time) => { + //let start_time: DateTime = serde_json::from_str(&time)?; + + info!( + "[{}] Request took {}ms", + &context.req_id.to_string()[..6], + 12345 + //(Utc::now() - start_time).num_milliseconds() + ); + } + None => error!("[Logger] start time not found in state"), + } + Ok(Next) + } +} + +impl Logger { + pub fn new() -> Self { + Logger {} + } +} From d4853707c7d2e024774107602f0390d385137329 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Mon, 29 Jun 2020 00:24:14 -0400 Subject: [PATCH 03/48] Testing routing --- src/main.rs | 40 ++++++++++++++++++++-------- src/middlewares.rs | 66 ---------------------------------------------- 2 files changed, 29 insertions(+), 77 deletions(-) delete mode 100644 src/middlewares.rs diff --git a/src/main.rs b/src/main.rs index 36af190fbe..2ee5e5e32f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,13 +9,30 @@ pub struct SpeakRequest { text: String, }*/ -use hyper::server::conn::AddrStream; -use hyper::{Body, Request, Response, Server}; -use hyper::service::{service_fn, make_service_fn}; use futures::future::{self, Future}; +use hyper::server::conn::AddrStream; +use hyper::service::{service_fn, make_service_fn}; +use hyper::{Body, Request, Response, Server}; +use std::env; type BoxFut = Box, Error=hyper::Error> + Send>; +const ENV_ROUTE_ONE : &'static str = "ROUTE_ONE"; +const ENV_ROUTE_TWO : &'static str = "ROUTE_TWO"; + +const ROUTE_ONE_DEFAULT : &'static str = "http://127.0.0.1:12345"; +const ROUTE_TWO_DEFAULT : &'static str = "http://127.0.0.1:3000"; + +fn get_env_string(env_name: &str, default: &str) -> String { + match env::var(env_name).as_ref().ok() { + Some(s) => s.to_string(), + None => { + warn!("Env var '{}' not supplied. Using default '{}'.", env_name, default); + default.to_string() + }, + } +} + fn debug_request(req: Request) -> BoxFut { let body_str = format!("{:?}", req); let response = Response::new(Body::from(body_str)); @@ -23,6 +40,11 @@ fn debug_request(req: Request) -> BoxFut { } fn main() { + let route_one = get_env_string(ENV_ROUTE_ONE, ROUTE_ONE_DEFAULT); + let route_two = get_env_string(ENV_ROUTE_TWO, ROUTE_TWO_DEFAULT); + + info!("Route one: {}", route_one); + info!("Route two: {}", route_two); // This is our socket address... let addr = ([127, 0, 0, 1], 13900).into(); @@ -32,15 +54,11 @@ fn main() { let remote_addr = socket.remote_addr(); service_fn(move |req: Request| { // returns BoxFut - if req.uri().path().starts_with("/target/first") { + if req.uri().path().starts_with("/first") { + return hyper_reverse_proxy::call(remote_addr.ip(), &route_one, req) - // will forward requests to port 13901 - return hyper_reverse_proxy::call(remote_addr.ip(), "http://127.0.0.1:12345", req) - - } else if req.uri().path().starts_with("/target/second") { - - // will forward requests to port 13902 - return hyper_reverse_proxy::call(remote_addr.ip(), "http://127.0.0.1:3000", req) + } else if req.uri().path().starts_with("/second") { + return hyper_reverse_proxy::call(remote_addr.ip(), &route_two, req) } else { debug_request(req) diff --git a/src/middlewares.rs b/src/middlewares.rs deleted file mode 100644 index fc51944362..0000000000 --- a/src/middlewares.rs +++ /dev/null @@ -1,66 +0,0 @@ -//use chrono::{DateTime, Utc}; -//use serde_json; - -use hyper::{Body, Request, Response}; -use simple_proxy::proxy::error::MiddlewareError; -use simple_proxy::proxy::middleware::MiddlewareResult::Next; -use simple_proxy::proxy::middleware::{Middleware, MiddlewareResult}; -use simple_proxy::proxy::service::{ServiceContext, State}; - -#[derive(Clone, Default)] -pub struct Logger; - -/// # Panics -/// May panic if the request state has not been initialized in `before_request`. -/// e.g If a middleware responded early before the logger in `before_request`. -impl Middleware for Logger { - fn name() -> String { - String::from("Logger") - } - - fn before_request( - &mut self, - req: &mut Request, - context: &ServiceContext, - state: &State, - ) -> Result { - info!( - "[{}] Starting a {} request to {}", - &context.req_id.to_string()[..6], - req.method(), - req.uri() - ); - //let now = serde_json::to_string(&Utc::now()).expect("[Logger] Cannot serialize DateTime"); - self.set_state(context.req_id, state, "foo".to_string())?; - Ok(Next) - } - - fn after_request( - &mut self, - _res: Option<&mut Response>, - context: &ServiceContext, - state: &State, - ) -> Result { - let start_time = self.get_state(context.req_id, state)?; - match start_time { - Some(time) => { - //let start_time: DateTime = serde_json::from_str(&time)?; - - info!( - "[{}] Request took {}ms", - &context.req_id.to_string()[..6], - 12345 - //(Utc::now() - start_time).num_milliseconds() - ); - } - None => error!("[Logger] start time not found in state"), - } - Ok(Next) - } -} - -impl Logger { - pub fn new() -> Self { - Logger {} - } -} From de5a5b078332f4eda1a3177ed99ee47ff27a661b Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Mon, 29 Jun 2020 00:30:28 -0400 Subject: [PATCH 04/48] Add github workflow and dockerfile --- .dockerignore | 11 +++++++ .github/workflows/publish-to-gpr.yml | 22 ++++++++++++++ Cargo.lock | 24 +++++++-------- Cargo.toml | 4 +-- Dockerfile | 44 ++++++++++++++++++++++++++++ src/main.rs | 9 ++++-- 6 files changed, 97 insertions(+), 17 deletions(-) create mode 100644 .dockerignore create mode 100644 .github/workflows/publish-to-gpr.yml create mode 100644 Dockerfile diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000000..883b08a8df --- /dev/null +++ b/.dockerignore @@ -0,0 +1,11 @@ +.git +.idea/ +target/ + +# Ignore Files +*png +*pt +*pyc +*pyo +*wav +*~ diff --git a/.github/workflows/publish-to-gpr.yml b/.github/workflows/publish-to-gpr.yml new file mode 100644 index 0000000000..46f76a9d81 --- /dev/null +++ b/.github/workflows/publish-to-gpr.yml @@ -0,0 +1,22 @@ +name: Publish Docker image to GitHub Package Registry +on: push +jobs: + build: + runs-on: ubuntu-latest + if: github.ref == 'refs/heads/master' # Only on master + + steps: + - name: Copy Repo Files + uses: actions/checkout@master + + #This Action Emits 2 Variables, IMAGE_SHA_NAME and IMAGE_URL + #which you can reference in subsequent steps + - name: Publish Docker Image to GPR + uses: machine-learning-apps/gpr-docker-publish@master + id: docker + with: + IMAGE_NAME: 'tts-service-proxy' + DOCKERFILE_PATH: 'Dockerfile' + BUILD_CONTEXT: '.' + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/Cargo.lock b/Cargo.lock index d2a4e5ac3d..ae8a83cf7e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -695,6 +695,18 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e604eb7b43c06650e854be16a2a03155743d3752dd1c943f6829e26b7a36e382" +[[package]] +name = "tts-service-proxy" +version = "0.0.1" +dependencies = [ + "anyhow", + "env_logger", + "futures", + "hyper", + "hyper-reverse-proxy", + "log", +] + [[package]] name = "unicase" version = "2.6.0" @@ -710,18 +722,6 @@ version = "0.9.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b5a972e5669d67ba988ce3dc826706fb0a8b01471c088cb0b6110b805cc36aed" -[[package]] -name = "voder-proxy" -version = "0.0.1" -dependencies = [ - "anyhow", - "env_logger", - "futures", - "hyper", - "hyper-reverse-proxy", - "log", -] - [[package]] name = "want" version = "0.2.0" diff --git a/Cargo.toml b/Cargo.toml index cc200bae84..d7cedf24ad 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "voder-proxy" +name = "tts-service-proxy" edition = "2018" version = "0.0.1" authors = [ @@ -9,7 +9,7 @@ authors = [ publish = false [[bin]] -name = "voder-proxy" +name = "tts-service-proxy" path = "src/main.rs" [dependencies] diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000000..fa8c2bb368 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,44 @@ +# Unsure if I want to create minimal images yet that run under Alpine Linux. +# I have instructions for that here: https://hub.docker.com/r/echelon/testcontainer/dockerfile +FROM ubuntu:xenial as build +WORKDIR /tmp + +RUN apt-get update \ + && apt-get install -y \ + build-essential \ + curl \ + libssl-dev \ + libssl1.0.0 \ + pkg-config +RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs \ + | sh -s -- --default-toolchain stable -y + +COPY Cargo.lock . +COPY Cargo.toml . +COPY src/ ./src + +RUN $HOME/.cargo/bin/cargo fetch + +#RUN LD_LIBRARY_PATH=/usr/lib:${LD_LIBRARY_PATH} $HOME/.cargo/bin/cargo build +RUN LD_LIBRARY_PATH=/usr/lib:${LD_LIBRARY_PATH} $HOME/.cargo/bin/cargo build --release + +# Final image +FROM ubuntu:xenial +WORKDIR / + +#COPY --from=build /tmp/target/debug/tts-service-proxy / +COPY --from=build /tmp/target/release/tts-service-proxy / + +# SSL certs are required for crypto +COPY --from=build /etc/ssl /etc/ssl + +# Required dynamically linked libraries +COPY --from=build /lib/x86_64-linux-gnu/libssl.* /lib/x86_64-linux-gnu/ +COPY --from=build /lib/x86_64-linux-gnu/libcrypto.* /lib/x86_64-linux-gnu/ + +# Make sure all the links resolve +RUN ldd tts-service-proxy + +EXPOSE 8080 +CMD LD_LIBRARY_PATH=/usr/lib /tts-service-proxy + diff --git a/src/main.rs b/src/main.rs index 2ee5e5e32f..b917b03845 100644 --- a/src/main.rs +++ b/src/main.rs @@ -50,15 +50,18 @@ fn main() { let addr = ([127, 0, 0, 1], 13900).into(); // A `Service` is needed for every connection. - let make_svc = make_service_fn(|socket: &AddrStream| { + let make_svc = make_service_fn(move |socket: &AddrStream| { let remote_addr = socket.remote_addr(); + let route_a = route_one.clone(); + let route_b = route_two.clone(); + service_fn(move |req: Request| { // returns BoxFut if req.uri().path().starts_with("/first") { - return hyper_reverse_proxy::call(remote_addr.ip(), &route_one, req) + return hyper_reverse_proxy::call(remote_addr.ip(), &route_a, req) } else if req.uri().path().starts_with("/second") { - return hyper_reverse_proxy::call(remote_addr.ip(), &route_two, req) + return hyper_reverse_proxy::call(remote_addr.ip(), &route_b, req) } else { debug_request(req) From 461db14259235b6e565211605aceb12033e23e22 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Mon, 29 Jun 2020 01:55:43 -0400 Subject: [PATCH 05/48] Add curl in final image --- Dockerfile | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Dockerfile b/Dockerfile index fa8c2bb368..9ddf127cbc 100644 --- a/Dockerfile +++ b/Dockerfile @@ -36,6 +36,10 @@ COPY --from=build /etc/ssl /etc/ssl COPY --from=build /lib/x86_64-linux-gnu/libssl.* /lib/x86_64-linux-gnu/ COPY --from=build /lib/x86_64-linux-gnu/libcrypto.* /lib/x86_64-linux-gnu/ +# Copy curl utility +COPY --from=build /usr/bin/curl /usr/bin/ +COPY --from=build /lib/x86_64-linux-gnu/libcurl.* /lib/x86_64-linux-gnu/ + # Make sure all the links resolve RUN ldd tts-service-proxy From 4e97688e9b788e01bfb0074565a50a361dba32a5 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Mon, 29 Jun 2020 02:08:10 -0400 Subject: [PATCH 06/48] fix libcurl src --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 9ddf127cbc..26b332ab3e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -38,7 +38,7 @@ COPY --from=build /lib/x86_64-linux-gnu/libcrypto.* /lib/x86_64-linux-g # Copy curl utility COPY --from=build /usr/bin/curl /usr/bin/ -COPY --from=build /lib/x86_64-linux-gnu/libcurl.* /lib/x86_64-linux-gnu/ +COPY --from=build /usr/lib/x86_64-linux-gnu/libcurl* /lib/x86_64-linux-gnu/ # Make sure all the links resolve RUN ldd tts-service-proxy From fc50a06e7fc35553be3284b359c01efd1b861ae9 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Mon, 29 Jun 2020 02:19:24 -0400 Subject: [PATCH 07/48] another curl dep --- Dockerfile | 1 + 1 file changed, 1 insertion(+) diff --git a/Dockerfile b/Dockerfile index 26b332ab3e..db709db014 100644 --- a/Dockerfile +++ b/Dockerfile @@ -39,6 +39,7 @@ COPY --from=build /lib/x86_64-linux-gnu/libcrypto.* /lib/x86_64-linux-g # Copy curl utility COPY --from=build /usr/bin/curl /usr/bin/ COPY --from=build /usr/lib/x86_64-linux-gnu/libcurl* /lib/x86_64-linux-gnu/ +COPY --from=build /usr/lib/x86_64-linux-gnu/libidn.so* /lib/x86_64-linux-gnu/ # Make sure all the links resolve RUN ldd tts-service-proxy From dce95dea187298fe04587ce972cb7d71acbf2400 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Mon, 29 Jun 2020 02:24:19 -0400 Subject: [PATCH 08/48] all the libs --- Dockerfile | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/Dockerfile b/Dockerfile index db709db014..f3505e2abb 100644 --- a/Dockerfile +++ b/Dockerfile @@ -37,11 +37,18 @@ COPY --from=build /lib/x86_64-linux-gnu/libssl.* /lib/x86_64-linux-g COPY --from=build /lib/x86_64-linux-gnu/libcrypto.* /lib/x86_64-linux-gnu/ # Copy curl utility -COPY --from=build /usr/bin/curl /usr/bin/ -COPY --from=build /usr/lib/x86_64-linux-gnu/libcurl* /lib/x86_64-linux-gnu/ -COPY --from=build /usr/lib/x86_64-linux-gnu/libidn.so* /lib/x86_64-linux-gnu/ +COPY --from=build /usr/bin/curl /usr/bin/ +COPY --from=build /usr/lib/x86_64-linux-gnu/libcurl* /lib/x86_64-linux-gnu/ +COPY --from=build /usr/lib/x86_64-linux-gnu/libgnutls.so* /lib/x86_64-linux-gnu/ +COPY --from=build /usr/lib/x86_64-linux-gnu/libgssapi_krb5.so* /lib/x86_64-linux-gnu/ +COPY --from=build /usr/lib/x86_64-linux-gnu/libidn.so* /lib/x86_64-linux-gnu/ +COPY --from=build /usr/lib/x86_64-linux-gnu/liblber-2.4.so* /lib/x86_64-linux-gnu/ +COPY --from=build /usr/lib/x86_64-linux-gnu/libldap_r-2.4.so* /lib/x86_64-linux-gnu/ +COPY --from=build /usr/lib/x86_64-linux-gnu/libnettle.so* /lib/x86_64-linux-gnu/ +COPY --from=build /usr/lib/x86_64-linux-gnu/librtmp.so* /lib/x86_64-linux-gnu/ # Make sure all the links resolve +RUN ldd /usr/bin/curl RUN ldd tts-service-proxy EXPOSE 8080 From 67d094d38ef4c4ab2101affbbcacca5e0c4f6841 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Mon, 29 Jun 2020 02:35:39 -0400 Subject: [PATCH 09/48] screw it, install curl with apt --- Dockerfile | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/Dockerfile b/Dockerfile index f3505e2abb..c9f1595212 100644 --- a/Dockerfile +++ b/Dockerfile @@ -36,21 +36,15 @@ COPY --from=build /etc/ssl /etc/ssl COPY --from=build /lib/x86_64-linux-gnu/libssl.* /lib/x86_64-linux-gnu/ COPY --from=build /lib/x86_64-linux-gnu/libcrypto.* /lib/x86_64-linux-gnu/ -# Copy curl utility -COPY --from=build /usr/bin/curl /usr/bin/ -COPY --from=build /usr/lib/x86_64-linux-gnu/libcurl* /lib/x86_64-linux-gnu/ -COPY --from=build /usr/lib/x86_64-linux-gnu/libgnutls.so* /lib/x86_64-linux-gnu/ -COPY --from=build /usr/lib/x86_64-linux-gnu/libgssapi_krb5.so* /lib/x86_64-linux-gnu/ -COPY --from=build /usr/lib/x86_64-linux-gnu/libidn.so* /lib/x86_64-linux-gnu/ -COPY --from=build /usr/lib/x86_64-linux-gnu/liblber-2.4.so* /lib/x86_64-linux-gnu/ -COPY --from=build /usr/lib/x86_64-linux-gnu/libldap_r-2.4.so* /lib/x86_64-linux-gnu/ -COPY --from=build /usr/lib/x86_64-linux-gnu/libnettle.so* /lib/x86_64-linux-gnu/ -COPY --from=build /usr/lib/x86_64-linux-gnu/librtmp.so* /lib/x86_64-linux-gnu/ - # Make sure all the links resolve -RUN ldd /usr/bin/curl RUN ldd tts-service-proxy +# Install things we want in the final container +RUN apt-get update \ + && apt-get install -y \ + curl \ + && apt-get clean + EXPOSE 8080 CMD LD_LIBRARY_PATH=/usr/lib /tts-service-proxy From f6619e5fef0f4f89d7a5beb8199807df07927638 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Mon, 29 Jun 2020 04:06:28 -0400 Subject: [PATCH 10/48] add proxy_configs.toml (unread currently) --- Cargo.lock | 64 ++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 5 ++-- proxy_configs.toml | 9 +++++++ src/main.rs | 50 ++++++++++++++++++++++++++++++++++-- 4 files changed, 124 insertions(+), 4 deletions(-) create mode 100644 proxy_configs.toml diff --git a/Cargo.lock b/Cargo.lock index ae8a83cf7e..8cc66252d3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -445,12 +445,30 @@ dependencies = [ "winapi 0.3.9", ] +[[package]] +name = "proc-macro2" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "beae6331a816b1f65d04c45b078fd8e6c93e8071771f41b8163255bbd8d7c8fa" +dependencies = [ + "unicode-xid", +] + [[package]] name = "quick-error" version = "1.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0" +[[package]] +name = "quote" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa563d17ecb180e500da1cfd2b028310ac758de548efdd203e18f283af693f37" +dependencies = [ + "proc-macro2", +] + [[package]] name = "redox_syscall" version = "0.1.56" @@ -505,6 +523,23 @@ version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" +[[package]] +name = "serde" +version = "1.0.114" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5317f7588f0a5078ee60ef675ef96735a1442132dc645eb1d12c018620ed8cd3" + +[[package]] +name = "serde_derive" +version = "1.0.114" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2a0be94b04690fbaed37cddffc5c134bf537c8e3329d53e982fe04c374978f8e" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "slab" version = "0.4.2" @@ -529,6 +564,17 @@ dependencies = [ "bytes", ] +[[package]] +name = "syn" +version = "1.0.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8d5d96e8cbb005d6959f119f773bfaebb5684296108fb32600c00cde305b2cd" +dependencies = [ + "proc-macro2", + "quote", + "unicode-xid", +] + [[package]] name = "termcolor" version = "1.1.0" @@ -689,6 +735,15 @@ dependencies = [ "tokio-executor", ] +[[package]] +name = "toml" +version = "0.5.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ffc92d160b1eef40665be3a05630d003936a3bc7da7421277846c2613e92c71a" +dependencies = [ + "serde", +] + [[package]] name = "try-lock" version = "0.2.2" @@ -705,6 +760,9 @@ dependencies = [ "hyper", "hyper-reverse-proxy", "log", + "serde", + "serde_derive", + "toml", ] [[package]] @@ -716,6 +774,12 @@ dependencies = [ "version_check", ] +[[package]] +name = "unicode-xid" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564" + [[package]] name = "version_check" version = "0.9.2" diff --git a/Cargo.toml b/Cargo.toml index d7cedf24ad..f391b6cb1c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,8 +16,9 @@ path = "src/main.rs" anyhow = "1.0.31" env_logger = "0.7.1" futures = "0.1" -#hyper = "0.13.6" hyper = "0.12" hyper-reverse-proxy = "0.4" log = "0.4.8" -#simple_proxy = "1.3.1" +serde = "1.0.104" +serde_derive = "1.0.104" +toml = "0.5.6" diff --git a/proxy_configs.toml b/proxy_configs.toml new file mode 100644 index 0000000000..c813adcd0e --- /dev/null +++ b/proxy_configs.toml @@ -0,0 +1,9 @@ +[[backends]] +voice = "foo" +ip = "127.0.0.1" +port = "12345" + +[[backends]] +voice = "bar" +ip = "1.1.1.1" +port = "12345" diff --git a/src/main.rs b/src/main.rs index b917b03845..a4aaa8994c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,5 @@ #[macro_use] extern crate log; +#[macro_use] extern crate serde_derive; /*#[derive(Deserialize)] @@ -13,15 +14,40 @@ use futures::future::{self, Future}; use hyper::server::conn::AddrStream; use hyper::service::{service_fn, make_service_fn}; use hyper::{Body, Request, Response, Server}; +use anyhow::Result as AnyhowResult; use std::env; type BoxFut = Box, Error=hyper::Error> + Send>; +const ENV_PROXY_CONFIG_FILE : &'static str = "PROXY_CONFIG_FILE"; const ENV_ROUTE_ONE : &'static str = "ROUTE_ONE"; const ENV_ROUTE_TWO : &'static str = "ROUTE_TWO"; +const ENV_RUST_LOG : &'static str = "RUST_LOG"; const ROUTE_ONE_DEFAULT : &'static str = "http://127.0.0.1:12345"; const ROUTE_TWO_DEFAULT : &'static str = "http://127.0.0.1:3000"; +const DEFAULT_PROXY_CONFIG_FILE : &'static str = "proxy_configs.toml"; +const DEFAULT_RUST_LOG: &'static str = "debug"; + +#[derive(Deserialize, Debug, Clone)] +struct ProxyConfigs { + pub backends: Vec, +} + +#[derive(Deserialize, Debug, Clone)] +struct ProxyConfig { + pub voice : String, + pub ip: String, + pub port: String, +} + +impl ProxyConfigs { + pub fn load_from_file(filename: &str) -> AnyhowResult { + let contents = std::fs::read_to_string(filename)?; + let configs = toml::from_str(&contents)?; + Ok(configs) + } +} fn get_env_string(env_name: &str, default: &str) -> String { match env::var(env_name).as_ref().ok() { @@ -39,13 +65,31 @@ fn debug_request(req: Request) -> BoxFut { Box::new(future::ok(response)) } -fn main() { +fn main() -> AnyhowResult<()> { + if env::var(ENV_RUST_LOG) + .as_ref() + .ok() + .is_none() + { + println!("Setting default logging level to \"{}\", override with env var {}.", + DEFAULT_RUST_LOG, ENV_RUST_LOG); + std::env::set_var(ENV_RUST_LOG, DEFAULT_RUST_LOG); + } + + env_logger::init(); + let route_one = get_env_string(ENV_ROUTE_ONE, ROUTE_ONE_DEFAULT); let route_two = get_env_string(ENV_ROUTE_TWO, ROUTE_TWO_DEFAULT); info!("Route one: {}", route_one); info!("Route two: {}", route_two); + let proxy_configs_file = get_env_string(ENV_PROXY_CONFIG_FILE, DEFAULT_PROXY_CONFIG_FILE); + info!("Proxy config file: {}", proxy_configs_file); + + let proxy_configs = ProxyConfigs::load_from_file(&proxy_configs_file)?; + info!("Proxy configs: {:?}", proxy_configs); + // This is our socket address... let addr = ([127, 0, 0, 1], 13900).into(); @@ -77,4 +121,6 @@ fn main() { // Run this server for... forever! hyper::rt::run(server); -} \ No newline at end of file + + Ok(()) +} From 6396b6a690c8b7fe91f5d1c4f83d9e1a31858674 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Mon, 29 Jun 2020 04:24:54 -0400 Subject: [PATCH 11/48] configure port --- Cargo.lock | 18 ++++++++++++++++ Cargo.toml | 1 + src/main.rs | 60 +++++++++++++++++++++++++++++++++++++++-------------- 3 files changed, 64 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8cc66252d3..82452bef6b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -502,6 +502,12 @@ dependencies = [ "semver", ] +[[package]] +name = "ryu" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e" + [[package]] name = "scopeguard" version = "1.1.0" @@ -540,6 +546,17 @@ dependencies = [ "syn", ] +[[package]] +name = "serde_json" +version = "1.0.55" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec2c5d7e739bc07a3e73381a39d61fdb5f671c60c1df26a130690665803d8226" +dependencies = [ + "itoa", + "ryu", + "serde", +] + [[package]] name = "slab" version = "0.4.2" @@ -762,6 +779,7 @@ dependencies = [ "log", "serde", "serde_derive", + "serde_json", "toml", ] diff --git a/Cargo.toml b/Cargo.toml index f391b6cb1c..3dad6d2067 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,4 +21,5 @@ hyper-reverse-proxy = "0.4" log = "0.4.8" serde = "1.0.104" serde_derive = "1.0.104" +serde_json = "1.0.55" toml = "0.5.6" diff --git a/src/main.rs b/src/main.rs index a4aaa8994c..5519a6bccd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,21 +1,16 @@ +#[macro_use] extern crate anyhow; #[macro_use] extern crate log; #[macro_use] extern crate serde_derive; - -/*#[derive(Deserialize)] -pub struct SpeakRequest { - /// Slug for the speaker - speaker: String, - /// Raw text to be spoken - text: String, -}*/ - +use anyhow::Result as AnyhowResult; use futures::future::{self, Future}; use hyper::server::conn::AddrStream; use hyper::service::{service_fn, make_service_fn}; use hyper::{Body, Request, Response, Server}; -use anyhow::Result as AnyhowResult; use std::env; +use std::fmt::Debug; +use std::fmt::Display; +use std::str::FromStr; type BoxFut = Box, Error=hyper::Error> + Send>; @@ -23,11 +18,13 @@ const ENV_PROXY_CONFIG_FILE : &'static str = "PROXY_CONFIG_FILE"; const ENV_ROUTE_ONE : &'static str = "ROUTE_ONE"; const ENV_ROUTE_TWO : &'static str = "ROUTE_TWO"; const ENV_RUST_LOG : &'static str = "RUST_LOG"; +const ENV_SERVICE_PORT : &'static str = "SERVICE_PORT"; -const ROUTE_ONE_DEFAULT : &'static str = "http://127.0.0.1:12345"; -const ROUTE_TWO_DEFAULT : &'static str = "http://127.0.0.1:3000"; const DEFAULT_PROXY_CONFIG_FILE : &'static str = "proxy_configs.toml"; const DEFAULT_RUST_LOG: &'static str = "debug"; +const DEFAULT_SERVICE_PORT: u16 = 5555; +const ROUTE_ONE_DEFAULT : &'static str = "http://127.0.0.1:12345"; +const ROUTE_TWO_DEFAULT : &'static str = "http://127.0.0.1:3000"; #[derive(Deserialize, Debug, Clone)] struct ProxyConfigs { @@ -49,6 +46,14 @@ impl ProxyConfigs { } } +#[derive(Deserialize, Debug)] +pub struct SpeakRequest { + /// Slug for the speaker + speaker: String, + /// Raw text to be spoken + text: String, +} + fn get_env_string(env_name: &str, default: &str) -> String { match env::var(env_name).as_ref().ok() { Some(s) => s.to_string(), @@ -59,6 +64,25 @@ fn get_env_string(env_name: &str, default: &str) -> String { } } +// the trait `std::error::Error` is not implemented for `::Err` +fn get_env_num(env_name: &str, default: T) -> AnyhowResult + where T: FromStr + Display, + T::Err: Debug +{ + match env::var(env_name).as_ref().ok() { + None => { + warn!("Env var '{}' not supplied. Using default '{}'.", env_name, default); + Ok(default) + }, + Some(val) => { + match val.parse::() { + Err(e) => bail!("Can't parse value '{:?}'. Error: {:?}", val, e), + Ok(val) => Ok(val), + } + }, + } +} + fn debug_request(req: Request) -> BoxFut { let body_str = format!("{:?}", req); let response = Response::new(Body::from(body_str)); @@ -80,6 +104,7 @@ fn main() -> AnyhowResult<()> { let route_one = get_env_string(ENV_ROUTE_ONE, ROUTE_ONE_DEFAULT); let route_two = get_env_string(ENV_ROUTE_TWO, ROUTE_TWO_DEFAULT); + let service_port = get_env_num::(ENV_SERVICE_PORT, DEFAULT_SERVICE_PORT)?; info!("Route one: {}", route_one); info!("Route two: {}", route_two); @@ -91,7 +116,7 @@ fn main() -> AnyhowResult<()> { info!("Proxy configs: {:?}", proxy_configs); // This is our socket address... - let addr = ([127, 0, 0, 1], 13900).into(); + let addr = ([127, 0, 0, 1], service_port).into(); // A `Service` is needed for every connection. let make_svc = make_service_fn(move |socket: &AddrStream| { @@ -100,11 +125,16 @@ fn main() -> AnyhowResult<()> { let route_b = route_two.clone(); service_fn(move |req: Request| { // returns BoxFut + if req.uri().path().eq("/speak") { + //let request : SpeakRequest = serde_json::from_str(&req.body().into()).unwrap(); + //info!("Request: {:?}", request); - if req.uri().path().starts_with("/first") { return hyper_reverse_proxy::call(remote_addr.ip(), &route_a, req) - } else if req.uri().path().starts_with("/second") { + } else if req.uri().path().eq("/speak_spectrogram") { + //let request : SpeakRequest = serde_json::from_str(&req.body().into()).unwrap(); + //info!("Request: {:?}", request); + return hyper_reverse_proxy::call(remote_addr.ip(), &route_b, req) } else { From aa7345be2db9a491cdd1ec017a91b9dc20113723 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Mon, 29 Jun 2020 05:08:41 -0400 Subject: [PATCH 12/48] Proxy OPTIONS header --- src/main.rs | 48 +++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 41 insertions(+), 7 deletions(-) diff --git a/src/main.rs b/src/main.rs index 5519a6bccd..daea2f8a50 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,8 +2,10 @@ #[macro_use] extern crate log; #[macro_use] extern crate serde_derive; -use anyhow::Result as AnyhowResult; +use anyhow::{Result as AnyhowResult, Error}; use futures::future::{self, Future}; +use hyper::Method; +use hyper::rt::Stream; use hyper::server::conn::AddrStream; use hyper::service::{service_fn, make_service_fn}; use hyper::{Body, Request, Response, Server}; @@ -12,7 +14,7 @@ use std::fmt::Debug; use std::fmt::Display; use std::str::FromStr; -type BoxFut = Box, Error=hyper::Error> + Send>; +type BoxFut = Box, Error=hyper::Error> + Send>; const ENV_PROXY_CONFIG_FILE : &'static str = "PROXY_CONFIG_FILE"; const ENV_ROUTE_ONE : &'static str = "ROUTE_ONE"; @@ -121,21 +123,53 @@ fn main() -> AnyhowResult<()> { // A `Service` is needed for every connection. let make_svc = make_service_fn(move |socket: &AddrStream| { let remote_addr = socket.remote_addr(); + info!("Got a request from: {:?}", remote_addr); + let route_a = route_one.clone(); let route_b = route_two.clone(); service_fn(move |req: Request| { // returns BoxFut - if req.uri().path().eq("/speak") { - //let request : SpeakRequest = serde_json::from_str(&req.body().into()).unwrap(); - //info!("Request: {:?}", request); + match req.method() { + &Method::OPTIONS => { + // TODO: CORS should be hardcoded here, not wastefully proxied. + return hyper_reverse_proxy::call(remote_addr.ip(), &route_a, req); + }, + _ => {}, + } - return hyper_reverse_proxy::call(remote_addr.ip(), &route_a, req) + if req.uri().path().eq("/speak") { + info!("/speak"); + + let (_parts, body) = req.into_parts(); + + let body_bytes = body.concat2().wait().unwrap().into_bytes(); + let request: SpeakRequest = serde_json::from_slice(&body_bytes).unwrap(); + + print!("Request: {:?}", request); + + let new_req = Request::new(Body::empty()); + return hyper_reverse_proxy::call(remote_addr.ip(), &route_a, new_req) } else if req.uri().path().eq("/speak_spectrogram") { + info!("/speak_spectrogram"); //let request : SpeakRequest = serde_json::from_str(&req.body().into()).unwrap(); //info!("Request: {:?}", request); - return hyper_reverse_proxy::call(remote_addr.ip(), &route_b, req) + let (_parts, body) = req.into_parts(); + + let body_bytes = body.concat2().wait().unwrap().into_bytes(); + + let json_string = String::from_utf8(body_bytes.to_vec()); + + info!("Request String: {:?}", json_string); + + let request: SpeakRequest = serde_json::from_slice(&body_bytes).unwrap(); + + print!("Request: {:?}", request); + + let new_req = Request::new(Body::empty()); + + return hyper_reverse_proxy::call(remote_addr.ip(), &route_b, new_req) } else { debug_request(req) From 09ed0163afe0ee5a0c7a8c93b1e2f2b39211a994 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Mon, 29 Jun 2020 06:55:24 -0400 Subject: [PATCH 13/48] WIP: Futures not working --- src/main.rs | 71 ++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 60 insertions(+), 11 deletions(-) diff --git a/src/main.rs b/src/main.rs index daea2f8a50..c5f0deea9c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -125,6 +125,7 @@ fn main() -> AnyhowResult<()> { let remote_addr = socket.remote_addr(); info!("Got a request from: {:?}", remote_addr); + let route_a = route_one.clone(); let route_b = route_two.clone(); @@ -132,12 +133,15 @@ fn main() -> AnyhowResult<()> { match req.method() { &Method::OPTIONS => { // TODO: CORS should be hardcoded here, not wastefully proxied. - return hyper_reverse_proxy::call(remote_addr.ip(), &route_a, req); + return hyper_reverse_proxy::call(remote_addr.ip(), &route_a, req); // Return BoxFut }, _ => {}, } + let remote_addr2 = remote_addr.clone(); + if req.uri().path().eq("/speak") { + info!("/speak"); let (_parts, body) = req.into_parts(); @@ -152,24 +156,69 @@ fn main() -> AnyhowResult<()> { } else if req.uri().path().eq("/speak_spectrogram") { info!("/speak_spectrogram"); - //let request : SpeakRequest = serde_json::from_str(&req.body().into()).unwrap(); - //info!("Request: {:?}", request); - let (_parts, body) = req.into_parts(); + let boxed = Box::new(req.into_body().concat2().map(move |b| { // Builds a BoxedFut to return + // Parse the request body. form_urlencoded::parse + // always succeeds, but in general parsing may + // fail (for example, an invalid post of json), so + // returning early with BadRequest may be + // necessary. + // + // Warning: this is a simplified use case. In + // principle names can appear multiple times in a + // form, and the values should be rolled up into a + // HashMap>. However in this + // example the simpler approach is sufficient. + //let params = form_urlencoded::parse(b.as_ref()).into_owned().collect::>(); - let body_bytes = body.concat2().wait().unwrap().into_bytes(); + // Validate the request parameters, returning + // early if an invalid input is detected. + /*let name = if let Some(n) = params.get("name") { + n + } else { + return Response::builder() + .status(StatusCode::UNPROCESSABLE_ENTITY) + .body(MISSING.into()) + .unwrap(); + }; + let number = if let Some(n) = params.get("number") { + if let Ok(v) = n.parse::() { + v + } else { + return Response::builder() + .status(StatusCode::UNPROCESSABLE_ENTITY) + .body(NOTNUMERIC.into()) + .unwrap(); + } + } else { + return Response::builder() + .status(StatusCode::UNPROCESSABLE_ENTITY) + .body(MISSING.into()) + .unwrap(); + };*/ - let json_string = String::from_utf8(body_bytes.to_vec()); + let name = "asdf"; + let number = "asdf"; - info!("Request String: {:?}", json_string); - let request: SpeakRequest = serde_json::from_slice(&body_bytes).unwrap(); + // Render the response. This will often involve + // calls to a database or web service, which will + // require creating a new stream for the response + // body. Since those may fail, other error + // responses such as InternalServiceError may be + // needed here, too. + let body = format!("Hello {}, your number is {}", name, number); + Response::new(body.into()) - print!("Request: {:?}", request); + })); - let new_req = Request::new(Body::empty()); + Box::new(boxed.then( |b : Result, Error> | { + let new_req = Request::new(Body::empty()); + hyper_reverse_proxy::call(remote_addr2.ip(), "http://127.0.0.1:12345/speak_request", new_req) + })) - return hyper_reverse_proxy::call(remote_addr.ip(), &route_b, new_req) + //let new_req = Request::new(Body::empty()); + //return hyper_reverse_proxy::call(remote_addr.ip(), &route_b, new_req) } else { debug_request(req) From 0e2990ee2c914f3dde4568cc11578942314ba9fb Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Mon, 29 Jun 2020 07:03:11 -0400 Subject: [PATCH 14/48] This compiles, but isn't logical --- src/main.rs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/main.rs b/src/main.rs index c5f0deea9c..8bd70c3b64 100644 --- a/src/main.rs +++ b/src/main.rs @@ -157,7 +157,7 @@ fn main() -> AnyhowResult<()> { } else if req.uri().path().eq("/speak_spectrogram") { info!("/speak_spectrogram"); - let boxed = Box::new(req.into_body().concat2().map(move |b| { // Builds a BoxedFut to return + Box::new(req.into_body().concat2().map(move |b| { // Builds a BoxedFut to return // Parse the request body. form_urlencoded::parse // always succeeds, but in general parsing may // fail (for example, an invalid post of json), so @@ -210,13 +210,17 @@ fn main() -> AnyhowResult<()> { let body = format!("Hello {}, your number is {}", name, number); Response::new(body.into()) - })); - - Box::new(boxed.then( |b : Result, Error> | { + }).and_then(move |f: Response| { + let remote_addr3 = remote_addr2.clone(); let new_req = Request::new(Body::empty()); - hyper_reverse_proxy::call(remote_addr2.ip(), "http://127.0.0.1:12345/speak_request", new_req) + hyper_reverse_proxy::call(remote_addr3.ip(), "http://127.0.0.1:12345/speak_request", new_req) })) + //Box::new(boxed.then( |b : Result, Error> | { + // let new_req = Request::new(Body::empty()); + // hyper_reverse_proxy::call(remote_addr2.ip(), "http://127.0.0.1:12345/speak_request", new_req) + //})) + //let new_req = Request::new(Body::empty()); //return hyper_reverse_proxy::call(remote_addr.ip(), &route_b, new_req) From 663284df87c108b10066761baaf4957db6c5d5bb Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Mon, 29 Jun 2020 07:07:19 -0400 Subject: [PATCH 15/48] This decodes the request successfully! --- src/main.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/main.rs b/src/main.rs index 8bd70c3b64..ed0502dd60 100644 --- a/src/main.rs +++ b/src/main.rs @@ -197,6 +197,11 @@ fn main() -> AnyhowResult<()> { .unwrap(); };*/ + //let request : SpeakRequest = serde_json::from_str(&req.body().into()).unwrap(); + //let request: SpeakRequest = serde_json::from_slice(&body_bytes).unwrap(); + let request : SpeakRequest = serde_json::from_slice(b.as_ref()).unwrap(); + info!("Request: {:?}", request); + let name = "asdf"; let number = "asdf"; From cd68e43389af0a0fea84cde11d7e12b702057efa Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Mon, 29 Jun 2020 14:56:22 -0400 Subject: [PATCH 16/48] cleanup (still need to pass original request) --- src/main.rs | 63 +++-------------------------------------------------- 1 file changed, 3 insertions(+), 60 deletions(-) diff --git a/src/main.rs b/src/main.rs index ed0502dd60..fffc16a5eb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -155,67 +155,10 @@ fn main() -> AnyhowResult<()> { return hyper_reverse_proxy::call(remote_addr.ip(), &route_a, new_req) } else if req.uri().path().eq("/speak_spectrogram") { - info!("/speak_spectrogram"); - Box::new(req.into_body().concat2().map(move |b| { // Builds a BoxedFut to return - // Parse the request body. form_urlencoded::parse - // always succeeds, but in general parsing may - // fail (for example, an invalid post of json), so - // returning early with BadRequest may be - // necessary. - // - // Warning: this is a simplified use case. In - // principle names can appear multiple times in a - // form, and the values should be rolled up into a - // HashMap>. However in this - // example the simpler approach is sufficient. - //let params = form_urlencoded::parse(b.as_ref()).into_owned().collect::>(); - - // Validate the request parameters, returning - // early if an invalid input is detected. - /*let name = if let Some(n) = params.get("name") { - n - } else { - return Response::builder() - .status(StatusCode::UNPROCESSABLE_ENTITY) - .body(MISSING.into()) - .unwrap(); - }; - let number = if let Some(n) = params.get("number") { - if let Ok(v) = n.parse::() { - v - } else { - return Response::builder() - .status(StatusCode::UNPROCESSABLE_ENTITY) - .body(NOTNUMERIC.into()) - .unwrap(); - } - } else { - return Response::builder() - .status(StatusCode::UNPROCESSABLE_ENTITY) - .body(MISSING.into()) - .unwrap(); - };*/ - - //let request : SpeakRequest = serde_json::from_str(&req.body().into()).unwrap(); - //let request: SpeakRequest = serde_json::from_slice(&body_bytes).unwrap(); - let request : SpeakRequest = serde_json::from_slice(b.as_ref()).unwrap(); - info!("Request: {:?}", request); - - let name = "asdf"; - let number = "asdf"; - - - // Render the response. This will often involve - // calls to a database or web service, which will - // require creating a new stream for the response - // body. Since those may fail, other error - // responses such as InternalServiceError may be - // needed here, too. - let body = format!("Hello {}, your number is {}", name, number); - Response::new(body.into()) - - }).and_then(move |f: Response| { + serde_json::from_slice::(b.as_ref()) + .unwrap() + }).and_then(move |speak_request: SpeakRequest| { let remote_addr3 = remote_addr2.clone(); let new_req = Request::new(Body::empty()); hyper_reverse_proxy::call(remote_addr3.ip(), "http://127.0.0.1:12345/speak_request", new_req) From c89569aa8ed28b0884e9a89f4400380d2fd75161 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Mon, 29 Jun 2020 15:21:20 -0400 Subject: [PATCH 17/48] Proxying works! --- src/main.rs | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index fffc16a5eb..dbaf59b12a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -48,7 +48,7 @@ impl ProxyConfigs { } } -#[derive(Deserialize, Debug)] +#[derive(Serialize, Deserialize, Debug)] pub struct SpeakRequest { /// Slug for the speaker speaker: String, @@ -156,12 +156,25 @@ fn main() -> AnyhowResult<()> { } else if req.uri().path().eq("/speak_spectrogram") { Box::new(req.into_body().concat2().map(move |b| { // Builds a BoxedFut to return - serde_json::from_slice::(b.as_ref()) + let request_bytes = b.as_ref(); + serde_json::from_slice::(request_bytes) .unwrap() }).and_then(move |speak_request: SpeakRequest| { let remote_addr3 = remote_addr2.clone(); - let new_req = Request::new(Body::empty()); - hyper_reverse_proxy::call(remote_addr3.ip(), "http://127.0.0.1:12345/speak_request", new_req) + + let request_json = serde_json::to_string(&speak_request) + .unwrap(); + + let new_req = Request::builder() + .method(Method::POST) + .uri("/speak_spectrogram") + .header("X-Forwarded-For", "https://localhost:12345/frontend/index.html") + .header("Origin", "http://localhost:12345") + .header("Referer", "http://localhost:12345/frontend/index.html") + .body(Body::from(request_json)) + .unwrap(); + + hyper_reverse_proxy::call(remote_addr3.ip(), "http://127.0.0.1:12345", new_req) })) //Box::new(boxed.then( |b : Result, Error> | { From 48a3e831ea1559819366b52b35f444a303bd47dc Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Mon, 29 Jun 2020 18:58:56 -0400 Subject: [PATCH 18/48] refactor proxying (works!) --- src/main.rs | 99 +++++++++++++++++++++++++++++------------------------ 1 file changed, 55 insertions(+), 44 deletions(-) diff --git a/src/main.rs b/src/main.rs index dbaf59b12a..6a19734212 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,6 +13,7 @@ use std::env; use std::fmt::Debug; use std::fmt::Display; use std::str::FromStr; +use std::net::SocketAddr; type BoxFut = Box, Error=hyper::Error> + Send>; @@ -91,6 +92,58 @@ fn debug_request(req: Request) -> BoxFut { Box::new(future::ok(response)) } +fn speak_request(req: Request, remote_addr: SocketAddr) -> BoxFut { + speak_proxy(req, remote_addr, "/speak") +} + +fn speak_spectrogram_request(req: Request, remote_addr: SocketAddr) -> BoxFut { + speak_proxy(req, remote_addr, "/speak_spectrogram") +} + +fn speak_proxy(req: Request, remote_addr: SocketAddr, endpoint: &'static str) -> BoxFut { + Box::new(req.into_body().concat2().map(move |b| { // Builds a BoxedFut to return + let request_bytes = b.as_ref(); + let mut request = serde_json::from_slice::(request_bytes) + .unwrap(); + + request.speaker = "glow-ljs-single".to_string(); + + request + }).and_then(move |speak_request: SpeakRequest| { + let request_json = serde_json::to_string(&speak_request) + .unwrap(); + + info!("Request: {:?}", request_json); + println!("{}", request_json); + + /* + Accept-Encoding: gzip, deflate, br + Accept-Language: en-US,en;q=0.5 + Accept: application/json + Connection: keep-alive + Content-Length: 42 + Content-Type: application/json + Host: mumble.stream + Origin: https://trumped.com + Referer: https://trumped.com/ + User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:77.0) Gecko/20100101 Firefox/77.0 + */ + // TODO: Copy all headers. + let new_req = Request::builder() + .method(Method::POST) + .uri(endpoint) + .header("Host", "localhost:12345") + .header("Content-Type", "application/json") + .header("Origin", "http://localhost:7000") + .header("Referer", "http://localhost:7000/frontend/index.html") + //.header("X-Forwarded-For", "http://localhost:7000/frontend/index.html") + .body(Body::from(request_json)) + .unwrap(); + + hyper_reverse_proxy::call(remote_addr.ip(), "http://127.0.0.1:12345", new_req) + })) +} + fn main() -> AnyhowResult<()> { if env::var(ENV_RUST_LOG) .as_ref() @@ -125,7 +178,6 @@ fn main() -> AnyhowResult<()> { let remote_addr = socket.remote_addr(); info!("Got a request from: {:?}", remote_addr); - let route_a = route_one.clone(); let route_b = route_two.clone(); @@ -141,50 +193,9 @@ fn main() -> AnyhowResult<()> { let remote_addr2 = remote_addr.clone(); if req.uri().path().eq("/speak") { - - info!("/speak"); - - let (_parts, body) = req.into_parts(); - - let body_bytes = body.concat2().wait().unwrap().into_bytes(); - let request: SpeakRequest = serde_json::from_slice(&body_bytes).unwrap(); - - print!("Request: {:?}", request); - - let new_req = Request::new(Body::empty()); - return hyper_reverse_proxy::call(remote_addr.ip(), &route_a, new_req) - + speak_request(req, remote_addr2) } else if req.uri().path().eq("/speak_spectrogram") { - Box::new(req.into_body().concat2().map(move |b| { // Builds a BoxedFut to return - let request_bytes = b.as_ref(); - serde_json::from_slice::(request_bytes) - .unwrap() - }).and_then(move |speak_request: SpeakRequest| { - let remote_addr3 = remote_addr2.clone(); - - let request_json = serde_json::to_string(&speak_request) - .unwrap(); - - let new_req = Request::builder() - .method(Method::POST) - .uri("/speak_spectrogram") - .header("X-Forwarded-For", "https://localhost:12345/frontend/index.html") - .header("Origin", "http://localhost:12345") - .header("Referer", "http://localhost:12345/frontend/index.html") - .body(Body::from(request_json)) - .unwrap(); - - hyper_reverse_proxy::call(remote_addr3.ip(), "http://127.0.0.1:12345", new_req) - })) - - //Box::new(boxed.then( |b : Result, Error> | { - // let new_req = Request::new(Body::empty()); - // hyper_reverse_proxy::call(remote_addr2.ip(), "http://127.0.0.1:12345/speak_request", new_req) - //})) - - //let new_req = Request::new(Body::empty()); - //return hyper_reverse_proxy::call(remote_addr.ip(), &route_b, new_req) - + speak_spectrogram_request(req, remote_addr2) } else { debug_request(req) } From 56f707e89b8a6a26f3ac1f8f564d3779cfec5c2e Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Mon, 29 Jun 2020 19:29:16 -0400 Subject: [PATCH 19/48] copy (most) headers --- src/main.rs | 42 ++++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/src/main.rs b/src/main.rs index 6a19734212..90a79b46b5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,6 +14,9 @@ use std::fmt::Debug; use std::fmt::Display; use std::str::FromStr; use std::net::SocketAddr; +use std::collections::HashMap; +use hyper::header::HeaderValue; +use hyper::http::header::HeaderName; type BoxFut = Box, Error=hyper::Error> + Send>; @@ -101,6 +104,15 @@ fn speak_spectrogram_request(req: Request, remote_addr: SocketAddr) -> Box } fn speak_proxy(req: Request, remote_addr: SocketAddr, endpoint: &'static str) -> BoxFut { + let mut headers : HashMap = HashMap::new(); + + for (header_name, header_value) in req.headers().into_iter() { + match header_name.as_str().to_lowercase().as_ref() { + "content-length" => continue, // We have to recalculate since we change the spacing. + _ => { headers.insert(header_name.clone(), header_value.clone()); }, + } + } + Box::new(req.into_body().concat2().map(move |b| { // Builds a BoxedFut to return let request_bytes = b.as_ref(); let mut request = serde_json::from_slice::(request_bytes) @@ -113,30 +125,20 @@ fn speak_proxy(req: Request, remote_addr: SocketAddr, endpoint: &'static s let request_json = serde_json::to_string(&speak_request) .unwrap(); - info!("Request: {:?}", request_json); - println!("{}", request_json); + let mut request_builder = Request::builder(); + request_builder.method(Method::POST) + .uri(endpoint); - /* - Accept-Encoding: gzip, deflate, br - Accept-Language: en-US,en;q=0.5 - Accept: application/json - Connection: keep-alive - Content-Length: 42 - Content-Type: application/json - Host: mumble.stream - Origin: https://trumped.com - Referer: https://trumped.com/ - User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:77.0) Gecko/20100101 Firefox/77.0 - */ - // TODO: Copy all headers. - let new_req = Request::builder() - .method(Method::POST) - .uri(endpoint) - .header("Host", "localhost:12345") + for (k, v) in headers.iter() { + request_builder.header(k, v); + } + /*.header("Host", "localhost:12345") .header("Content-Type", "application/json") .header("Origin", "http://localhost:7000") .header("Referer", "http://localhost:7000/frontend/index.html") - //.header("X-Forwarded-For", "http://localhost:7000/frontend/index.html") + //.header("X-Forwarded-For", "http://localhost:7000/frontend/index.html")*/ + + let new_req = request_builder .body(Body::from(request_json)) .unwrap(); From 649623c39f4882f3f323cff6536bae7de437576f Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Mon, 29 Jun 2020 19:32:57 -0400 Subject: [PATCH 20/48] more efficient header copy --- src/main.rs | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/src/main.rs b/src/main.rs index 90a79b46b5..a54a7fe67c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -104,14 +104,9 @@ fn speak_spectrogram_request(req: Request, remote_addr: SocketAddr) -> Box } fn speak_proxy(req: Request, remote_addr: SocketAddr, endpoint: &'static str) -> BoxFut { - let mut headers : HashMap = HashMap::new(); - - for (header_name, header_value) in req.headers().into_iter() { - match header_name.as_str().to_lowercase().as_ref() { - "content-length" => continue, // We have to recalculate since we change the spacing. - _ => { headers.insert(header_name.clone(), header_value.clone()); }, - } - } + let mut headers = req.headers().clone(); + // NB: Rehydrating the request payload can change the content length if whitespace is changed. + headers.remove(HeaderName::from_static("content-length")); Box::new(req.into_body().concat2().map(move |b| { // Builds a BoxedFut to return let request_bytes = b.as_ref(); @@ -132,11 +127,6 @@ fn speak_proxy(req: Request, remote_addr: SocketAddr, endpoint: &'static s for (k, v) in headers.iter() { request_builder.header(k, v); } - /*.header("Host", "localhost:12345") - .header("Content-Type", "application/json") - .header("Origin", "http://localhost:7000") - .header("Referer", "http://localhost:7000/frontend/index.html") - //.header("X-Forwarded-For", "http://localhost:7000/frontend/index.html")*/ let new_req = request_builder .body(Body::from(request_json)) From b268d98b86ade5f5833a3c74a3ab42bde983f039 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Mon, 29 Jun 2020 19:42:32 -0400 Subject: [PATCH 21/48] cleanup --- src/main.rs | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/src/main.rs b/src/main.rs index a54a7fe67c..7d69d8b005 100644 --- a/src/main.rs +++ b/src/main.rs @@ -95,14 +95,6 @@ fn debug_request(req: Request) -> BoxFut { Box::new(future::ok(response)) } -fn speak_request(req: Request, remote_addr: SocketAddr) -> BoxFut { - speak_proxy(req, remote_addr, "/speak") -} - -fn speak_spectrogram_request(req: Request, remote_addr: SocketAddr) -> BoxFut { - speak_proxy(req, remote_addr, "/speak_spectrogram") -} - fn speak_proxy(req: Request, remote_addr: SocketAddr, endpoint: &'static str) -> BoxFut { let mut headers = req.headers().clone(); // NB: Rehydrating the request payload can change the content length if whitespace is changed. @@ -182,14 +174,13 @@ fn main() -> AnyhowResult<()> { _ => {}, } - let remote_addr2 = remote_addr.clone(); - - if req.uri().path().eq("/speak") { - speak_request(req, remote_addr2) - } else if req.uri().path().eq("/speak_spectrogram") { - speak_spectrogram_request(req, remote_addr2) - } else { - debug_request(req) + match req.uri().path() { + "/speak" => + speak_proxy(req, remote_addr.clone(), "/speak"), + "/speak_spectrogram" => + speak_proxy(req, remote_addr.clone(), "/speak_spectrogram"), + _ => + debug_request(req), } }) }); From 8238977553aa0d21e4ee183883cb259790f14a91 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Mon, 29 Jun 2020 20:02:32 -0400 Subject: [PATCH 22/48] Copy entire body too, and remove header hack --- src/main.rs | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/src/main.rs b/src/main.rs index 7d69d8b005..d8c4a5567e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -52,6 +52,8 @@ impl ProxyConfigs { } } +/// NB: This much match the shape of SpeakRequest in the 'voder/tts_service' code. +/// This is used for both /speak and /speak_spectrogram requests. #[derive(Serialize, Deserialize, Debug)] pub struct SpeakRequest { /// Slug for the speaker @@ -95,23 +97,26 @@ fn debug_request(req: Request) -> BoxFut { Box::new(future::ok(response)) } +#[derive(Default)] +struct RequestDetails { + pub request_bytes: Vec, + pub speaker: String, +} + fn speak_proxy(req: Request, remote_addr: SocketAddr, endpoint: &'static str) -> BoxFut { let mut headers = req.headers().clone(); - // NB: Rehydrating the request payload can change the content length if whitespace is changed. - headers.remove(HeaderName::from_static("content-length")); Box::new(req.into_body().concat2().map(move |b| { // Builds a BoxedFut to return let request_bytes = b.as_ref(); - let mut request = serde_json::from_slice::(request_bytes) - .unwrap(); - - request.speaker = "glow-ljs-single".to_string(); - - request - }).and_then(move |speak_request: SpeakRequest| { - let request_json = serde_json::to_string(&speak_request) + + let request = serde_json::from_slice::(request_bytes) .unwrap(); + RequestDetails { + request_bytes: request_bytes.to_vec(), + speaker: request.speaker, + } + }).and_then(move |request_details: RequestDetails| { let mut request_builder = Request::builder(); request_builder.method(Method::POST) .uri(endpoint); @@ -121,7 +126,7 @@ fn speak_proxy(req: Request, remote_addr: SocketAddr, endpoint: &'static s } let new_req = request_builder - .body(Body::from(request_json)) + .body(Body::from(request_details.request_bytes)) .unwrap(); hyper_reverse_proxy::call(remote_addr.ip(), "http://127.0.0.1:12345", new_req) From 96e900457e2d3b33aeacbd2c120afa439b1911ac Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Mon, 29 Jun 2020 20:12:07 -0400 Subject: [PATCH 23/48] cleanup default routing --- src/main.rs | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/src/main.rs b/src/main.rs index d8c4a5567e..2cc38d9b9f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -97,7 +97,6 @@ fn debug_request(req: Request) -> BoxFut { Box::new(future::ok(response)) } -#[derive(Default)] struct RequestDetails { pub request_bytes: Vec, pub speaker: String, @@ -168,24 +167,15 @@ fn main() -> AnyhowResult<()> { info!("Got a request from: {:?}", remote_addr); let route_a = route_one.clone(); - let route_b = route_two.clone(); - service_fn(move |req: Request| { // returns BoxFut - match req.method() { - &Method::OPTIONS => { - // TODO: CORS should be hardcoded here, not wastefully proxied. - return hyper_reverse_proxy::call(remote_addr.ip(), &route_a, req); // Return BoxFut - }, - _ => {}, - } - - match req.uri().path() { - "/speak" => + service_fn(move |req: Request| { + match (req.method(), req.uri().path()) { + (&Method::POST, "/speak") => speak_proxy(req, remote_addr.clone(), "/speak"), - "/speak_spectrogram" => + (&Method::POST, "/speak_spectrogram") => speak_proxy(req, remote_addr.clone(), "/speak_spectrogram"), _ => - debug_request(req), + hyper_reverse_proxy::call(remote_addr.ip(), &route_a, req), } }) }); From e850941f40fb150b7077a954886eb0e7bb9a1830 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Mon, 29 Jun 2020 22:54:05 -0400 Subject: [PATCH 24/48] Working proxy design --- Cargo.lock | 65 ++++++++++++++++++++++++++++ Cargo.toml | 1 + proxy_configs.toml | 6 +-- src/main.rs | 105 ++++++++++++++++++++++++++++++++------------- 4 files changed, 144 insertions(+), 33 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 82452bef6b..5e1f55a2d7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -175,6 +175,17 @@ dependencies = [ "num_cpus", ] +[[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.1.26" @@ -445,6 +456,12 @@ dependencies = [ "winapi 0.3.9", ] +[[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.18" @@ -469,6 +486,47 @@ 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.56" @@ -777,6 +835,7 @@ dependencies = [ "hyper", "hyper-reverse-proxy", "log", + "rand", "serde", "serde_derive", "serde_json", @@ -815,6 +874,12 @@ dependencies = [ "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 = "winapi" version = "0.2.8" diff --git a/Cargo.toml b/Cargo.toml index 3dad6d2067..07267c4bbb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,6 +19,7 @@ futures = "0.1" hyper = "0.12" hyper-reverse-proxy = "0.4" log = "0.4.8" +rand = "0.7.3" serde = "1.0.104" serde_derive = "1.0.104" serde_json = "1.0.55" diff --git a/proxy_configs.toml b/proxy_configs.toml index c813adcd0e..414d2e00e3 100644 --- a/proxy_configs.toml +++ b/proxy_configs.toml @@ -1,9 +1,9 @@ [[backends]] -voice = "foo" +voice = "trump" ip = "127.0.0.1" port = "12345" [[backends]] -voice = "bar" -ip = "1.1.1.1" +voice = "glow-ljs-single" +ip = "localhost" port = "12345" diff --git a/src/main.rs b/src/main.rs index 2cc38d9b9f..cf3970de4e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,35 +2,33 @@ #[macro_use] extern crate log; #[macro_use] extern crate serde_derive; -use anyhow::{Result as AnyhowResult, Error}; +use anyhow::Result as AnyhowResult; use futures::future::{self, Future}; use hyper::Method; use hyper::rt::Stream; use hyper::server::conn::AddrStream; use hyper::service::{service_fn, make_service_fn}; use hyper::{Body, Request, Response, Server}; +use std::collections::HashMap; use std::env; use std::fmt::Debug; use std::fmt::Display; -use std::str::FromStr; use std::net::SocketAddr; -use std::collections::HashMap; -use hyper::header::HeaderValue; -use hyper::http::header::HeaderName; +use std::str::FromStr; +use std::sync::Arc; +use rand::seq::IteratorRandom; type BoxFut = Box, Error=hyper::Error> + Send>; +const ENV_DEFAULT_ROUTE : &'static str = "DEFAULT_ROUTE"; const ENV_PROXY_CONFIG_FILE : &'static str = "PROXY_CONFIG_FILE"; -const ENV_ROUTE_ONE : &'static str = "ROUTE_ONE"; -const ENV_ROUTE_TWO : &'static str = "ROUTE_TWO"; const ENV_RUST_LOG : &'static str = "RUST_LOG"; const ENV_SERVICE_PORT : &'static str = "SERVICE_PORT"; +const DEFAULT_DEFAULT_ROUTE : &'static str = "http://127.0.0.1:12345"; const DEFAULT_PROXY_CONFIG_FILE : &'static str = "proxy_configs.toml"; -const DEFAULT_RUST_LOG: &'static str = "debug"; +const DEFAULT_RUST_LOG: &'static str = "tokio_reactor=warn,hyper=info,debug"; const DEFAULT_SERVICE_PORT: u16 = 5555; -const ROUTE_ONE_DEFAULT : &'static str = "http://127.0.0.1:12345"; -const ROUTE_TWO_DEFAULT : &'static str = "http://127.0.0.1:3000"; #[derive(Deserialize, Debug, Clone)] struct ProxyConfigs { @@ -91,7 +89,7 @@ fn get_env_num(env_name: &str, default: T) -> AnyhowResult } } -fn debug_request(req: Request) -> BoxFut { +fn _debug_request(req: Request) -> BoxFut { let body_str = format!("{:?}", req); let response = Response::new(Body::from(body_str)); Box::new(future::ok(response)) @@ -102,8 +100,26 @@ struct RequestDetails { pub speaker: String, } -fn speak_proxy(req: Request, remote_addr: SocketAddr, endpoint: &'static str) -> BoxFut { - let mut headers = req.headers().clone(); +struct Router { + pub routes: HashMap, +} + +impl Router { + fn get_random_host(&self) -> Option { + let mut rng = rand::thread_rng(); + self.routes.values() + .choose(&mut rng) + .map(|url| url.to_string()) + } + + fn get_speaker_host(&self, voice: &str) -> Option { + self.routes.get(voice) + .map(|url| url.to_string()) + } +} + +fn speak_proxy(req: Request, remote_addr: SocketAddr, router: Arc, endpoint: &'static str) -> BoxFut { + let headers = req.headers().clone(); Box::new(req.into_body().concat2().map(move |b| { // Builds a BoxedFut to return let request_bytes = b.as_ref(); @@ -116,6 +132,19 @@ fn speak_proxy(req: Request, remote_addr: SocketAddr, endpoint: &'static s speaker: request.speaker, } }).and_then(move |request_details: RequestDetails| { + let proxy_host = match router.get_speaker_host(&request_details.speaker) { + Some(host) => host, + None => { + let response = Response::builder() + .body(Body::from("error")) + .unwrap(); + let result : BoxFut = Box::new(future::ok(response)); + return result; + }, + }; + + info!("Routing {} for {} to {}", endpoint, &request_details.speaker, &proxy_host); + let mut request_builder = Request::builder(); request_builder.method(Method::POST) .uri(endpoint); @@ -128,7 +157,7 @@ fn speak_proxy(req: Request, remote_addr: SocketAddr, endpoint: &'static s .body(Body::from(request_details.request_bytes)) .unwrap(); - hyper_reverse_proxy::call(remote_addr.ip(), "http://127.0.0.1:12345", new_req) + hyper_reverse_proxy::call(remote_addr.ip(), &proxy_host, new_req) })) } @@ -145,12 +174,10 @@ fn main() -> AnyhowResult<()> { env_logger::init(); - let route_one = get_env_string(ENV_ROUTE_ONE, ROUTE_ONE_DEFAULT); - let route_two = get_env_string(ENV_ROUTE_TWO, ROUTE_TWO_DEFAULT); + let default_route = get_env_string(ENV_DEFAULT_ROUTE, DEFAULT_DEFAULT_ROUTE); let service_port = get_env_num::(ENV_SERVICE_PORT, DEFAULT_SERVICE_PORT)?; - info!("Route one: {}", route_one); - info!("Route two: {}", route_two); + info!("Default route: {}", default_route); let proxy_configs_file = get_env_string(ENV_PROXY_CONFIG_FILE, DEFAULT_PROXY_CONFIG_FILE); info!("Proxy config file: {}", proxy_configs_file); @@ -158,33 +185,51 @@ fn main() -> AnyhowResult<()> { let proxy_configs = ProxyConfigs::load_from_file(&proxy_configs_file)?; info!("Proxy configs: {:?}", proxy_configs); - // This is our socket address... - let addr = ([127, 0, 0, 1], service_port).into(); + let mut route_map = HashMap::new(); + + for backend in proxy_configs.backends.iter() { + let host = format!("http://{}:{}", backend.ip, backend.port); + route_map.insert(backend.voice.clone(), host); + } + + let router = Arc::new(Router { + routes: route_map, + }); + + //let routes = route_map.clone(); // A `Service` is needed for every connection. - let make_svc = make_service_fn(move |socket: &AddrStream| { + let make_service = make_service_fn(move |socket: &AddrStream| { let remote_addr = socket.remote_addr(); info!("Got a request from: {:?}", remote_addr); - let route_a = route_one.clone(); + let router2 = router.clone(); + let default_route2 = default_route.clone(); service_fn(move |req: Request| { + let router3 = router2.clone(); + match (req.method(), req.uri().path()) { (&Method::POST, "/speak") => - speak_proxy(req, remote_addr.clone(), "/speak"), + speak_proxy(req, remote_addr.clone(), router3, "/speak"), (&Method::POST, "/speak_spectrogram") => - speak_proxy(req, remote_addr.clone(), "/speak_spectrogram"), - _ => - hyper_reverse_proxy::call(remote_addr.ip(), &route_a, req), + speak_proxy(req, remote_addr.clone(), router3, "/speak_spectrogram"), + _ => { + let forward = router3.get_random_host() + .unwrap_or(default_route2.clone()); + hyper_reverse_proxy::call(remote_addr.ip(), &forward, req) + } } }) }); - let server = Server::bind(&addr) - .serve(make_svc) - .map_err(|e| eprintln!("server error: {}", e)); + let addr = ([127, 0, 0, 1], service_port).into(); - println!("Running server on {:?}", addr); + info!("Running server on {:?}", addr); + + let server = Server::bind(&addr) + .serve(make_service) + .map_err(|e| eprintln!("server error: {}", e)); // Run this server for... forever! hyper::rt::run(server); From b50f84c28034046ed8f668df9888ad1089c9792f Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Mon, 29 Jun 2020 22:59:05 -0400 Subject: [PATCH 25/48] always fall back to default host --- src/main.rs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/main.rs b/src/main.rs index cf3970de4e..67a06f1c45 100644 --- a/src/main.rs +++ b/src/main.rs @@ -102,19 +102,22 @@ struct RequestDetails { struct Router { pub routes: HashMap, + pub default_route: String, } impl Router { - fn get_random_host(&self) -> Option { + fn get_random_host(&self) -> String { let mut rng = rand::thread_rng(); self.routes.values() .choose(&mut rng) .map(|url| url.to_string()) + .unwrap_or(self.default_route.clone()) } - fn get_speaker_host(&self, voice: &str) -> Option { + fn get_speaker_host(&self, voice: &str) -> String { self.routes.get(voice) .map(|url| url.to_string()) + .unwrap_or(self.default_route.clone()) } } @@ -132,7 +135,7 @@ fn speak_proxy(req: Request, remote_addr: SocketAddr, router: Arc, speaker: request.speaker, } }).and_then(move |request_details: RequestDetails| { - let proxy_host = match router.get_speaker_host(&request_details.speaker) { + /*let proxy_host = match router.get_speaker_host(&request_details.speaker) { Some(host) => host, None => { let response = Response::builder() @@ -141,7 +144,9 @@ fn speak_proxy(req: Request, remote_addr: SocketAddr, router: Arc, let result : BoxFut = Box::new(future::ok(response)); return result; }, - }; + };*/ + + let proxy_host = router.get_speaker_host(&request_details.speaker); info!("Routing {} for {} to {}", endpoint, &request_details.speaker, &proxy_host); @@ -194,6 +199,7 @@ fn main() -> AnyhowResult<()> { let router = Arc::new(Router { routes: route_map, + default_route: default_route, }); //let routes = route_map.clone(); @@ -204,7 +210,6 @@ fn main() -> AnyhowResult<()> { info!("Got a request from: {:?}", remote_addr); let router2 = router.clone(); - let default_route2 = default_route.clone(); service_fn(move |req: Request| { let router3 = router2.clone(); @@ -215,8 +220,7 @@ fn main() -> AnyhowResult<()> { (&Method::POST, "/speak_spectrogram") => speak_proxy(req, remote_addr.clone(), router3, "/speak_spectrogram"), _ => { - let forward = router3.get_random_host() - .unwrap_or(default_route2.clone()); + let forward = router3.get_random_host(); hyper_reverse_proxy::call(remote_addr.ip(), &forward, req) } } From 4a5ec4bdbadfeb37897ec15da0b69049bffbf6a7 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Mon, 29 Jun 2020 23:56:26 -0400 Subject: [PATCH 26/48] add proxy config to docker, fix IP x-forwarded-for --- Dockerfile | 2 ++ src/main.rs | 9 ++++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index c9f1595212..cb3581c1ef 100644 --- a/Dockerfile +++ b/Dockerfile @@ -45,6 +45,8 @@ RUN apt-get update \ curl \ && apt-get clean +COPY proxy_configs.toml . + EXPOSE 8080 CMD LD_LIBRARY_PATH=/usr/lib /tts-service-proxy diff --git a/src/main.rs b/src/main.rs index 67a06f1c45..c2db839712 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,6 +17,8 @@ use std::net::SocketAddr; use std::str::FromStr; use std::sync::Arc; use rand::seq::IteratorRandom; +use hyper::http::header::HeaderName; +use hyper::header::HeaderValue; type BoxFut = Box, Error=hyper::Error> + Send>; @@ -122,7 +124,12 @@ impl Router { } fn speak_proxy(req: Request, remote_addr: SocketAddr, router: Arc, endpoint: &'static str) -> BoxFut { - let headers = req.headers().clone(); + let mut headers = req.headers().clone(); + + let forwarded_for = HeaderValue::from_str(&remote_addr.ip().to_string()) + .ok() + .unwrap_or(HeaderValue::from_static("127.0.0.1")); + headers.insert(HeaderName::from_static("x-forwarded-for"), forwarded_for); Box::new(req.into_body().concat2().map(move |b| { // Builds a BoxedFut to return let request_bytes = b.as_ref(); From 7455e51148c5aea83ada97df5aed5b76031654c0 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Tue, 30 Jun 2020 00:11:42 -0400 Subject: [PATCH 27/48] populate 'forwarded' header as well --- src/main.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index c2db839712..c5bae0f43f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -126,10 +126,11 @@ impl Router { fn speak_proxy(req: Request, remote_addr: SocketAddr, router: Arc, endpoint: &'static str) -> BoxFut { let mut headers = req.headers().clone(); - let forwarded_for = HeaderValue::from_str(&remote_addr.ip().to_string()) + let forwarded_ip = HeaderValue::from_str(&remote_addr.ip().to_string()) .ok() .unwrap_or(HeaderValue::from_static("127.0.0.1")); - headers.insert(HeaderName::from_static("x-forwarded-for"), forwarded_for); + headers.insert(HeaderName::from_static("forwarded"), forwarded_ip.clone()); + headers.insert(HeaderName::from_static("x-forwarded-for"), forwarded_ip.clone()); Box::new(req.into_body().concat2().map(move |b| { // Builds a BoxedFut to return let request_bytes = b.as_ref(); From 7e4d5f375d66d65a4abfb89b25340976e3d97bb5 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Tue, 30 Jun 2020 06:10:20 -0400 Subject: [PATCH 28/48] add debugging to catch-all route --- src/main.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main.rs b/src/main.rs index c5bae0f43f..63c69c13d2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -229,6 +229,7 @@ fn main() -> AnyhowResult<()> { speak_proxy(req, remote_addr.clone(), router3, "/speak_spectrogram"), _ => { let forward = router3.get_random_host(); + info!("Forwarding to `{}` random host: {}", &forward, req.uri()); hyper_reverse_proxy::call(remote_addr.ip(), &forward, req) } } From 801304adde5e370b9819b8c6c6a67faeb4bbdc24 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Tue, 30 Jun 2020 06:21:36 -0400 Subject: [PATCH 29/48] bind 0.0.0.0 --- src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 63c69c13d2..6acd9ce827 100644 --- a/src/main.rs +++ b/src/main.rs @@ -236,7 +236,7 @@ fn main() -> AnyhowResult<()> { }) }); - let addr = ([127, 0, 0, 1], service_port).into(); + let addr = ([0, 0, 0, 0], service_port).into(); info!("Running server on {:?}", addr); From ae060729dca553b3096b429ababace8c136a39c2 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Tue, 30 Jun 2020 06:51:35 -0400 Subject: [PATCH 30/48] add proxy healthcheck endpoint --- src/main.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/main.rs b/src/main.rs index 6acd9ce827..6c59c4b21d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -97,6 +97,11 @@ fn _debug_request(req: Request) -> BoxFut { Box::new(future::ok(response)) } +fn health_check_response(_req: Request) -> BoxFut { + let response = Response::new(Body::from("healthy")); + Box::new(future::ok(response)) +} + struct RequestDetails { pub request_bytes: Vec, pub speaker: String, @@ -227,6 +232,8 @@ fn main() -> AnyhowResult<()> { speak_proxy(req, remote_addr.clone(), router3, "/speak"), (&Method::POST, "/speak_spectrogram") => speak_proxy(req, remote_addr.clone(), router3, "/speak_spectrogram"), + (&Method::GET, "/proxy_health") => + health_check_response(req), _ => { let forward = router3.get_random_host(); info!("Forwarding to `{}` random host: {}", &forward, req.uri()); From 0fda9f97c3b2ac4c62f9248401e9f92ba30f4948 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Wed, 1 Jul 2020 03:22:57 -0400 Subject: [PATCH 31/48] cleanup stale comments, add debug statement for IP --- src/main.rs | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) diff --git a/src/main.rs b/src/main.rs index 6c59c4b21d..a487504051 100644 --- a/src/main.rs +++ b/src/main.rs @@ -137,6 +137,8 @@ fn speak_proxy(req: Request, remote_addr: SocketAddr, router: Arc, headers.insert(HeaderName::from_static("forwarded"), forwarded_ip.clone()); headers.insert(HeaderName::from_static("x-forwarded-for"), forwarded_ip.clone()); + info!("Remote IP: {:?}", remote_addr.ip().to_string()); + Box::new(req.into_body().concat2().map(move |b| { // Builds a BoxedFut to return let request_bytes = b.as_ref(); @@ -148,17 +150,6 @@ fn speak_proxy(req: Request, remote_addr: SocketAddr, router: Arc, speaker: request.speaker, } }).and_then(move |request_details: RequestDetails| { - /*let proxy_host = match router.get_speaker_host(&request_details.speaker) { - Some(host) => host, - None => { - let response = Response::builder() - .body(Body::from("error")) - .unwrap(); - let result : BoxFut = Box::new(future::ok(response)); - return result; - }, - };*/ - let proxy_host = router.get_speaker_host(&request_details.speaker); info!("Routing {} for {} to {}", endpoint, &request_details.speaker, &proxy_host); @@ -215,9 +206,6 @@ fn main() -> AnyhowResult<()> { default_route: default_route, }); - //let routes = route_map.clone(); - - // A `Service` is needed for every connection. let make_service = make_service_fn(move |socket: &AddrStream| { let remote_addr = socket.remote_addr(); info!("Got a request from: {:?}", remote_addr); @@ -251,7 +239,6 @@ fn main() -> AnyhowResult<()> { .serve(make_service) .map_err(|e| eprintln!("server error: {}", e)); - // Run this server for... forever! hyper::rt::run(server); Ok(()) From e7025d035f8cbb53a80a2aa72bd6e4859709bd22 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Wed, 1 Jul 2020 03:38:16 -0400 Subject: [PATCH 32/48] confirmed this is correct; no longer need to log --- src/main.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index a487504051..aebcb7f73d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -131,14 +131,14 @@ impl Router { fn speak_proxy(req: Request, remote_addr: SocketAddr, router: Arc, endpoint: &'static str) -> BoxFut { let mut headers = req.headers().clone(); + // This is the correct client IP, not the IP of the load balancer. + // We're adding it to the headers so that downstream can use it for rate limiting and logging. let forwarded_ip = HeaderValue::from_str(&remote_addr.ip().to_string()) .ok() .unwrap_or(HeaderValue::from_static("127.0.0.1")); headers.insert(HeaderName::from_static("forwarded"), forwarded_ip.clone()); headers.insert(HeaderName::from_static("x-forwarded-for"), forwarded_ip.clone()); - info!("Remote IP: {:?}", remote_addr.ip().to_string()); - Box::new(req.into_body().concat2().map(move |b| { // Builds a BoxedFut to return let request_bytes = b.as_ref(); From c7aba90ee9dd8bf1bfe0f7f45ff33c40c2ae73cf Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Wed, 1 Jul 2020 04:53:43 -0400 Subject: [PATCH 33/48] custom forwarded-for type header --- src/main.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main.rs b/src/main.rs index aebcb7f73d..734a679938 100644 --- a/src/main.rs +++ b/src/main.rs @@ -138,6 +138,9 @@ fn speak_proxy(req: Request, remote_addr: SocketAddr, router: Arc, .unwrap_or(HeaderValue::from_static("127.0.0.1")); headers.insert(HeaderName::from_static("forwarded"), forwarded_ip.clone()); headers.insert(HeaderName::from_static("x-forwarded-for"), forwarded_ip.clone()); + // Unfortunately it looks like this middleware trounces the standard headers, so + // here we put it somewhere it won't be overwritten. + headers.insert(HeaderName::from_static("x-voder-proxy-for"), forwarded_ip.clone()); Box::new(req.into_body().concat2().map(move |b| { // Builds a BoxedFut to return let request_bytes = b.as_ref(); From 6aef89c6ef1af362256e8c7d3465b56490868f42 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Wed, 1 Jul 2020 05:46:04 -0400 Subject: [PATCH 34/48] debug by printing all headers --- src/main.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main.rs b/src/main.rs index 734a679938..33a2a83e0b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -131,6 +131,10 @@ impl Router { fn speak_proxy(req: Request, remote_addr: SocketAddr, router: Arc, endpoint: &'static str) -> BoxFut { let mut headers = req.headers().clone(); + for (k, v) in headers.iter() { + info!("Header: {:?} = {:?}", k, v); + } + // This is the correct client IP, not the IP of the load balancer. // We're adding it to the headers so that downstream can use it for rate limiting and logging. let forwarded_ip = HeaderValue::from_str(&remote_addr.ip().to_string()) From aeb905357e0d7828a8bd25ced62d1e7e8e64bcd7 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Wed, 1 Jul 2020 06:08:06 -0400 Subject: [PATCH 35/48] this should finally report the correct IP --- src/main.rs | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/src/main.rs b/src/main.rs index 33a2a83e0b..b731b52b3c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -131,20 +131,26 @@ impl Router { fn speak_proxy(req: Request, remote_addr: SocketAddr, router: Arc, endpoint: &'static str) -> BoxFut { let mut headers = req.headers().clone(); - for (k, v) in headers.iter() { - info!("Header: {:?} = {:?}", k, v); - } - - // This is the correct client IP, not the IP of the load balancer. - // We're adding it to the headers so that downstream can use it for rate limiting and logging. - let forwarded_ip = HeaderValue::from_str(&remote_addr.ip().to_string()) + // This is the IP of the Digital Ocean load balancer. + let gateway_ip = HeaderValue::from_str(&remote_addr.ip().to_string()) .ok() - .unwrap_or(HeaderValue::from_static("127.0.0.1")); - headers.insert(HeaderName::from_static("forwarded"), forwarded_ip.clone()); - headers.insert(HeaderName::from_static("x-forwarded-for"), forwarded_ip.clone()); + .unwrap_or(HeaderValue::from_static("")); + + // This is the IP of the upstream client browser. + let upstream_client_ip = req.headers().get(HeaderName::from_static("x-forwarded-for")) + .map(|ip| ip.to_str().unwrap_or("")) + .unwrap_or(""); + + let upstream_client_ip = HeaderValue::from_str(upstream_client_ip) + .ok() + .unwrap_or(HeaderValue::from_static("")); + + headers.insert(HeaderName::from_static("forwarded"), upstream_client_ip.clone()); + headers.insert(HeaderName::from_static("x-forwarded-for"), upstream_client_ip.clone()); // Unfortunately it looks like this middleware trounces the standard headers, so // here we put it somewhere it won't be overwritten. - headers.insert(HeaderName::from_static("x-voder-proxy-for"), forwarded_ip.clone()); + headers.insert(HeaderName::from_static("x-voder-proxy-for"), upstream_client_ip.clone()); + headers.insert(HeaderName::from_static("x-gateway-ip"), gateway_ip); Box::new(req.into_body().concat2().map(move |b| { // Builds a BoxedFut to return let request_bytes = b.as_ref(); From 6bd56aad75aa8e96e85a9e6d36b28010265cb2be Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Tue, 4 Aug 2020 22:26:30 -0400 Subject: [PATCH 36/48] add newrelic logging capability --- Cargo.lock | 17 +++++++++++++++++ Cargo.toml | 1 + src/main.rs | 34 ++++++++++++++++++++++++++++------ src/newrelic_logger.rs | 36 ++++++++++++++++++++++++++++++++++++ 4 files changed, 82 insertions(+), 6 deletions(-) create mode 100644 src/newrelic_logger.rs diff --git a/Cargo.lock b/Cargo.lock index 5e1f55a2d7..aeefe27573 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -420,6 +420,22 @@ dependencies = [ "winapi 0.3.9", ] +[[package]] +name = "newrelic" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7ec1eb312ccd33c2d88ff317d59164465a41014ad465ba8a67e2c062343e3a8" +dependencies = [ + "log", + "newrelic-sys", +] + +[[package]] +name = "newrelic-sys" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0a9b9030b5f433ac2afb956eeaf89020e07d3cb9b0561cd07f1737c97a4af93a" + [[package]] name = "num_cpus" version = "1.13.0" @@ -835,6 +851,7 @@ dependencies = [ "hyper", "hyper-reverse-proxy", "log", + "newrelic", "rand", "serde", "serde_derive", diff --git a/Cargo.toml b/Cargo.toml index 07267c4bbb..977a05f305 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,6 +19,7 @@ futures = "0.1" hyper = "0.12" hyper-reverse-proxy = "0.4" log = "0.4.8" +newrelic = "0.2.2" rand = "0.7.3" serde = "1.0.104" serde_derive = "1.0.104" diff --git a/src/main.rs b/src/main.rs index b731b52b3c..de6faf22b1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,8 @@ #[macro_use] extern crate log; #[macro_use] extern crate serde_derive; +pub mod newrelic_logger; + use anyhow::Result as AnyhowResult; use futures::future::{self, Future}; use hyper::Method; @@ -19,18 +21,22 @@ use std::sync::Arc; use rand::seq::IteratorRandom; use hyper::http::header::HeaderName; use hyper::header::HeaderValue; +use newrelic::App; +use crate::newrelic_logger::NewRelicLogger; type BoxFut = Box, Error=hyper::Error> + Send>; const ENV_DEFAULT_ROUTE : &'static str = "DEFAULT_ROUTE"; +const ENV_NEWRELIC_API_KEY : &'static str = "NEWRELIC_API_KEY"; const ENV_PROXY_CONFIG_FILE : &'static str = "PROXY_CONFIG_FILE"; const ENV_RUST_LOG : &'static str = "RUST_LOG"; const ENV_SERVICE_PORT : &'static str = "SERVICE_PORT"; const DEFAULT_DEFAULT_ROUTE : &'static str = "http://127.0.0.1:12345"; +const DEFAULT_NEWRELIC_API_KEY : &'static str = ""; const DEFAULT_PROXY_CONFIG_FILE : &'static str = "proxy_configs.toml"; -const DEFAULT_RUST_LOG: &'static str = "tokio_reactor=warn,hyper=info,debug"; -const DEFAULT_SERVICE_PORT: u16 = 5555; +const DEFAULT_RUST_LOG : &'static str = "tokio_reactor=warn,hyper=info,debug"; +const DEFAULT_SERVICE_PORT : u16 = 5555; #[derive(Deserialize, Debug, Clone)] struct ProxyConfigs { @@ -198,6 +204,7 @@ fn main() -> AnyhowResult<()> { let default_route = get_env_string(ENV_DEFAULT_ROUTE, DEFAULT_DEFAULT_ROUTE); let service_port = get_env_num::(ENV_SERVICE_PORT, DEFAULT_SERVICE_PORT)?; + let newrelic_api_key = get_env_string(ENV_NEWRELIC_API_KEY, DEFAULT_NEWRELIC_API_KEY); info!("Default route: {}", default_route); @@ -219,23 +226,38 @@ fn main() -> AnyhowResult<()> { default_route: default_route, }); + let newrelic_logger = if newrelic_api_key.is_empty() { + NewRelicLogger::null_instance() + } else { + NewRelicLogger::try_new_or_null("voder-proxy", &newrelic_api_key) + }; + + let newrelic_logger = Arc::new(newrelic_logger); + let make_service = make_service_fn(move |socket: &AddrStream| { let remote_addr = socket.remote_addr(); info!("Got a request from: {:?}", remote_addr); let router2 = router.clone(); + let newrelic_logger2 = newrelic_logger.clone(); service_fn(move |req: Request| { let router3 = router2.clone(); + let newrelic_logger3 = newrelic_logger2.clone(); match (req.method(), req.uri().path()) { - (&Method::POST, "/speak") => - speak_proxy(req, remote_addr.clone(), router3, "/speak"), - (&Method::POST, "/speak_spectrogram") => - speak_proxy(req, remote_addr.clone(), router3, "/speak_spectrogram"), + (&Method::POST, "/speak") => { + let _droppable_transaction = newrelic_logger3.web_transaction("proxy /speak"); + speak_proxy(req, remote_addr.clone(), router3, "/speak") + }, + (&Method::POST, "/speak_spectrogram") => { + let _droppable_transaction = newrelic_logger3.web_transaction("proxy /speak_spectrogram"); + speak_proxy(req, remote_addr.clone(), router3, "/speak_spectrogram") + }, (&Method::GET, "/proxy_health") => health_check_response(req), _ => { + let _droppable_transaction = newrelic_logger3.web_transaction("unmatched endpoint"); let forward = router3.get_random_host(); info!("Forwarding to `{}` random host: {}", &forward, req.uri()); hyper_reverse_proxy::call(remote_addr.ip(), &forward, req) diff --git a/src/newrelic_logger.rs b/src/newrelic_logger.rs new file mode 100644 index 0000000000..ea6b74b489 --- /dev/null +++ b/src/newrelic_logger.rs @@ -0,0 +1,36 @@ +use anyhow::Result as AnyhowResult; +use newrelic::{App, Transaction}; + +pub struct NewRelicLogger { + newrelic_api: Option, +} + +impl NewRelicLogger { + pub fn new(app_name: &str, api_key: &str) -> AnyhowResult { + let newrelic_api = App::new(app_name, api_key)?; + Ok(Self { + newrelic_api: Some(newrelic_api), + }) + } + + pub fn null_instance() -> Self { + Self { + newrelic_api: None, + } + } + + pub fn try_new_or_null(app_name: &str, api_key: &str) -> Self { + match Self::new(app_name, api_key) { + Ok(instance) => instance, + Err(_) => Self::null_instance(), + } + } + + pub fn web_transaction(&self, transaction_name: &str) -> Option { + if let Some(ref newrelic_api) = self.newrelic_api { + info!("Logging request to NewRelic"); + return newrelic_api.web_transaction(transaction_name).ok(); + } + None + } +} From 47f4f75eb52ade91192da97ac020278a05c645c9 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Tue, 4 Aug 2020 23:36:01 -0400 Subject: [PATCH 37/48] build newrelic libs --- Dockerfile | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index cb3581c1ef..a93d6eaf82 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,22 +3,33 @@ FROM ubuntu:xenial as build WORKDIR /tmp +# libpcre3-dev is for newrelic RUN apt-get update \ && apt-get install -y \ build-essential \ curl \ + libpcre3-dev \ libssl-dev \ libssl1.0.0 \ pkg-config + RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs \ | sh -s -- --default-toolchain stable -y -COPY Cargo.lock . +COPY Cargo.lock . COPY Cargo.toml . COPY src/ ./src RUN $HOME/.cargo/bin/cargo fetch +RUN curl https://github.com/newrelic/c-sdk/archive/v1.3.0.tar.gz -L -o newrelic.tar.gz +RUN tar -xf newrelic.tar.gz + +# NB: There's also a dynamic library target +# Static output is './c-sdk-1.3.0/libnewrelic.a' +RUN cd ./c-sdk-1.3.0 && make static +RUN cp ./c-sdk-1.3.0/libnewrelic.a . + #RUN LD_LIBRARY_PATH=/usr/lib:${LD_LIBRARY_PATH} $HOME/.cargo/bin/cargo build RUN LD_LIBRARY_PATH=/usr/lib:${LD_LIBRARY_PATH} $HOME/.cargo/bin/cargo build --release From 43599c2a025ba50cdd4d01d7cefdd9ebfdeb8869 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Wed, 5 Aug 2020 00:45:56 -0400 Subject: [PATCH 38/48] add newrelic attribute for speaker --- src/main.rs | 33 +++++++++++++++++++++------------ src/newrelic_logger.rs | 25 ++++++++++++++++++++++--- 2 files changed, 43 insertions(+), 15 deletions(-) diff --git a/src/main.rs b/src/main.rs index de6faf22b1..ef22eb1483 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,12 +5,16 @@ pub mod newrelic_logger; use anyhow::Result as AnyhowResult; +use crate::newrelic_logger::{NewRelicLogger, MaybeNewRelicTransaction}; use futures::future::{self, Future}; use hyper::Method; +use hyper::header::HeaderValue; +use hyper::http::header::HeaderName; use hyper::rt::Stream; use hyper::server::conn::AddrStream; use hyper::service::{service_fn, make_service_fn}; use hyper::{Body, Request, Response, Server}; +use rand::seq::IteratorRandom; use std::collections::HashMap; use std::env; use std::fmt::Debug; @@ -18,11 +22,6 @@ use std::fmt::Display; use std::net::SocketAddr; use std::str::FromStr; use std::sync::Arc; -use rand::seq::IteratorRandom; -use hyper::http::header::HeaderName; -use hyper::header::HeaderValue; -use newrelic::App; -use crate::newrelic_logger::NewRelicLogger; type BoxFut = Box, Error=hyper::Error> + Send>; @@ -134,7 +133,13 @@ impl Router { } } -fn speak_proxy(req: Request, remote_addr: SocketAddr, router: Arc, endpoint: &'static str) -> BoxFut { +fn speak_proxy( + req: Request, + remote_addr: SocketAddr, + router: Arc, + transaction: MaybeNewRelicTransaction, + endpoint: &'static str) -> BoxFut +{ let mut headers = req.headers().clone(); // This is the IP of the Digital Ocean load balancer. @@ -173,6 +178,8 @@ fn speak_proxy(req: Request, remote_addr: SocketAddr, router: Arc, info!("Routing {} for {} to {}", endpoint, &request_details.speaker, &proxy_host); + transaction.add_attribute("speaker", &request_details.speaker); + let mut request_builder = Request::builder(); request_builder.method(Method::POST) .uri(endpoint); @@ -247,15 +254,17 @@ fn main() -> AnyhowResult<()> { match (req.method(), req.uri().path()) { (&Method::POST, "/speak") => { - let _droppable_transaction = newrelic_logger3.web_transaction("proxy /speak"); - speak_proxy(req, remote_addr.clone(), router3, "/speak") + let nr_transaction = newrelic_logger3.web_transaction("/speak"); + speak_proxy(req, remote_addr.clone(), router3, nr_transaction, "/speak") }, (&Method::POST, "/speak_spectrogram") => { - let _droppable_transaction = newrelic_logger3.web_transaction("proxy /speak_spectrogram"); - speak_proxy(req, remote_addr.clone(), router3, "/speak_spectrogram") + let nr_transaction = newrelic_logger3.web_transaction("/speak_spectrogram"); + speak_proxy(req, remote_addr.clone(), router3, nr_transaction, "/speak_spectrogram") + }, + (&Method::GET, "/proxy_health") => { + let _droppable_transaction = newrelic_logger3.web_transaction("/proxy_health"); + health_check_response(req) }, - (&Method::GET, "/proxy_health") => - health_check_response(req), _ => { let _droppable_transaction = newrelic_logger3.web_transaction("unmatched endpoint"); let forward = router3.get_random_host(); diff --git a/src/newrelic_logger.rs b/src/newrelic_logger.rs index ea6b74b489..031ba0ce11 100644 --- a/src/newrelic_logger.rs +++ b/src/newrelic_logger.rs @@ -26,11 +26,30 @@ impl NewRelicLogger { } } - pub fn web_transaction(&self, transaction_name: &str) -> Option { + pub fn web_transaction(&self, transaction_name: &str) -> MaybeNewRelicTransaction { if let Some(ref newrelic_api) = self.newrelic_api { info!("Logging request to NewRelic"); - return newrelic_api.web_transaction(transaction_name).ok(); + let maybe_transaction = newrelic_api.web_transaction(transaction_name).ok(); + return MaybeNewRelicTransaction::from_optional(maybe_transaction); } - None + MaybeNewRelicTransaction::from_optional(None) } } + +pub struct MaybeNewRelicTransaction { + transaction: Option, +} + +impl MaybeNewRelicTransaction { + pub fn from_optional(optional: Option) -> Self { + Self { + transaction: optional, + } + } + + pub fn add_attribute(&self, name: &str, value: &str) { + if let Some(ref transaction) = self.transaction { + let _result = transaction.add_attribute(name, value); + } + } +} \ No newline at end of file From 6c66da2964967922cccded1be1a820db3f38f843 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Wed, 5 Aug 2020 01:10:08 -0400 Subject: [PATCH 39/48] add logging to attributes --- src/newrelic_logger.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/newrelic_logger.rs b/src/newrelic_logger.rs index 031ba0ce11..4e654583a3 100644 --- a/src/newrelic_logger.rs +++ b/src/newrelic_logger.rs @@ -49,6 +49,7 @@ impl MaybeNewRelicTransaction { pub fn add_attribute(&self, name: &str, value: &str) { if let Some(ref transaction) = self.transaction { + info!("Logging attribute to NewRelic"); let _result = transaction.add_attribute(name, value); } } From 625dac6c1976d115531dcc2564a34509babd2aa9 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Sat, 5 Sep 2020 00:37:49 -0400 Subject: [PATCH 40/48] Move speak_proxy forwarding function into module --- src/main.rs | 82 ++------------------------------------- src/speak_proxy.rs | 97 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+), 79 deletions(-) create mode 100644 src/speak_proxy.rs diff --git a/src/main.rs b/src/main.rs index ef22eb1483..8c1c93c775 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,9 +3,11 @@ #[macro_use] extern crate serde_derive; pub mod newrelic_logger; +pub mod speak_proxy; use anyhow::Result as AnyhowResult; use crate::newrelic_logger::{NewRelicLogger, MaybeNewRelicTransaction}; +use crate::speak_proxy::speak_proxy; use futures::future::{self, Future}; use hyper::Method; use hyper::header::HeaderValue; @@ -57,16 +59,6 @@ impl ProxyConfigs { } } -/// NB: This much match the shape of SpeakRequest in the 'voder/tts_service' code. -/// This is used for both /speak and /speak_spectrogram requests. -#[derive(Serialize, Deserialize, Debug)] -pub struct SpeakRequest { - /// Slug for the speaker - speaker: String, - /// Raw text to be spoken - text: String, -} - fn get_env_string(env_name: &str, default: &str) -> String { match env::var(env_name).as_ref().ok() { Some(s) => s.to_string(), @@ -107,12 +99,7 @@ fn health_check_response(_req: Request) -> BoxFut { Box::new(future::ok(response)) } -struct RequestDetails { - pub request_bytes: Vec, - pub speaker: String, -} - -struct Router { +pub struct Router { pub routes: HashMap, pub default_route: String, } @@ -133,69 +120,6 @@ impl Router { } } -fn speak_proxy( - req: Request, - remote_addr: SocketAddr, - router: Arc, - transaction: MaybeNewRelicTransaction, - endpoint: &'static str) -> BoxFut -{ - let mut headers = req.headers().clone(); - - // This is the IP of the Digital Ocean load balancer. - let gateway_ip = HeaderValue::from_str(&remote_addr.ip().to_string()) - .ok() - .unwrap_or(HeaderValue::from_static("")); - - // This is the IP of the upstream client browser. - let upstream_client_ip = req.headers().get(HeaderName::from_static("x-forwarded-for")) - .map(|ip| ip.to_str().unwrap_or("")) - .unwrap_or(""); - - let upstream_client_ip = HeaderValue::from_str(upstream_client_ip) - .ok() - .unwrap_or(HeaderValue::from_static("")); - - headers.insert(HeaderName::from_static("forwarded"), upstream_client_ip.clone()); - headers.insert(HeaderName::from_static("x-forwarded-for"), upstream_client_ip.clone()); - // Unfortunately it looks like this middleware trounces the standard headers, so - // here we put it somewhere it won't be overwritten. - headers.insert(HeaderName::from_static("x-voder-proxy-for"), upstream_client_ip.clone()); - headers.insert(HeaderName::from_static("x-gateway-ip"), gateway_ip); - - Box::new(req.into_body().concat2().map(move |b| { // Builds a BoxedFut to return - let request_bytes = b.as_ref(); - - let request = serde_json::from_slice::(request_bytes) - .unwrap(); - - RequestDetails { - request_bytes: request_bytes.to_vec(), - speaker: request.speaker, - } - }).and_then(move |request_details: RequestDetails| { - let proxy_host = router.get_speaker_host(&request_details.speaker); - - info!("Routing {} for {} to {}", endpoint, &request_details.speaker, &proxy_host); - - transaction.add_attribute("speaker", &request_details.speaker); - - let mut request_builder = Request::builder(); - request_builder.method(Method::POST) - .uri(endpoint); - - for (k, v) in headers.iter() { - request_builder.header(k, v); - } - - let new_req = request_builder - .body(Body::from(request_details.request_bytes)) - .unwrap(); - - hyper_reverse_proxy::call(remote_addr.ip(), &proxy_host, new_req) - })) -} - fn main() -> AnyhowResult<()> { if env::var(ENV_RUST_LOG) .as_ref() diff --git a/src/speak_proxy.rs b/src/speak_proxy.rs new file mode 100644 index 0000000000..6b638a9445 --- /dev/null +++ b/src/speak_proxy.rs @@ -0,0 +1,97 @@ +use anyhow::Result as AnyhowResult; +use crate::{BoxFut, Router}; +use crate::newrelic_logger::{NewRelicLogger, MaybeNewRelicTransaction}; +use futures::future::{self, Future}; +use hyper::Method; +use hyper::header::HeaderValue; +use hyper::http::header::HeaderName; +use hyper::rt::Stream; +use hyper::server::conn::AddrStream; +use hyper::service::{service_fn, make_service_fn}; +use hyper::{Body, Request, Response, Server}; +use rand::seq::IteratorRandom; +use std::collections::HashMap; +use std::env; +use std::fmt::Debug; +use std::fmt::Display; +use std::net::SocketAddr; +use std::str::FromStr; +use std::sync::Arc; + +/// NB: This much match the shape of SpeakRequest in the 'voder/tts_service' code. +/// This is used for both /speak and /speak_spectrogram requests. +#[derive(Serialize, Deserialize, Debug)] +pub struct SpeakRequest { + /// Slug for the speaker + speaker: String, + /// Raw text to be spoken + text: String, +} + +struct RequestDetails { + pub request_bytes: Vec, + pub speaker: String, +} + +pub fn speak_proxy( + req: Request, + remote_addr: SocketAddr, + router: Arc, + transaction: MaybeNewRelicTransaction, + endpoint: &'static str) -> BoxFut +{ + let mut headers = req.headers().clone(); + + // This is the IP of the Digital Ocean load balancer. + let gateway_ip = HeaderValue::from_str(&remote_addr.ip().to_string()) + .ok() + .unwrap_or(HeaderValue::from_static("")); + + // This is the IP of the upstream client browser. + let upstream_client_ip = req.headers().get(HeaderName::from_static("x-forwarded-for")) + .map(|ip| ip.to_str().unwrap_or("")) + .unwrap_or(""); + + let upstream_client_ip = HeaderValue::from_str(upstream_client_ip) + .ok() + .unwrap_or(HeaderValue::from_static("")); + + headers.insert(HeaderName::from_static("forwarded"), upstream_client_ip.clone()); + headers.insert(HeaderName::from_static("x-forwarded-for"), upstream_client_ip.clone()); + // Unfortunately it looks like this middleware trounces the standard headers, so + // here we put it somewhere it won't be overwritten. + headers.insert(HeaderName::from_static("x-voder-proxy-for"), upstream_client_ip.clone()); + headers.insert(HeaderName::from_static("x-gateway-ip"), gateway_ip); + + Box::new(req.into_body().concat2().map(move |b| { // Builds a BoxedFut to return + let request_bytes = b.as_ref(); + + let request = serde_json::from_slice::(request_bytes) + .unwrap(); + + RequestDetails { + request_bytes: request_bytes.to_vec(), + speaker: request.speaker, + } + }).and_then(move |request_details: RequestDetails| { + let proxy_host = router.get_speaker_host(&request_details.speaker); + + info!("Routing {} for {} to {}", endpoint, &request_details.speaker, &proxy_host); + + transaction.add_attribute("speaker", &request_details.speaker); + + let mut request_builder = Request::builder(); + request_builder.method(Method::POST) + .uri(endpoint); + + for (k, v) in headers.iter() { + request_builder.header(k, v); + } + + let new_req = request_builder + .body(Body::from(request_details.request_bytes)) + .unwrap(); + + hyper_reverse_proxy::call(remote_addr.ip(), &proxy_host, new_req) + })) +} From 74de16f952ae32333c9422990a60704989600465 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Sat, 5 Sep 2020 00:46:17 -0400 Subject: [PATCH 41/48] if I just read the docs, I would have figured this out sooner :| --- src/main.rs | 4 ++-- src/speak_proxy.rs | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index 8c1c93c775..f733e4e24f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,7 +7,7 @@ pub mod speak_proxy; use anyhow::Result as AnyhowResult; use crate::newrelic_logger::{NewRelicLogger, MaybeNewRelicTransaction}; -use crate::speak_proxy::speak_proxy; +use crate::speak_proxy::{speak_proxy, speak_proxy_with_retry}; use futures::future::{self, Future}; use hyper::Method; use hyper::header::HeaderValue; @@ -179,7 +179,7 @@ fn main() -> AnyhowResult<()> { match (req.method(), req.uri().path()) { (&Method::POST, "/speak") => { let nr_transaction = newrelic_logger3.web_transaction("/speak"); - speak_proxy(req, remote_addr.clone(), router3, nr_transaction, "/speak") + speak_proxy_with_retry(req, remote_addr.clone(), router3, nr_transaction, "/speak") }, (&Method::POST, "/speak_spectrogram") => { let nr_transaction = newrelic_logger3.web_transaction("/speak_spectrogram"); diff --git a/src/speak_proxy.rs b/src/speak_proxy.rs index 6b638a9445..fbe199ff81 100644 --- a/src/speak_proxy.rs +++ b/src/speak_proxy.rs @@ -17,6 +17,7 @@ use std::fmt::Display; use std::net::SocketAddr; use std::str::FromStr; use std::sync::Arc; +use futures::IntoFuture; /// NB: This much match the shape of SpeakRequest in the 'voder/tts_service' code. /// This is used for both /speak and /speak_spectrogram requests. @@ -33,6 +34,23 @@ struct RequestDetails { pub speaker: String, } +pub fn speak_proxy_with_retry( + req: Request, + remote_addr: SocketAddr, + router: Arc, + transaction: MaybeNewRelicTransaction, + endpoint: &'static str) -> BoxFut +{ + speak_proxy(req, remote_addr, router, transaction, endpoint) + .then(|result| { + match result { + Ok(t) => future::ok(t), + Err(t) => future::err(t), + } + }) + .boxed() +} + pub fn speak_proxy( req: Request, remote_addr: SocketAddr, From e1c39e65de92c214037397ec4c1ad73c1c7de3e9 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Mon, 7 Sep 2020 14:12:40 -0400 Subject: [PATCH 42/48] Robust proxying with retry Also includes upgraded hyper-reverse-proxy, hyper, and futures. This is looking and feeling way more ergonomic. --- Cargo.lock | 590 +++++++++++++++++++++------------------------ Cargo.toml | 9 +- src/api.rs | 102 ++++++++ src/main.rs | 136 +++++++---- src/speak_proxy.rs | 168 +++++++------ 5 files changed, 556 insertions(+), 449 deletions(-) create mode 100644 src/api.rs diff --git a/Cargo.lock b/Cargo.lock index aeefe27573..dcd869e540 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -15,6 +15,12 @@ version = "1.0.31" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "85bb70cc08ec97ca5450e6eba421deeea5f172c0fc61f78b5357b2a8e8be195f" +[[package]] +name = "arc-swap" +version = "0.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4d25d88fd6b8041580a654f9d0c581a047baee2b3efee13275f2fc392fc75034" + [[package]] name = "atty" version = "0.2.14" @@ -38,22 +44,11 @@ version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" -[[package]] -name = "byteorder" -version = "1.3.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "08c48aae112d48ed9f069b33538ea9e3e90aa263cfa3d1c24309612b1f7472de" - [[package]] name = "bytes" -version = "0.4.12" +version = "0.5.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "206fdffcfa2df7cbe15601ef46c813fce0965eb3286db6b56c583b814b51c81c" -dependencies = [ - "byteorder", - "either", - "iovec", -] +checksum = "0e4cec68f03f32e44924783795810fa50a7035d8c8ebe78580ad7e6c703fba38" [[package]] name = "cfg-if" @@ -61,69 +56,6 @@ version = "0.1.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" -[[package]] -name = "cloudabi" -version = "0.0.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f" -dependencies = [ - "bitflags", -] - -[[package]] -name = "crossbeam-deque" -version = "0.7.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9f02af974daeee82218205558e51ec8768b48cf524bd01d550abe5573a608285" -dependencies = [ - "crossbeam-epoch", - "crossbeam-utils", - "maybe-uninit", -] - -[[package]] -name = "crossbeam-epoch" -version = "0.8.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "058ed274caafc1f60c4997b5fc07bf7dc7cca454af7c6e81edffe5f33f70dace" -dependencies = [ - "autocfg", - "cfg-if", - "crossbeam-utils", - "lazy_static", - "maybe-uninit", - "memoffset", - "scopeguard", -] - -[[package]] -name = "crossbeam-queue" -version = "0.2.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "774ba60a54c213d409d5353bda12d49cd68d14e45036a285234c8d6f91f92570" -dependencies = [ - "cfg-if", - "crossbeam-utils", - "maybe-uninit", -] - -[[package]] -name = "crossbeam-utils" -version = "0.7.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c3c7c73a2d1e9fc0886a08b93e98eb643461230d5f1925e4036204d5f2e261a8" -dependencies = [ - "autocfg", - "cfg-if", - "lazy_static", -] - -[[package]] -name = "either" -version = "1.5.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bb1f6b1ce1c140482ea30ddd3335fc0024ac7ee112895426e0a629a6c20adfe3" - [[package]] name = "env_logger" version = "0.7.1" @@ -161,18 +93,97 @@ checksum = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7" [[package]] name = "futures" -version = "0.1.29" +version = "0.3.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1b980f2816d6ee8673b6517b52cb0e808a180efc92e5c19d02cdda79066703ef" +checksum = "1e05b85ec287aac0dc34db7d4a569323df697f9c55b99b15d6b4ef8cde49f613" +dependencies = [ + "futures-channel", + "futures-core", + "futures-executor", + "futures-io", + "futures-sink", + "futures-task", + "futures-util", +] [[package]] -name = "futures-cpupool" -version = "0.1.8" +name = "futures-channel" +version = "0.3.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ab90cde24b3319636588d0c35fe03b1333857621051837ed769faefb4c2162e4" +checksum = "f366ad74c28cca6ba456d95e6422883cfb4b252a83bed929c83abfdbbf2967d5" dependencies = [ - "futures", - "num_cpus", + "futures-core", + "futures-sink", +] + +[[package]] +name = "futures-core" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59f5fff90fd5d971f936ad674802482ba441b6f09ba5e15fd8b39145582ca399" + +[[package]] +name = "futures-executor" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "10d6bb888be1153d3abeb9006b11b02cf5e9b209fda28693c31ae1e4e012e314" +dependencies = [ + "futures-core", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-io" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "de27142b013a8e869c14957e6d2edeef89e97c289e69d042ee3a49acd8b51789" + +[[package]] +name = "futures-macro" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d0b5a30a4328ab5473878237c447333c093297bded83a4983d10f4deea240d39" +dependencies = [ + "proc-macro-hack", + "proc-macro2", + "quote", + "syn", +] + +[[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" +dependencies = [ + "once_cell", +] + +[[package]] +name = "futures-util" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8764574ff08b701a084482c3c7031349104b07ac897393010494beaa18ce32c6" +dependencies = [ + "futures-channel", + "futures-core", + "futures-io", + "futures-macro", + "futures-sink", + "futures-task", + "memchr", + "pin-project", + "pin-utils", + "proc-macro-hack", + "proc-macro-nested", + "slab", ] [[package]] @@ -188,20 +199,21 @@ dependencies = [ [[package]] name = "h2" -version = "0.1.26" +version = "0.2.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a5b34c246847f938a410a03c5458c7fee2274436675e76d8b903c08efc29c462" +checksum = "993f9e0baeed60001cf565546b0d3dbe6a6ad23f2bd31644a133c641eccf6d53" dependencies = [ - "byteorder", "bytes", "fnv", - "futures", + "futures-core", + "futures-sink", + "futures-util", "http", "indexmap", - "log", "slab", - "string", - "tokio-io", + "tokio", + "tokio-util", + "tracing", ] [[package]] @@ -215,9 +227,9 @@ dependencies = [ [[package]] name = "http" -version = "0.1.21" +version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d6ccf5ede3a895d8856620237b2f02972c1bbc78d2965ad7fe8838d4a0ed41f0" +checksum = "28d569972648b2c512421b5f2a405ad6ac9666547189d0c5477a3f200f3e02f9" dependencies = [ "bytes", "fnv", @@ -226,14 +238,12 @@ dependencies = [ [[package]] name = "http-body" -version = "0.1.0" +version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6741c859c1b2463a423a1dbce98d418e6c3c3fc720fb0d45528657320920292d" +checksum = "13d5ff830006f7646652e057693569bfe0d51760c0085a071769d142a205111b" dependencies = [ "bytes", - "futures", "http", - "tokio-buf", ] [[package]] @@ -253,41 +263,33 @@ dependencies = [ [[package]] name = "hyper" -version = "0.12.35" +version = "0.13.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9dbe6ed1438e1f8ad955a4701e9a944938e9519f6888d12d8558b645e247d5f6" +checksum = "3e68a8dd9716185d9e64ea473ea6ef63529252e3e27623295a0378a19665d5eb" dependencies = [ "bytes", - "futures", - "futures-cpupool", + "futures-channel", + "futures-core", + "futures-util", "h2", "http", "http-body", "httparse", - "iovec", "itoa", - "log", - "net2", - "rustc_version", + "pin-project", + "socket2", "time", "tokio", - "tokio-buf", - "tokio-executor", - "tokio-io", - "tokio-reactor", - "tokio-tcp", - "tokio-threadpool", - "tokio-timer", + "tower-service", + "tracing", "want", ] [[package]] name = "hyper-reverse-proxy" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "474f19c624c13005d914fc4a73aa2a2921b0371f5f7b748146f5130685492ce8" +version = "0.5.0" +source = "git+https://github.com/felipenoris/hyper-reverse-proxy.git#d109fcbd629c62eeb5c34bec1074d737e7b58529" dependencies = [ - "futures", "hyper", "lazy_static", "unicase", @@ -339,15 +341,6 @@ version = "0.2.71" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9457b06509d27052635f90d6466700c65095fdf75409b3fbdd903e988b886f49" -[[package]] -name = "lock_api" -version = "0.3.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c4da24a77a3d8a6d4862d95f72e6fdb9c09a643ecdb402d754004a557f2bec75" -dependencies = [ - "scopeguard", -] - [[package]] name = "log" version = "0.4.8" @@ -357,27 +350,12 @@ dependencies = [ "cfg-if", ] -[[package]] -name = "maybe-uninit" -version = "2.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "60302e4db3a61da70c0cb7991976248362f30319e88850c487b9b95bbf059e00" - [[package]] name = "memchr" version = "2.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3728d817d99e5ac407411fa471ff9800a778d88a24685968b36824eaf4bee400" -[[package]] -name = "memoffset" -version = "0.5.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b4fc2c02a7e374099d4ee95a193111f72d2110197fe200272371758f6c3643d8" -dependencies = [ - "autocfg", -] - [[package]] name = "mio" version = "0.6.22" @@ -391,12 +369,35 @@ dependencies = [ "kernel32-sys", "libc", "log", - "miow", + "miow 0.2.1", "net2", "slab", "winapi 0.2.8", ] +[[package]] +name = "mio-named-pipes" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0840c1c50fd55e521b247f949c241c9997709f23bd7f023b9762cd561e935656" +dependencies = [ + "log", + "mio", + "miow 0.3.5", + "winapi 0.3.9", +] + +[[package]] +name = "mio-uds" +version = "0.6.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "afcb699eb26d4332647cc848492bbc15eafb26f08d0304550d5aa1f612e066f0" +dependencies = [ + "iovec", + "libc", + "mio", +] + [[package]] name = "miow" version = "0.2.1" @@ -409,6 +410,16 @@ dependencies = [ "ws2_32-sys", ] +[[package]] +name = "miow" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07b88fb9795d4d36d62a012dfbf49a8f5cf12751f36d31a9dbe66d528e58979e" +dependencies = [ + "socket2", + "winapi 0.3.9", +] + [[package]] name = "net2" version = "0.2.34" @@ -447,37 +458,61 @@ dependencies = [ ] [[package]] -name = "parking_lot" -version = "0.9.0" +name = "once_cell" +version = "1.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f842b1982eb6c2fe34036a4fbfb06dd185a3f5c8edfaacdf7d1ea10b07de6252" +checksum = "260e51e7efe62b592207e9e13a68e43692a7a279171d6ba57abd208bf23645ad" + +[[package]] +name = "pin-project" +version = "0.4.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca4433fff2ae79342e497d9f8ee990d174071408f28f726d6d83af93e58e48aa" dependencies = [ - "lock_api", - "parking_lot_core", - "rustc_version", + "pin-project-internal", ] [[package]] -name = "parking_lot_core" -version = "0.6.2" +name = "pin-project-internal" +version = "0.4.23" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b876b1b9e7ac6e1a74a6da34d25c42e17e8862aa409cbbbdcfc8d86c6f3bc62b" +checksum = "2c0e815c3ee9a031fdf5af21c10aa17c573c9c6a566328d99e3936c34e36461f" dependencies = [ - "cfg-if", - "cloudabi", - "libc", - "redox_syscall", - "rustc_version", - "smallvec", - "winapi 0.3.9", + "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 = "ppv-lite86" version = "0.2.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "237a5ed80e274dbc66f86bd59c1e25edc039660be53194b5fe0a482e0f2612ea" +[[package]] +name = "proc-macro-hack" +version = "0.5.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "99c605b9a0adc77b7211c6b1f722dcb613d68d66859a44f3d485a6da332b0598" + +[[package]] +name = "proc-macro-nested" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eba180dafb9038b050a4c280019bbedf9f2467b61e5d892dcad585bb57aadc5a" + [[package]] name = "proc-macro2" version = "1.0.18" @@ -567,42 +602,12 @@ version = "0.6.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "26412eb97c6b088a6997e05f69403a802a92d520de2f8e63c2b65f9e0f47c4e8" -[[package]] -name = "rustc_version" -version = "0.2.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a" -dependencies = [ - "semver", -] - [[package]] name = "ryu" version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e" -[[package]] -name = "scopeguard" -version = "1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" - -[[package]] -name = "semver" -version = "0.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403" -dependencies = [ - "semver-parser", -] - -[[package]] -name = "semver-parser" -version = "0.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" - [[package]] name = "serde" version = "1.0.114" @@ -631,6 +636,16 @@ dependencies = [ "serde", ] +[[package]] +name = "signal-hook-registry" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a3e12110bc539e657a646068aaf5eb5b63af9d0c1f7b29c97113fad80e15f035" +dependencies = [ + "arc-swap", + "libc", +] + [[package]] name = "slab" version = "0.4.2" @@ -638,21 +653,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c111b5bd5695e56cffe5129854aa230b39c93a305372fdbb2668ca2394eea9f8" [[package]] -name = "smallvec" -version = "0.6.13" +name = "socket2" +version = "0.3.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f7b0758c52e15a8b5e3691eae6cc559f08eee9406e548a4477ba4e67770a82b6" +checksum = "03088793f677dce356f3ccc2edb1b314ad191ab702a5de3faf49304f7e104918" dependencies = [ - "maybe-uninit", -] - -[[package]] -name = "string" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d24114bfcceb867ca7f71a0d3fe45d45619ec47a6fbfa98cb14e14250bfa5d6d" -dependencies = [ - "bytes", + "cfg-if", + "libc", + "redox_syscall", + "winapi 0.3.9", ] [[package]] @@ -696,134 +705,51 @@ dependencies = [ [[package]] name = "tokio" -version = "0.1.22" +version = "0.2.22" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5a09c0b5bb588872ab2f09afa13ee6e9dac11e10a0ec9e8e3ba39a5a5d530af6" +checksum = "5d34ca54d84bf2b5b4d7d31e901a8464f7b60ac145a284fba25ceb801f2ddccd" dependencies = [ "bytes", - "futures", - "mio", - "num_cpus", - "tokio-current-thread", - "tokio-executor", - "tokio-io", - "tokio-reactor", - "tokio-threadpool", - "tokio-timer", -] - -[[package]] -name = "tokio-buf" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8fb220f46c53859a4b7ec083e41dec9778ff0b1851c0942b211edb89e0ccdc46" -dependencies = [ - "bytes", - "either", - "futures", -] - -[[package]] -name = "tokio-current-thread" -version = "0.1.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b1de0e32a83f131e002238d7ccde18211c0a5397f60cbfffcb112868c2e0e20e" -dependencies = [ - "futures", - "tokio-executor", -] - -[[package]] -name = "tokio-executor" -version = "0.1.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fb2d1b8f4548dbf5e1f7818512e9c406860678f29c300cdf0ebac72d1a3a1671" -dependencies = [ - "crossbeam-utils", - "futures", -] - -[[package]] -name = "tokio-io" -version = "0.1.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "57fc868aae093479e3131e3d165c93b1c7474109d13c90ec0dda2a1bbfff0674" -dependencies = [ - "bytes", - "futures", - "log", -] - -[[package]] -name = "tokio-reactor" -version = "0.1.12" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "09bc590ec4ba8ba87652da2068d150dcada2cfa2e07faae270a5e0409aa51351" -dependencies = [ - "crossbeam-utils", - "futures", - "lazy_static", - "log", - "mio", - "num_cpus", - "parking_lot", - "slab", - "tokio-executor", - "tokio-io", - "tokio-sync", -] - -[[package]] -name = "tokio-sync" -version = "0.1.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "edfe50152bc8164fcc456dab7891fa9bf8beaf01c5ee7e1dd43a397c3cf87dee" -dependencies = [ "fnv", - "futures", + "futures-core", + "iovec", + "lazy_static", + "libc", + "memchr", + "mio", + "mio-named-pipes", + "mio-uds", + "num_cpus", + "pin-project-lite", + "signal-hook-registry", + "slab", + "tokio-macros", + "winapi 0.3.9", ] [[package]] -name = "tokio-tcp" -version = "0.1.4" +name = "tokio-macros" +version = "0.2.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "98df18ed66e3b72e742f185882a9e201892407957e45fbff8da17ae7a7c51f72" +checksum = "f0c3acc6aa564495a0f2e1d59fab677cd7f81a19994cfc7f3ad0e64301560389" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tokio-util" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "be8242891f2b6cbef26a2d7e8605133c2c554cd35b3e4948ea892d6d68436499" dependencies = [ "bytes", - "futures", - "iovec", - "mio", - "tokio-io", - "tokio-reactor", -] - -[[package]] -name = "tokio-threadpool" -version = "0.1.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "df720b6581784c118f0eb4310796b12b1d242a7eb95f716a8367855325c25f89" -dependencies = [ - "crossbeam-deque", - "crossbeam-queue", - "crossbeam-utils", - "futures", - "lazy_static", + "futures-core", + "futures-sink", "log", - "num_cpus", - "slab", - "tokio-executor", -] - -[[package]] -name = "tokio-timer" -version = "0.2.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "93044f2d313c95ff1cb7809ce9a7a05735b012288a888b62d4434fd58c94f296" -dependencies = [ - "crossbeam-utils", - "futures", - "slab", - "tokio-executor", + "pin-project-lite", + "tokio", ] [[package]] @@ -835,6 +761,32 @@ dependencies = [ "serde", ] +[[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.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d79ca061b032d6ce30c660fded31189ca0b9922bf483cd70759f13a2d86786c" +dependencies = [ + "cfg-if", + "log", + "tracing-core", +] + +[[package]] +name = "tracing-core" +version = "0.1.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4f0e00789804e99b20f12bc7003ca416309d28a6f495d6af58d1e2c2842461b5" +dependencies = [ + "lazy_static", +] + [[package]] name = "try-lock" version = "0.2.2" @@ -856,6 +808,7 @@ dependencies = [ "serde", "serde_derive", "serde_json", + "tokio", "toml", ] @@ -882,11 +835,10 @@ checksum = "b5a972e5669d67ba988ce3dc826706fb0a8b01471c088cb0b6110b805cc36aed" [[package]] name = "want" -version = "0.2.0" +version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b6395efa4784b027708f7451087e647ec73cc74f5d9bc2e418404248d679a230" +checksum = "1ce8a968cb1cd110d136ff8b819a556d6fb6d919363c61534f6860c7eb172ba0" dependencies = [ - "futures", "log", "try-lock", ] diff --git a/Cargo.toml b/Cargo.toml index 977a05f305..68f9542535 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,13 +15,14 @@ path = "src/main.rs" [dependencies] anyhow = "1.0.31" env_logger = "0.7.1" -futures = "0.1" -hyper = "0.12" -hyper-reverse-proxy = "0.4" +futures = "0.3" +hyper = "0.13" +hyper-reverse-proxy = { git = "https://github.com/felipenoris/hyper-reverse-proxy.git", commit = "d109fcbd629c62eeb5c34bec1074d737e7b58529" } log = "0.4.8" newrelic = "0.2.2" rand = "0.7.3" serde = "1.0.104" serde_derive = "1.0.104" serde_json = "1.0.55" -toml = "0.5.6" +tokio = { version = "0.2", features = ["full"] } +toml = "0.5.6" \ No newline at end of file diff --git a/src/api.rs b/src/api.rs new file mode 100644 index 0000000000..38926a8548 --- /dev/null +++ b/src/api.rs @@ -0,0 +1,102 @@ +use anyhow::Result as AnyhowResult; +use futures::TryStreamExt; +use hyper::http::HeaderValue; +use hyper::{Body, Request, HeaderMap, Response, StatusCode}; +use hyper::body::Bytes; + +/// NB: This much match the shape of SpeakRequest in the 'voder/tts_service' code. +/// This is used for both /speak and /speak_spectrogram requests. +#[derive(Deserialize, Debug)] +pub struct ExternalSpeakRequest { + /// Slug for the speaker + pub speaker: String, + + /// Raw text to be spoken + pub text: String, +} + +/// The full request +pub struct ExternalHttpRequest { + pub headers: HeaderMap, + pub speak_request: ExternalSpeakRequest, +} + +#[derive(Serialize, Debug)] +pub struct InternalSpeakRequest { + /// Slug for the speaker + pub speaker: String, + + /// Raw text to be spoken + pub text: String, + + /// Retry attempt. + /// The first request is "0", and all subsequent requests are incremented. + /// Subsequent requests do not trigger the rate limiter. + pub retry_attempt_number: u8, + + /// Admin/debug requests get to skip the rate limiter. + pub skip_rate_limiter: bool, +} + +pub enum ErrorType { + /// The proxy couldn't find a healthy host (or it died mid-connection) + ProxyError, + // The backend server reported a 500 + ServerError, + /// Client is rate limited + RateLimitError, +} + +pub struct ErrorResponse { + /// The type of error + pub error_type: ErrorType, + /// Description of the error + pub error_description: String, +} + +impl ExternalHttpRequest { + + pub async fn decode_request(request: Request) -> AnyhowResult { + let headers = request.headers().clone(); + let (_parts, body) = request.into_parts(); + + let body_bytes = body_to_bytes(body).await?; + let speak_request = serde_json::from_slice::(&body_bytes)?; + + Ok(ExternalHttpRequest { + headers: headers, + speak_request: speak_request, + }) + } +} + +impl ErrorType { + pub fn status_code(&self) -> StatusCode { + match *self { + ErrorType::ProxyError =>StatusCode::INTERNAL_SERVER_ERROR, + ErrorType::ServerError => StatusCode::INTERNAL_SERVER_ERROR, + ErrorType::RateLimitError => StatusCode::TOO_MANY_REQUESTS, + } + } +} + +impl ErrorResponse { + pub fn to_response(&self) -> Response { + let status_code = self.error_type.status_code(); + let body = Body::from(self.error_description.clone()); + Response::builder() + .status(status_code) + .body(body) + .unwrap() + } +} + +async fn body_to_bytes(body: Body) -> AnyhowResult> { + // Also: let body_bytes = hyper::body::to_bytes(body) + let bytes = body.try_fold(Vec::new(), |mut data, chunk| async move { + data.extend_from_slice(&chunk); + Ok(data) + }).await?; + + Ok(bytes) +} diff --git a/src/main.rs b/src/main.rs index f733e4e24f..970061bf68 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,32 +1,33 @@ +#![feature(toowned_clone_into)] + #[macro_use] extern crate anyhow; #[macro_use] extern crate log; #[macro_use] extern crate serde_derive; +pub mod api; pub mod newrelic_logger; pub mod speak_proxy; use anyhow::Result as AnyhowResult; use crate::newrelic_logger::{NewRelicLogger, MaybeNewRelicTransaction}; -use crate::speak_proxy::{speak_proxy, speak_proxy_with_retry}; +use crate::speak_proxy::speak_proxy_with_retry; use futures::future::{self, Future}; -use hyper::Method; use hyper::header::HeaderValue; use hyper::http::header::HeaderName; -use hyper::rt::Stream; use hyper::server::conn::AddrStream; use hyper::service::{service_fn, make_service_fn}; use hyper::{Body, Request, Response, Server}; +use hyper::{Method, StatusCode}; use rand::seq::IteratorRandom; use std::collections::HashMap; +use std::convert::Infallible; use std::env; use std::fmt::Debug; use std::fmt::Display; -use std::net::SocketAddr; +use std::net::{SocketAddr, IpAddr}; use std::str::FromStr; use std::sync::Arc; -type BoxFut = Box, Error=hyper::Error> + Send>; - const ENV_DEFAULT_ROUTE : &'static str = "DEFAULT_ROUTE"; const ENV_NEWRELIC_API_KEY : &'static str = "NEWRELIC_API_KEY"; const ENV_PROXY_CONFIG_FILE : &'static str = "PROXY_CONFIG_FILE"; @@ -88,17 +89,6 @@ fn get_env_num(env_name: &str, default: T) -> AnyhowResult } } -fn _debug_request(req: Request) -> BoxFut { - let body_str = format!("{:?}", req); - let response = Response::new(Body::from(body_str)); - Box::new(future::ok(response)) -} - -fn health_check_response(_req: Request) -> BoxFut { - let response = Response::new(Body::from("healthy")); - Box::new(future::ok(response)) -} - pub struct Router { pub routes: HashMap, pub default_route: String, @@ -120,7 +110,66 @@ impl Router { } } -fn main() -> AnyhowResult<()> { +fn debug_request(req: Request) -> Result, Infallible> { + let body_str = format!("{:?}", req); + Ok(Response::new(Body::from(body_str))) +} + +fn health_check_response(_req: Request) -> Result, Infallible> { + Ok(Response::new(Body::from("healthy"))) +} + +pub async fn handle( + client_ip: IpAddr, + req: Request, + router: Arc, + newrelic_logger: Arc) -> Result, Infallible> +{ + match req.uri().path() { + "/speak" => { + let nr_transaction = newrelic_logger.web_transaction("/speak"); + let result = speak_proxy_with_retry(client_ip, req, router, nr_transaction, "/speak").await; + match result { + Ok(response) => Ok(response), + Err(error) => Ok(error.to_response()), + } + }, + "/speak_spectrogram" => { + let nr_transaction = newrelic_logger.web_transaction("/speak_spectrogram"); + let result = speak_proxy_with_retry(client_ip, req, router, nr_transaction, "/speak_spectrogram").await; + match result { + Ok(response) => Ok(response), + Err(error) => Ok(error.to_response()), + } + }, + "/proxy_health" => { + let _droppable_transaction = newrelic_logger.web_transaction("/proxy_health"); + health_check_response(req) + }, + _ => { + let _droppable_transaction = newrelic_logger + .web_transaction("unmatched endpoint"); + let forward = router.get_random_host(); + + info!("Forwarding to `{}` random host: {}", &forward, req.uri()); + + match hyper_reverse_proxy::call(client_ip, &forward, req).await { + Ok(response) => { + Ok(response) + }, + Err(_) => { + Ok(Response::builder() + .status(StatusCode::INTERNAL_SERVER_ERROR) + .body(Body::from("unknown proxy error")) + .unwrap()) + } + } + }, + } +} + +#[tokio::main] +async fn main() { if env::var(ENV_RUST_LOG) .as_ref() .ok() @@ -134,7 +183,8 @@ fn main() -> AnyhowResult<()> { env_logger::init(); let default_route = get_env_string(ENV_DEFAULT_ROUTE, DEFAULT_DEFAULT_ROUTE); - let service_port = get_env_num::(ENV_SERVICE_PORT, DEFAULT_SERVICE_PORT)?; + let service_port = get_env_num::(ENV_SERVICE_PORT, DEFAULT_SERVICE_PORT) + .expect("should have env var"); let newrelic_api_key = get_env_string(ENV_NEWRELIC_API_KEY, DEFAULT_NEWRELIC_API_KEY); info!("Default route: {}", default_route); @@ -142,7 +192,8 @@ fn main() -> AnyhowResult<()> { let proxy_configs_file = get_env_string(ENV_PROXY_CONFIG_FILE, DEFAULT_PROXY_CONFIG_FILE); info!("Proxy config file: {}", proxy_configs_file); - let proxy_configs = ProxyConfigs::load_from_file(&proxy_configs_file)?; + let proxy_configs = ProxyConfigs::load_from_file(&proxy_configs_file) + .expect("should have configs"); info!("Proxy configs: {:?}", proxy_configs); let mut route_map = HashMap::new(); @@ -165,49 +216,30 @@ fn main() -> AnyhowResult<()> { let newrelic_logger = Arc::new(newrelic_logger); - let make_service = make_service_fn(move |socket: &AddrStream| { - let remote_addr = socket.remote_addr(); - info!("Got a request from: {:?}", remote_addr); - + let make_service = make_service_fn(move |conn: &AddrStream| { + let remote_addr = conn.remote_addr().ip(); let router2 = router.clone(); let newrelic_logger2 = newrelic_logger.clone(); - service_fn(move |req: Request| { + async move { let router3 = router2.clone(); let newrelic_logger3 = newrelic_logger2.clone(); - match (req.method(), req.uri().path()) { - (&Method::POST, "/speak") => { - let nr_transaction = newrelic_logger3.web_transaction("/speak"); - speak_proxy_with_retry(req, remote_addr.clone(), router3, nr_transaction, "/speak") - }, - (&Method::POST, "/speak_spectrogram") => { - let nr_transaction = newrelic_logger3.web_transaction("/speak_spectrogram"); - speak_proxy(req, remote_addr.clone(), router3, nr_transaction, "/speak_spectrogram") - }, - (&Method::GET, "/proxy_health") => { - let _droppable_transaction = newrelic_logger3.web_transaction("/proxy_health"); - health_check_response(req) - }, - _ => { - let _droppable_transaction = newrelic_logger3.web_transaction("unmatched endpoint"); - let forward = router3.get_random_host(); - info!("Forwarding to `{}` random host: {}", &forward, req.uri()); - hyper_reverse_proxy::call(remote_addr.ip(), &forward, req) - } - } - }) + Ok::<_, Infallible>(service_fn(move |req| { + let router4 = router3.clone(); + let newrelic_logger4 = newrelic_logger3.clone(); + handle(remote_addr, req, router4, newrelic_logger4) + })) + } }); let addr = ([0, 0, 0, 0], service_port).into(); info!("Running server on {:?}", addr); - let server = Server::bind(&addr) - .serve(make_service) - .map_err(|e| eprintln!("server error: {}", e)); + let server = Server::bind(&addr).serve(make_service); - hyper::rt::run(server); - - Ok(()) + if let Err(e) = server.await { + eprintln!("server error: {}", e); + } } diff --git a/src/speak_proxy.rs b/src/speak_proxy.rs index fbe199ff81..75fa09a804 100644 --- a/src/speak_proxy.rs +++ b/src/speak_proxy.rs @@ -1,72 +1,45 @@ use anyhow::Result as AnyhowResult; -use crate::{BoxFut, Router}; +use crate::Router; +use crate::api::{ExternalSpeakRequest, ExternalHttpRequest, InternalSpeakRequest, ErrorResponse, ErrorType}; use crate::newrelic_logger::{NewRelicLogger, MaybeNewRelicTransaction}; +use futures::TryStreamExt; use futures::future::{self, Future}; -use hyper::Method; use hyper::header::HeaderValue; use hyper::http::header::HeaderName; -use hyper::rt::Stream; use hyper::server::conn::AddrStream; use hyper::service::{service_fn, make_service_fn}; -use hyper::{Body, Request, Response, Server}; +use hyper::{Body, Request, Response, Server, HeaderMap}; +use hyper::{Method, StatusCode}; +use hyper_reverse_proxy::ProxyError; use rand::seq::IteratorRandom; use std::collections::HashMap; -use std::env; +use std::convert::Infallible; use std::fmt::Debug; use std::fmt::Display; -use std::net::SocketAddr; +use std::net::{SocketAddr, IpAddr}; use std::str::FromStr; use std::sync::Arc; -use futures::IntoFuture; +use std::time::Duration; +use std::{env, thread}; -/// NB: This much match the shape of SpeakRequest in the 'voder/tts_service' code. -/// This is used for both /speak and /speak_spectrogram requests. -#[derive(Serialize, Deserialize, Debug)] -pub struct SpeakRequest { - /// Slug for the speaker - speaker: String, - /// Raw text to be spoken - text: String, -} - -struct RequestDetails { - pub request_bytes: Vec, - pub speaker: String, -} - -pub fn speak_proxy_with_retry( - req: Request, - remote_addr: SocketAddr, +pub async fn speak_proxy_with_retry( + ip_addr: IpAddr, + request: Request, router: Arc, - transaction: MaybeNewRelicTransaction, - endpoint: &'static str) -> BoxFut + _transaction: MaybeNewRelicTransaction, + endpoint: &'static str) -> Result, ErrorResponse> { - speak_proxy(req, remote_addr, router, transaction, endpoint) - .then(|result| { - match result { - Ok(t) => future::ok(t), - Err(t) => future::err(t), - } - }) - .boxed() -} + let mut attempt : u8 = 0; -pub fn speak_proxy( - req: Request, - remote_addr: SocketAddr, - router: Arc, - transaction: MaybeNewRelicTransaction, - endpoint: &'static str) -> BoxFut -{ - let mut headers = req.headers().clone(); + let external_http_request = ExternalHttpRequest::decode_request(request).await.unwrap(); // This is the IP of the Digital Ocean load balancer. - let gateway_ip = HeaderValue::from_str(&remote_addr.ip().to_string()) + let gateway_ip = HeaderValue::from_str(&ip_addr.to_string()) .ok() .unwrap_or(HeaderValue::from_static("")); // This is the IP of the upstream client browser. - let upstream_client_ip = req.headers().get(HeaderName::from_static("x-forwarded-for")) + let upstream_client_ip = external_http_request.headers.get(HeaderName::from_static("x-forwarded-for")) .map(|ip| ip.to_str().unwrap_or("")) .unwrap_or(""); @@ -74,42 +47,89 @@ pub fn speak_proxy( .ok() .unwrap_or(HeaderValue::from_static("")); - headers.insert(HeaderName::from_static("forwarded"), upstream_client_ip.clone()); - headers.insert(HeaderName::from_static("x-forwarded-for"), upstream_client_ip.clone()); + let mut headers_to_proxy = external_http_request.headers.clone(); + + headers_to_proxy.insert(HeaderName::from_static("forwarded"), upstream_client_ip.clone()); + headers_to_proxy.insert(HeaderName::from_static("x-forwarded-for"), upstream_client_ip.clone()); + // Unfortunately it looks like this middleware trounces the standard headers, so // here we put it somewhere it won't be overwritten. - headers.insert(HeaderName::from_static("x-voder-proxy-for"), upstream_client_ip.clone()); - headers.insert(HeaderName::from_static("x-gateway-ip"), gateway_ip); + headers_to_proxy.insert(HeaderName::from_static("x-voder-proxy-for"), upstream_client_ip.clone()); + headers_to_proxy.insert(HeaderName::from_static("x-gateway-ip"), gateway_ip); - Box::new(req.into_body().concat2().map(move |b| { // Builds a BoxedFut to return - let request_bytes = b.as_ref(); + // We're going to be changing the request body length, so remove the reported value + // so Hyper doesn't choke on any mismatch. + headers_to_proxy.remove(HeaderName::from_static("content-length")); - let request = serde_json::from_slice::(request_bytes) - .unwrap(); + // The proxy host is a Kubernetes load balancer. We're not doing the round robin magic here, + // so it makes sense to reuse the same value for each retry. + let load_balancer_hostname = router.get_speaker_host(&external_http_request.speak_request.speaker); - RequestDetails { - request_bytes: request_bytes.to_vec(), - speaker: request.speaker, - } - }).and_then(move |request_details: RequestDetails| { - let proxy_host = router.get_speaker_host(&request_details.speaker); + loop { + let internal_speak_request = InternalSpeakRequest { + speaker: external_http_request.speak_request.speaker.clone(), + text: external_http_request.speak_request.text.clone(), + retry_attempt_number: attempt, + skip_rate_limiter: false, + }; - info!("Routing {} for {} to {}", endpoint, &request_details.speaker, &proxy_host); + let result = speak_proxy( + ip_addr, + internal_speak_request, + headers_to_proxy.clone(), + &load_balancer_hostname, + endpoint).await; - transaction.add_attribute("speaker", &request_details.speaker); - - let mut request_builder = Request::builder(); - request_builder.method(Method::POST) - .uri(endpoint); - - for (k, v) in headers.iter() { - request_builder.header(k, v); + match result { + Ok(good_response) => return Ok(good_response), + Err(_) => {}, } - let new_req = request_builder - .body(Body::from(request_details.request_bytes)) - .unwrap(); + attempt += 1; - hyper_reverse_proxy::call(remote_addr.ip(), &proxy_host, new_req) - })) + thread::sleep(Duration::from_millis(400)); // TODO: Needs backoff + jitter + + if attempt > 2 { + return Err(ErrorResponse { + error_type: ErrorType::ProxyError, + error_description: "max internal retry attempts reached".to_string(), + }); + } + } +} + +#[derive(Serialize, Debug)] +pub struct ResponseError { + pub reason: String, +} + +async fn speak_proxy( + ip_addr: IpAddr, + internal_speak_request: InternalSpeakRequest, + headers: HeaderMap, + load_balancer_hostname: &str, + endpoint: &'static str) -> Result, ()> +{ + info!("Routing {} for {} to {} (attempt # {})", endpoint, &internal_speak_request.speaker, + &load_balancer_hostname, internal_speak_request.retry_attempt_number); + + let mut request_builder = Request::builder() + .method(Method::POST) + .uri(endpoint); + + for (k, v) in headers.iter() { + // NB: Handling move because of builder class + request_builder = request_builder.header(k, v); + } + + let json_speak_request = serde_json::to_string(&internal_speak_request).unwrap(); + + let request = request_builder + .body(Body::from(json_speak_request)) + .unwrap(); + + match hyper_reverse_proxy::call(ip_addr, &load_balancer_hostname, request).await { + Ok(result) => Ok(result), + Err(_result) => Err(()), // TODO: Surface this? + } } From df55d55b8486afdd4ff4f5c6995dc37c3d9586c3 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Thu, 10 Sep 2020 04:30:11 -0400 Subject: [PATCH 43/48] configurable retry limit and duration --- src/main.rs | 53 +++++++++++++++++++++++++++++++++++++++++----- src/speak_proxy.rs | 6 ++++-- 2 files changed, 52 insertions(+), 7 deletions(-) diff --git a/src/main.rs b/src/main.rs index 970061bf68..ccd3b289fd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -29,14 +29,18 @@ use std::str::FromStr; use std::sync::Arc; const ENV_DEFAULT_ROUTE : &'static str = "DEFAULT_ROUTE"; +const ENV_MAX_RETRIES : &'static str = "MAX_RETRIES"; const ENV_NEWRELIC_API_KEY : &'static str = "NEWRELIC_API_KEY"; const ENV_PROXY_CONFIG_FILE : &'static str = "PROXY_CONFIG_FILE"; +const ENV_RETRY_WAIT_MS : &'static str = "RETRY_WAIT_MS"; const ENV_RUST_LOG : &'static str = "RUST_LOG"; const ENV_SERVICE_PORT : &'static str = "SERVICE_PORT"; const DEFAULT_DEFAULT_ROUTE : &'static str = "http://127.0.0.1:12345"; +const DEFAULT_MAX_RETRIES : u8 = 3; const DEFAULT_NEWRELIC_API_KEY : &'static str = ""; const DEFAULT_PROXY_CONFIG_FILE : &'static str = "proxy_configs.toml"; +const DEFAULT_RETRY_WAIT_MS : u64 = 400; const DEFAULT_RUST_LOG : &'static str = "tokio_reactor=warn,hyper=info,debug"; const DEFAULT_SERVICE_PORT : u16 = 5555; @@ -123,12 +127,20 @@ pub async fn handle( client_ip: IpAddr, req: Request, router: Arc, - newrelic_logger: Arc) -> Result, Infallible> + newrelic_logger: Arc, + server_params: ServerParams) -> Result, Infallible> { match req.uri().path() { "/speak" => { let nr_transaction = newrelic_logger.web_transaction("/speak"); - let result = speak_proxy_with_retry(client_ip, req, router, nr_transaction, "/speak").await; + let result = speak_proxy_with_retry( + client_ip, + req, + router, + nr_transaction, + server_params.max_retries, + server_params.retry_wait_ms, + "/speak").await; match result { Ok(response) => Ok(response), Err(error) => Ok(error.to_response()), @@ -136,7 +148,14 @@ pub async fn handle( }, "/speak_spectrogram" => { let nr_transaction = newrelic_logger.web_transaction("/speak_spectrogram"); - let result = speak_proxy_with_retry(client_ip, req, router, nr_transaction, "/speak_spectrogram").await; + let result = speak_proxy_with_retry( + client_ip, + req, + router, + nr_transaction, + server_params.max_retries, + server_params.retry_wait_ms, + "/speak_spectrogram").await; match result { Ok(response) => Ok(response), Err(error) => Ok(error.to_response()), @@ -168,6 +187,12 @@ pub async fn handle( } } +#[derive(Debug, Clone)] +pub struct ServerParams { + pub max_retries: u8, + pub retry_wait_ms: u64, +} + #[tokio::main] async fn main() { if env::var(ENV_RUST_LOG) @@ -184,7 +209,7 @@ async fn main() { let default_route = get_env_string(ENV_DEFAULT_ROUTE, DEFAULT_DEFAULT_ROUTE); let service_port = get_env_num::(ENV_SERVICE_PORT, DEFAULT_SERVICE_PORT) - .expect("should have env var"); + .expect("should have service port env var"); let newrelic_api_key = get_env_string(ENV_NEWRELIC_API_KEY, DEFAULT_NEWRELIC_API_KEY); info!("Default route: {}", default_route); @@ -192,10 +217,26 @@ async fn main() { let proxy_configs_file = get_env_string(ENV_PROXY_CONFIG_FILE, DEFAULT_PROXY_CONFIG_FILE); info!("Proxy config file: {}", proxy_configs_file); + let max_retries = get_env_num::(ENV_MAX_RETRIES, DEFAULT_MAX_RETRIES) + .expect("should have max retries env var"); + + info!("Max retries: {}", max_retries); + + let retry_wait_ms = get_env_num::(ENV_RETRY_WAIT_MS, DEFAULT_RETRY_WAIT_MS) + .expect("should have retry wait ms env var"); + + info!("Retry wait: {}", retry_wait_ms); + let proxy_configs = ProxyConfigs::load_from_file(&proxy_configs_file) .expect("should have configs"); + info!("Proxy configs: {:?}", proxy_configs); + let server_params = ServerParams { + max_retries, + retry_wait_ms, + }; + let mut route_map = HashMap::new(); for backend in proxy_configs.backends.iter() { @@ -220,15 +261,17 @@ async fn main() { let remote_addr = conn.remote_addr().ip(); let router2 = router.clone(); let newrelic_logger2 = newrelic_logger.clone(); + let server_params = server_params.clone(); async move { let router3 = router2.clone(); let newrelic_logger3 = newrelic_logger2.clone(); + let server_params = server_params.clone(); Ok::<_, Infallible>(service_fn(move |req| { let router4 = router3.clone(); let newrelic_logger4 = newrelic_logger3.clone(); - handle(remote_addr, req, router4, newrelic_logger4) + handle(remote_addr, req, router4, newrelic_logger4, server_params.clone()) })) } }); diff --git a/src/speak_proxy.rs b/src/speak_proxy.rs index 75fa09a804..2824591553 100644 --- a/src/speak_proxy.rs +++ b/src/speak_proxy.rs @@ -27,6 +27,8 @@ pub async fn speak_proxy_with_retry( request: Request, router: Arc, _transaction: MaybeNewRelicTransaction, + max_retries: u8, + retry_wait_ms: u64, endpoint: &'static str) -> Result, ErrorResponse> { let mut attempt : u8 = 0; @@ -87,9 +89,9 @@ pub async fn speak_proxy_with_retry( attempt += 1; - thread::sleep(Duration::from_millis(400)); // TODO: Needs backoff + jitter + thread::sleep(Duration::from_millis(retry_wait_ms)); // TODO: Needs backoff + jitter - if attempt > 2 { + if attempt >= max_retries { return Err(ErrorResponse { error_type: ErrorType::ProxyError, error_description: "max internal retry attempts reached".to_string(), From 897725508838099a3b63d414b37ca4d6a3c57314 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Thu, 10 Sep 2020 04:54:29 -0400 Subject: [PATCH 44/48] remove nightly-only build error source; remove unused imports --- src/api.rs | 1 - src/main.rs | 19 +++++++------------ src/speak_proxy.rs | 23 ++++++----------------- 3 files changed, 13 insertions(+), 30 deletions(-) diff --git a/src/api.rs b/src/api.rs index 38926a8548..3756ef5d52 100644 --- a/src/api.rs +++ b/src/api.rs @@ -2,7 +2,6 @@ use anyhow::Result as AnyhowResult; use futures::TryStreamExt; use hyper::http::HeaderValue; use hyper::{Body, Request, HeaderMap, Response, StatusCode}; -use hyper::body::Bytes; /// NB: This much match the shape of SpeakRequest in the 'voder/tts_service' code. /// This is used for both /speak and /speak_spectrogram requests. diff --git a/src/main.rs b/src/main.rs index ccd3b289fd..4e6b4de12d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,3 @@ -#![feature(toowned_clone_into)] - #[macro_use] extern crate anyhow; #[macro_use] extern crate log; #[macro_use] extern crate serde_derive; @@ -9,22 +7,19 @@ pub mod newrelic_logger; pub mod speak_proxy; use anyhow::Result as AnyhowResult; -use crate::newrelic_logger::{NewRelicLogger, MaybeNewRelicTransaction}; +use crate::newrelic_logger::NewRelicLogger; use crate::speak_proxy::speak_proxy_with_retry; -use futures::future::{self, Future}; -use hyper::header::HeaderValue; -use hyper::http::header::HeaderName; +use hyper::StatusCode; use hyper::server::conn::AddrStream; use hyper::service::{service_fn, make_service_fn}; use hyper::{Body, Request, Response, Server}; -use hyper::{Method, StatusCode}; use rand::seq::IteratorRandom; use std::collections::HashMap; use std::convert::Infallible; use std::env; use std::fmt::Debug; use std::fmt::Display; -use std::net::{SocketAddr, IpAddr}; +use std::net::IpAddr; use std::str::FromStr; use std::sync::Arc; @@ -114,10 +109,10 @@ impl Router { } } -fn debug_request(req: Request) -> Result, Infallible> { - let body_str = format!("{:?}", req); - Ok(Response::new(Body::from(body_str))) -} +// fn debug_request(req: Request) -> Result, Infallible> { +// let body_str = format!("{:?}", req); +// Ok(Response::new(Body::from(body_str))) +// } fn health_check_response(_req: Request) -> Result, Infallible> { Ok(Response::new(Body::from("healthy"))) diff --git a/src/speak_proxy.rs b/src/speak_proxy.rs index 2824591553..98a689d118 100644 --- a/src/speak_proxy.rs +++ b/src/speak_proxy.rs @@ -1,26 +1,15 @@ -use anyhow::Result as AnyhowResult; use crate::Router; -use crate::api::{ExternalSpeakRequest, ExternalHttpRequest, InternalSpeakRequest, ErrorResponse, ErrorType}; -use crate::newrelic_logger::{NewRelicLogger, MaybeNewRelicTransaction}; -use futures::TryStreamExt; -use futures::future::{self, Future}; +use crate::api::{ExternalHttpRequest, InternalSpeakRequest, ErrorResponse, ErrorType}; +use crate::newrelic_logger::{MaybeNewRelicTransaction}; +use hyper::Method; use hyper::header::HeaderValue; use hyper::http::header::HeaderName; -use hyper::server::conn::AddrStream; -use hyper::service::{service_fn, make_service_fn}; -use hyper::{Body, Request, Response, Server, HeaderMap}; -use hyper::{Method, StatusCode}; -use hyper_reverse_proxy::ProxyError; -use rand::seq::IteratorRandom; -use std::collections::HashMap; -use std::convert::Infallible; +use hyper::{Body, Request, Response, HeaderMap}; use std::fmt::Debug; -use std::fmt::Display; -use std::net::{SocketAddr, IpAddr}; -use std::str::FromStr; +use std::net::IpAddr; use std::sync::Arc; +use std::thread; use std::time::Duration; -use std::{env, thread}; pub async fn speak_proxy_with_retry( ip_addr: IpAddr, From 77ad6a7e08003b5e7d64d613cacfa3b38fce90d2 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Thu, 10 Sep 2020 05:21:03 -0400 Subject: [PATCH 45/48] Broke OPTION / CORS by not matching on method --- Cargo.toml | 4 ++-- src/main.rs | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 68f9542535..8d9aa862d9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "tts-service-proxy" edition = "2018" -version = "0.0.1" +version = "0.0.2" authors = [ "Brandon Thomas ", "Brandon Thomas ", @@ -25,4 +25,4 @@ serde = "1.0.104" serde_derive = "1.0.104" serde_json = "1.0.55" tokio = { version = "0.2", features = ["full"] } -toml = "0.5.6" \ No newline at end of file +toml = "0.5.6" diff --git a/src/main.rs b/src/main.rs index 4e6b4de12d..d3f0fd1a3b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,10 +9,10 @@ pub mod speak_proxy; use anyhow::Result as AnyhowResult; use crate::newrelic_logger::NewRelicLogger; use crate::speak_proxy::speak_proxy_with_retry; -use hyper::StatusCode; use hyper::server::conn::AddrStream; use hyper::service::{service_fn, make_service_fn}; use hyper::{Body, Request, Response, Server}; +use hyper::{StatusCode, Method}; use rand::seq::IteratorRandom; use std::collections::HashMap; use std::convert::Infallible; @@ -125,8 +125,8 @@ pub async fn handle( newrelic_logger: Arc, server_params: ServerParams) -> Result, Infallible> { - match req.uri().path() { - "/speak" => { + match (req.method(), req.uri().path()) { + (&Method::POST, "/speak") => { let nr_transaction = newrelic_logger.web_transaction("/speak"); let result = speak_proxy_with_retry( client_ip, @@ -141,7 +141,7 @@ pub async fn handle( Err(error) => Ok(error.to_response()), } }, - "/speak_spectrogram" => { + (&Method::POST, "/speak_spectrogram") => { let nr_transaction = newrelic_logger.web_transaction("/speak_spectrogram"); let result = speak_proxy_with_retry( client_ip, @@ -156,7 +156,7 @@ pub async fn handle( Err(error) => Ok(error.to_response()), } }, - "/proxy_health" => { + (&Method::GET, "/proxy_health") => { let _droppable_transaction = newrelic_logger.web_transaction("/proxy_health"); health_check_response(req) }, From f2b0ec1c466ec83731f9bd590021968bf3aaf2bd Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Sun, 13 Sep 2020 06:37:24 -0400 Subject: [PATCH 46/48] add proxy hostname to response (but this isn't elegant/safe) --- Cargo.lock | 20 +++++++++++++++++++- Cargo.toml | 3 ++- src/main.rs | 9 +++++++++ src/speak_proxy.rs | 25 ++++++++++++++++++++++++- 4 files changed, 54 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index dcd869e540..812794eecc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -225,6 +225,17 @@ dependencies = [ "libc", ] +[[package]] +name = "hostname" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c731c3e10504cc8ed35cfe2f1db4c9274c3d35fa486e3b31df46f068ef3e867" +dependencies = [ + "libc", + "match_cfg", + "winapi 0.3.9", +] + [[package]] name = "http" version = "0.2.1" @@ -350,6 +361,12 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "match_cfg" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ffbee8634e0d45d258acb448e7eaab3fce7a0a467395d4d9f228e3c1f01fb2e4" + [[package]] name = "memchr" version = "2.3.3" @@ -795,11 +812,12 @@ checksum = "e604eb7b43c06650e854be16a2a03155743d3752dd1c943f6829e26b7a36e382" [[package]] name = "tts-service-proxy" -version = "0.0.1" +version = "0.0.2" dependencies = [ "anyhow", "env_logger", "futures", + "hostname", "hyper", "hyper-reverse-proxy", "log", diff --git a/Cargo.toml b/Cargo.toml index 8d9aa862d9..4b1a26f644 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,6 +16,7 @@ path = "src/main.rs" anyhow = "1.0.31" env_logger = "0.7.1" futures = "0.3" +hostname = "0.3.1" hyper = "0.13" hyper-reverse-proxy = { git = "https://github.com/felipenoris/hyper-reverse-proxy.git", commit = "d109fcbd629c62eeb5c34bec1074d737e7b58529" } log = "0.4.8" @@ -25,4 +26,4 @@ serde = "1.0.104" serde_derive = "1.0.104" serde_json = "1.0.55" tokio = { version = "0.2", features = ["full"] } -toml = "0.5.6" +toml = "0.5.6" \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index d3f0fd1a3b..4423c43d8a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -135,6 +135,7 @@ pub async fn handle( nr_transaction, server_params.max_retries, server_params.retry_wait_ms, + &server_params.server_hostname, "/speak").await; match result { Ok(response) => Ok(response), @@ -150,6 +151,7 @@ pub async fn handle( nr_transaction, server_params.max_retries, server_params.retry_wait_ms, + &server_params.server_hostname, "/speak_spectrogram").await; match result { Ok(response) => Ok(response), @@ -186,6 +188,7 @@ pub async fn handle( pub struct ServerParams { pub max_retries: u8, pub retry_wait_ms: u64, + pub server_hostname: String, } #[tokio::main] @@ -227,9 +230,15 @@ async fn main() { info!("Proxy configs: {:?}", proxy_configs); + let server_hostname = hostname::get() + .ok() + .and_then(|h| h.into_string().ok()) + .unwrap_or("proxy-unknown".to_string()); + let server_params = ServerParams { max_retries, retry_wait_ms, + server_hostname, }; let mut route_map = HashMap::new(); diff --git a/src/speak_proxy.rs b/src/speak_proxy.rs index 98a689d118..a91c4ed579 100644 --- a/src/speak_proxy.rs +++ b/src/speak_proxy.rs @@ -18,6 +18,7 @@ pub async fn speak_proxy_with_retry( _transaction: MaybeNewRelicTransaction, max_retries: u8, retry_wait_ms: u64, + proxy_hostname: &str, endpoint: &'static str) -> Result, ErrorResponse> { let mut attempt : u8 = 0; @@ -72,7 +73,29 @@ pub async fn speak_proxy_with_retry( endpoint).await; match result { - Ok(good_response) => return Ok(good_response), + Ok(good_response) => { + // All we want to do is add our special header + let proxy_hostname_header = HeaderValue::from_str(proxy_hostname) + .ok() + .unwrap_or(HeaderValue::from_static("proxy-unknown")); + + let status = good_response.status(); + + let mut response_headers = good_response.headers().clone(); + response_headers.append(HeaderName::from_static("x-proxy-hostname"), proxy_hostname_header); + + let (_parts, body) = good_response.into_parts(); + + let mut response_builder = Response::builder() + .status(status); + + for header in response_headers { + response_builder = response_builder.header(header.0.unwrap(), header.1); + } + + return Ok(response_builder.body(body) + .unwrap()); + }, Err(_) => {}, } From fa05996e3466de60316318350ee876aff9fd5f21 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Sun, 13 Sep 2020 07:03:01 -0400 Subject: [PATCH 47/48] clean up response modification --- src/speak_proxy.rs | 60 ++++++++++++++++++++++++++++++---------------- 1 file changed, 40 insertions(+), 20 deletions(-) diff --git a/src/speak_proxy.rs b/src/speak_proxy.rs index a91c4ed579..ab716a7719 100644 --- a/src/speak_proxy.rs +++ b/src/speak_proxy.rs @@ -75,26 +75,7 @@ pub async fn speak_proxy_with_retry( match result { Ok(good_response) => { // All we want to do is add our special header - let proxy_hostname_header = HeaderValue::from_str(proxy_hostname) - .ok() - .unwrap_or(HeaderValue::from_static("proxy-unknown")); - - let status = good_response.status(); - - let mut response_headers = good_response.headers().clone(); - response_headers.append(HeaderName::from_static("x-proxy-hostname"), proxy_hostname_header); - - let (_parts, body) = good_response.into_parts(); - - let mut response_builder = Response::builder() - .status(status); - - for header in response_headers { - response_builder = response_builder.header(header.0.unwrap(), header.1); - } - - return Ok(response_builder.body(body) - .unwrap()); + return modify_response(good_response, proxy_hostname, attempt); }, Err(_) => {}, } @@ -147,3 +128,42 @@ async fn speak_proxy( Err(_result) => Err(()), // TODO: Surface this? } } + +fn modify_response(response: Response, proxy_hostname: &str, retry_count: u8) + -> Result, ErrorResponse> +{ + let status = response.status(); + + let response_headers = { + let mut response_headers = response.headers().clone(); + + let proxy_hostname = HeaderValue::from_str(proxy_hostname) + .ok() + .unwrap_or(HeaderValue::from_static("proxy-unknown")); + + let retry_count = HeaderValue::from_str(&retry_count.to_string()) + .ok() + .unwrap_or(HeaderValue::from_static("unknown")); + + response_headers.append(HeaderName::from_static("x-proxy-hostname"), proxy_hostname); + response_headers.append(HeaderName::from_static("x-retry-count"), retry_count); + response_headers + }; + + let (_parts, body) = response.into_parts(); + + let mut response_builder = Response::builder() + .status(status); + + for header in response_headers { + if let Some(header_name) = header.0.as_ref() { + response_builder = response_builder.header(header_name, header.1); + } + } + + response_builder.body(body) + .map_err(|_| ErrorResponse { + error_type: ErrorType::ProxyError, + error_description: "could not modify response".to_string() + }) +} \ No newline at end of file From e4ee56e0dc3263005755604a3d871e83fb889147 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Sun, 13 Sep 2020 07:45:29 -0400 Subject: [PATCH 48/48] don't build a new response, modify the old one --- src/speak_proxy.rs | 46 +++++++++++++--------------------------------- 1 file changed, 13 insertions(+), 33 deletions(-) diff --git a/src/speak_proxy.rs b/src/speak_proxy.rs index ab716a7719..4cb0ddddfa 100644 --- a/src/speak_proxy.rs +++ b/src/speak_proxy.rs @@ -75,7 +75,7 @@ pub async fn speak_proxy_with_retry( match result { Ok(good_response) => { // All we want to do is add our special header - return modify_response(good_response, proxy_hostname, attempt); + return Ok(modify_response(good_response, proxy_hostname, attempt)); }, Err(_) => {}, } @@ -129,41 +129,21 @@ async fn speak_proxy( } } -fn modify_response(response: Response, proxy_hostname: &str, retry_count: u8) - -> Result, ErrorResponse> +fn modify_response(mut response: Response, proxy_hostname: &str, retry_count: u8) + -> Response { - let status = response.status(); + let headers = response.headers_mut(); - let response_headers = { - let mut response_headers = response.headers().clone(); + let proxy_hostname = HeaderValue::from_str(proxy_hostname) + .ok() + .unwrap_or(HeaderValue::from_static("proxy-unknown")); - let proxy_hostname = HeaderValue::from_str(proxy_hostname) - .ok() - .unwrap_or(HeaderValue::from_static("proxy-unknown")); + let retry_count = HeaderValue::from_str(&retry_count.to_string()) + .ok() + .unwrap_or(HeaderValue::from_static("unknown")); - let retry_count = HeaderValue::from_str(&retry_count.to_string()) - .ok() - .unwrap_or(HeaderValue::from_static("unknown")); + headers.append(HeaderName::from_static("x-proxy-hostname"), proxy_hostname); + headers.append(HeaderName::from_static("x-retry-count"), retry_count); - response_headers.append(HeaderName::from_static("x-proxy-hostname"), proxy_hostname); - response_headers.append(HeaderName::from_static("x-retry-count"), retry_count); - response_headers - }; - - let (_parts, body) = response.into_parts(); - - let mut response_builder = Response::builder() - .status(status); - - for header in response_headers { - if let Some(header_name) = header.0.as_ref() { - response_builder = response_builder.header(header_name, header.1); - } - } - - response_builder.body(body) - .map_err(|_| ErrorResponse { - error_type: ErrorType::ProxyError, - error_description: "could not modify response".to_string() - }) + response } \ No newline at end of file