瀏覽代碼

Update clap and use struct opts

Signed-off-by: zu1k <[email protected]>
zu1k 3 年之前
父節點
當前提交
9dd8b85763
共有 5 個文件被更改,包括 131 次插入120 次删除
  1. 74 25
      Cargo.lock
  2. 2 2
      Cargo.toml
  3. 2 0
      docs/rule/README.md
  4. 4 3
      docs/rule/modify.md
  5. 49 90
      src/main.rs

+ 74 - 25
Cargo.lock

@@ -32,15 +32,6 @@ dependencies = [
  "alloc-no-stdlib",
  "alloc-no-stdlib",
 ]
 ]
 
 
-[[package]]
-name = "ansi_term"
-version = "0.11.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b"
-dependencies = [
- "winapi",
-]
-
 [[package]]
 [[package]]
 name = "async-compression"
 name = "async-compression"
 version = "0.3.8"
 version = "0.3.8"
@@ -261,17 +252,33 @@ dependencies = [
 
 
 [[package]]
 [[package]]
 name = "clap"
 name = "clap"
-version = "2.33.3"
+version = "3.0.0-beta.5"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "37e58ac78573c40708d45522f0d80fa2f01cc4f9b4e2bf749807255454312002"
+checksum = "feff3878564edb93745d58cf63e17b63f24142506e7a20c87a5521ed7bfb1d63"
 dependencies = [
 dependencies = [
- "ansi_term",
  "atty",
  "atty",
  "bitflags",
  "bitflags",
+ "clap_derive",
+ "indexmap",
+ "lazy_static",
+ "os_str_bytes",
  "strsim",
  "strsim",
+ "termcolor",
  "textwrap",
  "textwrap",
- "unicode-width",
- "vec_map",
+ "unicase",
+]
+
+[[package]]
+name = "clap_derive"
+version = "3.0.0-beta.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8b15c6b4f786ffb6192ffe65a36855bc1fc2444bcd0945ae16748dcd6ed7d0d3"
+dependencies = [
+ "heck",
+ "proc-macro-error",
+ "proc-macro2",
+ "quote",
+ "syn",
 ]
 ]
 
 
 [[package]]
 [[package]]
@@ -630,7 +637,7 @@ checksum = "9b919933a397b79c37e33b77bb2aa3dc8eb6e165ad809e58ff75bc7db2e34574"
 
 
 [[package]]
 [[package]]
 name = "good-mitm"
 name = "good-mitm"
-version = "0.2.1"
+version = "0.2.2"
 dependencies = [
 dependencies = [
  "bytes",
  "bytes",
  "clap",
  "clap",
@@ -697,6 +704,15 @@ dependencies = [
  "http",
  "http",
 ]
 ]
 
 
+[[package]]
+name = "heck"
+version = "0.3.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "6d621efb26863f0e9924c6ac577e8275e5e6b77455db64ffa6c65c904e9e132c"
+dependencies = [
+ "unicode-segmentation",
+]
+
 [[package]]
 [[package]]
 name = "hermit-abi"
 name = "hermit-abi"
 version = "0.1.19"
 version = "0.1.19"
@@ -1132,6 +1148,15 @@ version = "0.1.4"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "28988d872ab76095a6e6ac88d99b54fd267702734fd7ffe610ca27f533ddb95a"
 checksum = "28988d872ab76095a6e6ac88d99b54fd267702734fd7ffe610ca27f533ddb95a"
 
 
+[[package]]
+name = "os_str_bytes"
+version = "4.2.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "addaa943333a514159c80c97ff4a93306530d965d27e139188283cd13e06a799"
+dependencies = [
+ "memchr",
+]
+
 [[package]]
 [[package]]
 name = "parking"
 name = "parking"
 version = "2.0.0"
 version = "2.0.0"
@@ -1240,6 +1265,30 @@ version = "0.2.15"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "ed0cfbc8191465bed66e1718596ee0b0b35d5ee1f41c5df2189d0fe8bde535ba"
 checksum = "ed0cfbc8191465bed66e1718596ee0b0b35d5ee1f41c5df2189d0fe8bde535ba"
 
 
+[[package]]
+name = "proc-macro-error"
+version = "1.0.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c"
+dependencies = [
+ "proc-macro-error-attr",
+ "proc-macro2",
+ "quote",
+ "syn",
+ "version_check",
+]
+
+[[package]]
+name = "proc-macro-error-attr"
+version = "1.0.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "version_check",
+]
+
 [[package]]
 [[package]]
 name = "proc-macro-hack"
 name = "proc-macro-hack"
 version = "0.5.19"
 version = "0.5.19"
@@ -1676,9 +1725,9 @@ checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d"
 
 
 [[package]]
 [[package]]
 name = "strsim"
 name = "strsim"
-version = "0.8.0"
+version = "0.10.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
+checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
 
 
 [[package]]
 [[package]]
 name = "syn"
 name = "syn"
@@ -1716,9 +1765,9 @@ dependencies = [
 
 
 [[package]]
 [[package]]
 name = "textwrap"
 name = "textwrap"
-version = "0.11.0"
+version = "0.14.2"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060"
+checksum = "0066c8d12af8b5acd21e00547c3797fde4e8677254a7ee429176ccebbe93dd80"
 dependencies = [
 dependencies = [
  "unicode-width",
  "unicode-width",
 ]
 ]
@@ -1940,6 +1989,12 @@ dependencies = [
  "tinyvec",
  "tinyvec",
 ]
 ]
 
 
+[[package]]
+name = "unicode-segmentation"
+version = "1.8.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8895849a949e7845e06bd6dc1aa51731a103c42707010a5b591c0038fb73385b"
+
 [[package]]
 [[package]]
 name = "unicode-width"
 name = "unicode-width"
 version = "0.1.9"
 version = "0.1.9"
@@ -1985,12 +2040,6 @@ dependencies = [
  "getrandom",
  "getrandom",
 ]
 ]
 
 
-[[package]]
-name = "vec_map"
-version = "0.8.2"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"
-
 [[package]]
 [[package]]
 name = "version_check"
 name = "version_check"
 version = "0.9.3"
 version = "0.9.3"

+ 2 - 2
Cargo.toml

@@ -1,6 +1,6 @@
 [package]
 [package]
 name = "good-mitm"
 name = "good-mitm"
-version = "0.2.1"
+version = "0.2.2"
 authors = ["zu1k <[email protected]>"]
 authors = ["zu1k <[email protected]>"]
 edition = "2021"
 edition = "2021"
 description = "Use MITM technology to provide features like rewrite, redirect."
 description = "Use MITM technology to provide features like rewrite, redirect."
@@ -18,7 +18,7 @@ codegen-units = 1
 
 
 [dependencies]
 [dependencies]
 bytes = { version = "1", features = ["serde"] }
 bytes = { version = "1", features = ["serde"] }
-clap = "2.33"
+clap = "3.0.0-beta.5"
 env_logger = "0.9"
 env_logger = "0.9"
 fancy-regex = "0.7"
 fancy-regex = "0.7"
 http_mitm = { path = "http_mitm" }
 http_mitm = { path = "http_mitm" }

+ 2 - 0
docs/rule/README.md

@@ -7,9 +7,11 @@
 - `规则名`:用来区分不同的规则,便与维护
 - `规则名`:用来区分不同的规则,便与维护
 - [`筛选器`](rule/filter.md):用于从众多`请求`和`返回`中筛选出需要处理的内容
 - [`筛选器`](rule/filter.md):用于从众多`请求`和`返回`中筛选出需要处理的内容
 - [`动作`](rule/action.md):用于执行想要的行为,包括`重定向`、`阻断`、`修改`等
 - [`动作`](rule/action.md):用于执行想要的行为,包括`重定向`、`阻断`、`修改`等
+- 必要时指定需要MITM的域名
 
 
 ```yaml
 ```yaml
 - name: "屏蔽Yutube追踪"
 - name: "屏蔽Yutube追踪"
+  mitm: "*.youtube.com"
   filter:
   filter:
     url-regex: '^https?:\/\/(www|s)\.youtube\.com\/(pagead|ptracking)'
     url-regex: '^https?:\/\/(www|s)\.youtube\.com\/(pagead|ptracking)'
   action: reject
   action: reject

+ 4 - 3
docs/rule/modify.md

@@ -31,11 +31,12 @@
 
 
 ```yaml
 ```yaml
 - name: "奈菲影视去广告"
 - name: "奈菲影视去广告"
-  filter:
+  mitm: "*nfmovies*"
+  filters:
     url-regex: '(nfmovies)(?!.*?(\.css|\.js|\.jpeg|\.png|\.gif)).*'
     url-regex: '(nfmovies)(?!.*?(\.css|\.js|\.jpeg|\.png|\.gif)).*'
-  action:
+  actions:
     modify-response:
     modify-response:
       body:
       body:
         origin: '<head>'
         origin: '<head>'
-        new: '<link rel="stylesheet" href="https://limbopro.com/CSS/nfmovies.css" type="text/css"><script type="text javascript"  src="//limbopro.com/Adguard/nfmovies.js"></script></head>'
+        new: '<link rel="stylesheet" href="https://limbopro.com/CSS/nfmovies.css" type="text/css"><script type="text/javascript"  src="//limbopro.com/Adguard/nfmovies.js"></script></head>'
 ```
 ```

+ 49 - 90
src/main.rs

@@ -1,5 +1,3 @@
-#![allow(dead_code)]
-
 #[macro_use]
 #[macro_use]
 extern crate lazy_static;
 extern crate lazy_static;
 
 
@@ -7,7 +5,7 @@ mod ca;
 mod handler;
 mod handler;
 mod rule;
 mod rule;
 
 
-use clap::{App, Arg, SubCommand};
+use clap::Parser;
 use http_mitm::*;
 use http_mitm::*;
 use log::*;
 use log::*;
 use rustls_pemfile as pemfile;
 use rustls_pemfile as pemfile;
@@ -19,6 +17,54 @@ async fn shutdown_signal() {
         .expect("failed to install CTRL+C signal handler");
         .expect("failed to install CTRL+C signal handler");
 }
 }
 
 
+#[derive(Parser)]
+#[clap(name = "Good Man in the Middle", version, about, author)]
+struct AppOpts {
+    #[clap(subcommand)]
+    subcmd: SubCommand,
+}
+
+#[derive(Parser)]
+enum SubCommand {
+    /// run proxy serve
+    Run(Run),
+    /// gen your own ca cert and private key
+    Genca,
+}
+
+#[derive(Parser)]
+struct Run {
+    #[clap(
+        short,
+        long,
+        default_value = "ca/private.key",
+        about = "private key file path"
+    )]
+    key: String,
+    #[clap(short, long, default_value = "ca/cert.crt", about = "cert file path")]
+    cert: String,
+    #[clap(short, long, about = "load rules from file or dir")]
+    rule: String,
+    #[clap(short, long, default_value = "127.0.0.1:34567", about = "bind address")]
+    bind: String,
+}
+
+fn main() {
+    env_logger::builder().filter_level(LevelFilter::Info).init();
+
+    let opts = AppOpts::parse();
+    match opts.subcmd {
+        SubCommand::Run(opts) => {
+            if let Err(err) = rule::add_rules_from_fs(opts.rule) {
+                error!("parse rule file failed, err: {}", err);
+                std::process::exit(3);
+            }
+            run(&opts.key, &opts.cert, &opts.bind);
+        }
+        SubCommand::Genca => ca::gen_ca(),
+    }
+}
+
 #[tokio::main]
 #[tokio::main]
 async fn run(key_path: &str, cert_path: &str, bind: &str) {
 async fn run(key_path: &str, cert_path: &str, bind: &str) {
     let private_key_bytes = fs::read(key_path).expect("ca private key file path not valid!");
     let private_key_bytes = fs::read(key_path).expect("ca private key file path not valid!");
@@ -55,90 +101,3 @@ async fn run(key_path: &str, cert_path: &str, bind: &str) {
         error!("{}", e);
         error!("{}", e);
     }
     }
 }
 }
-
-fn main() {
-    env_logger::builder().filter_level(LevelFilter::Info).init();
-
-    let matches = App::new("Good Man in the Middle")
-        .author("zu1k <[email protected]>")
-        .about("Use MITM technology to provide features like rewrite, redirect.")
-        .subcommand(
-            SubCommand::with_name("run")
-                .about("start to run")
-                .display_order(1)
-                .arg(
-                    Arg::with_name("key")
-                        .short("k")
-                        .long("key")
-                        .alias("private")
-                        .help("private key file path")
-                        .long_help("private key file path")
-                        .default_value("ca/private.key")
-                        .takes_value(true)
-                        .required(true),
-                )
-                .arg(
-                    Arg::with_name("cert")
-                        .short("c")
-                        .long("cert")
-                        .help("cert file path")
-                        .long_help("cert file path")
-                        .default_value("ca/cert.crt")
-                        .takes_value(true)
-                        .required(true),
-                )
-                .arg(
-                    Arg::with_name("rule")
-                        .short("r")
-                        .long("rule")
-                        .help("rule file or dir")
-                        .long_help("load rules from file or dir")
-                        .takes_value(true)
-                        .required(true),
-                )
-                .arg(
-                    Arg::with_name("bind")
-                        .short("b")
-                        .long("bind")
-                        .help("bind address")
-                        .long_help("bind address")
-                        .default_value("127.0.0.1:34567")
-                        .takes_value(true)
-                        .required(true),
-                ),
-        )
-        .subcommand(
-            SubCommand::with_name("genca")
-                .display_order(2)
-                .about("generate your own ca private key and certificate"),
-        )
-        .get_matches();
-
-    match matches.subcommand_name() {
-        Some("run") => {
-            let matches = matches.subcommand_matches("run").unwrap();
-            let rule_file_or_dir = matches
-                .value_of("rule")
-                .expect("rule file path should not be none");
-            let bind = matches
-                .value_of("bind")
-                .expect("bind address should not be none");
-            if let Err(err) = rule::add_rules_from_fs(rule_file_or_dir) {
-                error!("parse rule file failed, err: {}", err);
-                std::process::exit(3);
-            }
-
-            let key_path = matches
-                .value_of("key")
-                .expect("need root ca private key file");
-
-            let cert_path = matches.value_of("cert").expect("need root ca cert file");
-
-            run(key_path, cert_path, bind)
-        }
-        Some("genca") => ca::gen_ca(),
-        _ => {
-            println!("subcommand not valid!")
-        }
-    }
-}