mirror of
https://github.com/checktheroads/hyperglass
synced 2024-05-11 05:55:08 +00:00
fix network info gathering
This commit is contained in:
@@ -4,7 +4,6 @@
|
||||
import os
|
||||
import json
|
||||
import time
|
||||
from ipaddress import ip_address
|
||||
|
||||
# Third Party
|
||||
from fastapi import HTTPException
|
||||
@@ -33,15 +32,7 @@ else:
|
||||
async def query(query_data: Query, request: Request):
|
||||
"""Ingest request data pass it to the backend application to perform the query."""
|
||||
|
||||
if ip_address(request.client.host).is_loopback:
|
||||
network_info = {"prefix": None, "asn": None}
|
||||
else:
|
||||
network_info = get_network_info(request.client.host)
|
||||
|
||||
network_info = {
|
||||
"prefix": str(network_info["prefix"]),
|
||||
"asn": network_info["asns"][0],
|
||||
}
|
||||
network_info = get_network_info(request.client.host, serialize=True)
|
||||
|
||||
header_keys = (
|
||||
"content-length",
|
||||
@@ -79,6 +70,7 @@ async def query(query_data: Query, request: Request):
|
||||
log.debug(f"Cache Timeout: {cache_timeout}")
|
||||
|
||||
log.info(f"Starting query execution for query {query_data.summary}")
|
||||
|
||||
# Check if cached entry exists
|
||||
if not await cache.get(cache_key):
|
||||
log.debug(f"No existing cache entry for query {cache_key}")
|
||||
|
@@ -695,7 +695,7 @@ def parse_exception(exc):
|
||||
return ", caused by ".join(parsed)
|
||||
|
||||
|
||||
def get_network_info(valid_ip):
|
||||
def get_network_info(ip, serialize=False):
|
||||
"""Get containing prefix for an IP host query from RIPEstat API.
|
||||
|
||||
Arguments:
|
||||
@@ -709,10 +709,17 @@ def get_network_info(valid_ip):
|
||||
{IPv4Network|IPv6Network} -- Valid IP Network object
|
||||
"""
|
||||
import httpx
|
||||
from ipaddress import ip_network
|
||||
from ipaddress import ip_network, ip_address
|
||||
from hyperglass.exceptions import InputInvalid
|
||||
|
||||
log.debug("Attempting to find containing prefix for {ip}", ip=str(valid_ip))
|
||||
log.debug("Attempting to find network details for {ip}", ip=str(ip))
|
||||
|
||||
try:
|
||||
valid_ip = ip_address(ip)
|
||||
if not valid_ip.is_global:
|
||||
return {"prefix": None, "asn": None}
|
||||
except ValueError:
|
||||
return {"prefix": None, "asn": None}
|
||||
|
||||
try:
|
||||
response = httpx.get(
|
||||
@@ -737,7 +744,11 @@ def get_network_info(valid_ip):
|
||||
i=str(valid_ip),
|
||||
)
|
||||
|
||||
network_info["prefix"] = ip_network(network_info["prefix"])
|
||||
if not serialize:
|
||||
network_info["prefix"] = ip_network(network_info["prefix"])
|
||||
|
||||
if serialize:
|
||||
network_info["asns"] = network_info["asns"][0]
|
||||
|
||||
return network_info
|
||||
|
||||
|
Reference in New Issue
Block a user