コード例 #1
0
ファイル: script.py プロジェクト: ronakodhaviya/project-X
    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
ファイル: test_script.py プロジェクト: prmths128/mitmproxy
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
ファイル: script.py プロジェクト: StevenVanAcker/mitmproxy
    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
ファイル: test_script.py プロジェクト: jbremer/mitmproxy
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
ファイル: script.py プロジェクト: tralivali1234/mitmproxy
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
ファイル: script.py プロジェクト: davidpshaw/mitmproxy
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
ファイル: script.py プロジェクト: cortesi/mitmproxy
 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
ファイル: script.py プロジェクト: cortesi/mitmproxy
 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
ファイル: script.py プロジェクト: timothywei/mitmproxy
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