192 lines
8.9 KiB
Python
192 lines
8.9 KiB
Python
#!/usr/bin/env python3
|
|
"""Render a dynamic Neko docker-compose file from the current environment."""
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
|
|
|
|
def env(name: str, default: str) -> str:
|
|
value = os.environ.get(name)
|
|
return value if value not in {None, ""} else default
|
|
|
|
|
|
def env_enabled(name: str, default: bool = False) -> bool:
|
|
raw = os.environ.get(name)
|
|
if raw in {None, ""}:
|
|
return default
|
|
return raw.strip().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 = base.strip()
|
|
if base in {"", ".", "./"}:
|
|
return f"./{name}"
|
|
return f"{base.rstrip('/')}/{name}"
|
|
|
|
|
|
def default_udp_range(idx: int) -> str:
|
|
if idx == 1:
|
|
return "52000-52099"
|
|
if idx == 2:
|
|
return "52101-52199"
|
|
if idx == 3:
|
|
return "52201-52299"
|
|
start = 52400 + ((idx - 4) * 100)
|
|
return f"{start}-{start + 99}"
|
|
|
|
|
|
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")
|
|
browser_proxy = env("NEKO_BROWSER_PROXY", "")
|
|
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",
|
|
)
|
|
docker_host_ip = env("NEKO_DOCKER_HOST_IP", "")
|
|
if docker_host_ip and docker_host_ip not in {"host-gateway", "host.docker.internal"}:
|
|
extra_hosts_line = f' - "host.docker.internal:{docker_host_ip}"'
|
|
else:
|
|
extra_hosts_line = ' - "host.docker.internal:host-gateway"'
|
|
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")
|
|
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}")
|
|
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" 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)}",
|
|
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:",
|
|
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)
|
|
args = parser.parse_args()
|
|
count = max(1, min(64, args.count))
|
|
output = Path(args.output)
|
|
output.parent.mkdir(parents=True, exist_ok=True)
|
|
output.write_text(render(count), encoding="utf-8")
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|