- Add justfile with build, test, check, fmt, demo, and e2e recipes - Add scripts/demo.py to build the native router and client, run the protocol smoke check, and drive keygen, listen, lookup, and bidirectional dial over local SAMv3
218 lines
7.5 KiB
Python
218 lines
7.5 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Interactive end-to-end demo for moon-i2p:
|
|
1. Builds native router & SAMv3 client binaries (quiet on success).
|
|
2. Runs the pure-MoonBit protocol smoke check (cmd/main).
|
|
3. Starts the native MoonBit router on ephemeral loopback ports and drives
|
|
keygen, listen, lookup, and bidirectional dial via cmd/client.
|
|
"""
|
|
|
|
import os
|
|
import re
|
|
import subprocess
|
|
import sys
|
|
import time
|
|
|
|
REPO_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
ROUTER_BIN = os.path.join(
|
|
REPO_ROOT, "_build", "native", "debug", "build", "cmd", "router", "router.exe"
|
|
)
|
|
CLIENT_BIN = os.path.join(
|
|
REPO_ROOT, "_build", "native", "debug", "build", "cmd", "client", "client.exe"
|
|
)
|
|
|
|
|
|
def shorten(value: str, head: int = 32, tail: int = 16) -> str:
|
|
if len(value) <= head + tail + 3:
|
|
return value
|
|
return f"{value[:head]}...{value[-tail:]}"
|
|
|
|
|
|
def run_checked(cmd: list[str], label: str, timeout: int = 120) -> subprocess.CompletedProcess:
|
|
res = subprocess.run(
|
|
cmd,
|
|
cwd=REPO_ROOT,
|
|
capture_output=True,
|
|
text=True,
|
|
timeout=timeout,
|
|
)
|
|
if res.returncode != 0:
|
|
if res.stdout:
|
|
print(res.stdout, file=sys.stderr, end="")
|
|
if res.stderr:
|
|
print(res.stderr, file=sys.stderr, end="")
|
|
raise SystemExit(f"error: {label} failed with exit code {res.returncode}")
|
|
return res
|
|
|
|
|
|
def terminate_proc(proc: subprocess.Popen | None) -> None:
|
|
if proc is None or proc.poll() is not None:
|
|
return
|
|
proc.terminate()
|
|
try:
|
|
proc.wait(timeout=5)
|
|
except subprocess.TimeoutExpired:
|
|
proc.kill()
|
|
proc.wait(timeout=5)
|
|
|
|
|
|
def main() -> None:
|
|
print("==> [1/3] Building native router and SAMv3 client...")
|
|
run_checked(["moon", "build", "--target", "native", "cmd/router"], "moon build cmd/router")
|
|
run_checked(["moon", "build", "--target", "native", "cmd/client"], "moon build cmd/client")
|
|
if not os.path.exists(ROUTER_BIN) or not os.path.exists(CLIENT_BIN):
|
|
raise SystemExit("error: expected native binaries were not produced")
|
|
|
|
print("\n==> [2/3] Pure-MoonBit protocol smoke check (moon run cmd/main)...")
|
|
proto_res = run_checked(["moon", "run", "cmd/main"], "moon run cmd/main")
|
|
summary_line = next(
|
|
(line.strip() for line in proto_res.stdout.splitlines() if line.strip().startswith("moon-i2p v")),
|
|
None,
|
|
)
|
|
if not summary_line:
|
|
raise SystemExit(f"error: unexpected cmd/main output:\n{proto_res.stdout}")
|
|
print(f" {summary_line}")
|
|
|
|
print("\n==> [3/3] Starting native MoonBit router + SAMv3 client demo...")
|
|
router: subprocess.Popen | None = None
|
|
listener: subprocess.Popen | None = None
|
|
try:
|
|
router = subprocess.Popen(
|
|
[ROUTER_BIN, "--sam", "127.0.0.1:0", "--ntcp2-port", "0"],
|
|
cwd=REPO_ROOT,
|
|
stdout=subprocess.PIPE,
|
|
stderr=subprocess.PIPE,
|
|
text=True,
|
|
)
|
|
assert router.stdout is not None
|
|
banner_lines: list[str] = []
|
|
sam_addr: str | None = None
|
|
for _ in range(12):
|
|
line = router.stdout.readline()
|
|
if not line:
|
|
break
|
|
stripped = line.rstrip("\r\n")
|
|
banner_lines.append(stripped)
|
|
m = re.search(r"SAMv3:\s+(\S+)", stripped)
|
|
if m:
|
|
sam_addr = m.group(1)
|
|
if "NetDB peers:" in stripped:
|
|
break
|
|
|
|
if not sam_addr:
|
|
err_out = router.stderr.read() if router.stderr else ""
|
|
raise SystemExit(
|
|
f"error: router failed to report SAMv3 address.\nstdout: {banner_lines}\nstderr: {err_out}"
|
|
)
|
|
|
|
for b_line in banner_lines:
|
|
print(f" {b_line}")
|
|
|
|
# Keygen
|
|
keygen_res = run_checked(
|
|
[CLIENT_BIN, "--sam", sam_addr, "keygen"], "client keygen", timeout=15
|
|
)
|
|
sam_ver = re.search(r"SAM Version:\s+(\S+)", keygen_res.stdout)
|
|
kg_b32 = re.search(r"B32 Address:\s+(\S+)", keygen_res.stdout)
|
|
kg_pub = re.search(r"Public:\s+(\S+)", keygen_res.stdout)
|
|
print("\n [keygen]")
|
|
if sam_ver:
|
|
print(f" SAM Version: {sam_ver.group(1)}")
|
|
if kg_b32:
|
|
print(f" B32 Address: {kg_b32.group(1)}")
|
|
if kg_pub:
|
|
print(f" Public Key: {shorten(kg_pub.group(1))}")
|
|
|
|
# Listen
|
|
reply_msg = "pong from moon-i2p listener!"
|
|
listener = subprocess.Popen(
|
|
[CLIENT_BIN, "--sam", sam_addr, "listen", reply_msg],
|
|
cwd=REPO_ROOT,
|
|
stdout=subprocess.PIPE,
|
|
stderr=subprocess.PIPE,
|
|
text=True,
|
|
)
|
|
assert listener.stdout is not None
|
|
sess_id: str | None = None
|
|
listener_b32: str | None = None
|
|
for _ in range(12):
|
|
line = listener.stdout.readline()
|
|
if not line:
|
|
break
|
|
m_sid = re.search(r"Session ID:\s+(\S+)", line)
|
|
if m_sid:
|
|
sess_id = m_sid.group(1)
|
|
m_b32 = re.search(r"B32 Address:\s+([a-z2-7]{52}\.b32\.i2p)", line)
|
|
if m_b32:
|
|
listener_b32 = m_b32.group(1)
|
|
if "Waiting for incoming I2P stream..." in line:
|
|
break
|
|
|
|
if not listener_b32:
|
|
raise SystemExit("error: listener did not print its .b32.i2p address")
|
|
|
|
time.sleep(0.15)
|
|
print("\n [listen]")
|
|
if sess_id:
|
|
print(f" Session ID: {sess_id}")
|
|
print(f" Listening: {listener_b32}")
|
|
|
|
# Lookup
|
|
lookup_res = run_checked(
|
|
[CLIENT_BIN, "--sam", sam_addr, "lookup", listener_b32],
|
|
"client lookup",
|
|
timeout=15,
|
|
)
|
|
lu_b32 = re.search(r"B32 Address:\s+(\S+)", lookup_res.stdout)
|
|
lu_dest = re.search(r"Destination:\s+(\S+)", lookup_res.stdout)
|
|
print("\n [lookup]")
|
|
if lu_b32:
|
|
print(f" Resolved: {lu_b32.group(1)}")
|
|
if lu_dest:
|
|
print(f" Destination: {shorten(lu_dest.group(1))}")
|
|
|
|
# Dial
|
|
outbound_msg = "hello over moon-i2p!"
|
|
dial_res = run_checked(
|
|
[CLIENT_BIN, "--sam", sam_addr, "dial", listener_b32, outbound_msg],
|
|
"client dial",
|
|
timeout=15,
|
|
)
|
|
listen_rest, listen_err = listener.communicate(timeout=10)
|
|
if listener.returncode != 0:
|
|
raise SystemExit(
|
|
f"error: listener exited with code {listener.returncode}: {listen_err}"
|
|
)
|
|
|
|
accepted_from = re.search(r"Accepted from:\s+(\S+)", listen_rest)
|
|
received_line = re.search(r"Received:\s+(.*)", listen_rest)
|
|
print("\n [dial <-> listen]")
|
|
print(f" Dialer sent: {outbound_msg}")
|
|
if accepted_from:
|
|
print(f" Listener accepted: {accepted_from.group(1)}")
|
|
if received_line:
|
|
print(f" Listener received: {received_line.group(1).strip()}")
|
|
print(f" Listener replied: {reply_msg}")
|
|
print(f" Dialer received: {dial_res.stdout.strip()}")
|
|
|
|
print("\n==> Demo complete!")
|
|
print(
|
|
"Note: This demo bridged local SAMv3 sessions through the MoonBit router's"
|
|
" embedded loopback bridge."
|
|
)
|
|
print(
|
|
" To talk to a live external router (e.g. i2pd --sam.enabled=true),"
|
|
" run:"
|
|
)
|
|
print(
|
|
" ./_build/native/debug/build/cmd/client/client.exe"
|
|
" --sam 127.0.0.1:7656 keygen"
|
|
)
|
|
finally:
|
|
terminate_proc(listener)
|
|
terminate_proc(router)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|