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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
mod cli_utils;
mod commands;
mod match_processor;
use anyhow::Result;
use clap::{Parser, Subcommand};
use std::path::PathBuf;
use commands::{
cmd_bench, cmd_build, cmd_extract, cmd_inspect, cmd_match, cmd_query, cmd_validate,
};
#[derive(Parser)]
#[command(name = "matchy")]
#[command(
about = "Unified database for IP addresses, string literals, and glob patterns",
long_about = "matchy - High-performance unified database for IP lookups, exact string matching, and glob pattern matching\n\n\
Build and query databases containing IP addresses (CIDR ranges), exact string literals, \n\
and glob patterns with wildcards. Uses memory-mapped files for fast, zero-copy queries.\n\n\
Features:\n\
• IP address lookups (IPv4/IPv6 with CIDR support)\n\
• Exact string matching (hash-based)\n\
• Multi-pattern glob matching (wildcards: *, ?, [abc], [!abc])\n\
• Extended MMDB format with backward compatibility\n\
• Zero-copy memory-mapped access\n\
• Attach custom metadata to any entry\n\n\
Examples:\n\
matchy build patterns.txt -o threats.mxy\n\
matchy query threats.mxy '192.168.1.1'\n\
matchy query threats.mxy 'evil.example.com'\n\
matchy inspect threats.mxy --verbose\n\
matchy validate threats.mxy --level strict"
)]
#[command(version)]
struct Cli {
#[command(subcommand)]
command: Commands,
}
#[derive(Subcommand)]
enum Commands {
/// Extract patterns (domains, IPs, emails) from log files or stdin
Extract {
/// Log files to process (one entry per line), or "-" for stdin
#[arg(value_name = "INPUT", required = true)]
inputs: Vec<PathBuf>,
/// Output format: json (default, NDJSON), csv, or text (one per line)
#[arg(long, default_value = "json")]
format: String,
/// Extraction types (comma-separated): ipv4, ipv6, ip, domain, email, all (default: all)
#[arg(long)]
types: Option<String>,
/// Minimum number of domain labels (default: 2 for example.com)
#[arg(long, default_value = "2")]
min_labels: usize,
/// Disable word boundary requirements (allow patterns in middle of text)
#[arg(long)]
no_boundaries: bool,
/// Output only unique patterns (deduplicate)
#[arg(short, long)]
unique: bool,
/// Show extraction statistics to stderr
#[arg(short, long)]
stats: bool,
/// Show candidate extraction details for debugging (to stderr)
#[arg(long)]
show_candidates: bool,
},
/// Match patterns against log files or stdin (operational testing)
Match {
/// Path to the matchy database (.mxy file, or .json/.csv source file)
/// When a JSON or CSV file is provided, matchy will automatically build
/// the database in-memory before matching.
#[arg(value_name = "DATABASE")]
database: PathBuf,
/// Log files to process (one entry per line), or "-" for stdin
#[arg(value_name = "INPUT", required = true)]
inputs: Vec<PathBuf>,
/// Follow log file(s) for new data (like tail -f)
#[arg(short = 'f', long)]
follow: bool,
/// Number of worker threads (default: auto-detect, use 1 for sequential)
/// "auto" or "0" uses all available CPU cores with auto-tuned reader/worker split
#[arg(short = 'j', long)]
threads: Option<String>,
/// Number of reader threads for I/O and decompression (default: auto-detect)
/// Only used with --threads > 1. Explicit value overrides auto-tuning.
/// Use more readers for compressed files (.gz). Example: --readers=4 --threads=12
#[arg(long)]
readers: Option<usize>,
/// Batch size in bytes for parallel mode (default: 131072 = 128KB)
#[arg(long, default_value = "131072")]
batch_bytes: usize,
/// Output format: json (default, NDJSON), or summary (statistics only)
#[arg(long, default_value = "json")]
format: String,
/// Show detailed statistics in stderr (extraction time, candidate breakdown, etc.)
#[arg(short, long)]
stats: bool,
/// Show live progress updates during processing (single-line updates if terminal)
#[arg(short, long)]
progress: bool,
/// LRU cache capacity per worker (default: 10000, use 0 to disable)
#[arg(long, default_value = "10000")]
cache_size: usize,
/// Enable/disable extractors (comma-separated): ipv4,ipv6,domain,email,hash,bitcoin,ethereum,monero
/// Prefix with '-' to disable (e.g., -domain,-email). Supports plurals (domains, hashes, emails)
/// Group aliases: 'crypto' (bitcoin+ethereum+monero), 'ip' (ipv4+ipv6)
/// Examples: --extractors=ip,domain --extractors=-crypto,-hash --extractors=-domains
/// Default: auto-detect from database capabilities
#[arg(long)]
extractors: Option<String>,
/// Show detailed routing decisions for each file (helpful for creating tests)
/// Outputs workload stats and per-file routing decisions to stderr
#[arg(long)]
debug_routing: bool,
/// Enable automatic reload when database file changes on disk
/// Database will be atomically swapped when changes are detected
#[arg(long)]
watch: bool,
/// Enable automatic updates from database's embedded URL (requires auto-update feature)
/// Database must have been built with --update-url to use this feature.
/// Downloads are stored in cache-dir (or ~/.cache/matchy/ by default).
#[cfg(feature = "auto-update")]
#[arg(long)]
auto_update: bool,
/// How often to check for remote updates, in seconds (default: 3600 = 1 hour)
/// Only used with --auto-update
#[cfg(feature = "auto-update")]
#[arg(long, default_value = "3600")]
update_interval: u64,
/// Cache directory for downloaded database updates
/// Only used with --auto-update. Default: ~/.cache/matchy/
#[cfg(feature = "auto-update")]
#[arg(long)]
cache_dir: Option<PathBuf>,
},
/// Query a pattern database
Query {
/// Path to the matchy database (.mxy file)
#[arg(value_name = "DATABASE")]
database: PathBuf,
/// Query string to match against patterns
#[arg(value_name = "QUERY")]
query: String,
/// Quiet mode - no output, only exit code (0 = found, 1 = not found)
#[arg(short, long)]
quiet: bool,
},
/// Inspect a pattern database
Inspect {
/// Path to the matchy database (.mxy file)
#[arg(value_name = "DATABASE")]
database: PathBuf,
/// Output metadata as JSON
#[arg(short, long)]
json: bool,
/// Show detailed statistics
#[arg(short, long)]
verbose: bool,
},
/// Build a unified database from patterns and/or IP addresses
Build {
/// Input files containing patterns, IP addresses, or MISP JSON (can specify multiple)
#[arg(value_name = "INPUT", required = true)]
inputs: Vec<PathBuf>,
/// Output database file (.mxy extension)
#[arg(short, long, value_name = "FILE")]
output: PathBuf,
/// Input file format (how to parse input files)
/// - text: One pattern per line (default)
/// - csv: Comma-separated values with 'entry' or 'key' column
/// - json: JSON array of {"key": "pattern", "data": {...}}
/// - misp: MISP threat intelligence JSON format
#[arg(short = 'f', long, default_value = "text", value_name = "FORMAT")]
format: String,
/// Custom database type name for metadata (e.g., "MyCompany-ThreatIntel")
/// This is NOT the input format - use --format/-f for that
#[arg(short = 't', long, value_name = "NAME")]
database_type: Option<String>,
/// Description text (can be specified multiple times with --desc-lang)
#[arg(short = 'd', long)]
description: Option<String>,
/// Language code for description (default: "en")
#[arg(long, default_value = "en")]
desc_lang: String,
/// Verbose output during build
#[arg(short, long)]
verbose: bool,
/// Show detailed debug output (entry processing)
#[arg(long)]
debug: bool,
/// Use case-insensitive matching for patterns (default: case-sensitive)
#[arg(short = 'i', long)]
case_insensitive: bool,
/// URL where updates to this database can be downloaded
/// Stored in metadata for use with auto-update functionality
#[arg(long, value_name = "URL")]
update_url: Option<String>,
},
/// Validate a database file for safety and correctness
Validate {
/// Path to the matchy database (.mxy file)
#[arg(value_name = "DATABASE")]
database: PathBuf,
/// Validation level: standard or strict (default)
#[arg(short, long, default_value = "strict")]
level: String,
/// Output results as JSON
#[arg(short, long)]
json: bool,
/// Show detailed information (warnings and info messages)
#[arg(short, long)]
verbose: bool,
},
/// Benchmark database performance (build, load, query)
Bench {
/// Type of database to benchmark: ip, literal, pattern, or combined
#[arg(value_name = "TYPE", default_value = "ip")]
db_type: String,
/// Number of entries to test with
#[arg(short = 'n', long, default_value = "1000000")]
count: usize,
/// Output file for the test database (temp file if not specified)
#[arg(short, long)]
output: Option<PathBuf>,
/// Keep the generated database file after benchmarking
#[arg(short, long)]
keep: bool,
/// Number of load iterations to average (default: 3)
#[arg(long, default_value = "3")]
load_iterations: usize,
/// Number of queries for batch benchmark (default: 100000)
#[arg(long, default_value = "100000")]
query_count: usize,
/// Percentage of queries that should match (0-100, default: 10)
#[arg(long, default_value = "10")]
hit_rate: usize,
/// LRU cache capacity (default: 10000, use 0 to disable)
#[arg(long, default_value = "10000")]
cache_size: usize,
/// Simulated cache hit rate percentage (0-100, default: 0 - all unique queries)
/// Set to 80-90 to simulate real-world log patterns where queries repeat
#[arg(long, default_value = "0")]
cache_hit_rate: usize,
/// Pattern style for pattern benchmarks: prefix, suffix, mixed, or complex (default: complex)
#[arg(long, default_value = "complex")]
pattern_style: String,
},
}
fn main() -> Result<()> {
let cli = Cli::parse();
match cli.command {
Commands::Extract {
inputs,
format,
types,
min_labels,
no_boundaries,
unique,
stats,
show_candidates,
} => cmd_extract(
&inputs,
&format,
types.as_deref(),
min_labels,
no_boundaries,
unique,
stats,
show_candidates,
),
Commands::Match {
database,
inputs,
follow,
threads,
readers,
batch_bytes,
format,
stats,
progress,
cache_size,
extractors,
debug_routing,
watch,
#[cfg(feature = "auto-update")]
auto_update,
#[cfg(feature = "auto-update")]
update_interval,
#[cfg(feature = "auto-update")]
cache_dir,
} => cmd_match(
&database,
&inputs,
follow,
threads.as_deref(),
readers,
batch_bytes,
&format,
stats,
progress,
cache_size,
extractors.as_deref(),
debug_routing,
watch,
#[cfg(feature = "auto-update")]
auto_update,
#[cfg(feature = "auto-update")]
update_interval,
#[cfg(feature = "auto-update")]
cache_dir.as_deref(),
),
Commands::Query {
database,
query,
quiet,
} => cmd_query(&database, &query, quiet),
Commands::Inspect {
database,
json,
verbose,
} => cmd_inspect(&database, json, verbose),
Commands::Validate {
database,
level,
json,
verbose,
} => cmd_validate(&database, &level, json, verbose),
Commands::Build {
inputs,
output,
format,
database_type,
description,
desc_lang,
verbose,
debug,
case_insensitive,
update_url,
} => cmd_build(
&inputs,
&output,
&format,
database_type.as_deref(),
description.as_deref(),
&desc_lang,
verbose,
debug,
case_insensitive,
update_url.as_deref(),
),
Commands::Bench {
db_type,
count,
output,
keep,
load_iterations,
query_count,
hit_rate,
cache_size,
cache_hit_rate,
pattern_style,
} => cmd_bench(
&db_type,
count,
output.as_deref(),
keep,
load_iterations,
query_count,
hit_rate,
cache_size,
cache_hit_rate,
&pattern_style,
),
}
}