whois-cli 0.3.2

A simple WHOIS query tool
Documentation
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
use colored::*;

#[derive(Debug, Clone, Copy)]
pub enum ColorScheme {
    Ripe,
    BgpTools,
    Mtf,
    None,
}

pub struct OutputColorizer;

impl OutputColorizer {
    /// Detect the appropriate color scheme for the output
    pub fn detect_scheme(output: &str) -> ColorScheme {
        if Self::is_bgp_tools_format(output) {
            ColorScheme::BgpTools
        } else {
            ColorScheme::Ripe
        }
    }

    /// Apply colorization based on the scheme
    pub fn colorize(output: &str, scheme: ColorScheme) -> String {
        match scheme {
            ColorScheme::Ripe => Self::colorize_ripe(output),
            ColorScheme::BgpTools => Self::colorize_bgptools(output),
            ColorScheme::Mtf => Self::colorize_mtf(output),
            ColorScheme::None => output.to_string(),
        }
    }

    /// Detect if the output is in BGP Tools format
    fn is_bgp_tools_format(output: &str) -> bool {
        let lines: Vec<&str> = output.lines().collect();
        if lines.len() >= 2 {
            let first_line = lines[0].trim();
            return first_line.contains("AS") && 
                   first_line.contains("|") && 
                   (first_line.contains("BGP") || 
                    first_line.contains("CC") || 
                    first_line.contains("Registry"));
        }
        false
    }

    /// Colorize RIPE format output (field: value pairs)
    fn colorize_ripe(output: &str) -> String {
        let mut colored_lines = Vec::new();
        let mut in_comment_block = false;
        
        for line in output.lines() {
            // Handle comment lines
            if line.starts_with('%') || line.starts_with('#') || line.starts_with("remarks:") {
                colored_lines.push(line.bright_black().to_string());
                in_comment_block = true;
                continue;
            }
            
            // Comment block state management
            if in_comment_block && line.trim().is_empty() {
                colored_lines.push(line.to_string());
                continue;
            }
            
            if in_comment_block && !line.trim().is_empty() {
                in_comment_block = false;
            }
            
            // Handle empty lines
            if line.trim().is_empty() {
                colored_lines.push(line.to_string());
                continue;
            }
            
            // Handle field: value pairs
            if line.contains(':') {
                if let Some(colored_line) = Self::colorize_field_value_pair(line) {
                    colored_lines.push(colored_line);
                    continue;
                }
            }
            
            // Handle special cases
            colored_lines.push(Self::colorize_special_lines(line));
        }
        
        colored_lines.join("\n")
    }

    /// Colorize a field: value pair
    fn colorize_field_value_pair(line: &str) -> Option<String> {
        let parts: Vec<&str> = line.splitn(2, ':').collect();
        if parts.len() != 2 {
            return None;
        }

        let field = parts[0].trim();
        let value = parts[1].trim();
        
        let colored_field = Self::colorize_field_name(field);
        let colored_value = Self::colorize_field_value(field, value);
        
        Some(format!("{}: {}", colored_field, colored_value))
    }

    /// Colorize field names based on their type
    fn colorize_field_name(field: &str) -> String {
        match field.to_lowercase().as_str() {
            // Network and AS fields
            "aut-num" | "as-block" | "inet6num" | "inetnum" | "route" | "route6" | "netname" =>
                field.bright_cyan().to_string(),
            
            // Domain fields
            "domain" | "domain name" =>
                field.bright_cyan().bold().to_string(),
            
            // DNS fields
            "nserver" | "name server" | "nameserver" | "name servers" =>
                field.yellow().bold().to_string(),
            
            // Status fields
            "domain status" | "status" =>
                field.bright_yellow().to_string(),
            
            // Registrar fields
            "registrar" | "sponsoring registrar" | "registrar iana id" | "reseller" =>
                field.bright_blue().to_string(),
            
            // Registry fields
            "registry domain id" | "registrar whois server" | "registrar url" =>
                field.blue().to_string(),
            
            // Date fields
            "creation date" | "created" | "created on" | "registration date" |
            "updated date" | "last modified" | "last update" | "changed" |
            "expiration date" | "expiry date" | "registry expiry date" | 
            "registrar registration expiration date" =>
                field.bright_magenta().to_string(),
            
            // Privacy fields
            "privacy" | "whois privacy" | "domain privacy" =>
                field.bright_red().to_string(),
            
            // Name fields
            "as-name" | "org-name" | "role" | "person" | "registrant name" | 
            "admin name" | "tech name" =>
                field.bright_green().to_string(),
            
            // Organization fields
            "org" | "organisation" | "org-type" | "registrant organization" | "registrant" =>
                field.yellow().to_string(),
            
            // Contact fields
            "admin-c" | "tech-c" | "abuse-c" | "nic-hdl" | "abuse-mailbox" |
            "registrant contact" | "admin contact" | "technical contact" | "billing contact" =>
                field.green().to_string(),
            
            // Maintainer fields
            "mnt-by" | "mnt-ref" | "mnt-domains" | "mnt-lower" | "mnt-routes" =>
                field.bright_blue().to_string(),
            
            // Routing fields
            "import" | "export" | "mp-import" | "mp-export" | "default" | "peer" =>
                field.magenta().to_string(),
            
            // Location fields
            "country" | "address" | "source" | "registrant country" | 
            "admin country" | "tech country" =>
                field.bright_white().to_string(),
            
            // Communication fields
            "e-mail" | "email" | "phone" | "registrant email" | "admin email" | "tech email" =>
                field.blue().to_string(),
            
            // DNSSEC fields
            "dnssec" | "ds record" =>
                field.magenta().bold().to_string(),
            
            // Special org field
            "sponsoring-org" =>
                field.bright_yellow().to_string(),
            
            // Default
            _ => field.white().to_string(),
        }
    }

    /// Colorize field values based on content and context
    fn colorize_field_value(field: &str, value: &str) -> String {
        let field_lower = field.to_lowercase();
        
        // Domain names
        if field_lower == "domain" || field_lower == "domain name" {
            return value.bright_white().bold().to_string();
        }
        
        // AS Numbers
        if field_lower == "aut-num" {
            return value.bright_red().bold().to_string();
        }
        
        // Status values
        if field_lower == "status" || field_lower == "domain status" {
            return Self::colorize_status_value(value);
        }
        
        // Source registry
        if field_lower == "source" {
            return value.bright_blue().to_string();
        }
        
        // Country codes
        if field_lower == "country" || field_lower.contains("country") {
            return value.yellow().to_string();
        }
        
        // Name servers
        if field_lower.contains("name server") || field_lower.contains("nserver") || 
           field_lower == "nameserver" {
            return value.bright_green().to_string();
        }
        
        // Registrar information
        if field_lower.contains("registrar") {
            return value.bright_blue().bold().to_string();
        }
        
        // DNSSEC status
        if field_lower.contains("dnssec") {
            return if value.to_lowercase().contains("signed") || value.to_lowercase().contains("yes") {
                value.bright_green().to_string()
            } else {
                value.bright_red().to_string()
            };
        }
        
        // Dates
        if field_lower.contains("date") || field_lower.contains("created") || 
           field_lower.contains("changed") || field_lower.contains("expir") || 
           field_lower.contains("update") {
            return value.bright_magenta().to_string();
        }
        
        // Email addresses
        if value.contains('@') {
            return value.bright_yellow().to_string();
        }
        
        // Phone numbers
        if field_lower.contains("phone") {
            return value.bright_white().to_string();
        }
        
        // AS numbers in values
        if value.starts_with("AS") && value.len() > 2 && value[2..].chars().all(|c| c.is_digit(10)) {
            return value.bright_red().to_string();
        }
        
        // Import/Export specialized coloring
        if (field == "import" || field == "export") && value.contains("AS") {
            return Self::colorize_routing_policy(value);
        }
        
        // IP addresses and CIDR blocks
        if Self::looks_like_ip_or_cidr(value) {
            return value.bright_cyan().to_string();
        }
        
        // Maintainer values
        if field.starts_with("mnt-") {
            return if value.contains("-") {
                value.bright_blue().to_string()
            } else {
                value.white().to_string()
            };
        }
        
        // Names
        if field_lower == "as-name" || field_lower == "org-name" || field_lower == "netname" {
            return value.bright_white().bold().to_string();
        }
        
        // Person/role names
        if field_lower == "role" || field_lower == "person" || 
           field_lower.contains("registrant name") {
            return value.bright_green().bold().to_string();
        }
        
        // Handles
        if field.ends_with("-c") {
            return value.green().to_string();
        }
        
        // Default
        value.white().to_string()
    }

    /// Colorize status values
    fn colorize_status_value(value: &str) -> String {
        match value.to_uppercase().as_str() {
            "ASSIGNED" | "ALLOCATED" => value.bright_green().to_string(),
            "AVAILABLE" => value.bright_cyan().to_string(),
            "RESERVED" => value.yellow().to_string(),
            "CLIENT DELETE PROHIBITED" | "CLIENT TRANSFER PROHIBITED" | 
            "CLIENT UPDATE PROHIBITED" => value.bright_yellow().to_string(),
            "INACTIVE" | "PENDING DELETE" => value.bright_red().to_string(),
            "OK" | "ACTIVE" | "CLIENT OK" => value.bright_green().to_string(),
            _ => value.bright_yellow().to_string(),
        }
    }

    /// Colorize routing policy lines (import/export)
    fn colorize_routing_policy(value: &str) -> String {
        let mut colored_parts = Vec::new();
        let parts: Vec<&str> = value.split_whitespace().collect();
        
        for part in parts {
            if part.starts_with("AS") && part.len() > 2 && part[2..].chars().all(|c| c.is_digit(10)) {
                colored_parts.push(part.bright_red().to_string());
            } else if matches!(part, "from" | "to" | "accept" | "announce") {
                colored_parts.push(part.bright_cyan().to_string());
            } else {
                colored_parts.push(part.white().to_string());
            }
        }
        
        colored_parts.join(" ")
    }

    /// Check if a string looks like an IP address or CIDR block
    fn looks_like_ip_or_cidr(value: &str) -> bool {
        value.chars().all(|c| c.is_digit(10) || c == '.' || c == ':' || c == '/')
    }

    /// Colorize special lines (errors, availability, etc.)
    fn colorize_special_lines(line: &str) -> String {
        let line_lower = line.to_lowercase();
        
        if line_lower.contains("error") || line_lower.contains("not found") || 
           line_lower.contains("no match") {
            line.bright_red().to_string()
        } else if line_lower.contains("available") {
            line.bright_green().to_string()
        } else {
            line.white().to_string()
        }
    }

    /// Colorize BGP Tools format output (table format)
    fn colorize_bgptools(output: &str) -> String {
        let lines: Vec<&str> = output.lines().collect();
        let mut colored_lines = Vec::new();
        let mut headers: Vec<&str> = Vec::new();
        
        for (i, line) in lines.iter().enumerate() {
            if line.trim().is_empty() {
                colored_lines.push("".to_string());
                continue;
            }
            
            // Process header row
            if i == 0 || (i == 1 && lines[0].trim().is_empty()) {
                headers = line.split('|').map(|s| s.trim()).collect();
                let colored_headers: Vec<String> = headers.iter()
                    .map(|&header| header.bright_cyan().bold().to_string())
                    .collect();
                colored_lines.push(colored_headers.join(" | "));
                continue;
            }
            
            // Process data rows
            let fields: Vec<&str> = line.split('|').map(|s| s.trim()).collect();
            let mut colored_fields = Vec::new();
            
            for (j, field) in fields.iter().enumerate() {
                let header = if j < headers.len() { headers[j] } else { "" };
                
                let colored_field = match header {
                    "AS" => field.bright_red().to_string(),
                    "IP" | "BGP Prefix" => field.bright_cyan().to_string(),
                    "CC" => field.bright_yellow().to_string(),
                    "Registry" => field.bright_blue().to_string(),
                    "Allocated" => field.bright_magenta().to_string(),
                    "AS Name" => field.bright_white().bold().to_string(),
                    _ => field.white().to_string(),
                };
                
                colored_fields.push(colored_field);
            }
            
            colored_lines.push(colored_fields.join(" | "));
        }
        
        colored_lines.join("\n")
    }

    /// MTF flag coloring (trans flag pattern)
    fn colorize_mtf(output: &str) -> String {
        let mut colored_lines = Vec::new();
        let mut line_count = 0;
        
        for line in output.lines() {
            if line.trim().is_empty() {
                colored_lines.push(line.to_string());
                continue;
            }
            
            // Trans flag pattern: blue, pink, white, pink, blue
            let colored_line = match line_count % 5 {
                0 => line.truecolor(91, 207, 250).to_string(),   // Blue #5BCFFA
                1 => line.truecolor(245, 171, 185).to_string(),  // Pink #F5ABB9
                2 => line.truecolor(255, 255, 255).to_string(),  // White #FFFFFF
                3 => line.truecolor(245, 171, 185).to_string(),  // Pink #F5ABB9
                4 => line.truecolor(91, 207, 250).to_string(),   // Blue #5BCFFA
                _ => unreachable!(),
            };
            
            colored_lines.push(colored_line);
            line_count += 1;
        }
        
        colored_lines.join("\n")
    }
}