From 8471392f25243f8e1a318851798dd14ba053c91b Mon Sep 17 00:00:00 2001 From: Abdul Munif Hanafi Date: Wed, 25 Dec 2024 12:33:24 +0700 Subject: [PATCH] Add http-client lib and split parse_hosts_json function --- Cargo.lock | 1100 +-------------------------------- Cargo.toml | 18 +- examples/parse_hosts_json.lua | 4 +- http-client/Cargo.toml | 8 + http-client/src/lib.rs | 609 ++++++++++++++++++ src/lib.rs | 24 +- src/util.rs | 135 ++-- 7 files changed, 750 insertions(+), 1148 deletions(-) create mode 100644 http-client/Cargo.toml create mode 100644 http-client/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index c2f65c1..5bcb40c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,21 +2,6 @@ # It is not intended for manual editing. version = 4 -[[package]] -name = "addr2line" -version = "0.24.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dfbe277e56a376000877090da837660b4427aad530e3028d44e0bffe4f89a1c1" -dependencies = [ - "gimli", -] - -[[package]] -name = "adler2" -version = "2.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "512761e0bb2578dd7380c6baaa0f4ce03e84f95e960231d1dec8bf4d7d6e2627" - [[package]] name = "aho-corasick" version = "1.1.3" @@ -81,33 +66,12 @@ version = "1.0.93" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4c95c10ba0b00a02636238b814946408b1322d5ac4760326e6fb8ec956d85775" -[[package]] -name = "atomic-waker" -version = "1.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0" - [[package]] name = "autocfg" version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" -[[package]] -name = "backtrace" -version = "0.3.74" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8d82cb332cdfaed17ae235a638438ac4d4839913cc2af585c3c6746e8f8bee1a" -dependencies = [ - "addr2line", - "cfg-if", - "libc", - "miniz_oxide", - "object", - "rustc-demangle", - "windows-targets", -] - [[package]] name = "base64" version = "0.22.1" @@ -136,24 +100,12 @@ dependencies = [ "serde", ] -[[package]] -name = "bumpalo" -version = "3.16.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c" - [[package]] name = "byteorder" version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" -[[package]] -name = "bytes" -version = "1.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "325918d6fe32f23b19878fe4b34794ae41fc19ddbe53b10571a4874d44ffd39b" - [[package]] name = "cc" version = "1.2.1" @@ -230,60 +182,18 @@ version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5b63caa9aa9397e2d9480a9b13673856c78d8ac123288526c37d7839f2a86990" -[[package]] -name = "core-foundation" -version = "0.9.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "91e195e091a93c46f7102ec7818a2aa394e1e1771c3ab4825963fa03e45afb8f" -dependencies = [ - "core-foundation-sys", - "libc", -] - -[[package]] -name = "core-foundation-sys" -version = "0.8.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" - -[[package]] -name = "displaydoc" -version = "0.2.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "97369cbbc041bc366949bc74d34658d6cda5621039731c6310521892a3a20ae0" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.89", -] - [[package]] name = "either" version = "1.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0" -[[package]] -name = "encoding_rs" -version = "0.8.35" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "75030f3c4f45dafd7586dd6780965a8c7e8e285a5ecb86713e63a79c5b2766f3" -dependencies = [ - "cfg-if", -] - [[package]] name = "endian-type" version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c34f04666d835ff5d62e058c3995147c06f42fe86ff053337632bca83e42702d" -[[package]] -name = "equivalent" -version = "1.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" - [[package]] name = "erased-serde" version = "0.4.5" @@ -327,12 +237,6 @@ dependencies = [ "windows-sys 0.52.0", ] -[[package]] -name = "fnv" -version = "1.0.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" - [[package]] name = "foreign-types" version = "0.3.2" @@ -348,65 +252,6 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b" -[[package]] -name = "form_urlencoded" -version = "1.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e13624c2627564efccf4934284bdd98cbaa14e79b0b5a141218e507b3a823456" -dependencies = [ - "percent-encoding", -] - -[[package]] -name = "futures-channel" -version = "0.3.31" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2dff15bf788c671c1934e366d07e30c1814a8ef514e1af724a602e8a2fbe1b10" -dependencies = [ - "futures-core", - "futures-sink", -] - -[[package]] -name = "futures-core" -version = "0.3.31" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "05f29059c0c2090612e8d742178b0580d2dc940c837851ad723096f87af6663e" - -[[package]] -name = "futures-io" -version = "0.3.31" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9e5c1b78ca4aae1ac06c48a526a655760685149f0d465d21f37abfe57ce075c6" - -[[package]] -name = "futures-sink" -version = "0.3.31" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e575fab7d1e0dcb8d0c7bcf9a63ee213816ab51902e6d244a95819acacf1d4f7" - -[[package]] -name = "futures-task" -version = "0.3.31" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f90f7dce0722e95104fcb095585910c0977252f286e354b5e3bd38902cd99988" - -[[package]] -name = "futures-util" -version = "0.3.31" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9fa08315bb612088cc391249efdc3bc77536f16c91f6cf495e6fbe85b20a4a81" -dependencies = [ - "futures-core", - "futures-io", - "futures-sink", - "futures-task", - "memchr", - "pin-project-lite", - "pin-utils", - "slab", -] - [[package]] name = "getrandom" version = "0.2.15" @@ -418,37 +263,6 @@ dependencies = [ "wasi", ] -[[package]] -name = "gimli" -version = "0.31.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "07e28edb80900c19c28f1072f2e8aeca7fa06b23cd4169cefe1af5aa3260783f" - -[[package]] -name = "h2" -version = "0.4.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ccae279728d634d083c00f6099cb58f01cc99c145b84b8be2f6c74618d79922e" -dependencies = [ - "atomic-waker", - "bytes", - "fnv", - "futures-core", - "futures-sink", - "http", - "indexmap", - "slab", - "tokio", - "tokio-util", - "tracing", -] - -[[package]] -name = "hashbrown" -version = "0.15.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bf151400ff0baff5465007dd2f3e717f3fe502074ca563069ce3a6629d07b289" - [[package]] name = "heck" version = "0.5.0" @@ -465,264 +279,11 @@ dependencies = [ ] [[package]] -name = "http" -version = "1.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f16ca2af56261c99fba8bac40a10251ce8188205a4c448fbb745a2e4daa76fea" -dependencies = [ - "bytes", - "fnv", - "itoa", -] - -[[package]] -name = "http-body" -version = "1.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1efedce1fb8e6913f23e0c92de8e62cd5b772a67e7b3946df930a62566c93184" -dependencies = [ - "bytes", - "http", -] - -[[package]] -name = "http-body-util" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "793429d76616a256bcb62c2a2ec2bed781c8307e797e2598c50010f2bee2544f" -dependencies = [ - "bytes", - "futures-util", - "http", - "http-body", - "pin-project-lite", -] - -[[package]] -name = "httparse" -version = "1.9.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7d71d3574edd2771538b901e6549113b4006ece66150fb69c0fb6d9a2adae946" - -[[package]] -name = "hyper" -version = "1.5.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "256fb8d4bd6413123cc9d91832d78325c48ff41677595be797d90f42969beae0" -dependencies = [ - "bytes", - "futures-channel", - "futures-util", - "h2", - "http", - "http-body", - "httparse", - "itoa", - "pin-project-lite", - "smallvec", - "tokio", - "want", -] - -[[package]] -name = "hyper-rustls" -version = "0.27.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f6884a48c6826ec44f524c7456b163cebe9e55a18d7b5e307cb4f100371cc767" -dependencies = [ - "futures-util", - "http", - "hyper", - "hyper-util", - "rustls", - "rustls-pki-types", - "tokio", - "tokio-rustls", - "tower-service", -] - -[[package]] -name = "hyper-tls" -version = "0.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "70206fc6890eaca9fde8a0bf71caa2ddfc9fe045ac9e5c70df101a7dbde866e0" -dependencies = [ - "bytes", - "http-body-util", - "hyper", - "hyper-util", - "native-tls", - "tokio", - "tokio-native-tls", - "tower-service", -] - -[[package]] -name = "hyper-util" -version = "0.1.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "df2dcfbe0677734ab2f3ffa7fa7bfd4706bfdc1ef393f2ee30184aed67e631b4" -dependencies = [ - "bytes", - "futures-channel", - "futures-util", - "http", - "http-body", - "hyper", - "pin-project-lite", - "socket2", - "tokio", - "tower-service", - "tracing", -] - -[[package]] -name = "icu_collections" -version = "1.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "db2fa452206ebee18c4b5c2274dbf1de17008e874b4dc4f0aea9d01ca79e4526" -dependencies = [ - "displaydoc", - "yoke", - "zerofrom", - "zerovec", -] - -[[package]] -name = "icu_locid" -version = "1.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "13acbb8371917fc971be86fc8057c41a64b521c184808a698c02acc242dbf637" -dependencies = [ - "displaydoc", - "litemap", - "tinystr", - "writeable", - "zerovec", -] - -[[package]] -name = "icu_locid_transform" -version = "1.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "01d11ac35de8e40fdeda00d9e1e9d92525f3f9d887cdd7aa81d727596788b54e" -dependencies = [ - "displaydoc", - "icu_locid", - "icu_locid_transform_data", - "icu_provider", - "tinystr", - "zerovec", -] - -[[package]] -name = "icu_locid_transform_data" -version = "1.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fdc8ff3388f852bede6b579ad4e978ab004f139284d7b28715f773507b946f6e" - -[[package]] -name = "icu_normalizer" -version = "1.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "19ce3e0da2ec68599d193c93d088142efd7f9c5d6fc9b803774855747dc6a84f" -dependencies = [ - "displaydoc", - "icu_collections", - "icu_normalizer_data", - "icu_properties", - "icu_provider", - "smallvec", - "utf16_iter", - "utf8_iter", - "write16", - "zerovec", -] - -[[package]] -name = "icu_normalizer_data" -version = "1.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f8cafbf7aa791e9b22bec55a167906f9e1215fd475cd22adfcf660e03e989516" - -[[package]] -name = "icu_properties" -version = "1.5.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "93d6020766cfc6302c15dbbc9c8778c37e62c14427cb7f6e601d849e092aeef5" -dependencies = [ - "displaydoc", - "icu_collections", - "icu_locid_transform", - "icu_properties_data", - "icu_provider", - "tinystr", - "zerovec", -] - -[[package]] -name = "icu_properties_data" -version = "1.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "67a8effbc3dd3e4ba1afa8ad918d5684b8868b3b26500753effea8d2eed19569" - -[[package]] -name = "icu_provider" -version = "1.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6ed421c8a8ef78d3e2dbc98a973be2f3770cb42b606e3ab18d6237c4dfde68d9" -dependencies = [ - "displaydoc", - "icu_locid", - "icu_provider_macros", - "stable_deref_trait", - "tinystr", - "writeable", - "yoke", - "zerofrom", - "zerovec", -] - -[[package]] -name = "icu_provider_macros" -version = "1.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1ec89e9337638ecdc08744df490b221a7399bf8d164eb52a665454e60e075ad6" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.89", -] - -[[package]] -name = "idna" -version = "1.0.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "686f825264d630750a544639377bae737628043f20d38bbc029e8f29ea968a7e" -dependencies = [ - "idna_adapter", - "smallvec", - "utf8_iter", -] - -[[package]] -name = "idna_adapter" -version = "1.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "daca1df1c957320b2cf139ac61e7bd64fed304c5040df000a745aa1de3b4ef71" -dependencies = [ - "icu_normalizer", - "icu_properties", -] - -[[package]] -name = "indexmap" -version = "2.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "62f822373a4fe84d4bb149bf54e584a7f4abec90e072ed49cda0edea5b95471f" +name = "http-client" +version = "0.1.0" dependencies = [ - "equivalent", - "hashbrown", + "base64", + "openssl", ] [[package]] @@ -734,12 +295,6 @@ dependencies = [ "cfg-if", ] -[[package]] -name = "ipnet" -version = "2.10.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ddc24109865250148c2e0f3d25d4f0f479571723792d3802153c60922a4fb708" - [[package]] name = "is_terminal_polyfill" version = "1.70.1" @@ -761,32 +316,22 @@ version = "1.0.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d75a2a4b1b190afb6f5425f10f6a8f959d2ea0b9c2b1d79553551850539e4674" -[[package]] -name = "js-sys" -version = "0.3.76" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6717b6b5b077764fb5966237269cb3c64edddde4b14ce42647430a78ced9e7b7" -dependencies = [ - "once_cell", - "wasm-bindgen", -] - [[package]] name = "komandan" version = "0.1.0" dependencies = [ "anyhow", + "base64", "clap", + "http-client", "minijinja", "mlua", "rand", "regex", - "reqwest", "rustyline", "serde_json", "ssh2", "tempfile", - "url", ] [[package]] @@ -827,12 +372,6 @@ version = "0.4.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "78b3ae25bc7c8c38cec158d1f2757ee79e9b3740fbc7ccf0e59e4b08d793fa89" -[[package]] -name = "litemap" -version = "0.7.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4ee93343901ab17bd981295f2cf0026d4ad018c7c31ba84549a4ddbb47a45104" - [[package]] name = "lock_api" version = "0.4.12" @@ -874,12 +413,6 @@ version = "2.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" -[[package]] -name = "mime" -version = "0.3.17" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a" - [[package]] name = "minijinja" version = "2.5.0" @@ -889,26 +422,6 @@ dependencies = [ "serde", ] -[[package]] -name = "miniz_oxide" -version = "0.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e2d80299ef12ff69b16a84bb182e3b9df68b5a91574d3d4fa6e41b65deec4df1" -dependencies = [ - "adler2", -] - -[[package]] -name = "mio" -version = "1.0.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2886843bf800fba2e3377cff24abf6379b4c4d5c6681eaf9ea5b0d15090450bd" -dependencies = [ - "libc", - "wasi", - "windows-sys 0.52.0", -] - [[package]] name = "mlua" version = "0.10.1" @@ -957,27 +470,10 @@ dependencies = [ ] [[package]] -name = "native-tls" -version = "0.2.12" +name = "nibble_vec" +version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a8614eb2c83d59d1c8cc974dd3f920198647674a0a035e1af1fa58707e317466" -dependencies = [ - "libc", - "log", - "openssl", - "openssl-probe", - "openssl-sys", - "schannel", - "security-framework", - "security-framework-sys", - "tempfile", -] - -[[package]] -name = "nibble_vec" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "77a5d83df9f36fe23f0c3648c6bbb8b0298bb5f1939c8f2704431371f4b84d43" +checksum = "77a5d83df9f36fe23f0c3648c6bbb8b0298bb5f1939c8f2704431371f4b84d43" dependencies = [ "smallvec", ] @@ -1003,15 +499,6 @@ dependencies = [ "autocfg", ] -[[package]] -name = "object" -version = "0.36.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aedf0a2d09c573ed1d8d85b30c119153926a2b36dce0ab28322c09a117a4683e" -dependencies = [ - "memchr", -] - [[package]] name = "once_cell" version = "1.20.2" @@ -1044,12 +531,6 @@ dependencies = [ "syn 2.0.89", ] -[[package]] -name = "openssl-probe" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf" - [[package]] name = "openssl-src" version = "300.4.1+3.4.0" @@ -1129,24 +610,6 @@ dependencies = [ "windows-targets", ] -[[package]] -name = "percent-encoding" -version = "2.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e" - -[[package]] -name = "pin-project-lite" -version = "0.2.15" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "915a1e146535de9163f3987b8944ed8cf49a18bb0056bcebcdcece385cece4ff" - -[[package]] -name = "pin-utils" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" - [[package]] name = "pkg-config" version = "0.3.31" @@ -1291,71 +754,6 @@ version = "0.8.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" -[[package]] -name = "reqwest" -version = "0.12.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a77c62af46e79de0a562e1a9849205ffcb7fc1238876e9bd743357570e04046f" -dependencies = [ - "base64", - "bytes", - "encoding_rs", - "futures-channel", - "futures-core", - "futures-util", - "h2", - "http", - "http-body", - "http-body-util", - "hyper", - "hyper-rustls", - "hyper-tls", - "hyper-util", - "ipnet", - "js-sys", - "log", - "mime", - "native-tls", - "once_cell", - "percent-encoding", - "pin-project-lite", - "rustls-pemfile", - "serde", - "serde_json", - "serde_urlencoded", - "sync_wrapper", - "system-configuration", - "tokio", - "tokio-native-tls", - "tower-service", - "url", - "wasm-bindgen", - "wasm-bindgen-futures", - "web-sys", - "windows-registry", -] - -[[package]] -name = "ring" -version = "0.17.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c17fa4cb658e3583423e915b9f3acc01cceaee1860e33d59ebae66adc3a2dc0d" -dependencies = [ - "cc", - "cfg-if", - "getrandom", - "libc", - "spin", - "untrusted", - "windows-sys 0.52.0", -] - -[[package]] -name = "rustc-demangle" -version = "0.1.24" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f" - [[package]] name = "rustc-hash" version = "2.0.0" @@ -1375,45 +773,6 @@ dependencies = [ "windows-sys 0.52.0", ] -[[package]] -name = "rustls" -version = "0.23.20" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5065c3f250cbd332cd894be57c40fa52387247659b14a2d6041d121547903b1b" -dependencies = [ - "once_cell", - "rustls-pki-types", - "rustls-webpki", - "subtle", - "zeroize", -] - -[[package]] -name = "rustls-pemfile" -version = "2.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dce314e5fee3f39953d46bb63bb8a46d40c2f8fb7cc5a3b6cab2bde9721d6e50" -dependencies = [ - "rustls-pki-types", -] - -[[package]] -name = "rustls-pki-types" -version = "1.10.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d2bf47e6ff922db3825eb750c4e2ff784c6ff8fb9e13046ef6a1d1c5401b0b37" - -[[package]] -name = "rustls-webpki" -version = "0.102.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "64ca1bc8749bd4cf37b5ce386cc146580777b4e8572c7b97baf22c83f444bee9" -dependencies = [ - "ring", - "rustls-pki-types", - "untrusted", -] - [[package]] name = "rustyline" version = "15.0.0" @@ -1442,44 +801,12 @@ version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" -[[package]] -name = "schannel" -version = "0.1.27" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1f29ebaa345f945cec9fbbc532eb307f0fdad8161f281b6369539c8d84876b3d" -dependencies = [ - "windows-sys 0.59.0", -] - [[package]] name = "scopeguard" version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" -[[package]] -name = "security-framework" -version = "2.11.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "897b2245f0b511c87893af39b033e5ca9cce68824c4d7e7630b5a1d339658d02" -dependencies = [ - "bitflags 2.6.0", - "core-foundation", - "core-foundation-sys", - "libc", - "security-framework-sys", -] - -[[package]] -name = "security-framework-sys" -version = "2.13.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1863fd3768cd83c56a7f60faa4dc0d403f1b6df0a38c3c25f44b7894e45370d5" -dependencies = [ - "core-foundation-sys", - "libc", -] - [[package]] name = "serde" version = "1.0.215" @@ -1522,55 +849,18 @@ dependencies = [ "serde", ] -[[package]] -name = "serde_urlencoded" -version = "0.7.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d3491c14715ca2294c4d6a88f15e84739788c1d030eed8c110436aafdaa2f3fd" -dependencies = [ - "form_urlencoded", - "itoa", - "ryu", - "serde", -] - [[package]] name = "shlex" version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" -[[package]] -name = "slab" -version = "0.4.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8f92a496fb766b417c996b9c5e57daf2f7ad3b0bebe1ccfca4856390e3d3bb67" -dependencies = [ - "autocfg", -] - [[package]] name = "smallvec" version = "1.13.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67" -[[package]] -name = "socket2" -version = "0.5.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c970269d99b64e60ec3bd6ad27270092a5394c4e309314b18ae3fe575695fbe8" -dependencies = [ - "libc", - "windows-sys 0.52.0", -] - -[[package]] -name = "spin" -version = "0.9.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67" - [[package]] name = "ssh2" version = "0.9.4" @@ -1583,24 +873,12 @@ dependencies = [ "parking_lot 0.11.2", ] -[[package]] -name = "stable_deref_trait" -version = "1.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3" - [[package]] name = "strsim" version = "0.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" -[[package]] -name = "subtle" -version = "2.6.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" - [[package]] name = "syn" version = "1.0.109" @@ -1622,47 +900,6 @@ dependencies = [ "unicode-ident", ] -[[package]] -name = "sync_wrapper" -version = "1.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0bf256ce5efdfa370213c1dabab5935a12e49f2c58d15e9eac2870d3b4f27263" -dependencies = [ - "futures-core", -] - -[[package]] -name = "synstructure" -version = "0.13.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c8af7666ab7b6390ab78131fb5b0fce11d6b7a6951602017c35fa82800708971" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.89", -] - -[[package]] -name = "system-configuration" -version = "0.6.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3c879d448e9d986b661742763247d3693ed13609438cf3d006f51f5368a5ba6b" -dependencies = [ - "bitflags 2.6.0", - "core-foundation", - "system-configuration-sys", -] - -[[package]] -name = "system-configuration-sys" -version = "0.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8e1d1b10ced5ca923a1fcb8d03e96b8d3268065d724548c0211415ff6ac6bac4" -dependencies = [ - "core-foundation-sys", - "libc", -] - [[package]] name = "tempfile" version = "3.14.0" @@ -1676,95 +913,6 @@ dependencies = [ "windows-sys 0.59.0", ] -[[package]] -name = "tinystr" -version = "0.7.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9117f5d4db391c1cf6927e7bea3db74b9a1c1add8f7eda9ffd5364f40f57b82f" -dependencies = [ - "displaydoc", - "zerovec", -] - -[[package]] -name = "tokio" -version = "1.42.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5cec9b21b0450273377fc97bd4c33a8acffc8c996c987a7c5b319a0083707551" -dependencies = [ - "backtrace", - "bytes", - "libc", - "mio", - "pin-project-lite", - "socket2", - "windows-sys 0.52.0", -] - -[[package]] -name = "tokio-native-tls" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bbae76ab933c85776efabc971569dd6119c580d8f5d448769dec1764bf796ef2" -dependencies = [ - "native-tls", - "tokio", -] - -[[package]] -name = "tokio-rustls" -version = "0.26.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5f6d0975eaace0cf0fcadee4e4aaa5da15b5c079146f2cffb67c113be122bf37" -dependencies = [ - "rustls", - "tokio", -] - -[[package]] -name = "tokio-util" -version = "0.7.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d7fcaa8d55a2bdd6b83ace262b016eca0d79ee02818c5c1bcdf0305114081078" -dependencies = [ - "bytes", - "futures-core", - "futures-sink", - "pin-project-lite", - "tokio", -] - -[[package]] -name = "tower-service" -version = "0.3.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8df9b6e13f2d32c91b9bd719c00d1958837bc7dec474d94952798cc8e69eeec3" - -[[package]] -name = "tracing" -version = "0.1.41" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "784e0ac535deb450455cbfa28a6f0df145ea1bb7ae51b821cf5e7927fdcfbdd0" -dependencies = [ - "pin-project-lite", - "tracing-core", -] - -[[package]] -name = "tracing-core" -version = "0.1.33" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e672c95779cf947c5311f83787af4fa8fffd12fb27e4993211a84bdfd9610f9c" -dependencies = [ - "once_cell", -] - -[[package]] -name = "try-lock" -version = "0.2.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b" - [[package]] name = "typeid" version = "1.0.2" @@ -1789,35 +937,6 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1fc81956842c57dac11422a97c3b8195a1ff727f06e85c84ed2e8aa277c9a0fd" -[[package]] -name = "untrusted" -version = "0.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1" - -[[package]] -name = "url" -version = "2.5.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "32f8b686cadd1473f4bd0117a5d28d36b1ade384ea9b5069a1c40aefed7fda60" -dependencies = [ - "form_urlencoded", - "idna", - "percent-encoding", -] - -[[package]] -name = "utf16_iter" -version = "1.0.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c8232dd3cdaed5356e0f716d285e4b40b932ac434100fe9b7e0e8e935b9e6246" - -[[package]] -name = "utf8_iter" -version = "1.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be" - [[package]] name = "utf8parse" version = "0.2.2" @@ -1836,98 +955,12 @@ version = "0.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" -[[package]] -name = "want" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bfa7760aed19e106de2c7c0b581b509f2f25d3dacaf737cb82ac61bc6d760b0e" -dependencies = [ - "try-lock", -] - [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" -[[package]] -name = "wasm-bindgen" -version = "0.2.99" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a474f6281d1d70c17ae7aa6a613c87fce69a127e2624002df63dcb39d6cf6396" -dependencies = [ - "cfg-if", - "once_cell", - "wasm-bindgen-macro", -] - -[[package]] -name = "wasm-bindgen-backend" -version = "0.2.99" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5f89bb38646b4f81674e8f5c3fb81b562be1fd936d84320f3264486418519c79" -dependencies = [ - "bumpalo", - "log", - "proc-macro2", - "quote", - "syn 2.0.89", - "wasm-bindgen-shared", -] - -[[package]] -name = "wasm-bindgen-futures" -version = "0.4.49" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "38176d9b44ea84e9184eff0bc34cc167ed044f816accfe5922e54d84cf48eca2" -dependencies = [ - "cfg-if", - "js-sys", - "once_cell", - "wasm-bindgen", - "web-sys", -] - -[[package]] -name = "wasm-bindgen-macro" -version = "0.2.99" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2cc6181fd9a7492eef6fef1f33961e3695e4579b9872a6f7c83aee556666d4fe" -dependencies = [ - "quote", - "wasm-bindgen-macro-support", -] - -[[package]] -name = "wasm-bindgen-macro-support" -version = "0.2.99" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "30d7a95b763d3c45903ed6c81f156801839e5ee968bb07e534c44df0fcd330c2" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.89", - "wasm-bindgen-backend", - "wasm-bindgen-shared", -] - -[[package]] -name = "wasm-bindgen-shared" -version = "0.2.99" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "943aab3fdaaa029a6e0271b35ea10b72b943135afe9bffca82384098ad0e06a6" - -[[package]] -name = "web-sys" -version = "0.3.76" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "04dd7223427d52553d3702c004d3b2fe07c148165faa56313cb00211e31c12bc" -dependencies = [ - "js-sys", - "wasm-bindgen", -] - [[package]] name = "which" version = "6.0.3" @@ -1962,36 +995,6 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" -[[package]] -name = "windows-registry" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e400001bb720a623c1c69032f8e3e4cf09984deec740f007dd2b03ec864804b0" -dependencies = [ - "windows-result", - "windows-strings", - "windows-targets", -] - -[[package]] -name = "windows-result" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1d1043d8214f791817bab27572aaa8af63732e11bf84aa21a45a78d6c317ae0e" -dependencies = [ - "windows-targets", -] - -[[package]] -name = "windows-strings" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4cd9b125c486025df0eabcb585e62173c6c9eddcec5d117d3b6e8c30e2ee4d10" -dependencies = [ - "windows-result", - "windows-targets", -] - [[package]] name = "windows-sys" version = "0.52.0" @@ -2080,42 +1083,6 @@ version = "0.0.19" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d135d17ab770252ad95e9a872d365cf3090e3be864a34ab46f48555993efc904" -[[package]] -name = "write16" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d1890f4022759daae28ed4fe62859b1236caebfc61ede2f63ed4e695f3f6d936" - -[[package]] -name = "writeable" -version = "0.5.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e9df38ee2d2c3c5948ea468a8406ff0db0b29ae1ffde1bcf20ef305bcc95c51" - -[[package]] -name = "yoke" -version = "0.7.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "120e6aef9aa629e3d4f52dc8cc43a015c7724194c97dfaf45180d2daf2b77f40" -dependencies = [ - "serde", - "stable_deref_trait", - "yoke-derive", - "zerofrom", -] - -[[package]] -name = "yoke-derive" -version = "0.7.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2380878cad4ac9aac1e2435f3eb4020e8374b5f13c296cb75b4620ff8e229154" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.89", - "synstructure", -] - [[package]] name = "zerocopy" version = "0.7.35" @@ -2136,52 +1103,3 @@ dependencies = [ "quote", "syn 2.0.89", ] - -[[package]] -name = "zerofrom" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cff3ee08c995dee1859d998dea82f7374f2826091dd9cd47def953cae446cd2e" -dependencies = [ - "zerofrom-derive", -] - -[[package]] -name = "zerofrom-derive" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "595eed982f7d355beb85837f651fa22e90b3c044842dc7f2c2842c086f295808" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.89", - "synstructure", -] - -[[package]] -name = "zeroize" -version = "1.8.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ced3678a2879b30306d323f4542626697a464a97c0a07c9aebf7ebca65cd4dde" - -[[package]] -name = "zerovec" -version = "0.10.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aa2b893d79df23bfb12d5461018d408ea19dfafe76c2c7ef6d4eba614f8ff079" -dependencies = [ - "yoke", - "zerofrom", - "zerovec-derive", -] - -[[package]] -name = "zerovec-derive" -version = "0.10.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6eafa6dfb17584ea3e2bd6e76e0cc15ad7af12b09abdd1ca55961bed9b1063c6" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.89", -] diff --git a/Cargo.toml b/Cargo.toml index bc8fbad..170d49d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,19 +7,31 @@ edition = "2021" [dependencies] anyhow = "1.0.93" +base64 = "0.22.1" clap = { version = "4.5.21", features = ["derive"] } +http-client = { path = "http-client" } minijinja = "2.5.0" -mlua = { version = "0.10.1", features = ["anyhow", "luajit", "macros", "serialize", "vendored"] } +mlua = { version = "0.10.1", features = [ + "anyhow", + "luajit", + "macros", + "serialize", + "vendored", +] } rand = "0.8.5" regex = "1.11.1" -reqwest = { version = "0.12.9", features = ["blocking", "json", "native-tls-vendored"] } rustyline = "15.0.0" serde_json = "1.0.133" ssh2 = { version = "0.9.4", features = ["vendored-openssl"] } -url = "2.5.4" [dev-dependencies] tempfile = "3.14.0" [profile.release] lto = "fat" + +[workspace] +members = [".", "http-client"] + +[workspace.dependencies] +openssl = { version = "0.10.68", features = ["vendored"] } diff --git a/examples/parse_hosts_json.lua b/examples/parse_hosts_json.lua index c13b955..cbb1d4a 100644 --- a/examples/parse_hosts_json.lua +++ b/examples/parse_hosts_json.lua @@ -1,6 +1,6 @@ -local hosts = komandan.parse_hosts_json("/path/to/hosts.json") +local hosts = komandan.parse_hosts_json_file("/path/to/hosts.json") -- or use a URL --- local hosts = komandan.parse_hosts_json("http://localhost:8000/hosts.json") +-- local hosts = komandan.parse_hosts_json_url("http://localhost:8000/hosts.json") komandan.set_defaults({ user = "user1", diff --git a/http-client/Cargo.toml b/http-client/Cargo.toml new file mode 100644 index 0000000..0640b9a --- /dev/null +++ b/http-client/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "http-client" +version = "0.1.0" +edition = "2024" + +[dependencies] +base64 = "0.22.1" +openssl = { workspace = true } diff --git a/http-client/src/lib.rs b/http-client/src/lib.rs new file mode 100644 index 0000000..d7ead2e --- /dev/null +++ b/http-client/src/lib.rs @@ -0,0 +1,609 @@ +use base64::{Engine as _, engine::general_purpose}; +use openssl::ssl::{SslConnector, SslMethod, SslVerifyMode}; +use std::collections::HashMap; +use std::error::Error; +use std::io::{Read, Write}; +use std::net::{SocketAddr, TcpStream, ToSocketAddrs}; +use std::path::PathBuf; +use std::str; +use std::time::Duration; + +#[derive(Debug, Clone)] +pub struct ProxyConfig { + host: String, + port: u16, + auth: Option<(String, String)>, + use_https: bool, +} + +impl ProxyConfig { + pub fn new(host: &str, port: u16) -> Self { + ProxyConfig { + host: host.to_string(), + port, + auth: None, + use_https: false, + } + } + + pub fn with_auth(mut self, username: &str, password: &str) -> Self { + self.auth = Some((username.to_string(), password.to_string())); + self + } + + pub fn with_https(mut self, use_https: bool) -> Self { + self.use_https = use_https; + self + } +} + +#[derive(Debug)] +pub enum HttpMethod { + GET, + POST, + PUT, + DELETE, + PATCH, + HEAD, + CONNECT, +} + +impl ToString for HttpMethod { + fn to_string(&self) -> String { + match self { + HttpMethod::GET => "GET", + HttpMethod::POST => "POST", + HttpMethod::PUT => "PUT", + HttpMethod::DELETE => "DELETE", + HttpMethod::PATCH => "PATCH", + HttpMethod::HEAD => "HEAD", + HttpMethod::CONNECT => "CONNECT", + } + .to_string() + } +} + +#[derive(Debug)] +pub enum HttpError { + ConnectionError(String), + RequestError(String), + ResponseError(String), + TimeoutError(String), + ParseError(String), + ProxyError(String), +} + +pub struct HttpClient { + host: String, + auth: Option<(String, String)>, + headers: HashMap, + timeout: Option, + max_redirects: u32, + proxy: Option, + verify_ssl: bool, + enable_ipv6: bool, +} + +#[derive(Debug)] +pub struct HttpResponse { + pub status_code: u16, + pub headers: HashMap, + pub body: Vec, + pub content_type: Option, +} + +impl HttpClient { + pub fn new(host: &str) -> Self { + let clean_host = host.trim_end_matches('/').to_string(); + + HttpClient { + host: clean_host, + auth: None, + headers: HashMap::new(), + timeout: None, + max_redirects: 5, + proxy: None, + verify_ssl: true, + enable_ipv6: true, + } + } + + pub fn set_auth(&mut self, username: &str, password: &str) { + self.auth = Some((username.to_string(), password.to_string())); + } + + pub fn set_header(&mut self, key: &str, value: &str) { + self.headers.insert(key.to_string(), value.to_string()); + } + + pub fn set_timeout(&mut self, timeout: Duration) { + self.timeout = Some(timeout); + } + + pub fn set_max_redirects(&mut self, max_redirects: u32) { + self.max_redirects = max_redirects; + } + + pub fn set_proxy(&mut self, proxy: ProxyConfig) { + self.proxy = Some(proxy); + } + + pub fn set_verify_ssl(&mut self, verify: bool) { + self.verify_ssl = verify; + } + + pub fn set_enable_ipv6(&mut self, enable: bool) { + self.enable_ipv6 = enable; + } + + pub fn request( + &self, + method: HttpMethod, + path: &str, + body: Option<&[u8]>, + ) -> Result { + let use_https = self.host.starts_with("https://"); + let host = self + .host + .trim_start_matches("http://") + .trim_start_matches("https://"); + + let mut headers = format!( + "{} {} HTTP/1.1\r\nHost: {}\r\n", + method.to_string(), + path, + host + ); + + if let Some(body_data) = body { + headers.push_str(&format!("Content-Length: {}\r\n", body_data.len())); + } + + if let Some((ref username, ref password)) = self.auth { + let auth = format!("{}:{}", username, password); + let encoded_auth = general_purpose::STANDARD.encode(auth); + headers.push_str(&format!("Authorization: Basic {}\r\n", encoded_auth)); + } + + for (key, value) in &self.headers { + headers.push_str(&format!("{}: {}\r\n", key, value)); + } + + headers.push_str("Connection: close\r\n\r\n"); + + let raw_response = if use_https { + self.send_https(&headers, body, host)? + } else { + self.send_http(&headers, body, host)? + }; + + let mut response = self.parse_response(&raw_response)?; + + let mut redirects = 0; + while let Some(location) = response.headers.get("Location") { + if redirects >= self.max_redirects { + return Err(HttpError::RequestError( + "Max redirects exceeded".to_string(), + )); + } + + if location.starts_with("http") { + let client = HttpClient::new(location); + response = client.request(HttpMethod::GET, "/", None)?; + } else { + response = self.request(HttpMethod::GET, location, None)?; + } + + redirects += 1; + } + + Ok(response) + } + + fn get_system_cert_path() -> Option { + if let Ok(path) = std::env::var("SSL_CERT_FILE") { + let path_buf = PathBuf::from(path); + if path_buf.exists() { + return Some(path_buf); + } + } + + if let Ok(dir) = std::env::var("SSL_CERT_DIR") { + let dir_path = PathBuf::from(dir); + if dir_path.exists() { + // Look for common certificate file names in the directory + for name in ["ca-certificates.crt", "cert.pem", "ca-bundle.crt"] { + let cert_path = dir_path.join(name); + if cert_path.exists() { + return Some(cert_path); + } + } + } + } + + // Common Unix-like system locations + let cert_locations = vec![ + // Debian/Ubuntu/Mint + PathBuf::from("/etc/ssl/certs/ca-certificates.crt"), + // RHEL/Fedora/CentOS + PathBuf::from("/etc/pki/tls/certs/ca-bundle.crt"), + PathBuf::from("/etc/pki/tls/cacert.pem"), + // OpenSUSE + PathBuf::from("/etc/ssl/ca-bundle.pem"), + // OpenBSD + PathBuf::from("/etc/ssl/cert.pem"), + // FreeBSD/BSD + PathBuf::from("/usr/local/share/certs/ca-root-nss.crt"), + // MacOS (Homebrew) + PathBuf::from("/usr/local/etc/openssl/cert.pem"), + PathBuf::from("/opt/homebrew/etc/openssl@3/cert.pem"), + ]; + + cert_locations.into_iter().find(|path| path.exists()) + } + + pub fn get(&self, path: &str) -> Result { + self.request(HttpMethod::GET, path, None) + } + + pub fn post(&self, path: &str, body: &[u8]) -> Result { + self.request(HttpMethod::POST, path, Some(body)) + } + + pub fn put(&self, path: &str, body: &[u8]) -> Result { + self.request(HttpMethod::PUT, path, Some(body)) + } + + pub fn delete(&self, path: &str) -> Result { + self.request(HttpMethod::DELETE, path, None) + } + + fn connect(&self, host: &str, port: u16) -> Result { + let clean_host = host.trim_end_matches('/'); + let addr_str = format!("{}:{}", clean_host, port); + + let addrs: Vec = match (clean_host, port).to_socket_addrs() { + Ok(iter) => iter + .filter(|addr| { + if self.enable_ipv6 { + true + } else { + addr.is_ipv4() + } + }) + .collect(), + Err(e) => { + return Err(HttpError::ConnectionError(format!( + "DNS resolution failed for {}: {}", + addr_str, e + ))); + } + }; + + if addrs.is_empty() { + return Err(HttpError::ConnectionError(format!( + "No {} addresses found for {}", + if self.enable_ipv6 { + "IPv4/IPv6" + } else { + "IPv4" + }, + addr_str + ))); + } + + let mut sorted_addrs = addrs; + if self.enable_ipv6 { + sorted_addrs.sort_by(|a, b| match (a.is_ipv6(), b.is_ipv6()) { + (true, false) => std::cmp::Ordering::Less, + (false, true) => std::cmp::Ordering::Greater, + _ => std::cmp::Ordering::Equal, + }); + } + + let timeout = self.timeout.unwrap_or(Duration::from_secs(30)); + let mut errors = Vec::new(); + + for addr in sorted_addrs { + match TcpStream::connect_timeout(&addr, timeout) { + Ok(stream) => { + if let Some(timeout) = self.timeout { + let _ = stream.set_read_timeout(Some(timeout)); + let _ = stream.set_write_timeout(Some(timeout)); + } + + if let Err(e) = stream.set_nodelay(true) { + eprintln!("Warning: Failed to set TCP_NODELAY: {}", e); + } + + return Ok(stream); + } + Err(e) => { + errors.push(format!("{}: {}", addr, e)); + } + } + } + + Err(HttpError::ConnectionError(format!( + "Failed to connect to {} (IPv6 {}). Attempted addresses:\n{}", + addr_str, + if self.enable_ipv6 { + "enabled" + } else { + "disabled" + }, + errors.join("\n") + ))) + } + + fn send_http( + &self, + headers: &str, + body: Option<&[u8]>, + host: &str, + ) -> Result { + let (host, port) = if let Some(i) = host.find(':') { + let (h, p) = host.split_at(i); + let port = p + .trim_start_matches(':') + .parse::() + .map_err(|_| HttpError::ParseError("Invalid port number".to_string()))?; + (h, port) + } else { + (host, 80) + }; + + let mut stream = match &self.proxy { + Some(proxy) if !proxy.use_https => self.connect_proxy(proxy)?, + _ => self.connect(host, port)?, + }; + + let request = if self.proxy.is_some() { + let scheme = if self.host.starts_with("https://") { + "https" + } else { + "http" + }; + headers.replace(" / ", &format!(" {scheme}://{host}/ ")) + } else { + headers.to_string() + }; + + stream + .write_all(request.as_bytes()) + .map_err(|e| HttpError::RequestError(e.to_string()))?; + + if let Some(body_data) = body { + stream + .write_all(body_data) + .map_err(|e| HttpError::RequestError(e.to_string()))?; + } + + let mut response = Vec::new(); + stream + .read_to_end(&mut response) + .map_err(|e| HttpError::ResponseError(e.to_string()))?; + + String::from_utf8(response).map_err(|e| HttpError::ParseError(e.to_string())) + } + + fn send_https( + &self, + headers: &str, + body: Option<&[u8]>, + host_with_port: &str, + ) -> Result { + let (host, port) = if let Some(i) = host_with_port.find(':') { + let (h, p) = host_with_port.split_at(i); + let port = p + .trim_start_matches(':') + .parse::() + .map_err(|_| HttpError::ParseError("Invalid port number".to_string()))?; + (h, port) + } else { + (host_with_port, 443) + }; + + let mut builder = SslConnector::builder(SslMethod::tls()) + .map_err(|e| HttpError::ConnectionError(e.to_string()))?; + + if self.verify_ssl { + if let Some(cert_path) = Self::get_system_cert_path() { + builder.set_ca_file(&cert_path).map_err(|e| { + HttpError::ConnectionError(format!( + "Failed to set CA file {}: {}", + cert_path.display(), + e + )) + })?; + } else { + return Err(HttpError::ConnectionError( + "Could not find system root certificates. Consider disabling SSL verification for testing.".to_string() + )); + } + } else { + builder.set_verify(SslVerifyMode::NONE); + } + + let connector = builder.build(); + + let stream = match &self.proxy { + Some(proxy) => { + let mut proxy_stream = self.connect_proxy(proxy)?; + + let connect_req = format!( + "CONNECT {}:{} HTTP/1.1\r\nHost: {}:{}\r\n\r\n", + host, port, host, port + ); + proxy_stream + .write_all(connect_req.as_bytes()) + .map_err(|e| { + HttpError::ProxyError(format!("Failed to send CONNECT request: {}", e)) + })?; + + let mut response = Vec::new(); + let mut buffer = [0; 1024]; + loop { + let n = proxy_stream.read(&mut buffer).map_err(|e| { + HttpError::ProxyError(format!("Failed to read proxy response: {}", e)) + })?; + response.extend_from_slice(&buffer[..n]); + if response.windows(4).any(|w| w == b"\r\n\r\n") { + break; + } + } + + let response_str = String::from_utf8_lossy(&response); + if !response_str.starts_with("HTTP/1.1 200") { + return Err(HttpError::ProxyError(format!( + "Proxy CONNECT failed: {}", + response_str + ))); + } + + proxy_stream + } + None => self.connect(host, port)?, + }; + + let mut ssl_stream = connector + .connect(host_with_port, stream) + .map_err(|e| HttpError::ConnectionError(e.to_string()))?; + + ssl_stream + .write_all(headers.as_bytes()) + .map_err(|e| HttpError::RequestError(e.to_string()))?; + + if let Some(body_data) = body { + ssl_stream + .write_all(body_data) + .map_err(|e| HttpError::RequestError(e.to_string()))?; + } + + let mut response = Vec::new(); + ssl_stream + .read_to_end(&mut response) + .map_err(|e| HttpError::ResponseError(e.to_string()))?; + + String::from_utf8(response).map_err(|e| HttpError::ParseError(e.to_string())) + } + + fn connect_proxy(&self, proxy: &ProxyConfig) -> Result { + let mut stream = self.connect(&proxy.host, proxy.port)?; + + if let Some((username, password)) = &proxy.auth { + let auth = format!("{}:{}", username, password); + let encoded_auth = general_purpose::STANDARD.encode(auth); + let auth_header = format!("Proxy-Authorization: Basic {}\r\n", encoded_auth); + stream + .write_all(auth_header.as_bytes()) + .map_err(|e| HttpError::ProxyError(format!("Failed to send proxy auth: {}", e)))?; + } + + Ok(stream) + } + + fn parse_response(&self, raw_response: &str) -> Result { + let mut lines = raw_response.lines(); + let status_line = lines + .next() + .ok_or_else(|| HttpError::ParseError("Missing status line".to_string()))?; + + let status_code = status_line + .split_whitespace() + .nth(1) + .ok_or_else(|| HttpError::ParseError("Malformed status line".to_string()))? + .parse::() + .map_err(|_| HttpError::ParseError("Invalid status code".to_string()))?; + + let mut headers = HashMap::new(); + let mut content_type = None; + + for line in lines.by_ref() { + if line.is_empty() { + break; + } + if let Some((key, value)) = line.split_once(": ") { + if key.to_lowercase() == "content-type" { + content_type = Some(value.to_string()); + } + headers.insert(key.to_string(), value.to_string()); + } + } + + let body = lines.collect::>().join("\n"); + + Ok(HttpResponse { + status_code, + headers, + body: body.into_bytes(), + content_type, + }) + } +} + +impl HttpResponse { + pub fn is_success(&self) -> bool { + (200..=299).contains(&self.status_code) + } + + pub fn is_client_error(&self) -> bool { + (400..=499).contains(&self.status_code) + } + + pub fn is_server_error(&self) -> bool { + (500..=599).contains(&self.status_code) + } + + pub fn is_error(&self) -> bool { + self.is_client_error() || self.is_server_error() + } +} + +#[derive(Debug)] +pub struct ParsedUrl { + pub scheme: String, + pub host: String, + pub path: String, + pub query: Option, +} + +pub fn parse_url(url: &str) -> Result> { + let (scheme, rest) = url + .split_once("://") + .ok_or("URL must contain scheme (http:// or https://)")?; + + let (host, path_and_query) = rest + .find('/') + .map(|i| rest.split_at(i)) + .unwrap_or((rest, "")); + + let (path, query) = if path_and_query.is_empty() { + ("/", None) + } else { + match path_and_query.split_once('?') { + Some((p, q)) => (p, Some(q.to_string())), + None => (path_and_query, None), + } + }; + + Ok(ParsedUrl { + scheme: scheme.to_lowercase(), + host: host.trim_end_matches('/').to_string(), + path: path.to_string(), + query: query, + }) +} + +pub fn create_client_from_url(url: &str) -> Result<(HttpClient, String), Box> { + let parsed = parse_url(url)?; + let base_url = format!("{}://{}", parsed.scheme, parsed.host); + + let full_path = if let Some(query) = parsed.query { + format!("{}?{}", parsed.path, query) + } else { + parsed.path + }; + + Ok((HttpClient::new(&base_url), full_path)) +} diff --git a/src/lib.rs b/src/lib.rs index 4afe312..604d956 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,8 +13,8 @@ use rustyline::DefaultEditor; use ssh::{ElevateMethod, Elevation, SSHAuthMethod, SSHSession}; use std::{env, fs, path::Path}; use util::{ - dprint, filter_hosts, host_display, parse_hosts_json, regex_is_match, set_defaults, - task_display, + dprint, filter_hosts, host_display, parse_hosts_json_file, parse_hosts_json_url, + regex_is_match, set_defaults, task_display, }; use validator::{validate_host, validate_task}; @@ -65,7 +65,14 @@ pub fn setup_komandan_table(lua: &Lua) -> mlua::Result<()> { // Add utils komandan.set("regex_is_match", lua.create_function(regex_is_match)?)?; komandan.set("filter_hosts", lua.create_function(filter_hosts)?)?; - komandan.set("parse_hosts_json", lua.create_function(parse_hosts_json)?)?; + komandan.set( + "parse_hosts_json_file", + lua.create_function(parse_hosts_json_file)?, + )?; + komandan.set( + "parse_hosts_json_url", + lua.create_function(parse_hosts_json_url)?, + )?; komandan.set("dprint", lua.create_function(dprint)?)?; // Add core modules @@ -319,7 +326,11 @@ pub fn run_main_file(lua: &Lua, main_file: &String) -> anyhow::Result<()> { let script = match fs::read_to_string(main_file) { Ok(script) => script, Err(e) => { - return Err(anyhow::anyhow!("Failed to read the main file ({}): {}", main_file, e)); + return Err(anyhow::anyhow!( + "Failed to read the main file ({}): {}", + main_file, + e + )); } }; @@ -411,7 +422,10 @@ mod tests { assert!(komandan_table.contains_key("komando").unwrap()); assert!(komandan_table.contains_key("regex_is_match").unwrap()); assert!(komandan_table.contains_key("filter_hosts").unwrap()); - assert!(komandan_table.contains_key("parse_hosts_json").unwrap()); + assert!(komandan_table + .contains_key("parse_hosts_json_file") + .unwrap()); + assert!(komandan_table.contains_key("parse_hosts_json_url").unwrap()); assert!(komandan_table.contains_key("dprint").unwrap()); let modules_table = komandan_table.get::("modules").unwrap(); diff --git a/src/util.rs b/src/util.rs index c408d2a..dd335e6 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,10 +1,9 @@ use crate::args::Args; use crate::validator::validate_host; use clap::Parser; +use http_client::create_client_from_url; use mlua::{chunk, Error::RuntimeError, Lua, LuaSerdeExt, Table, Value}; -use reqwest::blocking::get; use std::{fs::File, io::Read}; -use url::Url; pub fn set_defaults(lua: &Lua, data: Value) -> mlua::Result<()> { if !data.is_table() { @@ -117,55 +116,102 @@ pub fn filter_hosts(lua: &Lua, (hosts, pattern): (Value, Value)) -> mlua::Result Ok(matched_hosts) } -pub fn parse_hosts_json(lua: &Lua, src: Value) -> mlua::Result
{ - let src = match src.as_string_lossy() { - Some(s) => s, - None => return Err(RuntimeError(String::from("Invalid src path"))), +pub fn parse_hosts_json_file(lua: &Lua, path: Value) -> mlua::Result
{ + let path = match path.as_str() { + Some(path) => path.to_owned(), + None => return Err(RuntimeError(String::from("Path must be a string"))), + }; + let mut file = match File::open(&path) { + Ok(f) => f, + Err(_) => return Err(RuntimeError(String::from("Failed to open JSON file"))), + }; + let mut content = String::new(); + + let hosts = match file.read_to_string(&mut content) { + Ok(_) => match parse_hosts_json(lua, content) { + Ok(h) => h, + Err(_) => { + return Err(RuntimeError(String::from(format!( + "Failed to parse JSON file from '{}'", + path + )))); + } + }, + Err(_) => return Err(RuntimeError(String::from("Failed to read JSON file"))), }; - let content = if src.starts_with("http://") || src.starts_with("https://") { - let url = match Url::parse(&src) { - Ok(url) => url, - Err(_) => return Err(RuntimeError(String::from("Invalid URL"))), - }; + dprint( + lua, + Value::String( + lua.create_string(format!( + "Loaded {} hosts from JSON file '{}'", + hosts.len()?, + path + )) + .unwrap(), + ), + )?; - match get(url) { - Ok(response) => { - if !response.status().is_success() { - return Err(RuntimeError(format!( - "HTTP request failed with status: {}", - response.status() - ))); - } - match response.text() { - Ok(text) => text, - Err(_) => { - return Err(RuntimeError(String::from("Failed to read response body"))) - } - } + Ok(hosts) +} + +pub fn parse_hosts_json_url(lua: &Lua, url: Value) -> mlua::Result
{ + let url = match url.as_str() { + Some(url) => url, + None => return Err(RuntimeError(String::from("URL must be a string"))), + }; + let (client, path) = create_client_from_url(&url).unwrap(); + + let content = match client.get(&path) { + Ok(response) => { + if !response.is_success() { + return Err(RuntimeError(format!( + "HTTP request failed with status: {}", + response.status_code + ))); } - Err(_) => return Err(RuntimeError(String::from("Failed to fetch URL"))), + String::from_utf8_lossy(&response.body).to_string() } - } else { - let mut file = match File::open(&src) { - Ok(f) => f, - Err(_) => return Err(RuntimeError(String::from("Failed to open JSON file"))), - }; + Err(e) => { + return Err(RuntimeError(String::from(format!( + "Failed to fetch URL: {:?}", + e + )))); + } + }; - let mut content = String::new(); - match file.read_to_string(&mut content) { - Ok(_) => content, - Err(_) => return Err(RuntimeError(String::from("Failed to read JSON file"))), + let hosts = match parse_hosts_json(lua, content) { + Ok(h) => h, + Err(_) => { + return Err(RuntimeError(String::from(format!( + "Failed to parse JSON from '{}'", + url + )))); } }; + dprint( + lua, + Value::String( + lua.create_string(format!( + "Loaded {} hosts from JSON url '{}'", + hosts.len()?, + url + )) + .unwrap(), + ), + )?; + + Ok(hosts) +} + +fn parse_hosts_json(lua: &Lua, content: String) -> mlua::Result
{ let json: serde_json::Value = match serde_json::from_str(&content) { Ok(j) => j, Err(_) => return Err(RuntimeError(String::from("Failed to parse JSON"))), }; let hosts = lua.create_table()?; - let lua_value = match lua.to_value(&json) { Ok(o) => o, Err(_) => return Err(RuntimeError(String::from("Failed to convert JSON to Lua"))), @@ -186,11 +232,6 @@ pub fn parse_hosts_json(lua: &Lua, src: Value) -> mlua::Result
{ }; } - dprint( - &lua, - lua.to_value(&format!("Loaded {} hosts from '{}'", hosts.len()?, src))?, - )?; - Ok(hosts) } @@ -491,7 +532,7 @@ mod tests { let lua_string = lua .create_string(temp_file.path().to_str().unwrap()) .unwrap(); - let result = parse_hosts_json(&lua, Value::String(lua_string)); + let result = parse_hosts_json_file(&lua, Value::String(lua_string)); assert!(result.is_ok()); } @@ -499,7 +540,7 @@ mod tests { fn test_parse_hosts_json_invalid_path() { let lua = setup_lua(); let lua_string = Value::String(lua.create_string("/nonexistent/path").unwrap()); - let result = parse_hosts_json(&lua, lua_string); + let result = parse_hosts_json_file(&lua, lua_string); assert!(result.is_err()); assert!(result .unwrap_err() @@ -519,7 +560,7 @@ mod tests { let lua_string = lua .create_string(temp_file.path().to_str().unwrap()) .unwrap(); - let result = parse_hosts_json(&lua, Value::String(lua_string)); + let result = parse_hosts_json_file(&lua, Value::String(lua_string)); assert!(result.is_err()); assert!(result .unwrap_err() @@ -536,7 +577,7 @@ mod tests { let lua_string = lua .create_string(temp_file.path().to_str().unwrap()) .unwrap(); - let result = parse_hosts_json(&lua, Value::String(lua_string)); + let result = parse_hosts_json_file(&lua, Value::String(lua_string)); assert!(result.is_err()); } @@ -549,7 +590,7 @@ mod tests { let lua_string = lua .create_string(temp_file.path().to_str().unwrap()) .unwrap(); - let result = parse_hosts_json(&lua, Value::String(lua_string)); + let result = parse_hosts_json_file(&lua, Value::String(lua_string)); assert!(result.is_err()); assert!(result .unwrap_err() @@ -560,7 +601,7 @@ mod tests { #[test] fn test_parse_hosts_json_invalid_input_type() { let lua = setup_lua(); - let result = parse_hosts_json(&lua, Value::Nil); + let result = parse_hosts_json_url(&lua, Value::Nil); assert!(result.is_err()); assert!(result.unwrap_err().to_string().contains("Invalid src path")); }