web-analyzer 0.1.11

Enterprise domain security & intelligence platform — WHOIS, DNS, SEO, tech detection, subdomain takeover, API security scanning, and more
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
use regex::Regex;
use scraper::{Html, Selector};
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use std::time::Duration;

// ── Structs ─────────────────────────────────────────────────────────────────

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SocialProfile {
    pub platform: String,
    pub username: String,
    pub url: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub found_on: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ContactSpyResult {
    pub domain: String,
    pub emails: Vec<String>,
    pub phones: Vec<String>,
    pub social_media: Vec<SocialProfile>,
    pub social_media_by_platform: HashMap<String, Vec<SocialProfile>>,
    pub pages_scanned: usize,
    pub total_emails: usize,
    pub total_phones: usize,
    pub total_social_media: usize,
}

// ── Invalid social media usernames ──────────────────────────────────────────

const INVALID_USERNAMES: &[&str] = &[
    "share", "sharer", "intent", "oauth", "login", "register", "signup", "api", "www", "mobile",
    "m", "help", "support", "about", "privacy", "terms", "contact", "home", "index", "main",
    "page", "site", "web", "app", "download", "install", "get", "go", "redirect", "link", "url",
    "http", "https", "com", "org", "net", "plugins", "dialog", "p", "explore", "accounts",
];

// ── Skip extensions and directories ─────────────────────────────────────────

const SKIP_EXTENSIONS: &[&str] = &[
    ".pdf", ".jpg", ".jpeg", ".png", ".gif", ".zip", ".doc", ".mp4", ".css", ".js", ".svg", ".ico",
    ".woff", ".ttf",
];

const SKIP_DIRS: &[&str] = &[
    "/assets/",
    "/images/",
    "/css/",
    "/js/",
    "/fonts/",
    "/media/",
    "/wp-content/uploads/",
    "/static/",
];

// ── Email false positive filters ────────────────────────────────────────────

const EMAIL_SKIP_PATTERNS: &[&str] = &[
    "example.",
    "test@",
    "noreply@",
    "no-reply@",
    "admin@example",
    "user@example",
    "email@example",
    "name@example",
    ".jpg@",
    ".png@",
    "wixpress.",
    "sentry.",
    "webpack.",
];

// ── Phone false positive patterns (regex) ───────────────────────────────────

const PHONE_FALSE_POSITIVES: &[&str] = &[
    r"^(19|20)\d{6,8}$",                // Date patterns (1900-2099)
    r"^(\d)\1{6,}$",                    // Same digit repeated 7+ times
    r"^(123|456|789|987|654|321){2,}$", // Sequential numbers
];

// ── Main function ───────────────────────────────────────────────────────────

pub async fn crawl_contacts(
    domain: &str,
    max_pages: usize,
    progress_tx: Option<tokio::sync::mpsc::Sender<crate::ScanProgress>>,
) -> Result<ContactSpyResult, Box<dyn std::error::Error + Send + Sync>> {
    report_progress(
        &progress_tx,
        5.0,
        format!("Preparing contact crawl for {}", domain),
        "Info",
    );

    let base_url = if domain.starts_with("http") {
        domain.to_string()
    } else {
        format!("https://{}", domain)
    };

    let clean_domain = domain
        .trim_start_matches("https://")
        .trim_start_matches("http://")
        .split('/')
        .next()
        .unwrap_or(domain);

    let client = crate::http_client_builder()
        .timeout(Duration::from_secs(15))
        .danger_accept_invalid_certs(true)
        .redirect(reqwest::redirect::Policy::limited(3))
        .user_agent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36")
        .build()?;

    let email_regex = Regex::new(r"[A-Za-z0-9._%+\-]+@[A-Za-z0-9.\-]+\.[A-Za-z]{2,}").unwrap();
    let phone_regex = Regex::new(r"[\+]?[\d\s\-\(\)\.]{8,20}").unwrap();

    let social_patterns = build_social_patterns();
    let phone_fp_regexes: Vec<Regex> = PHONE_FALSE_POSITIVES
        .iter()
        .filter_map(|p| Regex::new(p).ok())
        .collect();

    let mut all_emails: HashSet<String> = HashSet::new();
    let mut all_phones: HashSet<String> = HashSet::new();
    let mut all_social: Vec<SocialProfile> = Vec::new();
    let mut seen_profiles: HashSet<String> = HashSet::new();

    let mut visited: HashSet<String> = HashSet::new();
    let mut to_visit: Vec<String> = vec![base_url.clone()];

    report_progress(
        &progress_tx,
        10.0,
        format!("Starting BFS crawl with max {} page(s)", max_pages),
        "Info",
    );

    // ── BFS Crawl ───────────────────────────────────────────────────────
    while let Some(current_url) = to_visit.pop() {
        if visited.len() >= max_pages {
            break;
        }
        if visited.contains(&current_url) {
            continue;
        }
        visited.insert(current_url.clone());
        let progress = if max_pages > 0 {
            10.0 + (70.0 * (visited.len() as f32 / max_pages as f32)).min(70.0)
        } else {
            80.0
        };
        report_progress(
            &progress_tx,
            progress,
            format!("Crawling {}", current_url),
            "Info",
        );

        let resp = match client.get(&current_url).send().await {
            Ok(r) if r.status().is_success() => r,
            _ => continue,
        };

        let html = match resp.text().await {
            Ok(t) => t,
            Err(_) => continue,
        };
        let doc = Html::parse_document(&html);

        // Remove script/style content — extract clean text
        let text_sel = Selector::parse("body").unwrap();
        let link_sel = Selector::parse("a[href]").unwrap();

        // Collect clean text (excluding script/style)
        let mut clean_text = String::new();
        if let Some(body) = doc.select(&text_sel).next() {
            for node in body.text() {
                clean_text.push(' ');
                clean_text.push_str(node);
            }
        }

        // Collect all href values for social media extraction
        let mut all_hrefs = String::new();
        for el in doc.select(&link_sel) {
            if let Some(href) = el.value().attr("href") {
                all_hrefs.push(' ');
                all_hrefs.push_str(href);
            }
        }

        let full_text = format!("{} {}", clean_text, all_hrefs);

        // ── Extract emails ──────────────────────────────────────────────
        for mat in email_regex.find_iter(&full_text) {
            let email = mat.as_str().to_lowercase();
            if email.len() > 5
                && email.chars().filter(|c| *c == '@').count() == 1
                && !EMAIL_SKIP_PATTERNS.iter().any(|skip| email.contains(skip))
            {
                all_emails.insert(email);
            }
        }

        // ── Extract phones (from clean text only) ───────────────────────
        for mat in phone_regex.find_iter(&clean_text) {
            let raw = mat.as_str().trim();
            let digits: String = raw
                .chars()
                .filter(|c| c.is_ascii_digit() || *c == '+')
                .collect();
            let digits_only: String = digits.replace('+', "");

            if is_valid_phone(&digits_only, &phone_fp_regexes)
                && (digits.starts_with('+') || digits_only.len() >= 10)
            {
                all_phones.insert(digits);
            }
        }

        // ── Extract social media ────────────────────────────────────────
        for (platform, regex) in &social_patterns {
            for caps in regex.captures_iter(&full_text) {
                let username = caps
                    .get(caps.len() - 1)
                    .or_else(|| caps.get(1))
                    .map(|m| m.as_str().to_string())
                    .unwrap_or_default();

                if username.is_empty() {
                    continue;
                }
                if !is_valid_social_username(&username, platform) {
                    continue;
                }

                let profile_id = format!("{}:{}", platform, username.to_lowercase());
                if !seen_profiles.insert(profile_id) {
                    continue;
                }

                let full_url = caps
                    .get(0)
                    .map(|m| {
                        let u = m.as_str().to_string();
                        if u.starts_with("http") {
                            u
                        } else {
                            format!("https://{}", u)
                        }
                    })
                    .unwrap_or_default();

                all_social.push(SocialProfile {
                    platform: platform.to_string(),
                    username,
                    url: full_url,
                    found_on: Some(current_url.clone()),
                });
            }
        }

        // ── Discover new links for crawling ─────────────────────────────
        if visited.len() < max_pages {
            for el in doc.select(&link_sel) {
                if let Some(href) = el.value().attr("href") {
                    if let Some(abs_url) = resolve_url(&base_url, href) {
                        if is_valid_crawl_url(&abs_url, clean_domain) && !visited.contains(&abs_url)
                        {
                            to_visit.push(abs_url);
                        }
                    }
                }
            }
        }
    }

    report_progress(
        &progress_tx,
        90.0,
        "Grouping discovered social profiles",
        "Info",
    );

    // ── Group social by platform ────────────────────────────────────────
    let mut by_platform: HashMap<String, Vec<SocialProfile>> = HashMap::new();
    for profile in &all_social {
        by_platform
            .entry(profile.platform.clone())
            .or_default()
            .push(profile.clone());
    }

    let total_emails = all_emails.len();
    let total_phones = all_phones.len();
    let total_social = all_social.len();

    report_progress(
        &progress_tx,
        100.0,
        format!(
            "Contact crawl complete: {} email(s), {} phone(s), {} social profile(s)",
            total_emails, total_phones, total_social
        ),
        "Success",
    );

    Ok(ContactSpyResult {
        domain: clean_domain.to_string(),
        emails: all_emails.into_iter().collect(),
        phones: all_phones.into_iter().collect(),
        social_media: all_social,
        social_media_by_platform: by_platform,
        pages_scanned: visited.len(),
        total_emails,
        total_phones,
        total_social_media: total_social,
    })
}

fn report_progress(
    progress_tx: &Option<tokio::sync::mpsc::Sender<crate::ScanProgress>>,
    percentage: f32,
    message: impl Into<String>,
    status: &str,
) {
    if let Some(tx) = progress_tx {
        let _ = tx.try_send(crate::ScanProgress {
            module: "Contact Spy".into(),
            percentage,
            message: message.into(),
            status: status.into(),
        });
    }
}

// ── Social media patterns ───────────────────────────────────────────────────

fn build_social_patterns() -> Vec<(String, Regex)> {
    vec![
        (
            "Facebook".into(),
            Regex::new(r"(?i)facebook\.com/([a-zA-Z0-9._-]+)").unwrap(),
        ),
        (
            "Twitter".into(),
            Regex::new(r"(?i)(?:twitter\.com|x\.com)/([a-zA-Z0-9._-]+)").unwrap(),
        ),
        (
            "Instagram".into(),
            Regex::new(r"(?i)instagram\.com/([a-zA-Z0-9._-]+)").unwrap(),
        ),
        (
            "LinkedIn".into(),
            Regex::new(r"(?i)linkedin\.com/(?:in|company)/([a-zA-Z0-9._-]+)").unwrap(),
        ),
        (
            "YouTube".into(),
            Regex::new(r"(?i)youtube\.com/(?:channel/|user/|c/|@)([a-zA-Z0-9._-]+)").unwrap(),
        ),
        (
            "GitHub".into(),
            Regex::new(r"(?i)github\.com/([a-zA-Z0-9._-]+)").unwrap(),
        ),
        (
            "TikTok".into(),
            Regex::new(r"(?i)tiktok\.com/@([a-zA-Z0-9._-]+)").unwrap(),
        ),
    ]
}

// ── Validation helpers ──────────────────────────────────────────────────────

fn is_valid_phone(digits: &str, fp_regexes: &[Regex]) -> bool {
    if digits.len() < 7 || digits.len() > 15 {
        return false;
    }
    if !digits.chars().all(|c| c.is_ascii_digit()) {
        return false;
    }
    if fp_regexes.iter().any(|rx| rx.is_match(digits)) {
        return false;
    }
    true
}

fn is_valid_social_username(username: &str, platform: &str) -> bool {
    if username.len() < 2 {
        return false;
    }
    if INVALID_USERNAMES.contains(&username.to_lowercase().as_str()) {
        return false;
    }

    // Platform-specific length limits
    match platform {
        "Twitter" => username.len() <= 15 && !username.starts_with('_'),
        "Instagram" => username.len() <= 30,
        "LinkedIn" => username.len() <= 100,
        "GitHub" => username.len() <= 39 && !username.starts_with('-'),
        "YouTube" => username.len() <= 100,
        "Facebook" => username.len() <= 50,
        "TikTok" => username.len() <= 24,
        _ => true,
    }
}

fn is_valid_crawl_url(url: &str, base_domain: &str) -> bool {
    let lower = url.to_lowercase();

    // Must contain the domain
    if !lower.contains(base_domain) {
        return false;
    }

    // Skip static assets
    if SKIP_EXTENSIONS.iter().any(|ext| lower.ends_with(ext)) {
        return false;
    }
    if SKIP_DIRS.iter().any(|dir| lower.contains(dir)) {
        return false;
    }

    // Skip fragments and javascript
    if url.starts_with('#') || url.starts_with("javascript:") || url.starts_with("mailto:") {
        return false;
    }

    true
}

fn resolve_url(base: &str, href: &str) -> Option<String> {
    if href.starts_with("javascript:")
        || href.starts_with('#')
        || href.starts_with("mailto:")
        || href.starts_with("tel:")
    {
        return None;
    }
    if href.starts_with("//") {
        return Some(format!("https:{}", href));
    }
    if href.starts_with("http://") || href.starts_with("https://") {
        return Some(href.to_string());
    }
    // Relative URL
    let base_trimmed = if let Some(idx) = base.rfind('/') {
        &base[..idx + 1]
    } else {
        base
    };
    Some(format!("{}{}", base_trimmed, href.trim_start_matches('/')))
}