def find_containing_network(ip_ranges, address): """Find an IP network that contains the given address.""" addr = ipaddress.ip_address(address) for network in ip_ranges: if addr in network: return network return None
def is_throttled(address): """Determine if an IP address is in a throttled range.""" addr = ipaddress.ip_address(address) for network in g.throttles: if addr in network: return True return False
def set_account_ip(account_id, ip, date=None): """Set an IP address as having accessed an account. Updates all underlying datastores. """ # don't store private IPs, send a graphite event so we can alert on this if ip_address(ip).is_private: g.stats.simple_event('ip.private_ip_storage_prevented') return if date is None: date = datetime.datetime.now(g.tz) m = Mutator(CONNECTION_POOL) m.insert(IPsByAccount._cf, str(account_id), {date: ip}, ttl=CF_TTL) m.insert(AccountsByIP._cf, ip, {date: str(account_id)}, ttl=CF_TTL) m.send()