"""
Name: onion_sniffer
Title: Onion Sniffer
Author: Cooper
Date: 30/01/2025

Desc:  This uses DNS-SD to discover devices with a given service type.  Useful for seeing what IP devices are at.
This is based off of the original onion_sniffer, but as the components required for it to run are built into hanip
then this is the "lite" version.

"""
import time
import argparse
from hanip.itxpt import DNS_SD

desc = "Onion sniffer"

parser = argparse.ArgumentParser(desc)
parser.add_argument("--verbose", action="store_true", help="Shows everything obtained over DNS-SD")
args = parser.parse_args()

service_dictionary = {
    "1": "_http._tcp.local.",
    "2": "_itxpt_http._tcp.local.",
    "3": "_mqtt._tcp.local.",
    "4": "_mqtt-broker._mqtt._tcp.local.",
    "5": "_homeconnect._tcp.local.",
    "6": "_ibisip_http._tcp.local.",
    "7": "_htcidentityd._udp.local."
}

print("Please pick an option, or enter your own service type: ")
print("\t0) List all available services")
for key, value in service_dictionary.items():
    print("\t%s) %s" % (key, value))

choice = input("Option: ")

try:
    service = service_dictionary[choice]

    dnssd = DNS_SD.DNSSD_Discover(service, "")
    dnssd.run()

    print("Allowing 5 seconds...")
    time.sleep(5)
    print("Devices discovered:")
    devices = dnssd.discovered_services

    for device in devices:
        if args.verbose:
            print(device)
        else:
            print("%s %s" % (device["name"], device["address"]))

    print("Done!\n")

except KeyError:
    if choice == "0":
        dnssd = DNS_SD.DNSSD_Discover("", "")
        dnssd.list_all_services()
        print("DONE!\n")
    elif choice[0] == "_":
        service = choice
    else:
        print("Invalid option")
        exit(1)


