Skip to content

Commit

Permalink
Add _tunnel.py
Browse files Browse the repository at this point in the history
  • Loading branch information
brentyi committed Aug 3, 2023
1 parent 26fc2e9 commit a38ddd5
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions viser/_tunnel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
"""Helper for creating shareable viser links using `bore`.
https://github.com/ekzhang/bore
"""

import atexit
import os
import signal
import subprocess
import sys
import threading
from pathlib import Path

BORE_SERVER = "chalupa.eecs.berkeley.edu"


def start_tunnel(port: int) -> None:
env_dir = Path(__file__).absolute().parent / ".rustenv"
bore_path = env_dir / "rust" / "bin" / "bore"

if not env_dir.exists():
print("[viser] Tunneling: setting up Rust environment")
subprocess.run(args=[sys.executable, "-m", "rustenv", str(env_dir)])

if not bore_path.exists():
print("[viser] Tunneling: installing bore")
subprocess.run(args=[str(env_dir / "bin" / "cargo"), "install", "bore-cli"])

process = subprocess.Popen(
[f"{bore_path}", "local", str(port), "--to", BORE_SERVER],
stdout=subprocess.PIPE,
)

# Handle normal exists.
@atexit.register
def _():
process.terminate()
process.wait()

# Handle SIGTERM.
script_pid = os.getpid()

def handle_termination_signal(signum, frame):
process.kill()
os.kill(script_pid, signum)

signal.signal(signal.SIGTERM, handle_termination_signal)

def _bore_watcher() -> None:
while True:
assert process.stdout is not None
line = process.stdout.readline().decode().strip()
if "Error:" in line:
print(line)
break
if "listening at " in line:
print("[viser] Tunnel created at", line.partition("listening at ")[2])

threading.Thread(target=_bore_watcher).start()

return None

0 comments on commit a38ddd5

Please sign in to comment.