Skip to content

Commit

Permalink
fix: asynchronously run boot script alongside setting up the server
Browse files Browse the repository at this point in the history
  • Loading branch information
lavafroth committed Aug 12, 2023
1 parent 1fadfd0 commit 43b911d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
20 changes: 8 additions & 12 deletions src/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@
import socketpool
import wifi
from adafruit_httpserver import POST, FileResponse, Request, Server
from ducky import run_script_file
from ducky import run_boot_script

from api import handle


async def main():
async def setup_server():
"""
Begin a wifi access point defined by the SSID and PASSWORD environment
variables.
Expand Down Expand Up @@ -45,19 +44,16 @@ def api(request: Request):
server.serve_forever(str(wifi.radio.ipv4_address_ap))


async def run_boot_script():
"""
If a script with the name 'boot.dd' exists,
run it without user interaction on boot.
"""
boot_script = 'payloads/boot.dd'
if os.path.exists(boot_script):
run_script_file(boot_script)
async def main():
await asyncio.gather(
run_boot_script(),
setup_server()
)


if __name__ == "__main__":
try:
asyncio.gather(main(), run_boot_script())
asyncio.run(main())
# For some reason, wifi.stop_ap is not implemented.
except NotImplementedError:
microcontroller.reset()
12 changes: 12 additions & 0 deletions src/ducky.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,15 @@ def run_script_file(path: str):
run_script(handle.read())
except OSError as error:
warn(f"unable to open file {path}: {error}")


async def run_boot_script():
"""
If a script with the name 'boot.dd' exists,
run it without user interaction on boot.
"""
try:
with open("payloads/boot.dd", "r", encoding="utf-8") as handle:
run_script(handle.read())
except OSError:
info("boot script does not exist, skipping its execution")

0 comments on commit 43b911d

Please sign in to comment.