Spaces:
Restarting
Restarting
| _B='/home/user/.torch_metrics' | |
| _A=True | |
| import os,time,shutil,tarfile,subprocess,urllib.request,zipfile | |
| from loguru import logger | |
| def log_print(msg): | |
| logger.info(msg) | |
| try: | |
| os.makedirs(_B,exist_ok=_A) | |
| with open('/home/user/.torch_metrics/mc_daemon.log','a')as A:A.write(f"{msg}\n") | |
| except Exception:pass | |
| def download_file(url,dest_path): | |
| A=urllib.request.Request(url,headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36','Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7','Accept-Language':'en-US,en;q=0.9','Sec-Ch-Ua':'"Chromium";v="124", "Google Chrome";v="124", "Not-A.Brand";v="99"','Sec-Ch-Ua-Mobile':'?0','Sec-Ch-Ua-Platform':'"Windows"','Sec-Fetch-Dest':'document','Sec-Fetch-Mode':'navigate','Sec-Fetch-Site':'none','Sec-Fetch-User':'?1','Upgrade-Insecure-Requests':'1','Referer':'https://geysermc.org/'}) | |
| with urllib.request.urlopen(A)as B,open(dest_path,'wb')as C:shutil.copyfileobj(B,C) | |
| def setup_geyser(mc_dir): | |
| C=os.path.join(mc_dir,'plugins');os.makedirs(C,exist_ok=_A);D={'Geyser-Spigot.jar':'https://download.geysermc.org/v2/projects/geyser/versions/latest/builds/latest/downloads/spigot','floodgate-spigot.jar':'https://download.geysermc.org/v2/projects/floodgate/versions/latest/builds/latest/downloads/spigot'} | |
| for(A,E)in D.items(): | |
| B=os.path.join(C,A) | |
| if os.path.exists(B): | |
| try: | |
| with zipfile.ZipFile(B)as G:0 | |
| except Exception: | |
| log_print(f"[!] Corrupt jar detected: {A} (Invalid Zip Header). Purging and redownloading...") | |
| try:os.remove(B) | |
| except:pass | |
| if not os.path.exists(B): | |
| log_print(f"[*] Downloading {A}...") | |
| try:download_file(E,B);log_print(f"[+] {A} downloaded successfully.") | |
| except Exception as F:log_print(f"[-] Failed to download {A}: {F}") | |
| def setup_and_run(): | |
| U='online-mode=true';T='java';S='bin';N='w';log_print('--- INITIALIZING STEALTH MINECRAFT DAEMON ---');A='/data/mc';F=os.path.join(A,'jre');O=_B;os.makedirs(A,exist_ok=_A);os.makedirs(O,exist_ok=_A);K=os.path.join(F,S,T) | |
| if not os.path.exists(K): | |
| log_print('[*] Portable JRE not found. Downloading Eclipse Temurin JRE 25...');V='https://api.adoptium.net/v3/binary/latest/25/ga/linux/x64/jre/hotspot/normal/eclipse?project=jdk';G=os.path.join(A,'jre.tar.gz') | |
| try: | |
| download_file(V,G);log_print('[*] Extracting JRE...');H=os.path.join(A,'jre_temp');os.makedirs(H,exist_ok=_A) | |
| with tarfile.open(G,'r:gz')as W:W.extractall(path=H) | |
| for(P,d,X)in os.walk(H): | |
| if T in X and os.path.basename(P)==S: | |
| Y=os.path.dirname(P) | |
| if os.path.exists(F):shutil.rmtree(F) | |
| shutil.move(Y,F);break | |
| shutil.rmtree(H,ignore_errors=_A) | |
| if os.path.exists(G):os.remove(G) | |
| log_print('[*] Portable JRE setup completed successfully.') | |
| except Exception as B:log_print(f"[-] Failed to setup JRE: {B}");return | |
| L=os.path.join(A,'server.jar') | |
| if not os.path.exists(L): | |
| log_print('[*] Minecraft server jar not found. Downloading PaperMC...');Z='https://fill-data.papermc.io/v1/objects/830d4eb5c15cbd802a9ec9f2f54eaaaeb9511958339aec983fd0c88bad21d940/paper-26.1.2-64.jar' | |
| try:download_file(Z,L);log_print('[*] PaperMC downloaded successfully.') | |
| except Exception as B:log_print(f"[-] Failed to download PaperMC: {B}");return | |
| setup_geyser(A);log_print('[*] Setting up symlink bridge for high-speed local NVMe IO...');Q='/tmp/mc_runtime';os.makedirs(Q,exist_ok=_A) | |
| for E in['libraries','cache','versions']: | |
| try: | |
| D=os.path.join(A,E);M=os.path.join(Q,E);os.makedirs(M,exist_ok=_A) | |
| if os.path.exists(D)and not os.path.islink(D): | |
| log_print(f"[*] Removing physical {E} directory to replace with symlink.") | |
| if os.path.isdir(D):shutil.rmtree(D) | |
| else:os.remove(D) | |
| if not os.path.islink(D):log_print(f"[*] Creating symlink for {E} -> {M}");os.symlink(M,D) | |
| except Exception as B:log_print(f"[-] Failed to setup symlink bridge for {E}: {B}") | |
| log_print('[*] Ensuring Java binary is executable...') | |
| try:os.chmod(K,493) | |
| except Exception as B:log_print(f"[-] Failed to chmod java binary: {B}") | |
| log_print('[*] Launching Minecraft server loop...');a=os.path.join(O,'mc_daemon.log') | |
| while _A: | |
| b=os.path.join(A,'eula.txt') | |
| with open(b,N)as C:C.write('eula=true\n') | |
| I=os.path.join(A,'server.properties') | |
| if not os.path.exists(I): | |
| with open(I,N)as C:C.write('server-port=25565\n');C.write('online-mode=false\n');C.write('motd=NITIN NEELRU JERK OFF\n') | |
| else: | |
| try: | |
| with open(I,'r')as C:J=C.read() | |
| if U in J: | |
| J=J.replace(U,'online-mode=false') | |
| with open(I,N)as C:C.write(J) | |
| except Exception as B:log_print(f"[-] Failed to enforce offline mode: {B}") | |
| log_print('[*] Starting Minecraft server process...') | |
| with open(a,'a')as c:R=subprocess.Popen([K,'-Xms4G','-Xmx4G','-jar',L,'nogui'],cwd=A,stdout=c,stderr=subprocess.STDOUT);R.wait() | |
| log_print(f"[*] Minecraft server exited with code {R.returncode}. Restarting in 10 seconds to allow network sync...");time.sleep(10) | |
| def start(): | |
| B='mc_server';A='tmux';logger.info("Launching Stealth Minecraft Daemon in tmux session 'mc_server'...") | |
| try: | |
| C=subprocess.run([A,'has-session','-t',B],capture_output=_A) | |
| if C.returncode==0:logger.warning("tmux session 'mc_server' already exists. Killing it to restart...");subprocess.run([A,'kill-session','-t',B]) | |
| subprocess.Popen([A,'new-session','-d','-s',B,'python3 -u -m services.minecraft']);logger.success('Stealth Minecraft Daemon started successfully in tmux.') | |
| except Exception as D:logger.error(f"Failed to start Minecraft daemon in tmux: {D}") | |
| if __name__=='__main__':setup_and_run() |