From 5524fd50118f3a2cfcac84b3a34cb194c4d1e359 Mon Sep 17 00:00:00 2001 From: Ara Sadoyan Date: Thu, 20 Mar 2025 18:33:21 +0100 Subject: [PATCH] temp remove command line arg parser --- Cargo.lock | 583 ++++++++++++----------------------------- Cargo.toml | 29 +- etc/upstreams.yaml | 57 ++-- src/utils/discovery.rs | 47 ++-- src/utils/parceyaml.rs | 63 +++-- src/web/proxyhttp.rs | 128 +++++---- src/web/start.rs | 59 +++-- src/web/webserver.rs | 24 +- start.sh | 2 +- 9 files changed, 430 insertions(+), 562 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d3e0a50..54d5bdc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -60,30 +60,6 @@ version = "0.2.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "683d7910e743518b0e34f1186f92494becacb047c7b6bf616c96772180fef923" -[[package]] -name = "android-tzdata" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0" - -[[package]] -name = "android_system_properties" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311" -dependencies = [ - "libc", -] - -[[package]] -name = "ansi_term" -version = "0.12.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2" -dependencies = [ - "winapi", -] - [[package]] name = "anstream" version = "0.6.18" @@ -165,18 +141,18 @@ checksum = "c7c24de15d275a1ecfd47a380fb4d5ec9bfe0933f309ed5e705b775596a3574d" dependencies = [ "proc-macro2", "quote", - "syn 2.0.96", + "syn 2.0.100", ] [[package]] name = "async-trait" -version = "0.1.85" +version = "0.1.88" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3f934833b4b7233644e5848f235df3f57ed8c80f1528a26c3dfa13d2147fa056" +checksum = "e539d3fca749fcee5236ab05e93a52867dd549cc157c8cb7f99595f3cedffdb5" dependencies = [ "proc-macro2", "quote", - "syn 2.0.96", + "syn 2.0.100", ] [[package]] @@ -185,36 +161,21 @@ version = "0.1.0" dependencies = [ "async-trait", "axum", - "bytes", - "clap 4.5.32", "dashmap", "env_logger", "futures", "log", - "mini-redis", "notify", "pingora", "pingora-core", "pingora-http", - "pingora-load-balancing", "pingora-proxy", - "rand", "reqwest", "serde", - "serde_json", "serde_yaml", "tokio", ] -[[package]] -name = "atoi" -version = "0.3.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5c897df197d57c25b37df9d8fa2f93ddbfeee9ebd2264350ac79c8ec4b795885" -dependencies = [ - "num-traits", -] - [[package]] name = "atomic-waker" version = "1.1.2" @@ -304,7 +265,7 @@ dependencies = [ "miniz_oxide", "object", "rustc-demangle", - "windows-targets", + "windows-targets 0.52.6", ] [[package]] @@ -405,25 +366,7 @@ version = "0.4.39" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7e36cc9d416881d2e24f9a963be5fb1cd90966419ac844274161d10488b3e825" dependencies = [ - "android-tzdata", - "iana-time-zone", "num-traits", - "windows-targets", -] - -[[package]] -name = "clap" -version = "2.34.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a0610544180c38b88101fecf2dd634b174a62eef6946f84dfc6a7127512b381c" -dependencies = [ - "ansi_term", - "atty", - "bitflags 1.3.2", - "strsim 0.8.0", - "textwrap 0.11.0", - "unicode-width", - "vec_map", ] [[package]] @@ -434,35 +377,13 @@ checksum = "4ea181bf566f71cb9a5d17a59e1871af638180a18fb0035c92ae62b705207123" dependencies = [ "atty", "bitflags 1.3.2", - "clap_derive 3.2.25", - "clap_lex 0.2.4", + "clap_derive", + "clap_lex", "indexmap 1.9.3", "once_cell", - "strsim 0.10.0", + "strsim", "termcolor", - "textwrap 0.16.1", -] - -[[package]] -name = "clap" -version = "4.5.32" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6088f3ae8c3608d19260cd7445411865a485688711b78b5be70d78cd96136f83" -dependencies = [ - "clap_builder", - "clap_derive 4.5.32", -] - -[[package]] -name = "clap_builder" -version = "4.5.32" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "22a7ef7f676155edfb82daa97f99441f3ebf4a58d5e32f295a56259f1b6facc8" -dependencies = [ - "anstream", - "anstyle", - "clap_lex 0.7.4", - "strsim 0.11.1", + "textwrap", ] [[package]] @@ -478,18 +399,6 @@ dependencies = [ "syn 1.0.109", ] -[[package]] -name = "clap_derive" -version = "4.5.32" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "09176aae279615badda0765c0c0b3f6ed53f4709118af73cf4655d85d1530cd7" -dependencies = [ - "heck 0.5.0", - "proc-macro2", - "quote", - "syn 2.0.96", -] - [[package]] name = "clap_lex" version = "0.2.4" @@ -499,12 +408,6 @@ dependencies = [ "os_str_bytes", ] -[[package]] -name = "clap_lex" -version = "0.7.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f46ad14479a25103f283c0f10005961cf086d8dc42205bb44c46ac563475dca6" - [[package]] name = "cmake" version = "0.1.52" @@ -638,7 +541,7 @@ checksum = "97369cbbc041bc366949bc74d34658d6cda5621039731c6310521892a3a20ae0" dependencies = [ "proc-macro2", "quote", - "syn 2.0.96", + "syn 2.0.100", ] [[package]] @@ -662,14 +565,14 @@ dependencies = [ [[package]] name = "env_logger" -version = "0.11.6" +version = "0.11.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dcaee3d8e3cfc3fd92428d477bc97fc29ec8716d180c0d74c643bb26166660e0" +checksum = "c3716d7a920fb4fac5d84e9d4bce8ceb321e9414b4409da61b07b75c1e3d0697" dependencies = [ "anstream", "anstyle", "env_filter", - "humantime", + "jiff", "log", ] @@ -819,7 +722,7 @@ checksum = "162ee34ebcb7c64a8abebc059ce0fee27c2262618d7b60ed8faf72fef13c3650" dependencies = [ "proc-macro2", "quote", - "syn 2.0.96", + "syn 2.0.100", ] [[package]] @@ -882,7 +785,7 @@ dependencies = [ "cfg-if", "libc", "wasi 0.13.3+wasi-0.2.2", - "windows-targets", + "windows-targets 0.52.6", ] [[package]] @@ -933,15 +836,6 @@ dependencies = [ "foldhash", ] -[[package]] -name = "heck" -version = "0.3.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6d621efb26863f0e9924c6ac577e8275e5e6b77455db64ffa6c65c904e9e132c" -dependencies = [ - "unicode-segmentation", -] - [[package]] name = "heck" version = "0.4.1" @@ -1026,12 +920,6 @@ version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" -[[package]] -name = "humantime" -version = "2.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" - [[package]] name = "hyper" version = "1.6.0" @@ -1105,29 +993,6 @@ dependencies = [ "tracing", ] -[[package]] -name = "iana-time-zone" -version = "0.1.61" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "235e081f3925a06703c2d0117ea8b91f042756fd6e7a6e5d901e8ca1a996b220" -dependencies = [ - "android_system_properties", - "core-foundation-sys", - "iana-time-zone-haiku", - "js-sys", - "wasm-bindgen", - "windows-core", -] - -[[package]] -name = "iana-time-zone-haiku" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f" -dependencies = [ - "cc", -] - [[package]] name = "icu_collections" version = "1.5.0" @@ -1243,7 +1108,7 @@ checksum = "1ec89e9337638ecdc08744df490b221a7399bf8d164eb52a665454e60e075ad6" dependencies = [ "proc-macro2", "quote", - "syn 2.0.96", + "syn 2.0.100", ] [[package]] @@ -1325,6 +1190,30 @@ version = "1.0.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d75a2a4b1b190afb6f5425f10f6a8f959d2ea0b9c2b1d79553551850539e4674" +[[package]] +name = "jiff" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d699bc6dfc879fb1bf9bdff0d4c56f0884fc6f0d0eb0fba397a6d00cd9a6b85e" +dependencies = [ + "jiff-static", + "log", + "portable-atomic", + "portable-atomic-util", + "serde", +] + +[[package]] +name = "jiff-static" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d16e75759ee0aa64c57a56acbf43916987b20c77373cb7e808979e02b93c9f9" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.100", +] + [[package]] name = "jobserver" version = "0.1.32" @@ -1427,9 +1316,9 @@ dependencies = [ [[package]] name = "log" -version = "0.4.25" +version = "0.4.26" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "04cbf5b083de1c7e0222a7a51dbfdba1cbe1c6ab0b15e29fff3f6c077fd9cd9f" +checksum = "30bde2b3dc3671ae49d8e2e9f044c7c005836e7a023ee57cffa25ab82764bb9e" [[package]] name = "lru" @@ -1446,15 +1335,6 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ffbee8634e0d45d258acb448e7eaab3fce7a0a467395d4d9f228e3c1f01fb2e4" -[[package]] -name = "matchers" -version = "0.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f099785f7595cc4b4553a174ce30dd7589ef93391ff414dbb67f62392b9e0ce1" -dependencies = [ - "regex-automata 0.1.10", -] - [[package]] name = "matchit" version = "0.8.4" @@ -1482,23 +1362,6 @@ version = "0.3.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a" -[[package]] -name = "mini-redis" -version = "0.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "528890a6b63c88c9bda173867128abe62c0f5050dd959f067aeea09e90439f33" -dependencies = [ - "async-stream", - "atoi", - "bytes", - "structopt", - "tokio", - "tokio-stream", - "tracing", - "tracing-futures", - "tracing-subscriber", -] - [[package]] name = "miniz_oxide" version = "0.8.3" @@ -1621,7 +1484,7 @@ checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c" dependencies = [ "proc-macro2", "quote", - "syn 2.0.96", + "syn 2.0.100", ] [[package]] @@ -1678,7 +1541,7 @@ dependencies = [ "libc", "redox_syscall", "smallvec", - "windows-targets", + "windows-targets 0.52.6", ] [[package]] @@ -1693,26 +1556,6 @@ version = "2.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e" -[[package]] -name = "pin-project" -version = "1.1.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e2ec53ad785f4d35dac0adea7f7dc6f1bb277ad84a680c7afefeae05d1f5916" -dependencies = [ - "pin-project-internal", -] - -[[package]] -name = "pin-project-internal" -version = "1.1.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d56a66c0c55993aa927429d0f8a0abfd74f084e4d9c192cffed01e418d83eefb" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.96", -] - [[package]] name = "pin-project-lite" version = "0.2.16" @@ -1785,7 +1628,7 @@ dependencies = [ "brotli", "bytes", "chrono", - "clap 3.2.25", + "clap", "daemonize", "flate2", "futures", @@ -1937,7 +1780,7 @@ checksum = "5031783d6743bd31e4de7d7c7a19e9eecf369174c3cbd8a57eb52bc6bf882d92" dependencies = [ "async-trait", "bytes", - "clap 3.2.25", + "clap", "futures", "h2", "http", @@ -1983,6 +1826,21 @@ version = "0.3.31" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "953ec861398dccce10c670dfeaf3ec4911ca479e9c02154b3a215178c5f566f2" +[[package]] +name = "portable-atomic" +version = "1.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "350e9b48cbc6b0e028b0473b114454c6316e57336ee184ceab6e53f72c178b3e" + +[[package]] +name = "portable-atomic-util" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d8a2f0d8d040d7848a709caf78912debcc3f33ee4b3cac47d73d1e1069e83507" +dependencies = [ + "portable-atomic", +] + [[package]] name = "ppv-lite86" version = "0.2.20" @@ -2102,17 +1960,8 @@ checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191" dependencies = [ "aho-corasick", "memchr", - "regex-automata 0.4.9", - "regex-syntax 0.8.5", -] - -[[package]] -name = "regex-automata" -version = "0.1.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132" -dependencies = [ - "regex-syntax 0.6.29", + "regex-automata", + "regex-syntax", ] [[package]] @@ -2123,15 +1972,9 @@ checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908" dependencies = [ "aho-corasick", "memchr", - "regex-syntax 0.8.5", + "regex-syntax", ] -[[package]] -name = "regex-syntax" -version = "0.6.29" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f162c6dd7b008981e4d40210aca20b4bd0f9b60ca9271061b07f78537722f2e1" - [[package]] name = "regex-syntax" version = "0.8.5" @@ -2140,9 +1983,9 @@ checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" [[package]] name = "reqwest" -version = "0.12.12" +version = "0.12.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "43e734407157c3c2034e0258f5e4473ddb361b1e85f95a66690d67264d7cd1da" +checksum = "d19c46a6fdd48bc4dab94b6103fccc55d34c67cc0ad04653aad4ea2a07cd7bbb" dependencies = [ "base64", "bytes", @@ -2374,22 +2217,22 @@ dependencies = [ [[package]] name = "serde" -version = "1.0.217" +version = "1.0.219" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "02fc4265df13d6fa1d00ecff087228cc0a2b5f3c0e87e258d8b94a156e984c70" +checksum = "5f0e2c6ed6606019b4e29e69dbaba95b11854410e5347d525002456dbbb786b6" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.217" +version = "1.0.219" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5a9bf7cf98d04a2b28aead066b7496853d4779c9cc183c440dbac457641e19a0" +checksum = "5b0276cf7f2c73365f7157c8123c21cd9a50fbbd844757af28ca1f5925fc2a00" dependencies = [ "proc-macro2", "quote", - "syn 2.0.96", + "syn 2.0.100", ] [[package]] @@ -2449,15 +2292,6 @@ dependencies = [ "rust_decimal", ] -[[package]] -name = "sharded-slab" -version = "0.1.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f40ca3c46823713e0d4209592e8d6e826aa57e928f09752619fc696c499637f6" -dependencies = [ - "lazy_static", -] - [[package]] name = "shlex" version = "1.3.0" @@ -2504,48 +2338,12 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3" -[[package]] -name = "strsim" -version = "0.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" - [[package]] name = "strsim" version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" -[[package]] -name = "strsim" -version = "0.11.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" - -[[package]] -name = "structopt" -version = "0.3.26" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0c6b5c64445ba8094a6ab0c3cd2ad323e07171012d9c98b0b15651daf1787a10" -dependencies = [ - "clap 2.34.0", - "lazy_static", - "structopt-derive", -] - -[[package]] -name = "structopt-derive" -version = "0.4.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dcb5ae327f9cc13b68763b5749770cb9e048a99bd9dfdfa58d0cf05d5f64afe0" -dependencies = [ - "heck 0.3.3", - "proc-macro-error", - "proc-macro2", - "quote", - "syn 1.0.109", -] - [[package]] name = "strum" version = "0.26.3" @@ -2565,7 +2363,7 @@ dependencies = [ "proc-macro2", "quote", "rustversion", - "syn 2.0.96", + "syn 2.0.100", ] [[package]] @@ -2587,9 +2385,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.96" +version = "2.0.100" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d5d0adab1ae378d7f53bdebc67a39f1f151407ef230f0ce2883572f5d8985c80" +checksum = "b09a44accad81e1ba1cd74a32461ba89dee89095ba17b32f5d03683b1b1fc2a0" dependencies = [ "proc-macro2", "quote", @@ -2613,7 +2411,7 @@ checksum = "c8af7666ab7b6390ab78131fb5b0fce11d6b7a6951602017c35fa82800708971" dependencies = [ "proc-macro2", "quote", - "syn 2.0.96", + "syn 2.0.100", ] [[package]] @@ -2660,15 +2458,6 @@ dependencies = [ "winapi-util", ] -[[package]] -name = "textwrap" -version = "0.11.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" -dependencies = [ - "unicode-width", -] - [[package]] name = "textwrap" version = "0.16.1" @@ -2692,7 +2481,7 @@ checksum = "4fee6c4efc90059e10f81e6d42c60a18f76588c3d74cb83a0b242a2b6c7504c1" dependencies = [ "proc-macro2", "quote", - "syn 2.0.96", + "syn 2.0.100", ] [[package]] @@ -2727,9 +2516,9 @@ dependencies = [ [[package]] name = "tokio" -version = "1.43.0" +version = "1.44.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3d61fa4ffa3de412bfea335c6ecff681de2b609ba3c77ef3e00e521813a9ed9e" +checksum = "f382da615b842244d4b8738c82ed1275e6c5dd90c459a30941cd07080b06c91a" dependencies = [ "backtrace", "bytes", @@ -2751,7 +2540,7 @@ checksum = "6e06d43f1345a3bcd39f6a56dbb7dcab2ba47e68e8ac134855e7e2bdbaf8cab8" dependencies = [ "proc-macro2", "quote", - "syn 2.0.96", + "syn 2.0.100", ] [[package]] @@ -2858,21 +2647,9 @@ checksum = "784e0ac535deb450455cbfa28a6f0df145ea1bb7ae51b821cf5e7927fdcfbdd0" dependencies = [ "log", "pin-project-lite", - "tracing-attributes", "tracing-core", ] -[[package]] -name = "tracing-attributes" -version = "0.1.28" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "395ae124c09f9e6918a2310af6038fba074bcf474ac352496d5910dd59a2226d" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.96", -] - [[package]] name = "tracing-core" version = "0.1.33" @@ -2880,60 +2657,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e672c95779cf947c5311f83787af4fa8fffd12fb27e4993211a84bdfd9610f9c" dependencies = [ "once_cell", - "valuable", -] - -[[package]] -name = "tracing-futures" -version = "0.2.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "97d095ae15e245a057c8e8451bab9b3ee1e1f68e9ba2b4fbc18d0ac5237835f2" -dependencies = [ - "pin-project", - "tracing", -] - -[[package]] -name = "tracing-log" -version = "0.1.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f751112709b4e791d8ce53e32c4ed2d353565a795ce84da2285393f41557bdf2" -dependencies = [ - "log", - "once_cell", - "tracing-core", -] - -[[package]] -name = "tracing-serde" -version = "0.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bc6b213177105856957181934e4920de57730fc69bf42c37ee5bb664d406d9e1" -dependencies = [ - "serde", - "tracing-core", -] - -[[package]] -name = "tracing-subscriber" -version = "0.2.25" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0e0d2eaa99c3c2e41547cfa109e910a68ea03823cccad4a0525dcbc9b01e8c71" -dependencies = [ - "ansi_term", - "chrono", - "lazy_static", - "matchers", - "regex", - "serde", - "serde_json", - "sharded-slab", - "smallvec", - "thread_local", - "tracing", - "tracing-core", - "tracing-log", - "tracing-serde", ] [[package]] @@ -2989,18 +2712,6 @@ version = "1.0.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "11cd88e12b17c6494200a9c1b683a04fcac9573ed74cd1b62aeb2727c5592243" -[[package]] -name = "unicode-segmentation" -version = "1.12.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f6ccf251212114b54433ec949fd6a7841275f9ada20dddd2f29e9ceea4501493" - -[[package]] -name = "unicode-width" -version = "0.1.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7dd6e30e90baa6f72411720665d41d89b9a3d039dc45b8faea1ddd07f617f6af" - [[package]] name = "untrusted" version = "0.9.0" @@ -3036,24 +2747,12 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" -[[package]] -name = "valuable" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ba73ea9cf16a25df0c8caa16c51acb937d5712a8429db78a3ee29d5dcacd3a65" - [[package]] name = "vcpkg" version = "0.2.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" -[[package]] -name = "vec_map" -version = "0.8.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" - [[package]] name = "version_check" version = "0.9.5" @@ -3116,7 +2815,7 @@ dependencies = [ "log", "proc-macro2", "quote", - "syn 2.0.96", + "syn 2.0.100", "wasm-bindgen-shared", ] @@ -3151,7 +2850,7 @@ checksum = "8ae87ea40c9f689fc23f209965b6fb8a99ad69aeeb0231408be24920604395de" dependencies = [ "proc-macro2", "quote", - "syn 2.0.96", + "syn 2.0.100", "wasm-bindgen-backend", "wasm-bindgen-shared", ] @@ -3207,42 +2906,38 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" [[package]] -name = "windows-core" -version = "0.52.0" +name = "windows-link" +version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "33ab640c8d7e35bf8ba19b884ba838ceb4fba93a4e8c65a9059d08afcfc683d9" -dependencies = [ - "windows-targets", -] +checksum = "76840935b766e1b0a05c0066835fb9ec80071d4c09a16f6bd5f7e655e3c14c38" [[package]] name = "windows-registry" -version = "0.2.0" +version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e400001bb720a623c1c69032f8e3e4cf09984deec740f007dd2b03ec864804b0" +checksum = "4286ad90ddb45071efd1a66dfa43eb02dd0dfbae1545ad6cc3c51cf34d7e8ba3" dependencies = [ "windows-result", "windows-strings", - "windows-targets", + "windows-targets 0.53.0", ] [[package]] name = "windows-result" -version = "0.2.0" +version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1d1043d8214f791817bab27572aaa8af63732e11bf84aa21a45a78d6c317ae0e" +checksum = "c64fd11a4fd95df68efcfee5f44a294fe71b8bc6a91993e2791938abcc712252" dependencies = [ - "windows-targets", + "windows-link", ] [[package]] name = "windows-strings" -version = "0.1.0" +version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4cd9b125c486025df0eabcb585e62173c6c9eddcec5d117d3b6e8c30e2ee4d10" +checksum = "87fa48cc5d406560701792be122a10132491cff9d0aeb23583cc2dcafc847319" dependencies = [ - "windows-result", - "windows-targets", + "windows-link", ] [[package]] @@ -3251,7 +2946,7 @@ version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" dependencies = [ - "windows-targets", + "windows-targets 0.52.6", ] [[package]] @@ -3260,7 +2955,7 @@ version = "0.59.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" dependencies = [ - "windows-targets", + "windows-targets 0.52.6", ] [[package]] @@ -3269,14 +2964,30 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" dependencies = [ - "windows_aarch64_gnullvm", - "windows_aarch64_msvc", - "windows_i686_gnu", - "windows_i686_gnullvm", - "windows_i686_msvc", - "windows_x86_64_gnu", - "windows_x86_64_gnullvm", - "windows_x86_64_msvc", + "windows_aarch64_gnullvm 0.52.6", + "windows_aarch64_msvc 0.52.6", + "windows_i686_gnu 0.52.6", + "windows_i686_gnullvm 0.52.6", + "windows_i686_msvc 0.52.6", + "windows_x86_64_gnu 0.52.6", + "windows_x86_64_gnullvm 0.52.6", + "windows_x86_64_msvc 0.52.6", +] + +[[package]] +name = "windows-targets" +version = "0.53.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1e4c7e8ceaaf9cb7d7507c974735728ab453b67ef8f18febdd7c11fe59dca8b" +dependencies = [ + "windows_aarch64_gnullvm 0.53.0", + "windows_aarch64_msvc 0.53.0", + "windows_i686_gnu 0.53.0", + "windows_i686_gnullvm 0.53.0", + "windows_i686_msvc 0.53.0", + "windows_x86_64_gnu 0.53.0", + "windows_x86_64_gnullvm 0.53.0", + "windows_x86_64_msvc 0.53.0", ] [[package]] @@ -3285,48 +2996,96 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.53.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "86b8d5f90ddd19cb4a147a5fa63ca848db3df085e25fee3cc10b39b6eebae764" + [[package]] name = "windows_aarch64_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" +[[package]] +name = "windows_aarch64_msvc" +version = "0.53.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c7651a1f62a11b8cbd5e0d42526e55f2c99886c77e007179efff86c2b137e66c" + [[package]] name = "windows_i686_gnu" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" +[[package]] +name = "windows_i686_gnu" +version = "0.53.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c1dc67659d35f387f5f6c479dc4e28f1d4bb90ddd1a5d3da2e5d97b42d6272c3" + [[package]] name = "windows_i686_gnullvm" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" +[[package]] +name = "windows_i686_gnullvm" +version = "0.53.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ce6ccbdedbf6d6354471319e781c0dfef054c81fbc7cf83f338a4296c0cae11" + [[package]] name = "windows_i686_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" +[[package]] +name = "windows_i686_msvc" +version = "0.53.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "581fee95406bb13382d2f65cd4a908ca7b1e4c2f1917f143ba16efe98a589b5d" + [[package]] name = "windows_x86_64_gnu" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" +[[package]] +name = "windows_x86_64_gnu" +version = "0.53.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2e55b5ac9ea33f2fc1716d1742db15574fd6fc8dadc51caab1c16a3d3b4190ba" + [[package]] name = "windows_x86_64_gnullvm" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.53.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0a6e035dd0599267ce1ee132e51c27dd29437f63325753051e71dd9e42406c57" + [[package]] name = "windows_x86_64_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" +[[package]] +name = "windows_x86_64_msvc" +version = "0.53.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "271414315aff87387382ec3d271b52d7ae78726f5d44ac98b4f4030c91880486" + [[package]] name = "wit-bindgen-rt" version = "0.33.0" @@ -3377,7 +3136,7 @@ checksum = "2380878cad4ac9aac1e2435f3eb4020e8374b5f13c296cb75b4620ff8e229154" dependencies = [ "proc-macro2", "quote", - "syn 2.0.96", + "syn 2.0.100", "synstructure", ] @@ -3399,7 +3158,7 @@ checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e" dependencies = [ "proc-macro2", "quote", - "syn 2.0.96", + "syn 2.0.100", ] [[package]] @@ -3419,7 +3178,7 @@ checksum = "595eed982f7d355beb85837f651fa22e90b3c044842dc7f2c2842c086f295808" dependencies = [ "proc-macro2", "quote", - "syn 2.0.96", + "syn 2.0.100", "synstructure", ] @@ -3448,7 +3207,7 @@ checksum = "6eafa6dfb17584ea3e2bd6e76e0cc15ad7af12b09abdd1ca55961bed9b1063c6" dependencies = [ "proc-macro2", "quote", - "syn 2.0.96", + "syn 2.0.100", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 44a0bf8..747b1d8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,25 +4,18 @@ version = "0.1.0" edition = "2021" [dependencies] -bytes = "1.9.0" -mini-redis = "0.4" -rand = "0.8.5" +tokio = { version = "1.44.1", features = ["full"] } +pingora = { version = "0.4.0", features = ["lb", "openssl"] } # openssl, rustls, boringssl +serde = { version = "1.0.219", features = ["derive"] } dashmap = "6.1.0" -tokio = { version = "1", features = ["full"] } -#pingora = { version = "0.4", features = ["full"] } -pingora = { version = "0.4.0", features = ["lb", "openssl"] } -pingora-core = { version = "0.4.0" } -pingora-proxy = { version = "0.4.0" } -pingora-load-balancing = { version = "0.4.0" } -pingora-http = { version = "0.4.0" } -async-trait = { version = "0.1.85" } -env_logger = { version = "0.11.6" } -log = "0.4.25" +pingora-core = "0.4.0" +pingora-proxy = "0.4.0" +pingora-http = "0.4.0" +async-trait = "0.1.88" +env_logger = "0.11.7" +log = "0.4.26" futures = "0.3.31" notify = "8.0.0" axum = "0.8.1" -serde = { version = "1.0.217", features = ["derive"] } -serde_json = "1.0.137" -reqwest = { version = "0.12" } -serde_yaml = "0.8.26" -clap = { version = "4.5.32", features = ["derive"] } +reqwest = "0.12.15" +serde_yaml = "0.8.26" \ No newline at end of file diff --git a/etc/upstreams.yaml b/etc/upstreams.yaml index 255082d..34a3148 100644 --- a/etc/upstreams.yaml +++ b/etc/upstreams.yaml @@ -1,49 +1,56 @@ upstreams: myip.netangels.net: paths: - "/draw": - protocol: "wsoc" - ssl: false - servers: - - "192.168.1.1:8000" - "/": - protocol: "http" - ssl: false - servers: - - "127.0.0.1:8000" - - "127.0.0.3:8000" - - "127.0.0.4:8000" - - "127.0.0.5:8000" - "/ping": - protocol: "http" + "/echo": + protocol: "X-Proxied-From:Fooooooooooooooo, X-DoSome-About:Yaaaaaaaaaaaaaaa" ssl: false servers: - "127.0.0.1:8000" - "127.0.0.2:8000" + - "127.0.0.3:8000" + - "127.0.0.4:8000" + "/ping": + protocol: "PLACEHOLDER" + ssl: false + servers: + - "127.0.0.1:8000" + - "127.0.0.2:8000" + "/draw": + protocol: "PLACEHOLDER" + ssl: false + servers: + - "192.168.1.1:8000" polo.netangels.net: paths: "/": - protocol: "http" + protocol: "PLACEHOLDER" ssl: false servers: - "192.168.1.10:8000" "/ws": - protocol: "wsoc" + protocol: "PLACEHOLDER" ssl: false servers: - "192.168.1.1:8000" glop.netangels.net: paths: "/": - protocol: "http" + protocol: "PLACEHOLDER" ssl: false servers: - "192.168.1.10:8000" - "192.168.1.10:8000" -# apt.netangels.net: -# paths: -# "/": -# protocol: "http" -# ssl: true -# servers: -# - "apt.netangels.net:443" \ No newline at end of file + apt.netangels.net: + paths: + "/": + protocol: "PLACEHOLDER" + ssl: true + servers: + - "apt.netangels.net:443" + 127.0.0.1: + paths: + "/camerastatus": + protocol: "PLACEHOLDER" + ssl: false + servers: + - "192.168.1.5:8080" \ No newline at end of file diff --git a/src/utils/discovery.rs b/src/utils/discovery.rs index 0646bd2..6cb32ff 100644 --- a/src/utils/discovery.rs +++ b/src/utils/discovery.rs @@ -2,21 +2,22 @@ use crate::utils::parceyaml::load_yaml_to_dashmap; use crate::utils::tools::*; use crate::web::webserver; use async_trait::async_trait; -use dashmap::DashMap; use futures::channel::mpsc::Sender; use futures::SinkExt; +use log::{error, info}; use notify::event::ModifyKind; use notify::{Config, Event, EventKind, RecommendedWatcher, RecursiveMode, Watcher}; -use std::fs; +use pingora::prelude::sleep; use std::path::Path; -use std::sync::atomic::AtomicUsize; use std::time::{Duration, Instant}; use tokio::task; pub struct FromFileProvider { pub path: String, } -pub struct APIUpstreamProvider; +pub struct APIUpstreamProvider { + pub address: String, +} #[async_trait] pub trait Discovery { @@ -26,7 +27,7 @@ pub trait Discovery { #[async_trait] impl Discovery for APIUpstreamProvider { async fn start(&self, toreturn: Sender) { - webserver::run_server(toreturn).await; + webserver::run_server(self.address.clone(), toreturn).await; } } @@ -37,16 +38,18 @@ impl Discovery for FromFileProvider { } } pub async fn watch_file(fp: String, mut toreturn: Sender) { + sleep(Duration::from_millis(50)).await; // For having nice logs :-) let file_path = fp.as_str(); let parent_dir = Path::new(file_path).parent().unwrap(); let (local_tx, mut local_rx) = tokio::sync::mpsc::channel::>(1); - println!("Watching for changes in {:?}", parent_dir); - let paths = fs::read_dir(parent_dir).unwrap(); - for path in paths { - println!(" {}", path.unwrap().path().display()) - } + info!("Watching for changes in {:?}", parent_dir); let snd = load_yaml_to_dashmap(file_path, "filepath"); - let _ = toreturn.send(snd).await.unwrap(); + match snd { + Some(snd) => { + toreturn.send(snd).await.unwrap(); + } + None => {} + } let _watcher_handle = task::spawn_blocking({ let parent_dir = parent_dir.to_path_buf(); // Move directory path into the closure @@ -72,29 +75,38 @@ pub async fn watch_file(fp: String, mut toreturn: Sender) { if e.paths[0].to_str().unwrap().ends_with("yaml") { if start.elapsed() > Duration::from_secs(2) { start = Instant::now(); - println!("Config File changed :=> {:?}", e); + info!("Config File changed :=> {:?}", e); let snd = load_yaml_to_dashmap(file_path, "filepath"); - let _ = toreturn.send(snd).await.unwrap(); + match snd { + Some(snd) => { + toreturn.send(snd).await.unwrap(); + } + None => {} + } } } } _ => (), }, - Err(e) => println!("Watch error: {:?}", e), + Err(e) => error!("Watch error: {:?}", e), } } } + +/* #[allow(dead_code)] pub fn build_upstreams(d: &str, kind: &str) -> UpstreamsDashMap { let upstreams: UpstreamsDashMap = DashMap::new(); let mut contents = d.to_string(); match kind { "filepath" => { - println!("Reading upstreams from {}", d); let _ = match fs::read_to_string(d) { - Ok(data) => contents = data, + Ok(data) => { + println!("Reading upstreams from {}", d); + contents = data + } Err(e) => { - eprintln!("Error reading file: {:?}", e); + error!("Reading upstreams file: {:?}", e); return upstreams; } }; @@ -146,3 +158,4 @@ pub fn build_upstreams(d: &str, kind: &str) -> UpstreamsDashMap { } upstreams } +*/ diff --git a/src/utils/parceyaml.rs b/src/utils/parceyaml.rs index f877af7..e84572b 100644 --- a/src/utils/parceyaml.rs +++ b/src/utils/parceyaml.rs @@ -1,6 +1,8 @@ use crate::utils::tools::*; use dashmap::DashMap; +use log::{error, info, warn}; use serde::{Deserialize, Serialize}; +use serde_yaml::Error; use std::collections::HashMap; use std::fs; use std::sync::atomic::AtomicUsize; @@ -22,40 +24,63 @@ struct PathConfig { servers: Vec, } -pub fn load_yaml_to_dashmap(d: &str, kind: &str) -> UpstreamsDashMap { +pub fn load_yaml_to_dashmap(d: &str, kind: &str) -> Option { let dashmap = UpstreamsDashMap::new(); let mut yaml_data = d.to_string(); match kind { "filepath" => { - println!("Reading upstreams from {}", d); let _ = match fs::read_to_string(d) { - Ok(data) => yaml_data = data, + Ok(data) => { + info!("Reading upstreams from {}", d); + yaml_data = data + } Err(e) => { - eprintln!("Error reading file: {:?}", e); - return dashmap; + error!("Reading: {}: {:?}", d, e.to_string()); + warn!("Running with empty upstreams list, chane it via API"); + return None; } }; } "content" => { - println!("Reading upstreams from API post body"); + info!("Reading upstreams from API post body"); } - _ => println!("*******************> nothing <*******************"), + _ => error!("Mismatched parameter, only filepath|content is allowed "), } - let parsed: Config = serde_yaml::from_str(&yaml_data).expect("Failed to parse YAML"); - for (hostname, host_config) in parsed.upstreams { - let path_map = DashMap::new(); - for (path, path_config) in host_config.paths { - let mut server_list = Vec::new(); - for server in path_config.servers { - if let Some((ip, port_str)) = server.split_once(':') { - if let Ok(port) = port_str.parse::() { - server_list.push((ip.to_string(), port, path_config.ssl, path_config.protocol.clone())); + + let p: Result = serde_yaml::from_str(&yaml_data); + match p { + Ok(parsed) => { + for (hostname, host_config) in parsed.upstreams { + let path_map = DashMap::new(); + for (path, path_config) in host_config.paths { + let mut server_list = Vec::new(); + for server in path_config.servers { + if let Some((ip, port_str)) = server.split_once(':') { + if let Ok(port) = port_str.parse::() { + server_list.push((ip.to_string(), port, path_config.ssl, path_config.protocol.clone())); + } + } } + path_map.insert(path, (server_list, AtomicUsize::new(0))); } + dashmap.insert(hostname, path_map); } - path_map.insert(path, (server_list, AtomicUsize::new(0))); + Some(dashmap) + } + Err(e) => { + error!("Failed to parse upstreams file: {}", e); + None } - dashmap.insert(hostname, path_map); } - dashmap +} + +pub fn parce_main_config(path: &str) -> DashMap { + info!("Parsing configuration"); + let data = fs::read_to_string(path).unwrap(); + let reply = DashMap::new(); + let cfg: HashMap = serde_yaml::from_str(&*data).expect("Failed to parse main config file"); + for (k, v) in cfg { + reply.insert(k.to_string(), v.to_string()); + } + reply } diff --git a/src/web/proxyhttp.rs b/src/web/proxyhttp.rs index 70ba378..7573a74 100644 --- a/src/web/proxyhttp.rs +++ b/src/web/proxyhttp.rs @@ -2,38 +2,59 @@ use crate::utils::discovery::{APIUpstreamProvider, Discovery, FromFileProvider}; use crate::utils::tools::*; use crate::utils::*; use async_trait::async_trait; +use dashmap::DashMap; use futures::channel::mpsc; use futures::StreamExt; -use log::{info, warn}; +use log::{debug, error, info, warn}; use pingora::prelude::*; use pingora_core::prelude::HttpPeer; use pingora_core::server::ShutdownWatch; use pingora_core::services::background::BackgroundService; use pingora_http::{RequestHeader, ResponseHeader}; use pingora_proxy::{ProxyHttp, Session}; +use std::ops::Deref; use std::sync::atomic::Ordering; use std::sync::Arc; pub struct LB { pub ump_upst: Arc, pub ump_full: Arc, + pub config: Arc>, + pub local: Arc<(String, u16)>, } #[async_trait] impl BackgroundService for LB { async fn start(&self, mut shutdown: ShutdownWatch) { - println!("Starting example background service"); - let (tux, mut rux) = mpsc::channel::(0); - let file_load2 = FromFileProvider { - path: "etc/upstreams.yaml".to_string(), - }; + info!("Starting background service"); + let (tx, mut rx) = mpsc::channel::(0); - let api_load = APIUpstreamProvider; + let from_file = self.config.get("upstreams_conf"); + match from_file { + Some(from_file) => { + let file_load = FromFileProvider { path: from_file.to_string() }; + let tx_file = tx.clone(); + let _ = tokio::spawn(async move { file_load.start(tx_file).await }); + } + None => { + error!("Can't read config file"); + } + } + + let config_address = self.config.get("config_address"); + match config_address { + Some(config_address) => { + let api_load = APIUpstreamProvider { + address: config_address.to_string(), + }; + let tx_api = tx.clone(); + let _ = tokio::spawn(async move { api_load.start(tx_api).await }); + } + None => { + error!("Can't read config file"); + } + } - let tux_file = tux.clone(); - let tux_api = tux.clone(); - let _ = tokio::spawn(async move { file_load2.start(tux_file).await }); - let _ = tokio::spawn(async move { api_load.start(tux_api).await }); let uu = self.ump_upst.clone(); let ff = self.ump_full.clone(); let _ = tokio::spawn(async move { healthcheck::hc2(uu, ff).await }); @@ -43,7 +64,7 @@ impl BackgroundService for LB { _ = shutdown.changed() => { break; } - val = rux.next() => { + val = rx.next() => { match val { Some(ss) => { let foo = compare_dashmaps(&*self.ump_full, &ss); @@ -67,26 +88,31 @@ pub trait GetHost { } #[async_trait] impl GetHost for LB { - async fn get_host(&self, peer: &str, path: &str, upgrade: bool) -> Option<(String, u16, bool, String)> { - let mut _proto = ""; - if upgrade { - _proto = "wsoc"; - } else { - _proto = "http" - } - let host_entry = self.ump_upst.get(peer).unwrap(); - let x = if let Some(entry) = host_entry.get(path) { - let (servers, index) = entry.value(); - if servers.is_empty() { - return None; + async fn get_host(&self, peer: &str, path: &str, _upgrade: bool) -> Option<(String, u16, bool, String)> { + let _proto = ""; + // if upgrade { + // _proto = "wsoc"; + // } else { + // _proto = "http" + // } + let host_entry = self.ump_upst.get(peer); + match host_entry { + Some(host_entry) => { + let upstream = if let Some(entry) = host_entry.get(path) { + let (servers, index) = entry.value(); + if servers.is_empty() { + return None; + } + let idx = index.fetch_add(1, Ordering::Relaxed) % servers.len(); + // info!("Host: {}, Path: {}, Peer: {:?}, len: {}, idx: {}", peer, path, servers[idx], servers.len(), idx); + Some(servers[idx].clone()) + } else { + None + }; + upstream } - let idx = index.fetch_add(1, Ordering::Relaxed) % servers.len(); - println!("{} {:?} => len: {}, idx: {}", peer, servers[idx], servers.len(), idx); - Some(servers[idx].clone()) - } else { - None - }; - x + None => None, + } } } @@ -95,31 +121,26 @@ impl ProxyHttp for LB { type CTX = (); fn new_ctx(&self) -> Self::CTX {} async fn upstream_peer(&self, session: &mut Session, _ctx: &mut Self::CTX) -> Result> { - // let before = Instant::now(); let host_name = session.req_header().headers.get("host"); match host_name { Some(host) => { let header_host = host.to_str().unwrap().split(':').collect::>(); - let ddr = self.get_host(header_host[0], session.req_header().uri.path(), session.is_upgrade_req()); match ddr.await { Some((host, port, ssl, _proto)) => { let peer = Box::new(HttpPeer::new((host, port), ssl, String::new())); - // info!("{:?}, Time => {:.2?}", session.request_summary(), before.elapsed()); Ok(peer) } None => { - warn!("Returning default list => {:?}, {:?}", host_name, session.req_header().uri); - let peer = Box::new(HttpPeer::new(("127.0.0.1", 3000), false, String::new())); - // info!("{:?}, Time => {:.2?}", session.request_summary(), before.elapsed()); + warn!("Upstream not found. Host: {:?}, Path: {}", host, session.req_header().uri); + let peer = Box::new(HttpPeer::new(self.local.deref(), false, String::new())); Ok(peer) } } } None => { - warn!("Returning default list => {:?}, {:?}", host_name, session.req_header().uri); - let peer = Box::new(HttpPeer::new(("127.0.0.1", 3000), false, String::new())); - // info!("{:?}, Time => {:.2?}", session.request_summary(), before.elapsed()); + warn!("Upstream not found. Host: {:?}, Path: {}", host_name, session.req_header().uri); + let peer = Box::new(HttpPeer::new(self.local.deref(), false, String::new())); Ok(peer) } } @@ -159,15 +180,32 @@ impl ProxyHttp for LB { } Ok(()) } - async fn response_filter(&self, _session: &mut Session, _upstream_response: &mut ResponseHeader, _ctx: &mut Self::CTX) -> pingora_core::Result<()> + + async fn response_filter(&self, _session: &mut Session, _upstream_response: &mut ResponseHeader, _ctx: &mut Self::CTX) -> Result<()> where Self::CTX: Send + Sync, { + // -------------------------------------------------------------------------------------------- // + // let header_host = _session.req_header().headers.get("host"); + // match header_host { + // Some(header_host) => { + // let data = self.ump_full.get(header_host.to_str().unwrap()).unwrap(); + // let detail = data.get(_session.req_header().uri.path()); + // let thelast = detail.unwrap().value().0[0].clone().3; + // println!(" Host ==> {}", thelast); + // } + // None => {} + // } + // _upstream_response.insert_header("X-DoSome-About", "Yaaaaaaaaaaaaaaa").unwrap(); + // -------------------------------------------------------------------------------------------- // + _upstream_response.insert_header("X-Proxied-From", "Fooooooooooooooo").unwrap(); + Ok(()) } - // async fn logging(&self, session: &mut Session, _e: Option<&pingora::Error>, ctx: &mut Self::CTX) { - // let response_code = session.response_written().map_or(0, |resp| resp.status.as_u16()); - // info!("{}, response code: {response_code}", self.request_summary(session, ctx)); - // } + + async fn logging(&self, session: &mut Session, _e: Option<&pingora::Error>, ctx: &mut Self::CTX) { + let response_code = session.response_written().map_or(0, |resp| resp.status.as_u16()); + debug!("{}, response code: {response_code}", self.request_summary(session, ctx)); + } } diff --git a/src/web/start.rs b/src/web/start.rs index 71dc367..1294407 100644 --- a/src/web/start.rs +++ b/src/web/start.rs @@ -1,47 +1,74 @@ use crate::utils::tools::*; use crate::web::proxyhttp::LB; -// use clap::{arg, Parser}; use dashmap::DashMap; use pingora_core::prelude::{background_service, Opt}; use pingora_core::server::Server; +use std::env; use std::sync::Arc; -// #[derive(Parser, Debug)] -// #[command(version, about, long_about = None)] -// struct Args { -// #[arg(short, long)] -// address: String, -// #[arg(short, long)] -// port: String, -// } - pub fn run() { - env_logger::init(); + let parameters = Some(Opt::parse_args()).unwrap(); + let file = parameters.conf.clone().unwrap(); + let maincfg = crate::utils::parceyaml::parce_main_config(file.as_str()); - // let mut server = Server::new(None).unwrap(); - let mut server = Server::new(Some(Opt::parse_args())).unwrap(); + let mut local_conf: (String, u16) = ("0.0.0.0".to_string(), 0); + if let Some((ip, port_str)) = maincfg.get("config_address").unwrap().split_once(':') { + if let Ok(port) = port_str.parse::() { + local_conf = (ip.to_string(), port); + } + } + + let mut server = Server::new(parameters).unwrap(); server.bootstrap(); let uf: UpstreamsDashMap = DashMap::new(); let ff: UpstreamsDashMap = DashMap::new(); let uf_config = Arc::new(uf); let ff_config = Arc::new(ff); + let cfg = Arc::new(maincfg); + let local = Arc::new(local_conf); let lb = LB { ump_upst: uf_config.clone(), ump_full: ff_config.clone(), + config: cfg.clone(), + local: local.clone(), }; let bg = LB { ump_upst: uf_config.clone(), ump_full: ff_config.clone(), + config: cfg.clone(), + local: local.clone(), }; + // env_logger::Env::new(); + // env_logger::init(); + + let log_level = cfg.get("log_level").unwrap(); + match log_level.as_str() { + "info" => env::set_var("RUST_LOG", "info"), + "error" => env::set_var("RUST_LOG", "error"), + "warn" => env::set_var("RUST_LOG", "warn"), + "debug" => env::set_var("RUST_LOG", "debug"), + "trace" => env::set_var("RUST_LOG", "trace"), + "off" => env::set_var("RUST_LOG", "off"), + _ => { + println!("Error reading log level, defaulting to: INFO"); + env::set_var("RUST_LOG", "info") + } + } + env_logger::builder() + // .format_timestamp(None) + // .format_module_path(false) + // .format_source_path(false) + // .format_target(false) + .init(); + let bg_srvc = background_service("bgsrvc", bg); let mut proxy = pingora_proxy::http_proxy_service(&server.configuration, lb); + let bindaddress = cfg.get("proxy_address_http").unwrap(); - // let args = Args::parse(); - // let addr = format!("{}:{}", args.address, args.port); - proxy.add_tcp("0.0.0.0:6193"); + proxy.add_tcp(bindaddress.as_str()); server.add_service(proxy); server.add_service(bg_srvc); diff --git a/src/web/webserver.rs b/src/web/webserver.rs index 7636872..a6f06c7 100644 --- a/src/web/webserver.rs +++ b/src/web/webserver.rs @@ -6,14 +6,11 @@ use axum::routing::{delete, get, head, post, put}; use axum::Router; use futures::channel::mpsc::Sender; use futures::SinkExt; +use log::info; use tokio::net::TcpListener; -// struct UpstreamData { -// servers: UpstreamsDashMap, -// } - #[allow(unused_mut)] -pub async fn run_server(mut toreturn: Sender) { +pub async fn run_server(bindaddress: String, mut toreturn: Sender) { let mut tr = toreturn.clone(); let app = Router::new() .route("/{*wildcard}", get(getconfig)) @@ -25,13 +22,22 @@ pub async fn run_server(mut toreturn: Sender) { "/conf", post(|up: String| async move { let serverlist = crate::utils::parceyaml::load_yaml_to_dashmap(up.as_str(), "content"); - let _ = tr.send(serverlist).await.unwrap(); - Response::builder().status(StatusCode::CREATED).body(Body::from("Config, conf file, updated!\n")).unwrap() + + match serverlist { + Some(serverlist) => { + let _ = tr.send(serverlist).await.unwrap(); + Response::builder().status(StatusCode::CREATED).body(Body::from("Config, conf file, updated!\n")).unwrap() + } + None => Response::builder() + .status(StatusCode::INTERNAL_SERVER_ERROR) + .body(Body::from("Failed to parce config file!\n")) + .unwrap(), + } }) .with_state("state"), ); - let listener = TcpListener::bind("0.0.0.0:3000").await.unwrap(); - println!("Axum API server running on port 3000"); + let listener = TcpListener::bind(bindaddress.clone()).await.unwrap(); + info!("Starting the API server on: {}", bindaddress); axum::serve(listener, app).await.unwrap(); } diff --git a/start.sh b/start.sh index 548a188..b677e9d 100755 --- a/start.sh +++ b/start.sh @@ -1,4 +1,4 @@ #!/bin/bash -export RUST_LOG=INFO +#export RUST_LOG=INFO #reflex -d none -r 'src/' -s -- sh -c 'reset && cargo run -- --address 0.0.0.0 --port 6193' reflex -d none -r 'src/' -s -- sh -c 'reset && cargo run -- -c etc/main.yaml'