# -*-mode: python; py-indent-offset: 2; indent-tabs-mode: nil; coding: utf-8-unix -*-
# https://stem.torproject.org/tutorials/examples/exit_used.html

import functools
import sys
import getpass
import os

from stem import StreamStatus
from stem.control import EventType, Controller

global LOG
import logging
LOG = logging.getLogger('app.'+'tox_factory')

def sMapaddressResolv(target, iPort=9051):
    if os.path.exists('/var/run/tor/control'):
        controller = Controller.from_socket_file(path='/var/run/tor/control')
    else:
        controller = Controller.from_port(port=iPort)

    try:
        sys.stdout.flush()
        p = getpass.unix_getpass(prompt='Controller Password: ', stream=sys.stderr)
        controller.authenticate(p)

        map_dict = {"0.0.0.0": target}
        map_ret = controller.map_address(map_dict)

        return map_ret
    except Exception as e:
        LOG.exception(e)
    finally:
        del controller

if __name__ == '__main__':
    if len(sys.argv) < 2:
        target = "l2ct3xnuaiwwtoybtn46qp2av4ndxcguwupzyv6xrsmnwi647vvmwtqd"
    else:
        target = sys.argv[1]
    print(sMapaddressResolv(target))

