1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
use std::path::PathBuf;
use clap::{Parser, Subcommand, ValueEnum};
/// Convert mitmproxy/HAR captures to OpenAPI 3.0 specifications
#[derive(Parser, Debug)]
#[command(name = "mitm2openapi", version, about)]
pub struct Cli {
#[command(subcommand)]
pub command: Command,
}
#[derive(Subcommand, Debug)]
pub enum Command {
/// Discover API endpoints from captured traffic and produce a templates file
Discover(DiscoverArgs),
/// Generate an OpenAPI specification from captured traffic using a templates file
Generate(GenerateArgs),
}
/// Input format for traffic captures
#[derive(ValueEnum, Clone, Debug, Default)]
pub enum InputFormat {
/// Auto-detect format from file extension/content
#[default]
Auto,
/// HAR (HTTP Archive) format
Har,
/// mitmproxy flow dump format
Mitmproxy,
}
fn parse_byte_size(s: &str) -> Result<u64, String> {
let s = s.trim();
let (num_str, multiplier) = if let Some(n) = s.strip_suffix("GiB") {
(n.trim(), 1024 * 1024 * 1024u64)
} else if let Some(n) = s.strip_suffix("MiB") {
(n.trim(), 1024 * 1024u64)
} else if let Some(n) = s.strip_suffix("KiB") {
(n.trim(), 1024u64)
} else {
(s, 1u64)
};
num_str
.parse::<u64>()
.map(|n| n * multiplier)
.map_err(|e| format!("invalid size: {e}"))
}
#[derive(Parser, Debug)]
pub struct DiscoverArgs {
/// Input file or directory path
#[arg(short, long)]
pub input: PathBuf,
/// Output YAML file path for discovered templates
#[arg(short, long)]
pub output: PathBuf,
/// API prefix URL (e.g., "https://api.example.com")
#[arg(short, long)]
pub prefix: String,
/// Input format override
#[arg(long, value_enum, default_value_t = InputFormat::Auto)]
pub format: InputFormat,
/// Comma-separated glob patterns for paths to drop from output entirely
/// (e.g. "/static/**,/images/**,*.css,*.js").
/// Use `*` for a single path segment, `**` for any number of segments.
#[arg(long, value_delimiter = ',')]
pub exclude_patterns: Vec<String>,
/// Comma-separated glob patterns for paths to emit WITHOUT the `ignore:`
/// prefix (i.e. auto-activate for generate). Everything else still gets
/// `ignore:` so you can review it. Saves a manual sed step.
#[arg(long, value_delimiter = ',')]
pub include_patterns: Vec<String>,
#[arg(long, value_parser = parse_byte_size, default_value = "2GiB")]
pub max_input_size: u64,
#[arg(long, default_value_t = false)]
pub allow_symlinks: bool,
/// Treat warnings as errors. Exits non-zero if any cap fires,
/// flow is rejected, or parse diagnostic is emitted.
#[arg(long, default_value_t = false)]
pub strict: bool,
/// Write a structured JSON processing report to the given path
#[arg(long)]
pub report: Option<PathBuf>,
}
#[derive(Parser, Debug)]
pub struct GenerateArgs {
/// Input file or directory path
#[arg(short, long)]
pub input: PathBuf,
/// Templates YAML file path (from discover output)
#[arg(short, long)]
pub templates: PathBuf,
/// Output OpenAPI YAML file path
#[arg(short, long)]
pub output: PathBuf,
/// API prefix URL
#[arg(short, long)]
pub prefix: String,
/// Input format override
#[arg(long, value_enum, default_value_t = InputFormat::Auto)]
pub format: InputFormat,
/// Custom title for the OpenAPI spec
#[arg(long)]
pub openapi_title: Option<String>,
/// Custom version for the OpenAPI spec
#[arg(long, default_value = "1.0.0")]
pub openapi_version: String,
/// Comma-separated headers to exclude
#[arg(long)]
pub exclude_headers: Option<String>,
/// Comma-separated cookies to exclude
#[arg(long)]
pub exclude_cookies: Option<String>,
/// Include headers in the generated spec
#[arg(long)]
pub include_headers: bool,
/// Ignore image content types
#[arg(long)]
pub ignore_images: bool,
/// Suppress parameter suggestions
#[arg(long)]
pub suppress_params: bool,
/// JSON string for tag overrides
#[arg(long)]
pub tags_overrides: Option<String>,
#[arg(long, value_parser = parse_byte_size, default_value = "2GiB")]
pub max_input_size: u64,
#[arg(long, value_parser = parse_byte_size, default_value = "256MiB")]
pub max_payload_size: u64,
#[arg(long, default_value_t = 256)]
pub max_depth: usize,
#[arg(long, value_parser = parse_byte_size, default_value = "64MiB")]
pub max_body_size: u64,
#[arg(long, default_value_t = false)]
pub allow_symlinks: bool,
/// Treat warnings as errors. Exits non-zero if any cap fires,
/// flow is rejected, or parse diagnostic is emitted.
#[arg(long, default_value_t = false)]
pub strict: bool,
/// Write a structured JSON processing report to the given path
#[arg(long)]
pub report: Option<PathBuf>,
}