Module: OpenASN::Classifier
- Defined in:
- lib/openasn/classifier.rb
Overview
The verdict precedence ladder. First match wins. This ordering IS the product — change it only with the data repo's DECISIONS.md open next to you, because every line encodes a documented false-positive lesson:
1. invalid input -> InvalidIPError (raised in IP.parse)
2. special ranges -> :private / :cgnat
3. relay overlays (Tier B: Apple) -> :relay
4. tor overlays (Tier B) -> :tor_exit
5. vpn ranges (canonical X4B ∪ Tier B provider lists) -> :vpn
6. ASN flag vpn_provider -> :vpn
7. ASN flag enterprise_gw ∪ Tier B gateway ranges -> :enterprise_gateway
8. cloud provider ranges (Tier B) -> :hosting (provider tagged)
9. canonical dc overlay (X4B) -> :hosting
10. flags bad_asn|hosting_extra|cdn or category==hosting -> :hosting
11. ASN flag mobile_carrier -> :mobile
12. category isp, role != tier1_transit -> :residential_isp
13. category business -> :business
14. category education_research -> :education
15. category government_admin -> :government
16. category isp + tier1_transit -> :unknown (pure-backbone ambiguity)
17. ASN found, no category -> :unknown
18. no ASN -> :unknown (unrouted)
Why relay outranks EVERYTHING data-driven: iCloud Private Relay egress lives inside Cloudflare/Akamai space, which the base layer correctly calls hosting — paying iCloud+ customers would classify as datacenter traffic without rule 3. Same defensive logic for enterprise gateways (rule 7 beats the hosting rules: Zscaler ranges are offices, not servers). Rules 12/16: see the data repo's DECISIONS.md D-IMPL-1 — every national telco carries a transit role in upstream data; only pure tier-1 backbone is genuinely ambiguous.
Class Method Summary collapse
- .classify(snapshot, ip_input) ⇒ Object
-
.decide(snapshot, layers, family, ip_int, flags, category, role, asn) ⇒ Object
rubocop:disable Metrics.
- .hosting_sources(flags, category) ⇒ Object
- .overlay_hit(snapshot, family, maps_to, ip_int) ⇒ Object
Class Method Details
.classify(snapshot, ip_input) ⇒ Object
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 |
# File 'lib/openasn/classifier.rb', line 38 def classify(snapshot, ip_input) family, ip_int = IP.parse(ip_input) ip_string = ip_input.is_a?(String) ? ip_input : ip_input.to_s # Rule 2 — specials don't need data at all. if (special = SpecialRanges.match(ip_int, family)) return Result.new(ip: ip_string, verdict: special[0], sources: [special[1]]) end layers = snapshot.family(family) # The base layer is consulted regardless of the winning rule: even a # Tor exit's Result should say which ASN announces it. asn, flags = layers.base.find(ip_int) flags ||= 0 category = BinaryFormat.category_name(flags) role = BinaryFormat.role_name(flags) # Context flags never decide verdicts; they ride along for app logic. context = [] snapshot.(family, "flag:cloudflare_range").each do |(_, layer)| context << :cloudflare_range if layer.cover?(ip_int) end snapshot.(family, "flag:mixed_high_risk").each do |(_, layer)| context << :mixed_high_risk if layer.cover?(ip_int) end verdict, provider, sources = decide(snapshot, layers, family, ip_int, flags, category, role, asn) Result.new( ip: ip_string, verdict: verdict, asn: asn, as_org: snapshot.org_name(asn), category: category, network_role: role, provider: provider, sources: sources, flags: flags, context_flags: context, unrouted: asn.nil? ) end |
.decide(snapshot, layers, family, ip_int, flags, category, role, asn) ⇒ Object
rubocop:disable Metrics
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 |
# File 'lib/openasn/classifier.rb', line 75 def decide(snapshot, layers, family, ip_int, flags, category, role, asn) # rubocop:disable Metrics # 3 — relay if (hit = (snapshot, family, "relay", ip_int)) return [:relay, hit.provider, [hit.id.to_sym]] end # 4 — tor if (hit = (snapshot, family, "tor_exit", ip_int)) return [:tor_exit, hit.provider, [hit.id.to_sym]] end # 5 — vpn ranges. Provider-attributed Tier B lists are consulted # BEFORE the anonymous canonical overlay on purpose: when both match # (common — X4B covers most VPN hosting space), the verdict is # identical but "ProtonVPN" beats provider=nil for explainability. if (hit = (snapshot, family, "vpn", ip_int)) return [:vpn, hit.provider, [hit.id.to_sym]] end return [:vpn, nil, [:x4b_vpn]] if layers.vpn.cover?(ip_int) # 6 — vpn by ASN flag return [:vpn, nil, [:asn_vpn_provider]] if flags.anybits?(BinaryFormat::FLAG_VPN_PROVIDER) # 7 — enterprise gateway (flag, then Tier B ranges) return [:enterprise_gateway, nil, [:asn_enterprise_gw]] if flags.anybits?(BinaryFormat::FLAG_ENTERPRISE_GW) if (hit = (snapshot, family, "enterprise_gateway", ip_int)) return [:enterprise_gateway, hit.provider, [hit.id.to_sym]] end # 8 — cloud provider ranges (provider attribution is the value-add) if (hit = (snapshot, family, "hosting", ip_int)) return [:hosting, hit.provider, [hit.id.to_sym]] end # 9 — canonical datacenter overlay return [:hosting, nil, [:x4b_dc]] if layers.dc.cover?(ip_int) # 10 — hosting by ASN signal if flags.anybits?(BinaryFormat::FLAG_BAD_ASN | BinaryFormat::FLAG_HOSTING_EXTRA | BinaryFormat::FLAG_CDN) || category == "hosting" return [:hosting, nil, hosting_sources(flags, category)] end # 11 — mobile return [:mobile, nil, [:asn_mobile_carrier]] if flags.anybits?(BinaryFormat::FLAG_MOBILE) # 12–17 — category ladder if category == "isp" return role == "tier1_transit" ? [:unknown, nil, [:isp_transit_ambiguous]] : [:residential_isp, nil, [:asn_category]] end return [:business, nil, [:asn_category]] if category == "business" return [:education, nil, [:asn_category]] if category == "education_research" return [:government, nil, [:asn_category]] if category == "government_admin" return [:unknown, nil, [:asn_no_category]] if asn # 18 — unrouted [:unknown, nil, [:unrouted]] end |
.hosting_sources(flags, category) ⇒ Object
143 144 145 146 147 148 149 150 |
# File 'lib/openasn/classifier.rb', line 143 def hosting_sources(flags, category) sources = [] sources << :asn_bad_asn if flags.anybits?(BinaryFormat::FLAG_BAD_ASN) sources << :asn_hosting_extra if flags.anybits?(BinaryFormat::FLAG_HOSTING_EXTRA) sources << :asn_cdn if flags.anybits?(BinaryFormat::FLAG_CDN) sources << :asn_category if category == "hosting" sources end |
.overlay_hit(snapshot, family, maps_to, ip_int) ⇒ Object
136 137 138 139 140 141 |
# File 'lib/openasn/classifier.rb', line 136 def (snapshot, family, maps_to, ip_int) snapshot.(family, maps_to).each do |(entry, layer)| return entry if layer.cover?(ip_int) end nil end |