Repository navigation
Expand file tree
/
Copy pathip_intelligence.py
More file actions
1490 lines (1344 loc) · 69.9 KB
/
Copy pathip_intelligence.py
File metadata and controls
1490 lines (1344 loc) · 69.9 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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""Portable, dependency-free multi-source IP intelligence fusion CLI."""
from __future__ import annotations
import argparse
import base64
import concurrent.futures
import datetime as dt
import email.utils
import html
import ipaddress
import json
import os
import re
import ssl
import statistics
import sys
import threading
import time
import urllib.error
import urllib.parse
import urllib.request
from dataclasses import dataclass, field
from pathlib import Path
from typing import Any, Callable, Dict, Iterable, List, Mapping, Optional, Sequence, Tuple
VERSION = "2.0.0"
USER_AGENT = f"ip-intelligence-fusion/{VERSION} (+https://github.com/GetIPProxy/ip-intelligence-fusion)"
DEFAULT_TIMEOUT = 8.0
DEFAULT_RETRIES = 2
MAX_RESPONSE_BYTES = 2_000_000
MAX_EXTERNAL_WORKERS = 3
MIN_REQUEST_INTERVAL = 0.25
POLICY_VERSION = "2.0"
RETRYABLE_HTTP_CODES = frozenset((408, 425, 429, 500, 502, 503, 504))
REQUEST_STATE = threading.local()
REQUEST_LIMIT_LOCK = threading.Lock()
REQUEST_LAST_BY_HOST: Dict[str, float] = {}
FACT_FIELDS = (
"country_code", "country", "region", "city", "asn", "organization", "isp",
"network_type", "allocation_prefix", "route_prefix", "registry_country",
)
RISK_FLAGS = ("is_proxy", "is_vpn", "is_tor", "is_hosting", "is_abuser", "is_bot")
RISK_WEIGHTS = {
"ipqs": 1.00, "scamalytics": 0.95, "abuseipdb": 0.95, "ipdata": 0.90,
"proxycheck": 0.85, "ipapi-is": 0.80, "ipinfo": 0.75, "ping0": 0.70,
}
RISK_PROVIDER_ORDER = (
"ipqs", "abuseipdb", "scamalytics", "ipdata", "proxycheck", "ipapi-is", "ping0",
)
DETAIL_PROVIDER_GROUPS = (
("Risk and reputation", ("ipqs", "abuseipdb", "scamalytics", "ipdata", "proxycheck", "ipapi-is", "ping0")),
("Network and privacy", ("ipinfo",)),
("Registry and routing", ("rdap", "ripestat")),
("Geolocation cross-check", ("geojs",)),
)
PROFILE_PROVIDER_IDS = {
"fast": ("rdap", "ripestat", "geojs", "ipapi-is", "proxycheck"),
"resilient": (
"geojs", "rdap", "ripestat", "ipapi-is", "proxycheck", "ipinfo",
"scamalytics", "ipqs", "ipdata", "abuseipdb",
),
"comprehensive": (),
}
PUBLIC_PAGE_DOMAINS = {
"ipinfo": {"ipinfo.io", "www.ipinfo.io"},
"scamalytics": {"scamalytics.com", "www.scamalytics.com"},
"ipqs": {"ipqualityscore.com", "www.ipqualityscore.com"},
"ipdata": {"ipdata.co", "www.ipdata.co"},
"ping0": {"ping0.cc", "www.ping0.cc", "ip.ping0.cc"},
}
PROVIDER_ALLOWED_DOMAINS = {
"self": {"api64.ipify.org"},
"geojs": {"get.geojs.io"},
"rdap": {"rdap.org", "rdap-bootstrap.arin.net"},
"ripestat": {"stat.ripe.net"},
"ipapi-is": {"api.ipapi.is"},
"proxycheck": {"proxycheck.io"},
"ping0": {"ping0.cc", "www.ping0.cc", "ip.ping0.cc"},
"ipinfo": {"api.ipinfo.io"},
"ipqs": set(),
"ipdata": set(),
"scamalytics": set(),
"abuseipdb": {"api.abuseipdb.com"},
}
EVIDENCE_FIELDS = set(FACT_FIELDS) | set(RISK_FLAGS) | {
"risk_score", "abuse_score", "latitude", "longitude", "timezone",
"range", "company", "domain", "anycast", "privacy", "native_ip",
"native_classification", "trust_score", "blocklist_reports", "recent_abuse",
"is_mobile", "is_residential_proxy", "is_anonymous", "is_relay",
}
REPORT_FORBIDDEN_FIELDS = frozenset({
"raw", "raw_response", "response", "payload", "analysis", "fn", "email",
"abuse_contacts", "contact", "contacts", "hostname", "reverse_dns",
})
CREDENTIAL_QUERY_KEYS = frozenset({
"key", "api-key", "api_key", "apikey", "token", "access-token", "access_token",
"authorization", "auth", "secret", "client-secret", "client_secret", "password", "signature",
})
EVIDENCE_BOOLEAN_FIELDS = set(RISK_FLAGS) | {
"anycast", "privacy", "native_ip", "recent_abuse", "is_mobile",
"is_residential_proxy", "is_anonymous", "is_relay",
}
LEVELS = ((30, "low"), (50, "guarded"), (70, "elevated"), (85, "high"), (101, "critical"))
class LookupError(RuntimeError):
"""An expected provider or input failure safe to display to the user."""
@dataclass(frozen=True)
class Provider:
id: str
name: str
category: str
lookup: Callable[[str, float], Dict[str, Any]]
credential_env: Tuple[str, ...] = ()
experimental: bool = False
api_enabled: bool = True
def configured(self) -> bool:
return all(os.environ.get(key, "").strip() for key in self.credential_env)
@dataclass
class ProviderResult:
id: str
name: str
category: str
status: str
elapsed_ms: int = 0
source_url: Optional[str] = None
data: Dict[str, Any] = field(default_factory=dict)
message: Optional[str] = None
experimental: bool = False
collection_method: str = "api"
observed_at: Optional[str] = None
attempts: int = 0
request_domains: Tuple[str, ...] = ()
def as_dict(self) -> Dict[str, Any]:
result = {
"id": self.id, "name": self.name, "category": self.category,
"status": self.status, "elapsed_ms": self.elapsed_ms,
"attempts": self.attempts,
"experimental": self.experimental,
"collection_method": self.collection_method,
}
if self.source_url:
result["source_url"] = redact_secrets(self.source_url)
safe_data = redact_report_value(self.data)
if safe_data:
result["data"] = safe_data
if self.message:
result["message"] = redact_secrets(self.message)
if self.observed_at:
result["observed_at"] = self.observed_at
return result
def utc_now() -> str:
return dt.datetime.now(dt.timezone.utc).replace(microsecond=0).isoformat().replace("+00:00", "Z")
def compact(value: Any) -> Any:
if value is None or value == "" or value == [] or value == {}:
return None
if isinstance(value, str) and value.strip().casefold() in ("nil", "null", "none", "n/a", "unknown", "undefined"):
return None
return value
def first(mapping: Mapping[str, Any], *keys: str) -> Any:
for key in keys:
value = mapping.get(key)
if compact(value) is not None:
return value
return None
def nested(mapping: Mapping[str, Any], *path: str) -> Mapping[str, Any]:
current: Any = mapping
for key in path:
if not isinstance(current, Mapping):
return {}
current = current.get(key, {})
return current if isinstance(current, Mapping) else {}
def text_value(value: Any) -> Optional[str]:
if value is None or isinstance(value, (dict, list)):
return None
value = str(value).strip()
return value if compact(value) is not None else None
def number(value: Any) -> Optional[float]:
if value is None or isinstance(value, bool):
return None
try:
result = float(value)
return max(0.0, min(100.0, result))
except (TypeError, ValueError):
return None
def boolean(value: Any) -> Optional[bool]:
if isinstance(value, bool):
return value
if isinstance(value, (int, float)) and value in (0, 1):
return bool(value)
if isinstance(value, str):
lowered = value.strip().lower()
if lowered in ("true", "yes", "y", "1", "enabled", "detected"):
return True
if lowered in ("false", "no", "n", "0", "disabled", "not detected"):
return False
return None
def normalize_asn(value: Any) -> Optional[str]:
value = text_value(value)
if not value:
return None
match = re.search(r"(?:AS)?\s*(\d+)", value, re.I)
return f"AS{match.group(1)}" if match else value
def clean_data(**values: Any) -> Dict[str, Any]:
return {key: value for key, value in values.items() if compact(value) is not None}
def public_ip(value: str) -> str:
try:
address = ipaddress.ip_address(value.strip())
except ValueError as exc:
raise LookupError(f"Invalid IP address: {value!r}") from exc
if not address.is_global:
raise LookupError(f"Refusing remote lookup for non-public IP address: {address.compressed}")
return address.compressed
def safe_error(exc: BaseException) -> str:
if isinstance(exc, urllib.error.HTTPError):
return redact_secrets(f"HTTP {exc.code} {exc.reason}".strip())
if isinstance(exc, urllib.error.URLError):
reason = getattr(exc, "reason", exc)
return redact_secrets(f"Network error: {reason}")
message = redact_secrets(str(exc))
message = re.sub(r"https?://[^\s]+", "<upstream-url>", message)
return message[:300] or exc.__class__.__name__
def redact_secrets(value: Any) -> str:
"""Remove configured credentials and common credential-bearing values from display text."""
message = str(value)
for name in ("IPINFO_TOKEN", "ABUSEIPDB_API_KEY", "PROXYCHECK_API_KEY"):
secret = os.environ.get(name, "").strip()
if secret:
message = message.replace(secret, "<redacted-secret>")
message = re.sub(
r"(?i)(bearer\s+)[^\s,;]+",
r"\1<redacted-secret>",
message,
)
message = re.sub(
r"(?i)((?:api[-_]?key|access[-_]?token|token|authorization|key)=)[^&\s]+",
r"\1<redacted-secret>",
message,
)
message = re.sub(
r"\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}\b",
"<redacted-contact>",
message,
flags=re.I,
)
return message
def redact_report_value(value: Any) -> Any:
if isinstance(value, Mapping):
return {
key: redact_report_value(item)
for key, item in value.items()
if str(key).casefold() not in REPORT_FORBIDDEN_FIELDS
}
if isinstance(value, list):
return [redact_report_value(item) for item in value]
if isinstance(value, str):
return redact_secrets(value)
return value
def allowed_external_domains() -> set[str]:
domains = set()
for provider_domains in PROVIDER_ALLOWED_DOMAINS.values():
domains.update(provider_domains)
return domains
def validate_external_url(url: str) -> urllib.parse.SplitResult:
try:
parsed = urllib.parse.urlsplit(url)
port = parsed.port
except ValueError as exc:
raise LookupError("External request URL is malformed") from exc
hostname = (parsed.hostname or "").lower()
if (parsed.scheme != "https" or not hostname or hostname not in allowed_external_domains()
or port not in (None, 443) or parsed.fragment):
raise LookupError("External requests require an approved HTTPS provider domain")
if parsed.username or parsed.password:
raise LookupError("External request URLs cannot contain user information")
query_keys = {key.casefold() for key, _ in urllib.parse.parse_qsl(parsed.query, keep_blank_values=True)}
if query_keys & CREDENTIAL_QUERY_KEYS:
raise LookupError("API credentials must not be placed in request URLs")
return parsed
def validate_provider_url(url: str) -> urllib.parse.SplitResult:
parsed = validate_external_url(url)
selected_domains = getattr(REQUEST_STATE, "allowed_domains", None)
if selected_domains is not None and (parsed.hostname or "").lower() not in selected_domains:
raise LookupError("External request URL is outside the selected provider's approved domains")
return parsed
def record_request_domain(hostname: str) -> None:
domains = getattr(REQUEST_STATE, "request_domains", None)
if domains is None:
domains = set()
REQUEST_STATE.request_domains = domains
if hostname:
domains.add(hostname.lower())
def throttle_host(hostname: str, max_wait: Optional[float] = None) -> None:
with REQUEST_LIMIT_LOCK:
now = time.monotonic()
previous = REQUEST_LAST_BY_HOST.get(hostname)
wait = max(0.0, (previous + MIN_REQUEST_INTERVAL - now) if previous is not None else 0.0)
if max_wait is not None:
wait = min(wait, max(0.0, max_wait))
REQUEST_LAST_BY_HOST[hostname] = now + wait
if wait:
time.sleep(wait)
def load_public_page_evidence(paths: Sequence[str], target_ip: str) -> Dict[str, ProviderResult]:
providers = {provider.id: provider for provider in PROVIDERS}
loaded: Dict[str, ProviderResult] = {}
for path_text in paths:
path = Path(path_text).expanduser()
try:
payload = json.loads(path.read_text(encoding="utf-8"))
except (OSError, UnicodeDecodeError, json.JSONDecodeError) as exc:
raise LookupError(f"Cannot read evidence file {path}: {safe_error(exc)}") from exc
items = payload.get("evidence") if isinstance(payload, Mapping) else payload
if isinstance(items, Mapping):
items = [items]
if not isinstance(items, list):
raise LookupError(f"Evidence file {path} must contain an object, list, or evidence list")
for index, item in enumerate(items):
if not isinstance(item, Mapping):
raise LookupError(f"Evidence item {index} in {path} must be an object")
provider_id = text_value(item.get("provider"))
if provider_id not in PUBLIC_PAGE_DOMAINS or provider_id not in providers:
raise LookupError(f"Evidence item {index} has unsupported provider: {provider_id!r}")
try:
item_ip = ipaddress.ip_address(str(item.get("target_ip", "")).strip()).compressed
except ValueError as exc:
raise LookupError(f"Evidence item {index} has invalid target_ip") from exc
if item_ip != target_ip:
raise LookupError(f"Evidence target {item_ip} does not match requested IP {target_ip}")
source_url = text_value(item.get("source_url"))
try:
parsed = urllib.parse.urlsplit(source_url or "")
port = parsed.port
except ValueError as exc:
raise LookupError(f"Evidence URL for {provider_id} is malformed") from exc
hostname = (parsed.hostname or "").lower()
query_keys = {key.casefold() for key, _ in urllib.parse.parse_qsl(
parsed.query, keep_blank_values=True)}
if (parsed.scheme != "https" or hostname not in PUBLIC_PAGE_DOMAINS[provider_id]
or port not in (None, 443) or parsed.username or parsed.password
or parsed.fragment
or parsed.query
or query_keys & CREDENTIAL_QUERY_KEYS):
raise LookupError(f"Evidence for {provider_id} must use its official HTTPS domain")
decoded_path = urllib.parse.unquote(parsed.path).casefold()
if target_ip.casefold() not in decoded_path:
raise LookupError(f"Evidence URL for {provider_id} must contain the target IP")
data = item.get("data")
if not isinstance(data, Mapping) or not data:
raise LookupError(f"Evidence for {provider_id} has no data object")
unknown = sorted(set(data) - EVIDENCE_FIELDS)
if unknown:
raise LookupError(f"Evidence for {provider_id} has unsupported field(s): {', '.join(unknown)}")
normalized: Dict[str, Any] = {}
for key, value in data.items():
if key in EVIDENCE_BOOLEAN_FIELDS:
parsed_bool = boolean(value)
if parsed_bool is None:
raise LookupError(f"Evidence field {key} for {provider_id} must be boolean")
normalized[key] = parsed_bool
elif key in ("risk_score", "abuse_score", "trust_score"):
parsed_number = number(value)
if parsed_number is None:
raise LookupError(f"Evidence field {key} for {provider_id} must be between 0 and 100")
normalized[key] = parsed_number
elif key == "asn":
parsed_asn = normalize_asn(value)
if not parsed_asn or not re.fullmatch(r"AS\d+", parsed_asn):
raise LookupError(f"Evidence field asn for {provider_id} is invalid")
normalized[key] = parsed_asn
elif key in ("allocation_prefix", "route_prefix", "range"):
try:
normalized[key] = str(ipaddress.ip_network(str(value).strip(), strict=False))
except ValueError as exc:
raise LookupError(f"Evidence field {key} for {provider_id} is invalid") from exc
else:
parsed_text = text_value(value)
if parsed_text is None or len(parsed_text) > 500:
raise LookupError(f"Evidence field {key} for {provider_id} must be concise text")
normalized[key] = parsed_text
if provider_id == "ipdata" and "trust_score" in normalized:
derived = round(100.0 - normalized["trust_score"], 1)
if "risk_score" in normalized and abs(normalized["risk_score"] - derived) > 1:
raise LookupError("ipdata risk_score conflicts with its trust_score")
normalized.setdefault("risk_score", derived)
observed_at = text_value(item.get("observed_at")) or utc_now()
try:
dt.datetime.fromisoformat(observed_at.replace("Z", "+00:00"))
except ValueError as exc:
raise LookupError(f"Evidence observed_at for {provider_id} must be ISO 8601") from exc
if provider_id in loaded:
raise LookupError(f"Duplicate public-page evidence for provider: {provider_id}")
provider = providers[provider_id]
loaded[provider_id] = ProviderResult(
provider.id, provider.name, provider.category, "success", source_url=source_url,
data=normalized, message="Public lookup page read with host-provided web tooling.",
experimental=provider.experimental, collection_method="browser-public-page",
observed_at=observed_at,
)
return loaded
def merge_evidence(results: Sequence[ProviderResult], evidence: Mapping[str, ProviderResult]) -> List[ProviderResult]:
merged = []
seen = set()
for result in results:
replacement = evidence.get(result.id)
if replacement and result.status != "success":
merged.append(replacement)
else:
merged.append(result)
seen.add(result.id)
return merged
def provider_domains(providers: Sequence[Provider]) -> List[str]:
domains = set()
for provider in providers:
domains.update(PROVIDER_ALLOWED_DOMAINS.get(provider.id, set()))
return sorted(domains)
def observed_provider_domains(providers: Sequence[Provider],
results: Sequence[ProviderResult]) -> List[str]:
"""Return the provider hosts that actually handled an external request."""
domains = set()
for result in results:
if result.attempts > 0:
domains.update(result.request_domains)
return sorted(domains)
def build_policy(network_mode: str, confirmation_mode: Optional[str],
providers: Sequence[Provider]) -> Dict[str, Any]:
external = network_mode == "external-confirmed"
return {
"network_mode": network_mode,
"confirmation_mode": confirmation_mode,
"provider_domains": provider_domains(providers) if external else [],
"sent_fields": ["public_ip"] if external else [],
"policy_version": POLICY_VERSION,
}
def local_provider_results(providers: Sequence[Provider]) -> List[ProviderResult]:
return [
ProviderResult(
provider.id, provider.name, provider.category, "not-requested",
message="External queries are disabled; use --external after confirming the data flow.",
experimental=provider.experimental, collection_method="local-only",
)
for provider in providers
]
def confirm_external_access(target_label: str, providers: Sequence[Provider],
flag_confirmed: bool, include_self_lookup: bool = False) -> str:
names = ", ".join(provider.name for provider in providers if provider.api_enabled)
domains_list = provider_domains(providers)
if include_self_lookup:
domains_list.append("api64.ipify.org")
domains = ", ".join(sorted(set(domains_list))) or "none"
prompt = (
f"External lookup will send the public IP {target_label} to approved provider(s): {names or 'none'}.\n"
f"Provider domains: {domains}. This may involve cross-border data transfer. Type YES to continue: "
)
try:
interactive = bool(sys.stdin.isatty())
except (AttributeError, OSError):
interactive = False
if not interactive and not flag_confirmed:
raise LookupError("Non-interactive external lookup requires --confirm-external")
if not interactive:
return "flag"
try:
answer = input(prompt)
except (EOFError, OSError) as exc:
raise LookupError("External lookup confirmation was not received") from exc
if answer.strip() != "YES":
raise LookupError("External lookup cancelled; type YES to confirm")
return "interactive"
class ApprovedRedirectHandler(urllib.request.HTTPRedirectHandler):
"""Reject a redirect before urllib sends the next request."""
def redirect_request(self, req: urllib.request.Request, fp: Any, code: int,
msg: str, headers: Mapping[str, str], newurl: str) -> Optional[urllib.request.Request]:
parsed = validate_provider_url(newurl)
record_request_domain(parsed.hostname or "")
redirected = super().redirect_request(req, fp, code, msg, headers, newurl)
if redirected and req.host and parsed.hostname and req.host.casefold() != parsed.hostname.casefold():
for header in ("Authorization", "Key", "X-API-Key", "Api-Key"):
redirected.headers.pop(header, None)
redirected.unredirected_hdrs.pop(header, None)
return redirected
def open_external_request(request: urllib.request.Request, timeout: float,
context: ssl.SSLContext) -> Any:
opener = urllib.request.build_opener(
ApprovedRedirectHandler(),
urllib.request.HTTPSHandler(context=context),
)
return opener.open(request, timeout=timeout)
def retry_after_seconds(value: Optional[str]) -> float:
if not value:
return 0.0
try:
return max(0.0, float(value))
except (TypeError, ValueError):
pass
try:
retry_at = email.utils.parsedate_to_datetime(value)
if retry_at is None:
return 0.0
if retry_at.tzinfo is None:
retry_at = retry_at.replace(tzinfo=dt.timezone.utc)
return max(0.0, (retry_at - dt.datetime.now(dt.timezone.utc)).total_seconds())
except (TypeError, ValueError, OverflowError):
return 0.0
def request_bytes(
url: str,
timeout: float,
headers: Optional[Mapping[str, str]] = None,
method: str = "GET",
) -> Tuple[bytes, Mapping[str, str]]:
parsed = validate_provider_url(url)
request_headers = {"User-Agent": USER_AGENT, "Accept": "application/json, text/html;q=0.8"}
request_headers.update(headers or {})
request = urllib.request.Request(url, headers=request_headers, method=method)
context = ssl.create_default_context()
retries = max(0, int(getattr(REQUEST_STATE, "retries", DEFAULT_RETRIES)))
deadline = time.monotonic() + timeout
for attempt in range(retries + 1):
REQUEST_STATE.attempts = getattr(REQUEST_STATE, "attempts", 0) + 1
try:
remaining = deadline - time.monotonic()
if remaining <= 0:
raise TimeoutError("request timeout budget exhausted")
throttle_host(parsed.hostname or "", remaining)
remaining = deadline - time.monotonic()
if remaining <= 0:
raise TimeoutError("request timeout budget exhausted")
record_request_domain(parsed.hostname or "")
with open_external_request(request, remaining, context) as response:
final_url = response.geturl() if hasattr(response, "geturl") else url
final_parsed = validate_provider_url(final_url)
record_request_domain(final_parsed.hostname or "")
length = response.headers.get("Content-Length")
if length and int(length) > MAX_RESPONSE_BYTES:
raise LookupError("Upstream response exceeds size limit")
body = response.read(MAX_RESPONSE_BYTES + 1)
if len(body) > MAX_RESPONSE_BYTES:
raise LookupError("Upstream response exceeds size limit")
return body, dict(response.headers.items())
except urllib.error.HTTPError as exc:
retryable = exc.code in RETRYABLE_HTTP_CODES
failure = exc
except (urllib.error.URLError, TimeoutError, ConnectionError, OSError) as exc:
retryable = True
failure = exc
if not retryable or attempt >= retries:
raise failure
retry_after = None
if isinstance(failure, urllib.error.HTTPError):
retry_after = failure.headers.get("Retry-After") if failure.headers else None
retry_delay = retry_after_seconds(retry_after)
backoff = 0.25 * (2 ** attempt)
remaining = max(0.0, deadline - time.monotonic())
delay = min(max(retry_delay, backoff), remaining)
if delay <= 0:
raise failure
time.sleep(delay)
raise LookupError("Upstream request failed")
def request_json(
url: str,
timeout: float,
headers: Optional[Mapping[str, str]] = None,
) -> Any:
body, _ = request_bytes(url, timeout, headers)
try:
return json.loads(body.decode("utf-8-sig"))
except (UnicodeDecodeError, json.JSONDecodeError) as exc:
raise LookupError("Upstream returned invalid JSON") from exc
def with_query(url: str, params: Mapping[str, Any]) -> str:
parsed = urllib.parse.urlsplit(url)
query = dict(urllib.parse.parse_qsl(parsed.query, keep_blank_values=True))
query.update({key: str(value) for key, value in params.items() if value is not None})
return urllib.parse.urlunsplit((parsed.scheme, parsed.netloc, parsed.path,
urllib.parse.urlencode(query), parsed.fragment))
def risk_fields(payload: Mapping[str, Any]) -> Dict[str, Any]:
return clean_data(
risk_score=number(first(payload, "risk_score", "fraud_score", "score", "abuse_score")),
is_proxy=boolean(first(payload, "is_proxy", "proxy")),
is_vpn=boolean(first(payload, "is_vpn", "vpn")),
is_tor=boolean(first(payload, "is_tor", "tor")),
is_hosting=boolean(first(payload, "is_hosting", "hosting", "is_datacenter", "datacenter")),
is_abuser=boolean(first(payload, "is_abuser", "abuser", "abuse")),
is_bot=boolean(first(payload, "is_bot", "bot", "is_crawler", "crawler")),
)
def lookup_geojs(ip: str, timeout: float) -> Dict[str, Any]:
url = f"https://get.geojs.io/v1/ip/geo/{urllib.parse.quote(ip)}.json"
raw = request_json(url, timeout)
if text_value(raw.get("ip")) != ip:
raise LookupError("GeoJS response did not echo the target IP")
data = clean_data(
country_code=raw.get("country_code"), country=raw.get("country"), region=raw.get("region"),
city=raw.get("city"), asn=normalize_asn(raw.get("asn")), organization=raw.get("organization"),
latitude=compact(raw.get("latitude")), longitude=compact(raw.get("longitude")),
timezone=compact(raw.get("timezone")),
)
return {"data": data, "source_url": f"https://get.geojs.io/v1/ip/geo/{ip}.json"}
def rdap_vcard(entity: Mapping[str, Any]) -> Dict[str, str]:
result: Dict[str, str] = {}
vcard = entity.get("vcardArray")
if not isinstance(vcard, list) or len(vcard) < 2 or not isinstance(vcard[1], list):
return result
for item in vcard[1]:
if not isinstance(item, list) or len(item) < 4:
continue
key = text_value(item[0])
if key != "org":
continue
value = text_value(item[3])
if value:
result.setdefault("organization", value)
return result
def prefix_from_range(start: Any, end: Any) -> Optional[str]:
try:
ranges = list(ipaddress.summarize_address_range(ipaddress.ip_address(str(start)), ipaddress.ip_address(str(end))))
return str(ranges[0]) if len(ranges) == 1 else f"{start} - {end}"
except ValueError:
return None
def lookup_rdap(ip: str, timeout: float) -> Dict[str, Any]:
rdap_urls = (
f"https://rdap.org/ip/{urllib.parse.quote(ip)}",
f"https://rdap-bootstrap.arin.net/bootstrap/ip/{urllib.parse.quote(ip)}",
)
raw = None
source_url = rdap_urls[0]
failures = []
deadline = time.monotonic() + timeout
for index, candidate in enumerate(rdap_urls):
remaining = deadline - time.monotonic()
remaining_candidates = len(rdap_urls) - index
if remaining <= 0:
failures.append("timeout budget exhausted")
break
try:
raw = request_json(candidate, max(1.0, remaining / remaining_candidates))
source_url = candidate
break
except (LookupError, urllib.error.URLError, urllib.error.HTTPError, TimeoutError, OSError) as exc:
failures.append(safe_error(exc))
if not isinstance(raw, Mapping):
raise LookupError("RDAP endpoints unavailable: " + "; ".join(failures))
entities = raw.get("entities") if isinstance(raw.get("entities"), list) else []
names = []
for entity in entities:
if not isinstance(entity, Mapping):
continue
card = rdap_vcard(entity)
if card.get("organization"):
names.append(card["organization"])
allocation = first(raw, "cidr0_cidrs", "cidr")
if isinstance(allocation, list) and allocation and isinstance(allocation[0], Mapping):
item = allocation[0]
allocation = f"{item.get('v4prefix') or item.get('v6prefix')}/{item.get('length')}"
if not isinstance(allocation, str):
allocation = prefix_from_range(raw.get("startAddress"), raw.get("endAddress"))
data = clean_data(
allocation_prefix=allocation, registry_country=raw.get("country"),
organization=names[0] if names else None,
)
return {"data": data, "source_url": source_url}
def lookup_ripestat(ip: str, timeout: float) -> Dict[str, Any]:
url = with_query("https://stat.ripe.net/data/routing-status/data.json", {"resource": ip})
raw = request_json(url, timeout)
data_raw = raw.get("data") if isinstance(raw.get("data"), Mapping) else {}
origins = data_raw.get("origins") if isinstance(data_raw.get("origins"), list) else []
origin = origins[0] if origins and isinstance(origins[0], Mapping) else {}
prefixes = data_raw.get("announced_space") if isinstance(data_raw.get("announced_space"), Mapping) else {}
last_seen = data_raw.get("last_seen") if isinstance(data_raw.get("last_seen"), Mapping) else {}
route_prefix = first(data_raw, "prefix") or first(prefixes, "v4", "v6") or last_seen.get("prefix")
data = clean_data(
route_prefix=route_prefix, asn=normalize_asn(first(origin, "origin", "asn")),
organization=first(origin, "holder", "name"), visibility=data_raw.get("visibility"),
first_seen=data_raw.get("first_seen"), last_seen=data_raw.get("last_seen"),
)
if not data:
raise LookupError("RIPEstat returned no routing data")
return {"data": data, "source_url": f"https://stat.ripe.net/{ip}"}
def lookup_ipapi_is(ip: str, timeout: float) -> Dict[str, Any]:
raw = request_json(with_query("https://api.ipapi.is/", {"q": ip}), timeout)
if raw.get("error"):
raise LookupError(text_value(raw.get("reason")) or "ipapi.is lookup failed")
echoed_ip = text_value(raw.get("ip"))
if not echoed_ip:
raise LookupError("ipapi.is response did not echo the target IP")
try:
echoed_ip = ipaddress.ip_address(echoed_ip).compressed
except ValueError as exc:
raise LookupError("ipapi.is returned an invalid target IP") from exc
if echoed_ip != ip:
raise LookupError("ipapi.is response did not echo the target IP")
company = nested(raw, "company")
asn = nested(raw, "asn")
location = nested(raw, "location")
data = clean_data(
country_code=first(location, "country_code", "country") or raw.get("cc"),
country=location.get("country") or raw.get("country"),
region=first(location, "state", "region"), city=location.get("city"),
asn=normalize_asn(first(asn, "asn", "number") or raw.get("asn_num")),
organization=first(asn, "org", "name") or raw.get("asn_org"),
isp=first(company, "name", "domain") or raw.get("company_name"),
network_type=first(company, "type", "network") or raw.get("company_type"),
route_prefix=first(asn, "route", "prefix"),
latitude=first(location, "latitude", "lat") or raw.get("lat"),
longitude=first(location, "longitude", "lon") or raw.get("lon"),
is_proxy=boolean(raw.get("is_proxy")), is_vpn=boolean(raw.get("is_vpn")),
is_tor=boolean(raw.get("is_tor")), is_hosting=boolean(raw.get("is_datacenter")),
is_abuser=boolean(raw.get("is_abuser")), is_bot=boolean(raw.get("is_crawler")),
abuse_score=number(first(raw, "abuse_score", "risk_score")),
)
return {"data": data, "source_url": f"https://api.ipapi.is/?q={ip}"}
def lookup_proxycheck(ip: str, timeout: float) -> Dict[str, Any]:
params = {"vpn": 1, "asn": 1, "risk": 1}
raw = request_json(with_query(f"https://proxycheck.io/v2/{urllib.parse.quote(ip)}", params), timeout)
if raw.get("status") != "ok" or not isinstance(raw.get(ip), Mapping):
raise LookupError(text_value(raw.get("message")) or "proxycheck.io lookup failed")
item = raw[ip]
operator = nested(item, "operator")
data = clean_data(
country_code=item.get("isocode"), country=item.get("country"), region=item.get("region"),
city=item.get("city"), asn=normalize_asn(item.get("asn")), organization=first(operator, "name", "url"),
network_type=item.get("type"), is_proxy=boolean(item.get("proxy")),
is_vpn=True if str(item.get("type", "")).lower() == "vpn" else None,
risk_score=number(item.get("risk")),
)
return {"data": data, "source_url": f"https://proxycheck.io/v2/{ip}"}
def lookup_ping0(ip: str, timeout: float) -> Dict[str, Any]:
# Ping0's documented /geo endpoint only supports the caller's own IP. Its public
# arbitrary-IP page is therefore best-effort and intentionally isolated here.
url = f"https://ping0.cc/ip/{urllib.parse.quote(ip)}"
body, headers = request_bytes(url, timeout, {"Accept": "text/html"})
charset_match = re.search(r"charset=([^;]+)", headers.get("Content-Type", ""), re.I)
charset = charset_match.group(1).strip() if charset_match else "utf-8"
try:
page = body.decode(charset, "replace")
except LookupError:
page = body.decode("utf-8", "replace")
visible = html.unescape(re.sub(r"<[^>]+>", " ", page))
visible = re.sub(r"\s+", " ", visible)
if ip not in visible and ip not in page:
raise LookupError("Ping0.cc response did not echo the target IP")
def capture(pattern: str) -> Optional[str]:
match = re.search(pattern, visible, re.I)
return match.group(1).strip() if match else None
risk = capture(r"(?:风控值|risk)[^0-9]{0,40}(\d{1,3})\s*%")
asn = capture(r"\b(AS\d{1,10})\b")
location = capture(r"(?:IP\s*位置|location)\s*[::]?\s*([^|]{2,100}?)(?:探测时间|ASN|风险|risk)")
page_type = capture(r"(?:IP\s*类型|type)\s*[::]?\s*([^|]{2,80}?)(?:为什么|风控值|risk)")
native_class = capture(r"原生\s*IP\s*(?:[((]?\s*说明\s*[??]?\s*[))]?)?\s*([^|]{2,60}?)(?:大模型检测|AI)")
location = re.sub(r"^[\s,,::??()()]+|[\s,,::??()()]+$", "", location or "") or None
page_type = re.sub(r"[((]?\s*说明\s*[??]?\s*[))]?", "", page_type or "").strip(" ,,::") or None
native_class = re.sub(r"[((]?\s*说明\s*[??]?\s*[))]?", "", native_class or "").strip(" ,,::") or None
native_bool = None
if native_class:
if re.search(r"(?:非原生|广播|not\s+native)", native_class, re.I):
native_bool = False
elif re.search(r"(?:原生|native)", native_class, re.I):
native_bool = True
lowered = visible.lower()
data = clean_data(
asn=normalize_asn(asn), location_text=location, network_type=page_type,
risk_score=number(risk), is_hosting=True if any(x in lowered for x in ("idc机房 ip", "datacenter ip")) else None,
is_vpn=True if re.search(r"\bVPN\b", visible, re.I) else None,
is_proxy=True if re.search(r"(?:代理|proxy)\s*IP", visible, re.I) else None,
native_classification=native_class, native_ip=native_bool,
)
if len(data) < 2:
raise LookupError("Ping0.cc page layout was not parseable")
return {"data": data, "source_url": url}
def lookup_ipinfo(ip: str, timeout: float) -> Dict[str, Any]:
token = os.environ["IPINFO_TOKEN"]
raw = request_json(f"https://api.ipinfo.io/lite/{urllib.parse.quote(ip)}", timeout,
{"Authorization": f"Bearer {token}"})
privacy = raw.get("privacy") if isinstance(raw.get("privacy"), Mapping) else {}
asn_obj = raw.get("asn") if isinstance(raw.get("asn"), Mapping) else {}
loc = text_value(raw.get("loc"))
latitude, longitude = (loc.split(",", 1) if loc and "," in loc else (None, None))
data = clean_data(
country_code=first(raw, "country_code", "country"), country=first(raw, "country_name"),
region=raw.get("region"), city=raw.get("city"), asn=normalize_asn(first(raw, "asn", "as_number") or asn_obj.get("asn")),
organization=first(raw, "as_name", "org") or first(asn_obj, "name", "domain"),
network_type=first(raw, "as_type") or asn_obj.get("type"),
latitude=latitude, longitude=longitude, is_proxy=boolean(privacy.get("proxy")),
is_vpn=boolean(privacy.get("vpn")), is_tor=boolean(privacy.get("tor")),
is_hosting=boolean(privacy.get("hosting")),
)
return {"data": data, "source_url": f"https://ipinfo.io/{ip}"}
def lookup_disabled(ip: str, timeout: float) -> Dict[str, Any]:
raise LookupError("Official API disabled in compliance mode; use validated public-page evidence")
def lookup_abuseipdb(ip: str, timeout: float) -> Dict[str, Any]:
url = with_query("https://api.abuseipdb.com/api/v2/check", {"ipAddress": ip, "maxAgeInDays": 90, "verbose": "true"})
raw = request_json(url, timeout, {"Key": os.environ["ABUSEIPDB_API_KEY"], "Accept": "application/json"})
item = raw.get("data") if isinstance(raw.get("data"), Mapping) else {}
if item.get("ipAddress") != ip:
raise LookupError("AbuseIPDB response did not echo the target IP")
data = clean_data(
country_code=item.get("countryCode"), isp=item.get("isp"), organization=item.get("domain"),
network_type=item.get("usageType"), risk_score=number(item.get("abuseConfidenceScore")),
is_abuser=True if (number(item.get("abuseConfidenceScore")) or 0) >= 50 else False,
total_reports=item.get("totalReports"), distinct_users=item.get("numDistinctUsers"),
last_reported_at=item.get("lastReportedAt"), is_whitelisted=boolean(item.get("isWhitelisted")),
is_tor=boolean(item.get("isTor")),
)
return {"data": data, "source_url": f"https://www.abuseipdb.com/check/{ip}"}
PROVIDERS: Tuple[Provider, ...] = (
Provider("geojs", "GeoJS", "geo", lookup_geojs),
Provider("rdap", "RDAP.org", "registry", lookup_rdap),
Provider("ripestat", "RIPEstat", "routing", lookup_ripestat),
Provider("ipapi-is", "ipapi.is", "risk-network", lookup_ipapi_is),
Provider("proxycheck", "proxycheck.io", "risk", lookup_proxycheck),
Provider("ping0", "Ping0.cc", "risk-network", lookup_ping0, experimental=True),
Provider("ipinfo", "IPinfo", "geo-network-risk", lookup_ipinfo, ("IPINFO_TOKEN",)),
Provider("scamalytics", "Scamalytics", "risk", lookup_disabled, api_enabled=False),
Provider("ipqs", "IPQualityScore", "risk", lookup_disabled, api_enabled=False),
Provider("ipdata", "ipdata", "geo-network-risk", lookup_disabled, api_enabled=False),
Provider("abuseipdb", "AbuseIPDB", "abuse", lookup_abuseipdb, ("ABUSEIPDB_API_KEY",)),
)
def run_provider(provider: Provider, ip: str, timeout: float, retries: int = DEFAULT_RETRIES) -> ProviderResult:
REQUEST_STATE.allowed_domains = None
REQUEST_STATE.request_domains = set()
if not provider.api_enabled:
return ProviderResult(provider.id, provider.name, provider.category, "not-requested",
message="Official API disabled in compliance mode; use validated public-page evidence",
experimental=provider.experimental, collection_method="disabled")
if provider.credential_env and not provider.configured():
missing = [name for name in provider.credential_env if not os.environ.get(name, "").strip()]
return ProviderResult(provider.id, provider.name, provider.category, "skipped",
message=f"Missing credentials: {', '.join(missing)}",
experimental=provider.experimental)
started = time.monotonic()
REQUEST_STATE.retries = retries
REQUEST_STATE.attempts = 0
configured_domains = PROVIDER_ALLOWED_DOMAINS.get(provider.id)
REQUEST_STATE.allowed_domains = (frozenset(configured_domains)
if configured_domains is not None else None)
REQUEST_STATE.request_domains = set()
try:
response = provider.lookup(ip, timeout)
elapsed = round((time.monotonic() - started) * 1000)
data = response.get("data", {})
if not isinstance(data, dict) or not data:
raise LookupError("Provider returned no usable fields")
return ProviderResult(provider.id, provider.name, provider.category, "success", elapsed,
response.get("source_url"), data,
experimental=provider.experimental, attempts=REQUEST_STATE.attempts,
request_domains=tuple(sorted(REQUEST_STATE.request_domains)))
except (LookupError, urllib.error.URLError, urllib.error.HTTPError, TimeoutError,
json.JSONDecodeError, OSError, ValueError) as exc:
elapsed = round((time.monotonic() - started) * 1000)
status = "unavailable" if provider.experimental else "error"
return ProviderResult(provider.id, provider.name, provider.category, status, elapsed,
message=safe_error(exc), experimental=provider.experimental,
attempts=REQUEST_STATE.attempts,
request_domains=tuple(sorted(REQUEST_STATE.request_domains)))
except Exception as exc: # isolate unexpected adapter failures without hiding their class
elapsed = round((time.monotonic() - started) * 1000)
return ProviderResult(provider.id, provider.name, provider.category, "error", elapsed,
message=f"Unexpected {exc.__class__.__name__}: {safe_error(exc)}",
experimental=provider.experimental, attempts=REQUEST_STATE.attempts,
request_domains=tuple(sorted(REQUEST_STATE.request_domains)))
finally:
REQUEST_STATE.allowed_domains = None
def normalized_key(field: str, value: Any) -> str:
text = str(value).strip()
if field in ("country_code", "asn"):
return text.upper()
return re.sub(r"\s+", " ", text).casefold()
def compatible_fact(field: str, left: Any, right: Any) -> bool:
if field not in ("city", "region", "organization", "isp"):
return False
a, b = normalized_key(field, left), normalized_key(field, right)
# Parenthetical locality qualifiers and legal suffix/detail frequently vary
# without representing a real contradiction.
base_a = re.sub(r"\s*[((][^))]*[))]\s*$", "", a).strip()
base_b = re.sub(r"\s*[((][^))]*[))]\s*$", "", b).strip()
return bool(base_a and base_b and (base_a == base_b or base_a in base_b or base_b in base_a))
def fuse_facts(results: Sequence[ProviderResult]) -> Dict[str, Any]:
fused: Dict[str, Any] = {}
for field_name in FACT_FIELDS:
groups: Dict[str, Dict[str, Any]] = {}
for result in results:
if result.status != "success" or compact(result.data.get(field_name)) is None:
continue
value = result.data[field_name]
key = normalized_key(field_name, value)
for existing_key, existing in groups.items():
if compatible_fact(field_name, existing["value"], value):
key = existing_key
break
group = groups.setdefault(key, {"value": value, "sources": []})
group["sources"].append(result.id)
if not groups:
continue
ranked = sorted(groups.values(), key=lambda item: (-len(item["sources"]), str(item["value"])))
top_count = len(ranked[0]["sources"])
tied = len(ranked) > 1 and len(ranked[1]["sources"]) == top_count