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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
use regex::Regex;
use std::env;
use urlencoding::encode;

/// Represents Regional Internet Registry URLs
pub struct RirUrls;

impl RirUrls {
    /// Get the appropriate URL for a given RIR and search term
    pub fn get_url(rir: &str, search_term: &str) -> String {
        let encoded_term = encode(search_term);
        
        match rir.to_uppercase().as_str() {
            "RIPE" => format!("https://apps.db.ripe.net/db-web-ui/query?searchtext={}", encoded_term),
            "ARIN" => format!("https://search.arin.net/rdap/?query={}", encoded_term),
            "APNIC" => format!("https://wq.apnic.net/apnic-bin/whois.pl?searchtext={}", encoded_term),
            "LACNIC" => {
                // LACNIC uses a different parameter format
                format!("https://query.milacnic.lacnic.net/home?searchtext={}", encoded_term)
            },
            "AFRINIC" => format!("https://afrinic.net/whois?searchtext={}", encoded_term),
            _ => {
                // Fallback to RIPE for unknown RIRs
                format!("https://apps.db.ripe.net/db-web-ui/query?searchtext={}", encoded_term)
            }
        }
    }
}

/// Detect RIR from source field - more accurate than content-based detection
pub fn detect_rir_from_source(response: &str) -> Vec<&'static str> {
    let mut rirs = Vec::new();
    
    // Use regex to find all source fields
    let source_regex = Regex::new(r"(?m)^source:\s*([A-Z-]+)").unwrap();
    
    for caps in source_regex.captures_iter(response) {
        if let Some(source) = caps.get(1) {
            let source_value = source.as_str().trim();
            let rir = match source_value {
                "RIPE" => Some("ripe"),
                "ARIN" => Some("arin"),
                "APNIC" => Some("apnic"),
                "LACNIC" => Some("lacnic"),
                "AFRINIC" => Some("afrinic"),
                _ => None,
            };
            
            if let Some(rir) = rir {
                if !rirs.contains(&rir) {
                    rirs.push(rir);
                }
            }
        }
    }
    
    rirs
}

/// Legacy function - detect which RIR the response is from (fallback method)
pub fn detect_rir(response: &str) -> Option<&'static str> {
    // First try source-based detection
    let rirs = detect_rir_from_source(response);
    if !rirs.is_empty() {
        return Some(rirs[0]);
    }
    
    // Fallback to content-based detection
    if response.contains("% This is the RIPE Database query service") ||
       response.contains("whois.ripe.net") ||
       response.contains("RIPE-NCC") {
        return Some("ripe");
    }

    if response.contains("American Registry for Internet Numbers") ||
       response.contains("ARIN WHOIS data") ||
       response.contains("NetRange:") ||
       response.contains("whois.arin.net") {
        return Some("arin");
    }

    if response.contains("Asia Pacific Network Information Centre") ||
       response.contains("APNIC WHOIS Database") ||
       response.contains("whois.apnic.net") {
        return Some("apnic");
    }

    if response.contains("Latin American and Caribbean IP address Regional Registry") ||
       response.contains("LACNIC WHOIS") ||
       response.contains("whois.lacnic.net") {
        return Some("lacnic");
    }

    if response.contains("African Network Information Centre") ||
       response.contains("AFRINIC WHOIS") ||
       response.contains("whois.afrinic.net") {
        return Some("afrinic");
    }

    None
}

/// Check if the WHOIS response is from any RIR
pub fn is_rir_response(response: &str) -> bool {
    !detect_rir_from_source(response).is_empty() || detect_rir(response).is_some()
}

/// Check if the WHOIS response is from RIPE NCC
pub fn is_ripe_response(response: &str) -> bool {
    detect_rir_from_source(response).contains(&"ripe") || detect_rir(response) == Some("ripe")
}

/// Check if terminal supports hyperlinks (OSC 8) - improved Windows detection
pub fn terminal_supports_hyperlinks() -> bool {
    // Check for Windows Terminal first (most reliable)
    if env::var("WT_SESSION").is_ok() || env::var("WT_PROFILE_ID").is_ok() {
        return true;
    }

    // Check for PowerShell with Windows Terminal
    if env::var("TERM_PROGRAM").map_or(false, |term| term == "vscode") {
        return true;
    }

    // Check common environment variables that indicate hyperlink support
    if let Ok(term) = env::var("TERM") {
        // These terminals are known to support OSC 8
        if term.contains("xterm") || 
           term.contains("screen") || 
           term.contains("tmux") ||
           term == "alacritty" ||
           term == "kitty" ||
           term == "foot" ||
           term.contains("256color") {
            return true;
        }
    }

    // Check for VTE-based terminals (GNOME Terminal, etc.)
    if env::var("VTE_VERSION").is_ok() {
        return true;
    }

    // Check for iTerm2
    if env::var("ITERM_SESSION_ID").is_ok() || env::var("TERM_PROGRAM").map_or(false, |term| term == "iTerm.app") {
        return true;
    }

    // Check for WezTerm
    if env::var("WEZTERM_EXECUTABLE").is_ok() || env::var("TERM_PROGRAM").map_or(false, |term| term == "WezTerm") {
        return true;
    }

    // Check for Hyper
    if env::var("TERM_PROGRAM").map_or(false, |term| term == "Hyper") {
        return true;
    }

    // Additional Windows Terminal detection
    if cfg!(windows) {
        // Check if we're in Windows Terminal by looking for common WT env vars
        if env::var("SESSIONNAME").is_ok() || 
           env::var("COMPUTERNAME").is_ok() {
            // Try to detect modern Windows environments
            if let Ok(term_program) = env::var("TERM_PROGRAM") {
                if term_program.contains("WindowsTerminal") || term_program.contains("wt") {
                    return true;
                }
            }
        }
    }

    // Default to true for modern systems - most terminals support OSC 8 now
    true
}

/// Create OSC 8 hyperlink
pub fn create_hyperlink(url: &str, text: &str) -> String {
    if !terminal_supports_hyperlinks() {
        return text.to_string();
    }

    format!("\x1b]8;;{}\x1b\\{}\x1b]8;;\x1b\\", url, text)
}

/// Split response into blocks by RIR source
fn split_response_by_source(response: &str) -> Vec<(String, &'static str)> {
    let mut blocks = Vec::new();
    let lines: Vec<&str> = response.lines().collect();
    let mut current_block = String::new();
    let mut current_rir = None;
    
    for line in lines {
        // Check if this line contains a source field
        if let Some(caps) = Regex::new(r"^source:\s*([A-Z-]+)").unwrap().captures(line) {
            if let Some(source) = caps.get(1) {
                let source_value = source.as_str().trim();
                let rir = match source_value {
                    "RIPE" => Some("ripe"),
                    "ARIN" => Some("arin"), 
                    "APNIC" => Some("apnic"),
                    "LACNIC" => Some("lacnic"),
                    "AFRINIC" => Some("afrinic"),
                    _ => None,
                };
                
                // If we found a new RIR source and have a current block, save it
                if let Some(current) = current_rir {
                    if rir != Some(current) && !current_block.trim().is_empty() {
                        blocks.push((current_block.clone(), current));
                        current_block.clear();
                    }
                }
                
                current_rir = rir;
            }
        }
        
        current_block.push_str(line);
        current_block.push('\n');
    }
    
    // Add the last block
    if let Some(rir) = current_rir {
        if !current_block.trim().is_empty() {
            blocks.push((current_block, rir));
        }
    } else if !current_block.trim().is_empty() {
        // Fallback: try to detect RIR from content
        if let Some(rir) = detect_rir(&current_block) {
            blocks.push((current_block, rir));
        }
    }
    
    // If no blocks were created, treat entire response as one block
    if blocks.is_empty() {
        if let Some(rir) = detect_rir(response) {
            blocks.push((response.to_string(), rir));
        }
    }
    
    blocks
}

/// Hyperlink processor for RIR database responses
pub struct RirHyperlinkProcessor;

impl RirHyperlinkProcessor {
    pub fn new() -> Self {
        Self
    }

    /// Process RIR response and add hyperlinks - handles multi-RIR responses
    pub fn process(&self, response: &str) -> String {
        if !terminal_supports_hyperlinks() {
            return response.to_string();
        }
        
        // Split response into blocks by RIR source
        let blocks = split_response_by_source(response);
        
        if blocks.is_empty() {
            return response.to_string();
        }
        
        let mut processed_blocks = Vec::new();
        
        for (block, rir) in blocks {
            let mut processed_block = block;
            
            // Apply RIR-specific patterns
            match rir {
                "ripe" => self.process_ripe(&mut processed_block),
                "arin" => self.process_arin(&mut processed_block),
                "apnic" => self.process_apnic(&mut processed_block),
                "lacnic" => self.process_lacnic(&mut processed_block),
                "afrinic" => self.process_afrinic(&mut processed_block),
                _ => {}
            }
            
            processed_blocks.push(processed_block);
        }
        
        processed_blocks.join("")
    }

    fn apply_patterns(&self, processed: &mut String, patterns: Vec<(&str, &str)>, rir: &str) {
        for (pattern_str, _) in patterns {
            if let Ok(pattern) = Regex::new(pattern_str) {
                *processed = pattern.replace_all(processed, |caps: &regex::Captures| {
                    let _full_match = caps.get(0).unwrap().as_str();
                    let prefix = caps.get(1).unwrap().as_str();
                    let value = caps.get(2).unwrap().as_str();
                    
                    // Generate URL for the detected RIR
                    let url = RirUrls::get_url(rir, value);
                    let hyperlinked_value = create_hyperlink(&url, value);
                    
                    format!("{}{}", prefix, hyperlinked_value)
                }).to_string();
            }
        }
    }

    fn process_ripe(&self, processed: &mut String) {
        let patterns = vec![
            // ASN patterns
            (r"(?m)^(aut-num:\s+)(AS\d+)", ""),
            (r"(?m)^(origin:\s+)(AS\d+)", ""),
            
            // IP network patterns
            (r"(?m)^(inetnum:\s+)([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+\s*-\s*[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)", ""),
            (r"(?m)^(inet6num:\s+)([0-9a-fA-F:]+/\d+)", ""),
            (r"(?m)^(route:\s+)([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/\d+)", ""),
            (r"(?m)^(route6:\s+)([0-9a-fA-F:]+/\d+)", ""),
            
            // Organization patterns
            (r"(?m)^(organisation:\s+)(ORG-[A-Z0-9-]+)", ""),
            (r"(?m)^(org:\s+)(ORG-[A-Z0-9-]+)", ""),
            
            // Person/Role patterns
            (r"(?m)^(nic-hdl:\s+)([A-Z0-9-]+)", ""),
            (r"(?m)^(admin-c:\s+)([A-Z0-9-]+)", ""),
            (r"(?m)^(tech-c:\s+)([A-Z0-9-]+)", ""),
            
            // Maintainer patterns
            (r"(?m)^(mntner:\s+)([A-Z][A-Z0-9-]*)", ""),
            (r"(?m)^(mnt-by:\s+)([A-Z][A-Z0-9-]*)", ""),
            
            // Domain patterns
            (r"(?m)^(domain:\s+)([a-zA-Z0-9.-]+\.arpa)", ""),
            
            // AS-block patterns
            (r"(?m)^(as-block:\s+)(AS\d+\s*-\s*AS\d+)", ""),
        ];

        self.apply_patterns(processed, patterns, "RIPE");
    }

    fn process_arin(&self, processed: &mut String) {
        let patterns = vec![
            // ARIN-specific patterns
            (r"(?m)^(NetRange:\s+)([0-9.-]+)", ""),
            (r"(?m)^(CIDR:\s+)([0-9./]+)", ""),
            (r"(?m)^(OriginAS:\s+)(AS\d+)", ""),
            (r"(?m)^(OrgId:\s+)([A-Z0-9-]+)", ""),
            (r"(?m)^(NetName:\s+)([A-Z0-9-]+)", ""),
            
            // Common ASN and IP patterns
            (r"(?m)^(aut-num:\s+)(AS\d+)", ""),
            (r"(?m)^(origin:\s+)(AS\d+)", ""),
            (r"(?m)^(inetnum:\s+)([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+\s*-\s*[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)", ""),
            (r"(?m)^(inet6num:\s+)([0-9a-fA-F:]+/\d+)", ""),
        ];

        self.apply_patterns(processed, patterns, "ARIN");
    }

    fn process_apnic(&self, processed: &mut String) {
        let patterns = vec![
            // Common patterns for APNIC
            (r"(?m)^(aut-num:\s+)(AS\d+)", ""),
            (r"(?m)^(origin:\s+)(AS\d+)", ""),
            (r"(?m)^(inetnum:\s+)([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+\s*-\s*[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)", ""),
            (r"(?m)^(inet6num:\s+)([0-9a-fA-F:]+/\d+)", ""),
            (r"(?m)^(nic-hdl:\s+)([A-Z0-9-]+)", ""),
            (r"(?m)^(admin-c:\s+)([A-Z0-9-]+)", ""),
            (r"(?m)^(tech-c:\s+)([A-Z0-9-]+)", ""),
        ];

        self.apply_patterns(processed, patterns, "APNIC");
    }

    fn process_lacnic(&self, processed: &mut String) {
        let patterns = vec![
            // Common patterns for LACNIC
            (r"(?m)^(aut-num:\s+)(AS\d+)", ""),
            (r"(?m)^(origin:\s+)(AS\d+)", ""),
            (r"(?m)^(inetnum:\s+)([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+\s*-\s*[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)", ""),
            (r"(?m)^(inet6num:\s+)([0-9a-fA-F:]+/\d+)", ""),
            (r"(?m)^(nic-hdl:\s+)([A-Z0-9-]+)", ""),
        ];

        self.apply_patterns(processed, patterns, "LACNIC");
    }

    fn process_afrinic(&self, processed: &mut String) {
        let patterns = vec![
            // Common patterns for AFRINIC
            (r"(?m)^(aut-num:\s+)(AS\d+)", ""),
            (r"(?m)^(origin:\s+)(AS\d+)", ""),
            (r"(?m)^(inetnum:\s+)([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+\s*-\s*[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)", ""),
            (r"(?m)^(inet6num:\s+)([0-9a-fA-F:]+/\d+)", ""),
            (r"(?m)^(nic-hdl:\s+)([A-Z0-9-]+)", ""),
        ];

        self.apply_patterns(processed, patterns, "AFRINIC");
    }
}

impl Default for RirHyperlinkProcessor {
    fn default() -> Self {
        Self::new()
    }
}

// For backward compatibility
pub type RipeHyperlinkProcessor = RirHyperlinkProcessor;

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_detect_rir_from_source() {
        let multi_rir_response = r#"
as-block: AS137530 - AS138553
descr: APNIC ASN block
source: APNIC

aut-num: AS3333
as-name: RIPE-NCC-AS
source: RIPE
        "#;
        
        let rirs = detect_rir_from_source(multi_rir_response);
        assert!(rirs.contains(&"apnic"));
        assert!(rirs.contains(&"ripe"));
    }

    #[test]
    fn test_split_response_by_source() {
        let multi_rir_response = r#"
as-block: AS137530 - AS138553
source: APNIC

aut-num: AS3333  
source: RIPE
        "#;
        
        let blocks = split_response_by_source(multi_rir_response);
        assert_eq!(blocks.len(), 2);
    }

    #[test]
    fn test_create_hyperlink() {
        let url = "https://example.com";
        let text = "Example";
        
        let result = create_hyperlink(url, text);
        assert!(result.contains("Example"));
    }

    #[test]
    fn test_rir_urls() {
        let query_url = RirUrls::get_url("RIPE", "AS3333");
        assert!(query_url.contains("AS3333"));
        assert!(!query_url.contains("types=")); // No types parameter
        assert!(query_url.contains("apps.db.ripe.net"));
        
        // Test different RIRs
        let arin_url = RirUrls::get_url("ARIN", "AS3333");
        assert!(arin_url.contains("search.arin.net"));
        assert!(arin_url.contains("AS3333"));
        
        let apnic_url = RirUrls::get_url("APNIC", "AS3333");
        assert!(apnic_url.contains("wq.apnic.net"));
        assert!(apnic_url.contains("AS3333"));
        
        let lacnic_url = RirUrls::get_url("LACNIC", "AS3333");
        assert!(lacnic_url.contains("query.milacnic.lacnic.net"));
        assert!(lacnic_url.contains("AS3333"));
        
        let afrinic_url = RirUrls::get_url("AFRINIC", "AS3333");
        assert!(afrinic_url.contains("afrinic.net"));
        assert!(afrinic_url.contains("AS3333"));
    }
}