Skip to content

Commit

Permalink
Update clap and use struct opts
Browse files Browse the repository at this point in the history
Signed-off-by: zu1k <i@lgf.im>
  • Loading branch information
zu1k committed Nov 21, 2021
1 parent 5bf62ec commit 9dd8b85
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 120 deletions.
99 changes: 74 additions & 25 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "good-mitm"
version = "0.2.1"
version = "0.2.2"
authors = ["zu1k <i@lgf.im>"]
edition = "2021"
description = "Use MITM technology to provide features like rewrite, redirect."
Expand All @@ -18,7 +18,7 @@ codegen-units = 1

[dependencies]
bytes = { version = "1", features = ["serde"] }
clap = "2.33"
clap = "3.0.0-beta.5"
env_logger = "0.9"
fancy-regex = "0.7"
http_mitm = { path = "http_mitm" }
Expand Down
2 changes: 2 additions & 0 deletions docs/rule/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
- `规则名`:用来区分不同的规则,便与维护
- [`筛选器`](rule/filter.md):用于从众多`请求``返回`中筛选出需要处理的内容
- [`动作`](rule/action.md):用于执行想要的行为,包括`重定向``阻断``修改`
- 必要时指定需要MITM的域名

```yaml
- name: "屏蔽Yutube追踪"
mitm: "*.youtube.com"
filter:
url-regex: '^https?:\/\/(www|s)\.youtube\.com\/(pagead|ptracking)'
action: reject
Expand Down
7 changes: 4 additions & 3 deletions docs/rule/modify.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@
```yaml
- name: "奈菲影视去广告"
filter:
mitm: "*nfmovies*"
filters:
url-regex: '(nfmovies)(?!.*?(\.css|\.js|\.jpeg|\.png|\.gif)).*'
action:
actions:
modify-response:
body:
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>'
```
139 changes: 49 additions & 90 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
#![allow(dead_code)]

#[macro_use]
extern crate lazy_static;

mod ca;
mod handler;
mod rule;

use clap::{App, Arg, SubCommand};
use clap::Parser;
use http_mitm::*;
use log::*;
use rustls_pemfile as pemfile;
Expand All @@ -19,6 +17,54 @@ async fn shutdown_signal() {
.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]
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!");
Expand Down Expand Up @@ -55,90 +101,3 @@ async fn run(key_path: &str, cert_path: &str, bind: &str) {
error!("{}", e);
}
}

fn main() {
env_logger::builder().filter_level(LevelFilter::Info).init();

let matches = App::new("Good Man in the Middle")
.author("zu1k <i@lgf.im>")
.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!")
}
}
}

1 comment on commit 9dd8b85

@vercel
Copy link

@vercel vercel bot commented on 9dd8b85 Nov 21, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.