Пример #1
0
    def tick(self):
        if time.time() - self.last_load > self.ReloadInterval:
            try:
                mtime = os.stat(self.fullpath).st_mtime
            except FileNotFoundError:
                scripts = list(ctx.options.scripts)
                scripts.remove(self.path)
                ctx.options.update(scripts=scripts)
                return

            if mtime > self.last_mtime:
                ctx.log.info("Loading script: %s" % self.path)
                if self.ns:
                    ctx.master.addons.remove(self.ns)
                self.ns = None
                with addonmanager.safecall():
                    ns = load_script(self.fullpath)
                    ctx.master.addons.register(ns)
                    self.ns = ns
                if self.ns:
                    # We're already running, so we have to explicitly register and
                    # configure the addon
                    ctx.master.addons.invoke_addon(self.ns, "running")
                    ctx.master.addons.invoke_addon(
                        self.ns,
                        "configure",
                        ctx.options.keys()
                    )
                self.last_load = time.time()
                self.last_mtime = mtime
Пример #2
0
async def test_script_print_stdout():
    with taddons.context() as tctx:
        with addonmanager.safecall():
            ns = script.load_script(
                tutils.test_data.path("mitmproxy/data/addonscripts/print.py"))
            ns.load(addonmanager.Loader(tctx.master))
        assert await tctx.master.await_log("stdoutprint")
Пример #3
0
    def tick(self):
        if time.time() - self.last_load > self.ReloadInterval:
            try:
                mtime = os.stat(self.fullpath).st_mtime
            except FileNotFoundError:
                scripts = list(ctx.options.scripts)
                scripts.remove(self.path)
                ctx.options.update(scripts=scripts)
                return

            if mtime > self.last_mtime:
                ctx.log.info("Loading script: %s" % self.path)
                if self.ns:
                    ctx.master.addons.remove(self.ns)
                self.ns = None
                with addonmanager.safecall():
                    ns = load_script(self.fullpath)
                    ctx.master.addons.register(ns)
                    self.ns = ns
                if self.ns:
                    # We're already running, so we have to explicitly register and
                    # configure the addon
                    ctx.master.addons.invoke_addon(self.ns, "running")
                    ctx.master.addons.invoke_addon(
                        self.ns,
                        "configure",
                        ctx.options.keys()
                    )
                self.last_load = time.time()
                self.last_mtime = mtime
Пример #4
0
async def test_script_print_stdout(tdata):
    with taddons.context() as tctx:
        with addonmanager.safecall():
            ns = script.load_script(
                tdata.path("mitmproxy/data/addonscripts/print.py")
            )
            ns.load(addonmanager.Loader(tctx.master))
        assert await tctx.master.await_log("stdoutprint")
Пример #5
0
def test_script_print_stdout():
    with taddons.context() as tctx:
        with mock.patch('mitmproxy.ctx.master.tell') as mock_warn:
            with addonmanager.safecall():
                ns = script.load_script(
                    tutils.test_data.path(
                        "mitmproxy/data/addonscripts/print.py"))
                ns.load(addonmanager.Loader(tctx.master))
        mock_warn.assert_called_once_with("log",
                                          log.LogEntry("stdoutprint", "warn"))
Пример #6
0
def test_script_print_stdout():
    with taddons.context() as tctx:
        with mock.patch('mitmproxy.ctx.master.tell') as mock_warn:
            with addonmanager.safecall():
                ns = script.load_script(
                    tutils.test_data.path(
                        "mitmproxy/data/addonscripts/print.py"
                    )
                )
                ns.load(addonmanager.Loader(tctx.master))
        mock_warn.assert_called_once_with("log", log.LogEntry("stdoutprint", "warn"))
Пример #7
0
 def loadscript(self):
     ctx.log.info("Loading script %s" % self.path)
     if self.ns:
         ctx.master.addons.remove(self.ns)
     self.ns = None
     with addonmanager.safecall():
         ns = load_script(self.fullpath)
         ctx.master.addons.register(ns)
         self.ns = ns
     if self.ns:
         ctx.master.addons.invoke_addon(self.ns, "configure",
                                        ctx.options.keys())
Пример #8
0
def load_script(actx, path):
    if not os.path.exists(path):
        ctx.log.info("No such file: %s" % path)
        return
    loader = importlib.machinery.SourceFileLoader(os.path.basename(path), path)
    try:
        oldpath = sys.path
        sys.path.insert(0, os.path.dirname(path))
        with addonmanager.safecall():
            m = loader.load_module()
            if not getattr(m, "name", None):
                m.name = path
            return m
    finally:
        sys.path[:] = oldpath
Пример #9
0
 def loadscript(self):
     ctx.log.info("Loading script %s" % self.path)
     if self.ns:
         ctx.master.addons.remove(self.ns)
     self.ns = None
     with addonmanager.safecall():
         ns = load_script(self.fullpath)
         ctx.master.addons.register(ns)
         self.ns = ns
     if self.ns:
         # We're already running, so we have to explicitly register and
         # configure the addon
         ctx.master.addons.invoke_addon(self.ns, "running")
         ctx.master.addons.invoke_addon(self.ns, "configure",
                                        ctx.options.keys())
Пример #10
0
def load_script(actx, path):
    if not os.path.exists(path):
        ctx.log.info("No such file: %s" % path)
        return
    loader = importlib.machinery.SourceFileLoader(os.path.basename(path), path)
    try:
        oldpath = sys.path
        sys.path.insert(0, os.path.dirname(path))
        with addonmanager.safecall():
            m = loader.load_module()
            if not getattr(m, "name", None):
                m.name = path
            return m
    finally:
        sys.path[:] = oldpath
Пример #11
0
 def loadscript(self):
     ctx.log.info("Loading script %s" % self.path)
     if self.ns:
         ctx.master.addons.remove(self.ns)
     self.ns = None
     with addonmanager.safecall():
         ns = load_script(self.fullpath)
         ctx.master.addons.register(ns)
         self.ns = ns
     if self.ns:
         # We're already running, so we have to explicitly register and
         # configure the addon
         ctx.master.addons.invoke_addon(self.ns, hooks.RunningHook())
         try:
             ctx.master.addons.invoke_addon(
                 self.ns, hooks.ConfigureHook(ctx.options.keys()))
         except exceptions.OptionsError as e:
             script_error_handler(self.fullpath, e, msg=str(e))
Пример #12
0
 def loadscript(self):
     ctx.log.info("Loading script %s" % self.path)
     if self.ns:
         ctx.master.addons.remove(self.ns)
     self.ns = None
     with addonmanager.safecall():
         ns = load_script(self.fullpath)
         ctx.master.addons.register(ns)
         self.ns = ns
     if self.ns:
         # We're already running, so we have to explicitly register and
         # configure the addon
         ctx.master.addons.invoke_addon(self.ns, "running")
         ctx.master.addons.invoke_addon(
             self.ns,
             "configure",
             ctx.options.keys()
         )
Пример #13
0
 def script_run(self, flows: typing.Sequence[flow.Flow],
                path: mtypes.Path) -> None:
     """
         Run a script on the specified flows. The script is configured with
         the current options and all lifecycle events for each flow are
         simulated. Note that the load event is not invoked.
     """
     if not os.path.isfile(path):
         ctx.log.error('No such script: %s' % path)
         return
     mod = load_script(path)
     if mod:
         with addonmanager.safecall():
             ctx.master.addons.invoke_addon(mod, "running")
             ctx.master.addons.invoke_addon(mod, "configure",
                                            ctx.options.keys())
             for f in flows:
                 for evt, arg in eventsequence.iterate(f):
                     ctx.master.addons.invoke_addon(mod, evt, arg)
Пример #14
0
 def script_run(self, flows: typing.Sequence[flow.Flow], path: mtypes.Path) -> None:
     """
         Run a script on the specified flows. The script is configured with
         the current options and all lifecycle events for each flow are
         simulated. Note that the load event is not invoked.
     """
     if not os.path.isfile(path):
         ctx.log.error('No such script: %s' % path)
         return
     mod = load_script(path)
     if mod:
         with addonmanager.safecall():
             ctx.master.addons.invoke_addon(mod, "running")
             ctx.master.addons.invoke_addon(
                 mod,
                 "configure",
                 ctx.options.keys()
             )
             for f in flows:
                 for evt, arg in eventsequence.iterate(f):
                     ctx.master.addons.invoke_addon(mod, evt, arg)
Пример #15
0
def load_script(actx, path):
    if not os.path.exists(path):
        ctx.log.info("No such file: %s" % path)
        return

    fullname = "__mitmproxy_script__.{}".format(
        os.path.splitext(os.path.basename(path))[0])
    # the fullname is not unique among scripts, so if there already is an existing script with said
    # fullname, remove it.
    sys.modules.pop(fullname, None)
    try:
        oldpath = sys.path
        sys.path.insert(0, os.path.dirname(path))
        with addonmanager.safecall():
            loader = importlib.machinery.SourceFileLoader(fullname, path)
            spec = importlib.util.spec_from_loader(fullname, loader=loader)
            m = importlib.util.module_from_spec(spec)
            loader.exec_module(m)
            if not getattr(m, "name", None):
                m.name = path
            return m
    finally:
        sys.path[:] = oldpath