| 8 | |
| 9 | |
| 10 | class PythonHandler(BaseHandler): |
| 11 | |
| 12 | def __init__(self, script): |
| 13 | self.module = self._load(script) |
| 14 | self.r = self.module.recv |
| 15 | self.s = self.module.send |
| 16 | |
| 17 | # shamelessly stolen from mitmproxy: |
| 18 | # Copyright (c) 2013, Aldo Cortesi. All rights reserved. |
| 19 | # |
| 20 | # Permission is hereby granted, free of charge, to any person obtaining a copy |
| 21 | # of this software and associated documentation files (the "Software"), to deal |
| 22 | # in the Software without restriction, including without limitation the rights |
| 23 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 24 | # copies of the Software, and to permit persons to whom the Software is |
| 25 | # furnished to do so, subject to the following conditions: |
| 26 | # |
| 27 | # The above copyright notice and this permission notice shall be included in |
| 28 | # all copies or substantial portions of the Software. |
| 29 | # |
| 30 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 31 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 32 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 33 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 34 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 35 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 36 | # SOFTWARE. |
| 37 | |
| 38 | def _load(self, path): |
| 39 | fullname = "__hallucinate_script__.{}".format( |
| 40 | os.path.splitext(os.path.basename(path))[0] |
| 41 | ) |
| 42 | sys.modules.pop(fullname, None) |
| 43 | oldpath = sys.path |
| 44 | sys.path.insert(0, os.path.dirname(path)) |
| 45 | m = None |
| 46 | try: |
| 47 | loader = importlib.machinery.SourceFileLoader(fullname, path) |
| 48 | spec = importlib.util.spec_from_loader(fullname, loader=loader) |
| 49 | m = importlib.util.module_from_spec(spec) |
| 50 | loader.exec_module(m) |
| 51 | if not getattr(m, "name", None): |
| 52 | m.name = path |
| 53 | return m |
| 54 | except Exception as exc: |
| 55 | exception = type(exc).__name__ |
| 56 | lineno = "" |
| 57 | if hasattr(exc, "lineno"): |
| 58 | lineno = str(exc.lineno) |
| 59 | logging.error("Error in script %s:%s %s", path, lineno, exception) |
| 60 | raise |
| 61 | finally: |
| 62 | sys.path[:] = oldpath |
| 63 | |
| 64 | def send(self, data, p): |
| 65 | return self.s(data, p) |
| 66 | |
| 67 | def recv(self, data, p): |
no outgoing calls
no test coverage detected