62 lines
2.1 KiB
Python
62 lines
2.1 KiB
Python
import yaml as Y
|
|
import os as O
|
|
import sys as S
|
|
import types as TYPES
|
|
from stem.control import Controller as C
|
|
|
|
class tor_controller():
|
|
|
|
def __init__(self, args):
|
|
self.is_func_or_meth = lambda f: (
|
|
TYPES.FunctionType
|
|
== type(f)
|
|
or TYPES.BuiltinFunctionType
|
|
== type(f)
|
|
or TYPES.BuiltinMethodType
|
|
== type(f)
|
|
or TYPES.MethodType
|
|
== type(f)
|
|
or TYPES.LambdaType
|
|
== type(f)
|
|
) and True or False
|
|
|
|
if not O.path.exists("{}/.torctlrc".format(O.path.expanduser("~") ) ):
|
|
self.create_config()
|
|
|
|
self.conf = Y.load(open("{}/.torctlrc".format(O.path.expanduser("~") ) ), Loader = Y.FullLoader).get('config')
|
|
|
|
self.controller = C.from_port(address = self.conf.get("host"), port = self.conf.get("port") )
|
|
self.controller.authenticate(self.conf.get("password") )
|
|
|
|
try:
|
|
meth_or_prop = getattr(self.controller, [ x for x in dir(self.controller)
|
|
if not x.startswith("_") and x == args[1].replace("-", "_") ][ 0 ] )
|
|
|
|
if self.is_func_or_meth(meth_or_prop):
|
|
print("result: {}".format(meth_or_prop(*args[ 2: ]) ) )
|
|
else:
|
|
print("{}".format(meth_or_prop))
|
|
|
|
except IndexError as e:
|
|
self.usage(args)
|
|
|
|
def create_config(self):
|
|
self.conf = {}
|
|
self.conf["config"] = {}
|
|
self.conf["config"]["host"] = "127.0.0.1"
|
|
self.conf["config"]["port"] = 9051
|
|
self.conf["config"]["password"] = "password"
|
|
|
|
Y.dump(self.conf, open("{}/.torctlrc".format(O.path.expanduser("~")), "w+") )
|
|
|
|
def usage(self, args):
|
|
print("""
|
|
valid commands are:
|
|
{commands}
|
|
""".format(commands = " ".join([x.replace("_", "-")
|
|
for x in dir(self.controller)
|
|
if not x.startswith("_")] ) ) )
|
|
|
|
tor_controller(S.argv)
|
|
|