RELEASE: Version 1.0 #3
@@ -6,6 +6,7 @@ from app.query_normalisation import get_public_ip
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def parse_args() -> argparse.Namespace:
 | 
			
		||||
    """Get arguments from user via the command line."""
 | 
			
		||||
    parser = argparse.ArgumentParser(
 | 
			
		||||
        description="Query information about an IP address or domain name."
 | 
			
		||||
    )
 | 
			
		||||
 
 | 
			
		||||
@@ -5,17 +5,20 @@ import requests
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def get_ip_information(ipv4_address: ipaddress.IPv4Address) -> dict:
 | 
			
		||||
    """Retrieves information about a given IPv4 address from IP-API.com."""
 | 
			
		||||
    api_endpoint = "http://ip-api.com/json/{}".format(ipv4_address)
 | 
			
		||||
    resp = requests.get(api_endpoint).json()
 | 
			
		||||
    return resp
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def get_autonomous_system_number(as_info: str) -> str:
 | 
			
		||||
    """Parses AS number from provided AS information."""
 | 
			
		||||
    as_number = as_info.split(" ")[0]
 | 
			
		||||
    return as_number
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def get_prefix_information(autonomous_system: int) -> list:
 | 
			
		||||
    """Retrieves prefix information about a given autonomous system."""
 | 
			
		||||
    api_endpoint = "https://api.hackertarget.com/aslookup/?q={}".format(
 | 
			
		||||
        str(autonomous_system)
 | 
			
		||||
    )
 | 
			
		||||
 
 | 
			
		||||
@@ -17,6 +17,7 @@ HEADER = """-----------------------------------------------
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def main():
 | 
			
		||||
    """Main function."""
 | 
			
		||||
    args = parse_args()
 | 
			
		||||
    if not args.noheader:
 | 
			
		||||
        print(HEADER)
 | 
			
		||||
 
 | 
			
		||||
@@ -4,6 +4,7 @@ from tabulate import tabulate
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def generate_prefix_string(prefixes: list) -> str:
 | 
			
		||||
    """Generate a string that spilts prefixes into rows of 5."""
 | 
			
		||||
    n = 4
 | 
			
		||||
    try:
 | 
			
		||||
        ret = ""
 | 
			
		||||
@@ -15,4 +16,5 @@ def generate_prefix_string(prefixes: list) -> str:
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def print_table(table_data) -> None:
 | 
			
		||||
    """Print table generated by tabulate."""
 | 
			
		||||
    print(tabulate(table_data))
 | 
			
		||||
 
 | 
			
		||||
@@ -6,6 +6,7 @@ import socket
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def is_ip_address(query: str) -> bool:
 | 
			
		||||
    """Verifies if a given query is a valid IPv4 address."""
 | 
			
		||||
    try:
 | 
			
		||||
        ipaddress.ip_address(query)
 | 
			
		||||
        return True
 | 
			
		||||
@@ -14,6 +15,7 @@ def is_ip_address(query: str) -> bool:
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def resolve_domain_name(domain_name: str) -> ipaddress.IPv4Address:
 | 
			
		||||
    """Resolve a domain name via DNS or return None."""
 | 
			
		||||
    try:
 | 
			
		||||
        ip = socket.gethostbyname(domain_name)
 | 
			
		||||
    except socket.gaierror:
 | 
			
		||||
@@ -22,5 +24,6 @@ def resolve_domain_name(domain_name: str) -> ipaddress.IPv4Address:
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def get_public_ip() -> ipaddress.IPv4Address:
 | 
			
		||||
    """Get the user's current public IPv4 address."""
 | 
			
		||||
    ip = requests.get("https://api.ipify.org").text
 | 
			
		||||
    return ip
 | 
			
		||||
 
 | 
			
		||||
@@ -0,0 +1,6 @@
 | 
			
		||||
certifi==2022.6.15
 | 
			
		||||
charset-normalizer==2.0.12
 | 
			
		||||
idna==3.3
 | 
			
		||||
requests==2.28.0
 | 
			
		||||
tabulate==0.8.10
 | 
			
		||||
urllib3==1.26.9
 | 
			
		||||
 
 | 
			
		||||
@@ -8,18 +8,21 @@ from app.ip_info import (
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_get_ip_information() -> None:
 | 
			
		||||
    """TEST: ensure that the IP information API is working correctly."""
 | 
			
		||||
    test_query = "1.2.3.4"
 | 
			
		||||
    ip_info = get_ip_information(test_query)
 | 
			
		||||
    assert ip_info.get("status") == "success" and ip_info.get("query") == test_query
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_get_autonomous_system_number() -> None:
 | 
			
		||||
    """TEST: ensure that AS information is parsed into AS number correctly."""
 | 
			
		||||
    as_info = "AS5089 Virgin Media Limited"
 | 
			
		||||
    as_number = get_autonomous_system_number(as_info)
 | 
			
		||||
    assert as_number == "AS5089"
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_get_prefix_information() -> None:
 | 
			
		||||
    """TEST: ensure that advertised prefixes are retrieved correctly."""
 | 
			
		||||
    autonomous_system = "AS109"
 | 
			
		||||
    prefixes = get_prefix_information(autonomous_system)
 | 
			
		||||
    assert "144.254.0.0/16" in prefixes
 | 
			
		||||
 
 | 
			
		||||
@@ -4,31 +4,37 @@ from app.query_normalisation import is_ip_address, resolve_domain_name, get_publ
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_is_ip_address_true() -> None:
 | 
			
		||||
    """TEST: Verifies if a given query is a valid IPv4 address."""
 | 
			
		||||
    test_query = "1.2.3.4"
 | 
			
		||||
    assert is_ip_address(test_query)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_is_ip_address_false_ip() -> None:
 | 
			
		||||
    """TEST: Verifies that None is returned if an invalid IP is given."""
 | 
			
		||||
    test_query = "256.315.16.23"
 | 
			
		||||
    assert not is_ip_address(test_query)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_is_ip_address_false_fqdn() -> None:
 | 
			
		||||
    """TEST: Verifies that None is returned if a domain name is given."""
 | 
			
		||||
    test_query = "google.com"
 | 
			
		||||
    assert not is_ip_address(test_query)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_resolve_domain_name_true() -> None:
 | 
			
		||||
    """TEST: Verifies that DNS resolution is working correctly."""
 | 
			
		||||
    domain_name = "one.one.one.one"
 | 
			
		||||
    expected_results = ["1.1.1.1", "1.0.0.1"]  # Could resolve to either IP
 | 
			
		||||
    assert resolve_domain_name(domain_name) in expected_results
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_resolve_domain_name_false() -> None:
 | 
			
		||||
    """TEST: Verifiees that a non-existent domain is not resolved."""
 | 
			
		||||
    domain_name = "hrrijoresdo.com"
 | 
			
		||||
    assert not resolve_domain_name(domain_name)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_get_public_ip() -> None:
 | 
			
		||||
    """TEST: Verifies that the current public IP is retrieved correctly."""
 | 
			
		||||
    public_ip = get_public_ip()
 | 
			
		||||
    assert is_ip_address(public_ip)
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user