import os, io, json, time, tarfile, subprocess, urllib.request BASE = "/home/container" BIN = os.path.join(BASE, "sing-box") CFG = os.path.join(BASE, "config.json") URL = "https://github.com/SagerNet/sing-box/releases/download/v1.14.4/sing-box-1.14.4-linux-amd64.tar.gz" PORT = int(os.environ.get("SERVER_PORT") or "25767") UUID = "9cd35b9c-0887-46e1-ae44-c31d63c168a3" PATH = "/assets-25767" def log(*a): print("[boot]", *a, flush=True) def ensure_bin(): if os.path.exists(BIN): log("sing-box already present") return log("downloading sing-box...") data = urllib.request.urlopen(URL, timeout=300).read() log("downloaded bytes:", len(data)) tf = tarfile.open(fileobj=io.BytesIO(data), mode="r:gz") member = None for m in tf.getmembers(): if m.name.endswith("/sing-box"): member = m src = tf.extractfile(member) with open(BIN, "wb") as out: out.write(src.read()) os.chmod(BIN, 0o755) log("sing-box ready") def write_cfg(): cfg = { "log": {"level": "info"}, "inbounds": [{ "type": "vless", "tag": "vless-ws-in", "listen": "0.0.0.0", "listen_port": PORT, "users": [{"uuid": UUID}], "transport": { "type": "ws", "path": PATH, "max_early_data": 2048, "early_data_header_name": "Sec-WebSocket-Protocol" } }], "outbounds": [{"type": "direct", "tag": "direct"}] } with open(CFG, "w") as f: json.dump(cfg, f) log("config written, port:", PORT, "path:", PATH) def main(): try: ip = urllib.request.urlopen("https://api.ipify.org", timeout=15).read().decode() log("egress ip:", ip) except Exception as e: log("ip check failed:", e) ensure_bin() write_cfg() while True: log("starting sing-box...") p = subprocess.Popen([BIN, "run", "-c", CFG]) rc = p.wait() log("sing-box exited rc:", rc, "- restart in 3s") time.sleep(3) main()