#!/usr/bin/env python3
# v0.1 - 2021-10-14

# config
IFNAME = "eth0"
SLEEP = 0.15


# code
from itertools import chain
from progressbar import *
from time import sleep
import netifaces as ni
import socket
import sys


class Vhosts:
    def __init__(self):
        ipv4_l = ni.ifaddresses(IFNAME)[ni.AF_INET]
        ipv6_l = [
            af_inet6
            for af_inet6 in ni.ifaddresses(IFNAME)[ni.AF_INET6]
            if not af_inet6.get("addr").startswith("fe80::")
        ]
        ip_l = list(chain(ipv4_l[::-1], ipv6_l[::-1]))
        vhosts_l = []

        widgets = [
            RotatingMarker(),
            " ",
            FormatLabel("resolving hosts..."),
            " ",
            Bar(),
            " ",
            Percentage(),
            " ",
            Counter(format="[%(value)d/%(max_value)d]"),
        ]

        with ProgressBar(widgets=widgets, maxval=len(ip_l)) as bar:
            for i, ip in enumerate(ip_l, 1):
                vhosts_l.append(
                    (socket.gethostbyaddr(ip.get("addr"))[0], ip.get("addr"))
                )
                bar.update(i)
                sleep(SLEEP)

        sys.stdout.write("\033[F\033[K")
        for vhost in vhosts_l:
            sys.stdout.write(f"* {vhost[0]}\r\t\t\t\t{vhost[1]}\n")


def main():
    vhosts = Vhosts()


if __name__ == "__main__":
    main()