Skip to content

Commit

Permalink
add a small script to pre-generate the mitmproxy certs
Browse files Browse the repository at this point in the history
  • Loading branch information
rarescosma committed Oct 16, 2024
1 parent 5ea2bb7 commit fae9cc1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
20 changes: 19 additions & 1 deletion octotail/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import dataclasses
import multiprocessing as mp
import sys
import time
from pathlib import Path
from threading import Event
from typing import Dict, Union
Expand All @@ -18,7 +19,7 @@
from octotail.cli import Opts, entrypoint
from octotail.fmt import Formatter
from octotail.gh import JobDone, RunWatcher, WorkflowDone, get_active_run, guess_repo
from octotail.mitm import ProxyWatcher, WsSub
from octotail.mitm import MITM_CONFIG_DIR, ProxyWatcher, WsSub
from octotail.streamer import OutputItem, run_streamer
from octotail.utils import debug, find_free_port, log

Expand Down Expand Up @@ -134,5 +135,22 @@ def _main(opts: Opts) -> None:
ActorRegistry.stop_all()


def generate_cert() -> None:
# start the proxy_watcher actor and wait until a cert is generated
port = find_free_port()
mitm = ProxyWatcher.start(None, port)
cert_file = MITM_CONFIG_DIR / "mitmproxy-ca-cert.cer"
tries = 0
while (not cert_file.exists()) or (not cert_file.stat().st_size):
if tries > 25:
debug("giving up waiting for mitmproxy to generate a certificate")
sys.exit(1)
time.sleep(0.2)
tries += 1

log(f"{cert_file}", skip_prefix=True)
mitm.stop()


if __name__ == "__main__":
_main()
8 changes: 5 additions & 3 deletions octotail/mitm.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,16 @@ class ProxyWatcher(ThreadingActor):
_proxy_ps: multiprocessing.Process
_q: multiprocessing.Queue

def __init__(self, mgr: ActorRef, port: int):
def __init__(self, mgr: ActorRef | None, port: int):
super().__init__()
self.mgr = mgr
if mgr is not None:
self.mgr = mgr
self._stop_event = mgr.proxy().stop_event.get()
self.port = port
self._stop_event = mgr.proxy().stop_event.get()

def on_start(self) -> None:
self._q = multiprocessing.Queue()
MITM_CONFIG_DIR.mkdir(exist_ok=True, parents=True)
self._proxy_ps = multiprocessing.Process(
target=_mitmdump_wrapper, args=(self._q, self.port)
)
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ dependencies = [

[project.scripts]
octotail = "octotail.main:_main"
generate-cert = "octotail.main:generate_cert"

[project.urls]
Homepage = "https://github.com/rarescosma/octotail"
Expand Down

0 comments on commit fae9cc1

Please sign in to comment.