package intel import ( "fmt" "net/netip" "regexp" "strings" "sync" ) var ( torMu sync.RWMutex torExits map[netip.Addr]struct{} torRelay map[netip.Addr]struct{} ) // SetTorRelays loads (or clears) the list of running Tor relays. func SetTorRelays(addrs []netip.Addr) { m := make(map[netip.Addr]struct{}, len(addrs)) for _, a := range addrs { m[a.Unmap()] = struct{}{} } torMu.Lock() torRelay = m torMu.Unlock() } // IsTorRelay reports whether ip is a running Tor relay (exits included when the // relay list carries them). func IsTorRelay(ip netip.Addr) bool { torMu.RLock() defer torMu.RUnlock() _, ok := torRelay[ip.Unmap()] return ok } // SetTorExits loads (or clears, with nil/empty) the Tor exit list. It is off // by default; the caller decides where the list comes from. func SetTorExits(addrs []netip.Addr) { m := make(map[netip.Addr]struct{}, len(addrs)) for _, a := range addrs { m[a.Unmap()] = struct{}{} } torMu.Lock() torExits = m torMu.Unlock() } // IsTorExit reports whether ip is in the loaded Tor exit list. func IsTorExit(ip netip.Addr) bool { torMu.RLock() defer torMu.RUnlock() if len(torExits) == 0 { return false } _, ok := torExits[ip.Unmap()] return ok } func asnSet(v ...uint32) map[uint32]bool { m := make(map[uint32]bool, len(v)) for _, x := range v { m[x] = true } return m } var ( vpnASNs = asnSet(9009, 212238, 60068, 136787, 39351) hostingASNs = asnSet(14061, 16509, 14618, 15169, 396982, 8075, 31898, 45102, 132203, 16276, 24940, 63949, 20473, 51167, 9009, 60781, 28753, 12876, 47583, 8560, 36352, 53667, 211252, 49870, 204428, 213371) researchASNs = asnSet(398324, 398705, 398722, 211298, 10439, 202425, 63999, 135377, 396507) cnASNs = asnSet(4134, 4837) mobileASNs = asnSet(26615) ispMobASNs = asnSet(28573, 26599, 18881) brISPASNs = asnSet(7738, 8167, 16735, 53006, 28126, 7162, 27699, 14868, 4230, 263880) vpnRDNS = regexp.MustCompile(`vpn|proxy|exit|relay`) hostOrgRe = regexp.MustCompile(`(?i)hosting|cloud|server|datacenter|data center|vps|colo|digitalocean|ovh|hetzner|linode|akamai|amazon|google|microsoft|oracle|alibaba|tencent|contabo|vultr|leaseweb|choopa|m247|scaleway|hostinger|ionos|kamatera|cloudflare|\bhost\b|locaweb|hospedagem`) hostRDNS = regexp.MustCompile(`(?i)compute|ec2|vps|server|static|cloud|host\d`) mobileRe = regexp.MustCompile(`(?i)mobile|wireless|lte|4g|5g|gprs|cellular`) resRDNS = regexp.MustCompile(`(?i)dsl|dynamic|dyn|pool|dhcp|cable|fibra|ftth|cpe|customer|user|\d+-\d+-\d+-\d+`) ispBrRe = regexp.MustCompile(`(?i)provedor|fibra|telecomunicac|comunicac|internet|tecnologia em internet|\bnet\b`) resOrgRe = regexp.MustCompile(`(?i)telecom|telecomunica|net |broadband|internet`) maxFieldLen = 256 ) func clip(s string) string { if len(s) > maxFieldLen { return s[:maxFieldLen] } return s } // NetTypeIP is NetType preceded by the optional Tor exit list check. func NetTypeIP(ip netip.Addr, asn uint32, asOrg, rdns string) (typ, reason string) { if ip.IsValid() && IsTorExit(ip) { return "tor", "tor-exit-list" } return NetType(asn, asOrg, rdns) } // NetType infers the kind of network behind an address from its ASN, AS // organisation and reverse DNS. Types: vpn, research-scanner, isp-cn, // hosting, mobile, isp/mobile, residential, unknown. reason says which rule // fired. Tor detection needs the address: see NetTypeIP. func NetType(asn uint32, asOrg, rdns string) (typ, reason string) { asOrg, rdns = clip(asOrg), strings.ToLower(clip(rdns)) switch { case vpnASNs[asn]: return "vpn", fmt.Sprintf("asn:%d", asn) case rdns != "" && vpnRDNS.MatchString(rdns): return "vpn", "rdns:" + vpnRDNS.FindString(rdns) case researchASNs[asn]: return "research-scanner", fmt.Sprintf("asn:%d", asn) case cnASNs[asn]: return "isp-cn", fmt.Sprintf("asn:%d", asn) case hostingASNs[asn]: return "hosting", fmt.Sprintf("asn:%d", asn) case mobileASNs[asn]: return "mobile", fmt.Sprintf("asn:%d", asn) case ispMobASNs[asn]: return "isp/mobile", fmt.Sprintf("asn:%d", asn) case brISPASNs[asn]: return "residential", fmt.Sprintf("asn:%d", asn) case asOrg != "" && hostOrgRe.MatchString(asOrg): return "hosting", "asorg:" + strings.ToLower(hostOrgRe.FindString(asOrg)) case rdns != "" && hostRDNS.MatchString(rdns): return "hosting", "rdns:" + strings.ToLower(hostRDNS.FindString(rdns)) case mobileRe.MatchString(asOrg) && asOrg != "": return "mobile", "asorg:" + strings.ToLower(mobileRe.FindString(asOrg)) case rdns != "" && mobileRe.MatchString(rdns): return "mobile", "rdns:" + mobileRe.FindString(rdns) case rdns != "" && resRDNS.MatchString(rdns): return "residential", "rdns:" + resRDNS.FindString(rdns) case asOrg != "" && ispBrRe.MatchString(asOrg): return "residential", "asorg:isp-br" case asOrg != "" && resOrgRe.MatchString(asOrg): return "residential", "asorg:" + strings.ToLower(resOrgRe.FindString(asOrg)) } return "unknown", "" }