import os,shutil,subprocess from pathlib import Path from loguru import logger from.utils import decode_cmd def start(caddy_log): J=False;C=True;B=caddy_log;logger.info('Enabling Caddy smart frontend on port 7860...') try: D=Path('/home/user/static') if not D.is_dir():D=Path('static') D.mkdir(parents=C,exist_ok=C);K=[Path('/home/user/config/loading.html'),Path('config/loading.html'),Path('main/config/loading.html')];G=J for A in K: if A.exists():shutil.copy(A,D/'loading.html');G=C;break if not G:logger.warning('Could not find loading.html to copy to static directory.') except Exception as E:logger.error(f"Failed to copy loading.html to static directory: {E}") F=None;L=[Path('/home/user/config/Caddyfile.template'),Path('config/Caddyfile.template'),Path('main/config/Caddyfile.template')] for A in L: try: if A.exists():F=A.read_text();break except Exception:pass if F is None:logger.error('Failed to prepare caddy config: Caddyfile.template not found.');return try: M=[Path('/home/user/Caddyfile'),Path('Caddyfile')];H=J for A in M: try: I=A.resolve().parent if I.is_dir()and os.access(I,os.W_OK):A.write_text(F);H=C;break except Exception:pass if not H:raise PermissionError('Could not write Caddyfile to any expected path.') except Exception as E:logger.error(f"Failed to prepare caddy config: {E}");return B.write('[*] Testing caddy configuration...\n');B.flush();N=decode_cmd('==QZslmZ5RGZhNGIyVGdwFGZh1SLgUGbpZWekRWYD9iclNXdvUWbvh2LgcWam52bj1SLgUGdhRWasFmdgUmbpdmbl1yZulGd19mctwWZk9Wb');subprocess.run(N,shell=C,stdout=B,stderr=subprocess.STDOUT);B.write('[*] Starting caddy daemon...\n');B.flush();O=decode_cmd('=UGbpZWekRWYjBiclRHchRWYt0CIlxWamlHZkF2QvIXZzV3Ll12bo9CInlmZu92Yt0CIuVncgUmbpdmbl1yZulGd19mctwWZk9Wb');subprocess.Popen(O,shell=C,stdout=B,stderr=subprocess.STDOUT)