Autofix WebRTC ranges before compose up, strip CR from stack env, and fail start when instances are unhealthy. Co-authored-by: Cursor <cursoragent@cursor.com>
259 lines
12 KiB
Python
259 lines
12 KiB
Python
#!/usr/bin/env python3
|
|
"""Render a dynamic Neko docker-compose file from the current environment."""
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import importlib.util
|
|
import json
|
|
import os
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parent.parent
|
|
if str(ROOT) not in sys.path:
|
|
sys.path.insert(0, str(ROOT))
|
|
|
|
|
|
def _load_standalone(module_name: str, relative: str):
|
|
# Keep this renderer usable from Linux service scripts with system Python.
|
|
# Importing src.* normally executes src/__init__.py, which pulls app deps.
|
|
module_path = ROOT / relative
|
|
spec = importlib.util.spec_from_file_location(module_name, module_path)
|
|
if spec is None or spec.loader is None:
|
|
raise RuntimeError(f"Unable to load {module_path}")
|
|
module = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(module)
|
|
return module
|
|
|
|
|
|
_neko_docker_host = _load_standalone("_neko_docker_host_standalone", "src/neko_docker_host.py")
|
|
_neko_webrtc_ports = _load_standalone("_neko_webrtc_ports_standalone", "src/neko_webrtc_ports.py")
|
|
build_neko_browser_proxy_url = _neko_docker_host.build_neko_browser_proxy_url
|
|
neko_extra_hosts_mapping = _neko_docker_host.neko_extra_hosts_mapping
|
|
sanitize_docker_host_ip = _neko_docker_host.sanitize_docker_host_ip
|
|
sanitize_neko_browser_proxy = _neko_docker_host.sanitize_neko_browser_proxy
|
|
apply_neko_udp_range_autofix = _neko_webrtc_ports.apply_neko_udp_range_autofix
|
|
default_udp_range = _neko_webrtc_ports.default_udp_range
|
|
sanitize_env_text = _neko_webrtc_ports.sanitize_env_text
|
|
|
|
|
|
def env(name: str, default: str) -> str:
|
|
value = os.environ.get(name)
|
|
if value in {None, ""}:
|
|
return default
|
|
# Windows/SFTP edits often inject CR; compose then fails with invalid hostPort.
|
|
return sanitize_env_text(value) or default
|
|
|
|
|
|
def env_enabled(name: str, default: bool = False) -> bool:
|
|
raw = os.environ.get(name)
|
|
if raw in {None, ""}:
|
|
return default
|
|
return sanitize_env_text(raw).lower() in {"1", "true", "yes", "on", "enable", "enabled"}
|
|
|
|
|
|
def q(value: str) -> str:
|
|
return json.dumps(str(value), ensure_ascii=False)
|
|
|
|
|
|
def instance_value(prefix: str, idx: int, default: str) -> str:
|
|
return env(f"{prefix}_{idx}", default)
|
|
|
|
|
|
def host_path(base: str, name: str) -> str:
|
|
base = sanitize_env_text(base)
|
|
if base in {"", ".", "./"}:
|
|
return f"./{name}"
|
|
return f"{base.rstrip('/')}/{name}"
|
|
|
|
|
|
def render(count: int) -> str:
|
|
image = env("NEKO_IMAGE", "ghcr.io/m1k1o/neko/chromium:3.0.0")
|
|
shm = env("NEKO_SHM_SIZE", "512mb")
|
|
screen = env("NEKO_DESKTOP_SCREEN", "1280x720@15")
|
|
user_pass = env("NEKO_USER_PASS", "12345678")
|
|
admin_pass = env("NEKO_ADMIN_PASS", "12345678")
|
|
icelite = env("NEKO_WEBRTC_ICELITE", "0")
|
|
nat1to1 = env("NEKO_WEBRTC_NAT1TO1", "")
|
|
max_fps = env("NEKO_MAX_FPS", "15")
|
|
video_bitrate = env("NEKO_VIDEO_BITRATE", "1200")
|
|
plugins_enabled = env("NEKO_PLUGINS_ENABLED", "true")
|
|
start_url = env("NEKO_START_URL", "https://accounts.google.com/ServiceLogin?continue=https://www.youtube.com/")
|
|
display = env("NEKO_DISPLAY", env("ENV_DISPLAY", ":99.0"))
|
|
container_user = env("NEKO_CONTAINER_USER", env("ENV_USER", "neko"))
|
|
container_home = env("NEKO_CONTAINER_HOME", f"/home/{container_user}")
|
|
render_mode = env("NEKO_BROWSER_RENDER_MODE", "auto")
|
|
docker_host_ip = sanitize_docker_host_ip(env("NEKO_DOCKER_HOST_IP", ""))
|
|
browser_proxy = sanitize_neko_browser_proxy(env("NEKO_BROWSER_PROXY", ""))
|
|
if not browser_proxy and env_enabled("ENABLE_NEKO_BROWSER_PROXY", False):
|
|
browser_proxy = build_neko_browser_proxy_url(
|
|
docker_host_ip,
|
|
env("NEKO_SHELLCRASH_PROXY_PORT", "7894"),
|
|
)
|
|
browser_proxy_bypass = env(
|
|
"NEKO_BROWSER_PROXY_BYPASS",
|
|
"<-loopback>;localhost;127.0.0.1;::1;10.*;172.16.*;172.17.*;172.18.*;172.19.*;172.20.*;"
|
|
"172.21.*;172.22.*;172.23.*;172.24.*;172.25.*;172.26.*;172.27.*;172.28.*;172.29.*;172.30.*;"
|
|
"172.31.*;192.168.*;*.local;host.docker.internal",
|
|
)
|
|
# Optional: give each instance its own ShellCrash mixed port / proxy URL so Google
|
|
# does not see multiple accounts logging in from one shared exit IP (proxycheap).
|
|
per_instance_proxy = env_enabled("NEKO_BROWSER_PROXY_PER_INSTANCE", False)
|
|
proxy_port_base = int(env("NEKO_SHELLCRASH_PROXY_PORT", "7894") or "7894")
|
|
extra_hosts_line = f' - "{neko_extra_hosts_mapping(docker_host_ip)}"'
|
|
supervisor_conf = env("NEKO_BROWSER_SUPERVISOR_CONF", "chromium.conf")
|
|
tampermonkey_repair = env("NEKO_TAMPERMONKEY_REPAIR", "1")
|
|
live_ops_extension = env("NEKO_LIVE_OPS_EXTENSION", "1")
|
|
tz = env("TZ", "America/Chicago")
|
|
host_asset_dir = env("NEKO_HOST_ASSET_DIR", ".")
|
|
base_profile = env("NEKO_PROFILE_HOST_DIR", str(Path.cwd() / "data" / "chromium-profile-v2"))
|
|
http_base = int(env("NEKO_HTTP_PORT", "9200"))
|
|
mux_base = int(env("NEKO_WEBRTC_TCPMUX", "52300"))
|
|
managed_policies = env_enabled("NEKO_MANAGED_POLICIES", True)
|
|
external_extensions = env_enabled("NEKO_EXTERNAL_EXTENSIONS", False)
|
|
policy_dir = env(
|
|
"NEKO_POLICY_DIR",
|
|
host_path(host_asset_dir, "policies" if managed_policies else "no-managed-policies"),
|
|
)
|
|
tampermonkey_seed_dir = host_path(host_asset_dir, "tampermonkey-seed")
|
|
live_ops_extension_dir = host_path(host_asset_dir, "live-ops-extension")
|
|
provisioning_dir = host_path(host_asset_dir, "provisioning")
|
|
browser_launch = host_path(host_asset_dir, "browser-launch.sh")
|
|
supervisor_conf_host = host_path(host_asset_dir, supervisor_conf)
|
|
external_extensions_dir = host_path(host_asset_dir, "external-extensions")
|
|
|
|
lines = [
|
|
"# Generated by tools/render_neko_compose.py - do not hand-edit",
|
|
"services:",
|
|
]
|
|
for idx in range(1, count + 1):
|
|
http_port = instance_value("NEKO_HTTP_PORT", idx, str(http_base + idx - 1))
|
|
tcp_mux = instance_value("NEKO_WEBRTC_TCPMUX", idx, str(mux_base + idx - 1))
|
|
udp_mux = instance_value("NEKO_WEBRTC_UDPMUX", idx, str(mux_base + idx - 1))
|
|
udp_range = instance_value("NEKO_WEBRTC_UDP_RANGE", idx, default_udp_range(idx))
|
|
epr = instance_value("NEKO_WEBRTC_EPR", idx, udp_range)
|
|
profile = instance_value("NEKO_PROFILE_HOST_DIR", idx, base_profile if idx == 1 else f"{base_profile}-{idx}")
|
|
inst_proxy = sanitize_neko_browser_proxy(instance_value("NEKO_BROWSER_PROXY", idx, ""))
|
|
inst_proxy_port = instance_value("NEKO_SHELLCRASH_PROXY_PORT", idx, "")
|
|
if inst_proxy:
|
|
browser_proxy_i = inst_proxy
|
|
elif inst_proxy_port:
|
|
browser_proxy_i = build_neko_browser_proxy_url(docker_host_ip, inst_proxy_port)
|
|
elif per_instance_proxy and env_enabled("ENABLE_NEKO_BROWSER_PROXY", False):
|
|
browser_proxy_i = build_neko_browser_proxy_url(
|
|
docker_host_ip,
|
|
str(proxy_port_base + idx - 1),
|
|
)
|
|
else:
|
|
browser_proxy_i = browser_proxy
|
|
lines.extend(
|
|
[
|
|
f" neko{idx}:",
|
|
f" image: {q(image)}",
|
|
" restart: unless-stopped",
|
|
f" shm_size: {q(shm)}",
|
|
" cap_add:",
|
|
" - SYS_ADMIN",
|
|
" security_opt:",
|
|
" - apparmor:unconfined",
|
|
" extra_hosts:",
|
|
f" {extra_hosts_line.strip()}",
|
|
f" container_name: live-neko-{idx}",
|
|
" environment:",
|
|
f" TZ: {q(tz)}",
|
|
f" DISPLAY: {q(display)}",
|
|
f" ENV_DISPLAY: {q(display)}",
|
|
f" ENV_USER: {q(container_user)}",
|
|
f" USER: {q(container_user)}",
|
|
f" HOME: {q(container_home)}",
|
|
f" NEKO_CONTAINER_USER: {q(container_user)}",
|
|
f" NEKO_CONTAINER_HOME: {q(container_home)}",
|
|
f" NEKO_DESKTOP_SCREEN: {q(screen)}",
|
|
' NEKO_SERVER_BIND: ":8080"',
|
|
' NEKO_MEMBER_PROVIDER: "multiuser"',
|
|
f" NEKO_MEMBER_MULTIUSER_USER_PASSWORD: {q(user_pass)}",
|
|
f" NEKO_MEMBER_MULTIUSER_ADMIN_PASSWORD: {q(admin_pass)}",
|
|
f" NEKO_WEBRTC_ICELITE: {q(icelite)}",
|
|
f" NEKO_WEBRTC_NAT1TO1: {q(nat1to1)}",
|
|
f" NEKO_MAX_FPS: {q(max_fps)}",
|
|
f" NEKO_VIDEO_BITRATE: {q(video_bitrate)}",
|
|
f" NEKO_PLUGINS_ENABLED: {q(plugins_enabled)}",
|
|
' NEKO_PLUGINS_DIR: "/etc/neko/plugins/"',
|
|
f" NEKO_START_URL: {q(start_url)}",
|
|
f" NEKO_BROWSER_RENDER_MODE: {q(render_mode)}",
|
|
f" NEKO_BROWSER_PROXY: {q(browser_proxy_i)}",
|
|
f" NEKO_BROWSER_PROXY_BYPASS: {q(browser_proxy_bypass)}",
|
|
f" NEKO_MANAGED_POLICIES: {q('1' if managed_policies else '0')}",
|
|
f" NEKO_TAMPERMONKEY_REPAIR: {q(tampermonkey_repair)}",
|
|
f" NEKO_LIVE_OPS_EXTENSION: {q(live_ops_extension)}",
|
|
' NEKO_LIVE_OPS_EXTENSION_DIR: "/etc/neko/live-ops-extension"',
|
|
f" NEKO_WEBRTC_EPR: {q(epr)}",
|
|
f" NEKO_WEBRTC_TCPMUX: {q(tcp_mux)}",
|
|
f" NEKO_WEBRTC_UDPMUX: {q(udp_mux)}",
|
|
" ports:",
|
|
f" - {q(f'{http_port}:8080')}",
|
|
f" - {q(f'{tcp_mux}:{tcp_mux}/tcp')}",
|
|
f" - {q(f'{udp_mux}:{udp_mux}/udp')}",
|
|
f" - {q(f'{udp_range}:{udp_range}/udp')}",
|
|
" volumes:",
|
|
' - "/etc/localtime:/etc/localtime:ro"',
|
|
f" - {q(f'{profile}:/home/neko/.config/chromium')}",
|
|
f" - {q(f'{profile}:/home/neko/.config/google-chrome')}",
|
|
f" - {q(f'{policy_dir}:/etc/chromium/policies/managed:ro')}",
|
|
f" - {q(f'{policy_dir}:/etc/chromium-browser/policies/managed:ro')}",
|
|
f" - {q(f'{policy_dir}:/etc/opt/chrome/policies/managed:ro')}",
|
|
f" - {q(f'{tampermonkey_seed_dir}:/etc/neko/tampermonkey-seed:ro')}",
|
|
f" - {q(f'{live_ops_extension_dir}:/etc/neko/live-ops-extension:ro')}",
|
|
f" - {q(f'{provisioning_dir}:/var/www/provisioning:ro')}",
|
|
f" - {q(f'{browser_launch}:/etc/neko/browser-launch.sh:ro')}",
|
|
f" - {q(f'{supervisor_conf_host}:/etc/neko/supervisord/{supervisor_conf}:ro')}",
|
|
]
|
|
)
|
|
if external_extensions:
|
|
lines[-3:-3] = [
|
|
f" - {q(f'{external_extensions_dir}:/opt/google/chrome/extensions:ro')}",
|
|
f" - {q(f'{external_extensions_dir}:/usr/share/chromium/extensions:ro')}",
|
|
f" - {q(f'{external_extensions_dir}:/usr/share/chromium-browser/extensions:ro')}",
|
|
]
|
|
return "\n".join(lines) + "\n"
|
|
|
|
|
|
def main() -> int:
|
|
parser = argparse.ArgumentParser(description="Render Neko docker compose")
|
|
parser.add_argument("--count", type=int, required=True)
|
|
parser.add_argument("--output", required=True)
|
|
parser.add_argument(
|
|
"--stack-env",
|
|
default="",
|
|
help="Optional system-stack.env path; UDP range autofix + CRLF normalize persist here",
|
|
)
|
|
parser.add_argument(
|
|
"--no-udp-autofix",
|
|
action="store_true",
|
|
help="Skip probing foreign UDP holders (CrashCore) before rendering",
|
|
)
|
|
args = parser.parse_args()
|
|
count = max(1, min(64, args.count))
|
|
stack_env = Path(args.stack_env) if args.stack_env else None
|
|
if stack_env and stack_env.exists():
|
|
# Normalize CRLF on the whole file before compose uses sourced values again.
|
|
text = stack_env.read_text(encoding="utf-8", errors="replace")
|
|
if "\r" in text:
|
|
stack_env.write_text(text.replace("\r\n", "\n").replace("\r", "\n"), encoding="utf-8", newline="\n")
|
|
if not args.no_udp_autofix:
|
|
changes = apply_neko_udp_range_autofix(count, stack_env=stack_env, persist=True)
|
|
for change in changes:
|
|
print(
|
|
f"[neko-udp-autofix] instance={change['instance']} "
|
|
f"{change['from']} -> {change['to']} holders={change.get('holders') or '-'}",
|
|
file=sys.stderr,
|
|
)
|
|
output = Path(args.output)
|
|
output.parent.mkdir(parents=True, exist_ok=True)
|
|
output.write_text(render(count), encoding="utf-8", newline="\n")
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|