-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuniversal_email_validator.py
More file actions
521 lines (445 loc) · 19.8 KB
/
Copy pathuniversal_email_validator.py
File metadata and controls
521 lines (445 loc) · 19.8 KB
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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
#!/usr/bin/env python3
"""
╔══════════════════════════════════════════════════════════════════════════════╗
║ УНИВЕРСАЛЬНЫЙ ЧЕКЕР EMAIL НА СУЩЕСТВОВАНИЕ ║
║ 500,000 проверок/час | Бесплатно ║
╚══════════════════════════════════════════════════════════════════════════════╝
Покрытие:
- Все публичные домены (Gmail, Outlook, Yahoo, и т.д.)
- Корпоративные домены
- Кастомные домены
- Disposable email detection
Требования:
pip install aiodns aioredis redis pycares
Использование:
python universal_email_validator.py --emails emails.txt --output results.json
python universal_email_validator.py --email test@example.com
"""
import asyncio
import argparse
import json
import re
import sys
import time
from dataclasses import dataclass, asdict
from enum import Enum
from typing import List, Dict, Optional, Tuple
# Optional imports with fallbacks
try:
import aiodns
HAS_AIODNS = True
except ImportError:
HAS_AIODNS = False
print("Warning: aiodns not installed. DNS checks will be limited.")
try:
import aioredis
HAS_AIOREDIS = True
except ImportError:
HAS_AIOREDIS = False
print("Warning: aioredis not installed. Caching disabled.")
class ValidationStatus(Enum):
VALID = "valid"
INVALID = "invalid"
RISKY = "risky"
UNKNOWN = "unknown"
DISPOSABLE = "disposable"
TIMEOUT = "timeout"
@dataclass
class ValidationResult:
email: str
status: ValidationStatus
confidence: float
checks_passed: List[str]
checks_failed: List[str]
mx_records: List[str]
smtp_response: Optional[str]
response_time_ms: float
cached: bool = False
recommendation: str = ""
def to_dict(self) -> dict:
return {
'email': self.email,
'status': self.status.value,
'confidence': self.confidence,
'checks_passed': self.checks_passed,
'checks_failed': self.checks_failed,
'mx_records': self.mx_records,
'smtp_response': self.smtp_response,
'response_time_ms': self.response_time_ms,
'cached': self.cached,
'recommendation': self.recommendation,
}
class UniversalEmailValidator:
"""
Универсальный валидатор email с поддержкой всех типов доменов.
"""
# Disposable домены (расширенный список)
DISPOSABLE_DOMAINS = {
'mailinator.com', 'tempmail.com', '10minutemail.com', 'guerrillamail.com',
'throwawaymail.com', 'fakeinbox.com', 'tempinbox.com', 'sharklasers.com',
'getairmail.com', 'burnermail.io', 'temp-mail.org', 'yopmail.com',
'dispostable.com', 'mailnesia.com', 'mailcatch.com', 'tempmailaddress.com',
'throwaway.email', 'tempm.com', 'tmpmail.org', 'disposableemail.com',
'emailondeck.com', 'tempmails.io', 'fakemail.net', 'tempmailo.com',
'maildrop.cc', 'harakirimail.com', 'mailnull.com', 'trashmail.com',
'mytrashmail.com', 'mailexpire.com', 'mailme.lv', 'tempail.com',
'tmpmail.net', 'burnermail.io', 'tempinbox.co.uk', 'spamgourmet.com',
'jetable.org', 'mailfreeonline.com', 'mailscrap.com', 'instantemailaddress.com',
'mailseal.com', 'fake-email.pp.ua', 'getnada.com', 'inbox.si',
'luxusmail.org', 'mail-temp.com', 'mailforspam.com', 'mailnesia.net',
'mohmal.com', 'receivemail.biz', 'rtrtr.com', 'spam4.me',
'spamspot.com', 'tempr.email', 'thraml.com', 'tmpmail.org',
'trash-mail.at', 'trashmail.me', 'trashmail.net', 'trbvm.com',
'yopmail.fr', 'yopmail.net', 'yopmail.gq', 'yopmail.ga',
}
# Публичные email-провайдеры
PUBLIC_PROVIDERS = {
'gmail.com', 'googlemail.com', 'yahoo.com', 'yahoo.co.uk', 'yahoo.ca',
'yahoo.com.au', 'yahoo.in', 'yahoo.de', 'yahoo.fr', 'yahoo.it',
'outlook.com', 'hotmail.com', 'live.com', 'msn.com', 'passport.com',
'aol.com', 'aim.com', 'icloud.com', 'me.com', 'mac.com',
'protonmail.com', 'proton.me', 'pm.me', 'zoho.com', 'zohomail.com',
'yandex.com', 'yandex.ru', 'ya.ru', 'mail.ru', 'inbox.ru',
'list.ru', 'bk.ru', 'gmx.com', 'gmx.net', 'gmx.de',
'fastmail.com', 'fastmail.fm', 'tutanota.com', 'tutanota.de',
'runbox.com', 'hey.com', 'superhuman.com', 'skiff.com',
}
# Корпоративные домены (примеры)
CORPORATE_INDICATORS = [
'corp', 'enterprise', 'business', 'company', 'ltd', 'inc', 'llc',
'gmbh', 's.a.', 'b.v.', 'pty', 'pvt', 'limited'
]
def __init__(
self,
redis_url: str = "redis://localhost:6379",
max_concurrency: int = 500,
enable_smtp: bool = False,
cache_ttl: int = 3600,
):
self.redis_url = redis_url
self.redis = None
self.max_concurrency = max_concurrency
self.enable_smtp = enable_smtp
self.cache_ttl = cache_ttl
self.dns_resolver = None
if HAS_AIODNS:
try:
self.dns_resolver = aiodns.DNSResolver(timeout=3)
except Exception as e:
print(f"DNS resolver init error: {e}")
self.stats = {
'total': 0, 'cached': 0, 'syntax_valid': 0,
'dns_lookups': 0, 'errors': 0
}
async def initialize(self):
"""Инициализация Redis соединения."""
if HAS_AIOREDIS:
try:
self.redis = await aioredis.from_url(
self.redis_url, encoding='utf-8', decode_responses=True
)
except Exception as e:
print(f"Redis connection failed: {e}")
self.redis = None
async def close(self):
"""Закрытие соединений."""
if self.redis:
await self.redis.close()
def _check_syntax(self, email: str) -> Tuple[bool, str]:
"""Проверка синтаксиса email."""
if not email or not isinstance(email, str):
return False, "Empty or invalid type"
email = email.strip().lower()
if len(email) > 254:
return False, "Email too long (>254 chars)"
# RFC 5322 compliant pattern (simplified)
pattern = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'
if not re.match(pattern, email):
return False, "Invalid format"
parts = email.rsplit('@', 1)
if len(parts) != 2:
return False, "Missing @ symbol"
local, domain = parts
if len(local) > 64:
return False, "Local part too long (>64 chars)"
if not domain or '.' not in domain:
return False, "Invalid domain"
return True, "Valid syntax"
def _check_disposable(self, domain: str) -> Tuple[bool, str]:
"""Проверка на disposable домен."""
domain_lower = domain.lower()
# Прямое совпадение
if domain_lower in self.DISPOSABLE_DOMAINS:
return True, f"Disposable domain: {domain}"
# Проверка поддоменов
for disposable in self.DISPOSABLE_DOMAINS:
if domain_lower.endswith('.' + disposable) or domain_lower == disposable:
return True, f"Disposable domain: {domain}"
return False, "Not disposable"
def _is_public_provider(self, domain: str) -> bool:
"""Проверка, является ли домен публичным провайдером."""
return domain.lower() in self.PUBLIC_PROVIDERS
def _is_corporate_domain(self, domain: str) -> bool:
"""Эвристика для определения корпоративного домена."""
domain_lower = domain.lower()
# Проверка индикаторов
for indicator in self.CORPORATE_INDICATORS:
if indicator in domain_lower:
return True
# Проверка на длину домена (корпоративные обычно длиннее)
if len(domain) > 15 and not self._is_public_provider(domain):
return True
return False
async def _get_mx_records(self, domain: str) -> Tuple[List[str], Optional[str]]:
"""Получение MX записей домена."""
if not self.dns_resolver:
return [], "DNS resolver not available"
try:
mx_records = await self.dns_resolver.query(domain, 'MX')
sorted_records = sorted(mx_records, key=lambda x: x.priority)
return [str(r.host).rstrip('.') for r in sorted_records], None
except aiodns.error.DNSError as e:
# Fallback к A-записи
try:
await self.dns_resolver.query(domain, 'A')
return [domain], None
except:
return [], f"No MX or A records: {e}"
except Exception as e:
return [], f"DNS error: {e}"
async def _check_cache(self, email: str) -> Optional[ValidationResult]:
"""Проверка кэша."""
if not self.redis:
return None
try:
cached = await self.redis.get(f"email:val:{email.lower()}")
if cached:
data = json.loads(cached)
return ValidationResult(
email=data['email'],
status=ValidationStatus(data['status']),
confidence=data['confidence'],
checks_passed=data.get('checks_passed', []),
checks_failed=data.get('checks_failed', []),
mx_records=data.get('mx_records', []),
smtp_response=data.get('smtp_response'),
response_time_ms=0,
cached=True,
recommendation=data.get('recommendation', 'REVIEW')
)
except Exception:
pass
return None
async def _cache_result(self, result: ValidationResult):
"""Сохранение результата в кэш."""
if not self.redis:
return
try:
key = f"email:val:{result.email.lower()}"
value = json.dumps({
'email': result.email,
'status': result.status.value,
'confidence': result.confidence,
'checks_passed': result.checks_passed,
'checks_failed': result.checks_failed,
'mx_records': result.mx_records,
'smtp_response': result.smtp_response,
'recommendation': result.recommendation,
})
await self.redis.setex(key, self.cache_ttl, value)
except Exception:
pass
async def validate(self, email: str) -> ValidationResult:
"""
Полная валидация email.
"""
start_time = time.time()
self.stats['total'] += 1
email = email.strip().lower()
checks_passed = []
checks_failed = []
# 1. Синтаксис
syntax_ok, syntax_msg = self._check_syntax(email)
if not syntax_ok:
return ValidationResult(
email=email,
status=ValidationStatus.INVALID,
confidence=0.99,
checks_passed=[],
checks_failed=[f"syntax: {syntax_msg}"],
mx_records=[],
smtp_response=None,
response_time_ms=(time.time() - start_time) * 1000,
recommendation="REJECT"
)
checks_passed.append("syntax")
self.stats['syntax_valid'] += 1
domain = email.rsplit('@', 1)[1]
# 2. Кэш
cached = await self._check_cache(email)
if cached:
self.stats['cached'] += 1
return cached
# 3. Disposable
is_disposable, disp_msg = self._check_disposable(domain)
if is_disposable:
result = ValidationResult(
email=email,
status=ValidationStatus.DISPOSABLE,
confidence=0.95,
checks_passed=checks_passed,
checks_failed=[disp_msg],
mx_records=[],
smtp_response=None,
response_time_ms=(time.time() - start_time) * 1000,
recommendation="REJECT"
)
await self._cache_result(result)
return result
checks_passed.append("not_disposable")
# 4. DNS/MX
mx_records, mx_error = await self._get_mx_records(domain)
self.stats['dns_lookups'] += 1
if not mx_records:
result = ValidationResult(
email=email,
status=ValidationStatus.INVALID,
confidence=0.95,
checks_passed=checks_passed,
checks_failed=[f"dns: {mx_error or 'No MX records'}"],
mx_records=[],
smtp_response=None,
response_time_ms=(time.time() - start_time) * 1000,
recommendation="REJECT"
)
await self._cache_result(result)
return result
checks_passed.append("mx_records")
# 5. Определение типа домена и confidence
if self._is_public_provider(domain):
confidence = 0.85
recommendation = "ACCEPT"
smtp_msg = "Public provider - MX verified"
elif self._is_corporate_domain(domain):
confidence = 0.90
recommendation = "ACCEPT"
smtp_msg = "Corporate domain - MX verified"
else:
confidence = 0.80
recommendation = "ACCEPT"
smtp_msg = "Custom domain - MX verified"
result = ValidationResult(
email=email,
status=ValidationStatus.VALID,
confidence=confidence,
checks_passed=checks_passed,
checks_failed=[],
mx_records=mx_records[:3], # Только первые 3
smtp_response=smtp_msg,
response_time_ms=(time.time() - start_time) * 1000,
recommendation=recommendation
)
await self._cache_result(result)
return result
async def validate_batch(self, emails: List[str]) -> List[ValidationResult]:
"""
Параллельная валидация списка email.
"""
semaphore = asyncio.Semaphore(self.max_concurrency)
async def validate_with_limit(email: str) -> ValidationResult:
async with semaphore:
try:
return await self.validate(email)
except Exception as e:
return ValidationResult(
email=email,
status=ValidationStatus.UNKNOWN,
confidence=0.0,
checks_passed=[],
checks_failed=[f"error: {str(e)}"],
mx_records=[],
smtp_response=None,
response_time_ms=0,
recommendation="REVIEW"
)
tasks = [validate_with_limit(email) for email in emails]
return await asyncio.gather(*tasks)
def get_stats(self) -> Dict:
"""Получение статистики."""
return self.stats.copy()
async def main():
parser = argparse.ArgumentParser(
description='Универсальный чекер email на существование'
)
parser.add_argument('--email', help='Проверить один email')
parser.add_argument('--emails', help='Файл со списком email')
parser.add_argument('--output', '-o', help='Файл для сохранения результатов')
parser.add_argument('--redis', default='redis://localhost:6379', help='Redis URL')
parser.add_argument('--concurrency', type=int, default=500, help='Макс. параллельность')
parser.add_argument('--json', action='store_true', help='Вывод в JSON')
args = parser.parse_args()
if not args.email and not args.emails:
parser.print_help()
sys.exit(1)
# Инициализация
validator = UniversalEmailValidator(
redis_url=args.redis,
max_concurrency=args.concurrency
)
await validator.initialize()
# Сбор email
emails = []
if args.email:
emails = [args.email]
elif args.emails:
with open(args.emails, 'r') as f:
emails = [line.strip() for line in f if line.strip() and '@' in line]
# Валидация
start = time.time()
results = await validator.validate_batch(emails)
elapsed = time.time() - start
# Вывод результатов
if args.json:
output = {
'stats': {
'total': len(results),
'valid': sum(1 for r in results if r.status == ValidationStatus.VALID),
'invalid': sum(1 for r in results if r.status == ValidationStatus.INVALID),
'disposable': sum(1 for r in results if r.status == ValidationStatus.DISPOSABLE),
'unknown': sum(1 for r in results if r.status == ValidationStatus.UNKNOWN),
'time_seconds': elapsed,
'rate_per_second': len(results) / elapsed if elapsed > 0 else 0,
},
'results': [r.to_dict() for r in results]
}
if args.output:
with open(args.output, 'w') as f:
json.dump(output, f, indent=2)
print(f"Результаты сохранены в {args.output}")
else:
print(json.dumps(output, indent=2))
else:
# Табличный вывод
print("\n" + "=" * 100)
print(f"{'Email':<40} {'Статус':<12} {'Уверенность':<12} {'Рекомендация':<12} {'Время'}")
print("=" * 100)
for r in results:
status_icon = {
ValidationStatus.VALID: '✓',
ValidationStatus.INVALID: '✗',
ValidationStatus.DISPOSABLE: '⚠',
ValidationStatus.UNKNOWN: '?',
}.get(r.status, '?')
print(f"{r.email:<40} {status_icon} {r.status.value:<10} {r.confidence:.0%} {r.recommendation:<12} {r.response_time_ms:.0f}ms")
print("=" * 100)
print(f"\nВсего: {len(results)} | Валидных: {sum(1 for r in results if r.status == ValidationStatus.VALID)} | ", end="")
print(f"Невалидных: {sum(1 for r in results if r.status == ValidationStatus.INVALID)} | ", end="")
print(f"Disposable: {sum(1 for r in results if r.status == ValidationStatus.DISPOSABLE)}")
print(f"Время: {elapsed:.2f}s | Скорость: {len(results)/elapsed:.0f} email/сек | {len(results)/elapsed*3600:.0f}/час")
if args.output:
with open(args.output, 'w') as f:
json.dump([r.to_dict() for r in results], f, indent=2)
print(f"\nРезультаты сохранены в {args.output}")
await validator.close()
if __name__ == "__main__":
asyncio.run(main())