diff --git a/Cargo.lock b/Cargo.lock index aee0893..b731764 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -173,6 +173,17 @@ version = "1.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0" +[[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi 0.1.19", + "libc", + "winapi", +] + [[package]] name = "autocfg" version = "1.5.0" @@ -333,6 +344,12 @@ dependencies = [ "serde", ] +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + [[package]] name = "bitflags" version = "2.10.0" @@ -483,6 +500,21 @@ dependencies = [ "windows-link", ] +[[package]] +name = "clap" +version = "3.2.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ea181bf566f71cb9a5d17a59e1871af638180a18fb0035c92ae62b705207123" +dependencies = [ + "atty", + "bitflags 1.3.2", + "clap_lex 0.2.4", + "indexmap 1.9.3", + "strsim 0.10.0", + "termcolor", + "textwrap", +] + [[package]] name = "clap" version = "4.5.58" @@ -501,8 +533,8 @@ checksum = "7f13174bda5dfd69d7e947827e5af4b0f2f94a4a3ee92912fba07a66150f21e2" dependencies = [ "anstream", "anstyle", - "clap_lex", - "strsim", + "clap_lex 1.0.0", + "strsim 0.11.1", ] [[package]] @@ -517,6 +549,15 @@ dependencies = [ "syn", ] +[[package]] +name = "clap_lex" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2850f2f5a82cbf437dd5af4d49848fbdfc27c157c3d010345776f952765261c5" +dependencies = [ + "os_str_bytes", +] + [[package]] name = "clap_lex" version = "1.0.0" @@ -687,6 +728,15 @@ dependencies = [ "crypto-common", ] +[[package]] +name = "dotenvy" +version = "0.15.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1aaf95b3e5c8f23aa320147307562d361db0ae0d51242340f558153b4eb2439b" +dependencies = [ + "clap 3.2.25", +] + [[package]] name = "dtoa" version = "1.0.11" @@ -939,13 +989,19 @@ dependencies = [ "futures-core", "futures-sink", "http", - "indexmap", + "indexmap 2.12.1", "slab", "tokio", "tokio-util", "tracing", ] +[[package]] +name = "hashbrown" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" + [[package]] name = "hashbrown" version = "0.15.5" @@ -1000,6 +1056,15 @@ version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" +[[package]] +name = "hermit-abi" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" +dependencies = [ + "libc", +] + [[package]] name = "hermit-abi" version = "0.5.2" @@ -1143,6 +1208,16 @@ version = "2.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3d3067d79b975e8844ca9eb072e16b31c3c1c36928edf9c6789548c524d0d954" +[[package]] +name = "indexmap" +version = "1.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bd070e393353796e801d209ad339e89596eb4c8d430d18ede6a1cced8fafbd99" +dependencies = [ + "autocfg", + "hashbrown 0.12.3", +] + [[package]] name = "indexmap" version = "2.12.1" @@ -1275,7 +1350,7 @@ checksum = "626c6fbcb5088716de86d0ccbdccedc17b13e59f41a605a3274029335e71fcbb" dependencies = [ "anyhow", "cargo_metadata", - "clap", + "clap 4.5.58", "libbpf-rs", "libbpf-sys", "memmap2", @@ -1292,7 +1367,7 @@ version = "0.25.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e23d252d93e246c8787198369f06806c99c5077b5295be29505295f4e5426dc4" dependencies = [ - "bitflags", + "bitflags 2.10.0", "libbpf-sys", "libc", "vsprintf", @@ -1433,7 +1508,7 @@ version = "0.30.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "74523f3a35e05aba87a1d978330aef40f67b0304ac79c1c00b294c9830543db6" dependencies = [ - "bitflags", + "bitflags 2.10.0", "cfg-if", "cfg_aliases", "libc", @@ -1469,7 +1544,7 @@ version = "1.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "91df4bbde75afed763b708b7eee1e8e7651e02d97f6d5dd763e89367e957b23b" dependencies = [ - "hermit-abi", + "hermit-abi 0.5.2", "libc", ] @@ -1491,6 +1566,12 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7c87def4c32ab89d880effc9e097653c8da5d6ef28e6b539d313baaacfbafcbe" +[[package]] +name = "os_str_bytes" +version = "6.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2355d85b9a3786f481747ced0e0ff2ba35213a1f9bd406ed906554d7af805a1" + [[package]] name = "parking_lot" version = "0.12.5" @@ -1752,7 +1833,7 @@ version = "0.5.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ed2bf2547551a7053d6fdfafda3f938979645c44812fbfcda098faae3f1a362d" dependencies = [ - "bitflags", + "bitflags 2.10.0", ] [[package]] @@ -1813,7 +1894,8 @@ dependencies = [ "build-time", "cgroup_traffic", "chrono", - "clap", + "clap 4.5.58", + "dotenvy", "flate2", "futures", "futures-util", @@ -1868,7 +1950,7 @@ version = "1.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "146c9e247ccc180c1f61615433868c99f3de3ae256a30a43b49f67c2d9171f34" dependencies = [ - "bitflags", + "bitflags 2.10.0", "errno", "libc", "linux-raw-sys", @@ -2012,7 +2094,7 @@ version = "3.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b3297343eaf830f66ede390ea39da1d462b6b0c1b000f420d0a83f898bbbe6ef" dependencies = [ - "bitflags", + "bitflags 2.10.0", "core-foundation", "core-foundation-sys", "libc", @@ -2121,7 +2203,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6e243647f07ad8fdbef8019c17df67dd4a1547b5cf0c1fe2b83728ff1ee3d4cc" dependencies = [ "base64", - "indexmap", + "indexmap 2.12.1", "itoa", "num-traits", "regex", @@ -2219,6 +2301,12 @@ dependencies = [ "der", ] +[[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" @@ -2261,6 +2349,21 @@ dependencies = [ "windows-sys 0.61.2", ] +[[package]] +name = "termcolor" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06794f8f6c5c898b3275aebefa6b8a1cb24cd2c6c79397ab15774837a0bc5755" +dependencies = [ + "winapi-util", +] + +[[package]] +name = "textwrap" +version = "0.16.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c13547615a44dc9c452a8a534638acdf07120d4b6847c8178705da06306a3057" + [[package]] name = "thiserror" version = "1.0.69" @@ -2388,7 +2491,7 @@ version = "0.9.12+spec-1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf92845e79fc2e2def6a5d828f0801e29a2f8acc037becc5ab08595c7d5e9863" dependencies = [ - "indexmap", + "indexmap 2.12.1", "serde_core", "serde_spanned", "toml_datetime", @@ -2444,7 +2547,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d4e6559d53cc268e5031cd8429d05415bc4cb4aefc4aa5d6cc35fbf5b924a1f8" dependencies = [ "async-compression", - "bitflags", + "bitflags 2.10.0", "bytes", "futures-core", "http", @@ -2683,7 +2786,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bb0e353e6a2fbdc176932bbaab493762eb1255a7900fe0fea1a2f96c296cc909" dependencies = [ "anyhow", - "indexmap", + "indexmap 2.12.1", "wasm-encoder", "wasmparser", ] @@ -2694,9 +2797,9 @@ version = "0.244.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "47b807c72e1bac69382b3a6fb3dbe8ea4c0ed87ff5629b8685ae6b9a611028fe" dependencies = [ - "bitflags", + "bitflags 2.10.0", "hashbrown 0.15.5", - "indexmap", + "indexmap 2.12.1", "semver", ] @@ -2802,7 +2905,7 @@ version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "193cae8e647981c35bc947fdd57ba7928b1fa0d4a79305f6dd2dc55221ac35ac" dependencies = [ - "bitflags", + "bitflags 2.10.0", "widestring", "windows-sys 0.59.0", ] @@ -3084,7 +3187,7 @@ checksum = "b7c566e0f4b284dd6561c786d9cb0142da491f46a9fbed79ea69cdad5db17f21" dependencies = [ "anyhow", "heck", - "indexmap", + "indexmap 2.12.1", "prettyplease", "syn", "wasm-metadata", @@ -3114,8 +3217,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9d66ea20e9553b30172b5e831994e35fbde2d165325bec84fc43dbf6f4eb9cb2" dependencies = [ "anyhow", - "bitflags", - "indexmap", + "bitflags 2.10.0", + "indexmap 2.12.1", "log", "serde", "serde_derive", @@ -3134,7 +3237,7 @@ checksum = "ecc8ac4bc1dc3381b7f59c34f00b67e18f910c2c0f50015669dde7def656a736" dependencies = [ "anyhow", "id-arena", - "indexmap", + "indexmap 2.12.1", "log", "semver", "serde", diff --git a/rust_http_proxy/Cargo.toml b/rust_http_proxy/Cargo.toml index c9dccb3..5cf53d1 100644 --- a/rust_http_proxy/Cargo.toml +++ b/rust_http_proxy/Cargo.toml @@ -40,7 +40,8 @@ pin-project-lite.workspace = true prometheus-client.workspace = true flate2 = { version = "1.0" } async-compression = { version = "0.4", features = ["tokio", "gzip"] } -clap = { version = "4.4", features = ["derive"] } +clap = { version = "4.4", features = ["derive", "env"] } +dotenvy = { version = "0", features = ["cli"] } base64 = "0.22" jemallocator = { version = "0.5", optional = true } mimalloc = { version = "0.1", optional = true } diff --git a/rust_http_proxy/src/config.rs b/rust_http_proxy/src/config.rs index ee3aa0f..b215480 100644 --- a/rust_http_proxy/src/config.rs +++ b/rust_http_proxy/src/config.rs @@ -14,36 +14,45 @@ use crate::{DynError, IDLE_TIMEOUT}; #[derive(Parser)] #[command(author, version=None, about, long_about = None)] pub struct Param { - #[arg(long, value_name = "LOG_DIR", default_value = "/tmp")] + #[arg(long, env = "LOG_DIR", value_name = "LOG_DIR", default_value = "/tmp")] pub log_dir: String, - #[arg(long, value_name = "LOG_FILE", default_value = "proxy.log")] + #[arg(long, env = "LOG_FILE", value_name = "LOG_FILE", default_value = "proxy.log")] pub log_file: String, #[arg( short, long, + env = "PORT", value_name = "PORT", default_value = "3128", help = "可以多次指定来实现多端口\n" )] port: Vec, - #[arg(short, long, value_name = "CERT", default_value = "cert.pem")] + #[arg(short, long, env = "CERT", value_name = "CERT", default_value = "cert.pem")] cert: String, - #[arg(short, long, value_name = "KEY", default_value = "privkey.pem")] + #[arg(short, long, env = "KEY", value_name = "KEY", default_value = "privkey.pem")] key: String, #[arg( short, long, + env = "USER", value_name = "USER", help = "默认为空,表示不鉴权。\n\ 格式为 'username:password'\n\ 可以多次指定来实现多用户" )] users: Vec, - #[arg(short, long, value_name = "WEB_CONTENT_PATH", help = "静态文件托管的根目录")] + #[arg( + short, + long, + env = "WEB_CONTENT_PATH", + value_name = "WEB_CONTENT_PATH", + help = "静态文件托管的根目录" + )] web_content_path: Option, #[arg( short, long, + env = "REFERER_KEYWORD_TO_SELF", value_name = "REFERER", help = "Http Referer请求头处理 \n\ 1. 图片资源的防盗链:针对png/jpeg/jpg等文件的请求,要求Request的Referer header要么为空,要么包含配置的值\n\ @@ -53,26 +62,34 @@ pub struct Param { referer_keywords_to_self: Vec, #[arg( long, + env = "NEVER_ASK_FOR_AUTH", help = "if enable, never send '407 Proxy Authentication Required' to client。\n\ 当作为正向代理使用时建议开启,否则有被嗅探的风险。" )] never_ask_for_auth: bool, #[arg( long, + env = "ALLOW_SERVING_NETWORK", value_name = "CIDR", help = "允许访问静态文件托管的网段白名单,格式为CIDR,例如: 192.168.1.0/24, 10.0.0.0/8\n\ 可以多次指定来允许多个网段\n\ 如未设置任何网段,则允许所有IP访问静态文件" )] allow_serving_network: Vec, - #[arg(short, long, help = "if enable, proxy server will listen on https")] + #[arg(short, long, env = "OVER_TLS", help = "if enable, proxy server will listen on https")] over_tls: bool, - #[arg(long, value_name = "FILE_PATH", help = r#"静态文件托管和反向代理的配置文件"#)] + #[arg( + long, + env = "LOCATION_CONFIG_FILE", + value_name = "FILE_PATH", + help = r#"静态文件托管和反向代理的配置文件"# + )] location_config_file: Option, - #[arg(long, help = r#"是否开启github proxy"#)] + #[arg(long, env = "ENABLE_GITHUB_PROXY", help = r#"是否开启github proxy"#)] enable_github_proxy: bool, #[arg( long, + env = "APPEND_UPSTREAM_URL", value_name = "https://example.com", help = "便捷反向代理配置\n\ 例如:--append-upstream-url=https://cdnjs.cloudflare.com\n\ @@ -81,12 +98,14 @@ pub struct Param { append_upstream_url: Vec, #[arg( long, + env = "FORWARD_BYPASS_URL", value_name = "https://username:password@example.com:123", help = "指定上游代理服务器" )] forward_bypass_url: Option, #[arg( long, + env = "IPV6_FIRST", help = "优先使用 IPv6 进行连接。true表示IPv6优先,false表示IPv4优先,不设置则保持DNS原始顺序" )] ipv6_first: Option, diff --git a/rust_http_proxy/src/main.rs b/rust_http_proxy/src/main.rs index dea0179..9765e3d 100644 --- a/rust_http_proxy/src/main.rs +++ b/rust_http_proxy/src/main.rs @@ -23,7 +23,11 @@ fn main() -> Result<(), DynError> { // 使用 _guard 进入 runtime 上下文,这样 create_futures 内部的 tokio::spawn 才能正常工作 let _guard = runtime.enter(); - let param = Param::parse(); + // 解析命令行参数和环境变量 + let param = { + dotenvy::dotenv().ok(); + Param::parse() + }; if let Err(log_init_error) = log_x::init_log(¶m.log_dir, ¶m.log_file, "info") { return Err(format!("init log error:{log_init_error}").into()); }