File size: 18,437 Bytes
b676880
 
 
 
 
417d689
33671b3
 
 
 
b676880
ef0e88b
 
 
 
 
 
 
 
 
 
 
 
 
 
8416633
 
 
 
b676880
417d689
b676880
417d689
b676880
 
 
417d689
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5ba1f2c
 
 
 
 
 
 
 
 
 
 
 
417d689
 
5ba1f2c
 
417d689
 
 
 
 
5ba1f2c
417d689
 
5ba1f2c
 
417d689
 
 
 
 
 
 
 
 
5ba1f2c
 
417d689
5ba1f2c
417d689
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ef0e88b
5ba1f2c
 
ef0e88b
 
 
 
417d689
 
 
 
 
 
 
 
 
ef0e88b
 
 
 
417d689
 
 
 
 
 
ef0e88b
5ba1f2c
 
ef0e88b
 
5ba1f2c
417d689
 
ef0e88b
 
 
 
417d689
ef0e88b
 
417d689
ef0e88b
 
 
cd56b8b
ef0e88b
cd56b8b
417d689
 
 
 
 
 
 
 
 
 
 
 
b63e107
417d689
 
 
 
 
 
 
 
 
b63e107
417d689
 
 
 
 
 
b676880
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
417d689
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b676880
 
417d689
 
 
 
 
 
 
 
 
 
 
b676880
 
 
 
 
 
 
 
378852a
 
b676880
 
 
b63e107
 
 
b676880
b63e107
 
 
b676880
378852a
 
b676880
417d689
b676880
 
417d689
b676880
 
417d689
 
 
 
b676880
 
 
417d689
b676880
 
 
 
417d689
 
 
 
 
 
 
b676880
 
417d689
b676880
417d689
 
 
b676880
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33671b3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cd419d0
 
 
33671b3
f9593fc
 
cd419d0
f9593fc
cd419d0
 
f9593fc
cd419d0
 
f9593fc
 
 
33671b3
f9593fc
33671b3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ef0e88b
 
 
 
 
 
 
 
 
33671b3
 
 
 
 
 
 
 
 
 
 
b676880
33671b3
 
 
 
5ba1f2c
 
ef0e88b
 
 
 
 
 
 
 
 
 
 
 
33671b3
 
 
 
 
 
ef0e88b
 
 
33671b3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ef0e88b
 
 
33671b3
ef0e88b
 
 
33671b3
ef0e88b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
import urllib.request
from pathlib import Path
import os
import subprocess
import sys
import shutil
import gradio as gr
import threading
import time
import signal

try:
    from pyngrok import ngrok, conf
    PYNGROK_AVAILABLE = True
except ImportError:
    print("pyngrok not found. Installing...")
    try:
        subprocess.check_call([sys.executable, "-m", "pip", "install", "pyngrok"])
        from pyngrok import ngrok, conf
        PYNGROK_AVAILABLE = True
        print("βœ“ pyngrok installed successfully")
    except Exception as e:
        print(f"Failed to install pyngrok: {e}")
        PYNGROK_AVAILABLE = False

subprocess.check_call([sys.executable, "-m", "pip", "install", "mcstatus"])
from mcstatus import JavaServer


PAPER_JAR_URL = "https://fill-data.papermc.io/v1/objects/234a9b32098100c6fc116664d64e36ccdb58b5b649af0f80bcccb08b0255eaea/paper-1.20.1-196.jar"

DATA_DIR = Path("/data")
JAVA_DIR = DATA_DIR / "java"
JAR_NAME = "paper-1.20.1-196.jar"
JAR_PATH = DATA_DIR / JAR_NAME

# Java download URLs (OpenJDK 17)
JAVA_URLS = {
    "linux_x64": "https://download.java.net/java/GA/jdk17.0.2/dfd4a8d0985749f896bed50d7138ee7f/8/GPL/openjdk-17.0.2_linux-x64_bin.tar.gz",
    "linux_aarch64": "https://download.java.net/java/GA/jdk17.0.2/dfd4a8d0985749f896bed50d7138ee7f/8/GPL/openjdk-17.0.2_linux-aarch64_bin.tar.gz"
}

def load_env_vars():
    """Load environment variables from .env file if it exists"""
    env_vars = {}
    env_file = Path(".env")
    
    if env_file.exists():
        try:
            with open(env_file, 'r') as f:
                for line in f:
                    line = line.strip()
                    if line and not line.startswith('#') and '=' in line:
                        key, value = line.split('=', 1)
                        env_vars[key.strip()] = value.strip().strip('"\'')
            print(f"Loaded {len(env_vars)} environment variables from .env")
        except Exception as e:
            print(f"Warning: Could not read .env file: {e}")
    
    # Also check system environment variables
    ngrok_token = env_vars.get('NGROK_TOKEN') or os.getenv('NGROK_TOKEN')
    
    return {
        'NGROK_TOKEN': ngrok_token
    }

def get_platform():
    """Detect the platform architecture"""
    import platform
    machine = platform.machine().lower()
    if 'x86_64' in machine or 'amd64' in machine:
        return "linux_x64"
    elif 'aarch64' in machine or 'arm64' in machine:
        return "linux_aarch64"
    else:
        print(f"Unsupported architecture: {machine}")
        return "linux_x64"  # Default fallback

def check_ngrok():
    """Check if ngrok is available and return the path"""
    print("Checking for ngrok...")
    
    # Check system PATH first
    ngrok_path = shutil.which("ngrok")
    if ngrok_path:
        try:
            result = subprocess.run([ngrok_path, "version"], capture_output=True, text=True)
            print(f"Found ngrok in PATH: {ngrok_path}")
            print(f"Version: {result.stdout.strip()}")
            return ngrok_path
        except Exception as e:
            print(f"Error checking system ngrok: {e}")
    
    # Check our custom ngrok installation
    custom_ngrok = NGROK_DIR / "ngrok"
    if custom_ngrok.exists():
        try:
            result = subprocess.run([str(custom_ngrok), "version"], capture_output=True, text=True)
            print(f"Found custom ngrok: {custom_ngrok}")
            print(f"Version: {result.stdout.strip()}")
            return str(custom_ngrok)
        except Exception as e:
            print(f"Error checking custom ngrok: {e}")
    
    print("ngrok not found")
    return None

def install_ngrok():
    """Download and install ngrok"""
    print("Installing ngrok...")
    
    # Create ngrok directory
    NGROK_DIR.mkdir(parents=True, exist_ok=True)
    
    # Download ngrok
    ngrok_archive = DATA_DIR / "ngrok.tgz"
    if not download_file(NGROK_URL, ngrok_archive):
        return False
    
    # Extract ngrok
    print("Extracting ngrok...")
    try:
        import tarfile
        with tarfile.open(ngrok_archive, 'r:gz') as tar:
            tar.extractall(NGROK_DIR)
        
        # Make ngrok executable
        ngrok_bin = NGROK_DIR / "ngrok"
        ngrok_bin.chmod(0o755)
        
        # Clean up
        ngrok_archive.unlink()
        
        print(f"βœ“ ngrok installed to: {ngrok_bin}")
        return True
        
    except Exception as e:
        print(f"Failed to extract ngrok: {e}")
        return False

def setup_ngrok():
    """Setup ngrok with authentication token using pyngrok"""
    print("Setting up ngrok...")
    
    if not PYNGROK_AVAILABLE:
        print("pyngrok is not available. Cannot setup ngrok.")
        return False
    
    env_vars = load_env_vars()
    ngrok_token = env_vars.get('NGROK_TOKEN')
    
    if not ngrok_token:
        print("Warning: NGROK_TOKEN not found in environment variables or .env file")
        print("Please set NGROK_TOKEN in your .env file or Hugging Face Space secrets")
        return False
    
    try:
        # Set the auth token
        ngrok.set_auth_token(ngrok_token)
        print("βœ“ ngrok configured successfully with pyngrok")
        return True
        
    except Exception as e:
        print(f"Failed to configure ngrok: {e}")
        return False

def start_ngrok_tunnel(port=25565):
    """Start ngrok tunnel for the Minecraft server using pyngrok"""
    print(f"Starting ngrok tunnel for port {port}...")
    
    if not PYNGROK_AVAILABLE:
        print("pyngrok is not available. Cannot start tunnel.")
        return None, None
    
    try:
        # Create a TCP tunnel
        print("Creating ngrok tunnel...")
        tunnel = ngrok.connect(port, "tcp")
        tunnel_url = tunnel.public_url
        
        print(f"βœ“ ngrok tunnel active: {tunnel_url}")
        print(f"Players can connect to: {tunnel_url.replace('tcp://', '')}")
        
        # Get all active tunnels for reference
        tunnels = ngrok.get_tunnels()
        print(f"Active tunnels: {len(tunnels)}")
        
        return tunnel, tunnel_url
        
    except Exception as e:
        print(f"Failed to start ngrok tunnel: {e}")
        return None, None

def check_java():
    """Check if Java is available and return the path"""
    # Check if java is already in PATH
    java_path = shutil.which("java")
    if java_path:
        try:
            result = subprocess.run([java_path, "-version"], capture_output=True, text=True)
            print(f"Found Java: {java_path}")
            print(result.stderr.split('\n')[0])
            return java_path
        except:
            pass
    
    custom_java = JAVA_DIR / "bin" / "java"
    if custom_java.exists():
        try:
            result = subprocess.run([str(custom_java), "-version"], capture_output=True, text=True)
            print(f"Found custom Java: {custom_java}")
            print(result.stderr.split('\n')[0])
            return str(custom_java)
        except:
            pass
    
    return None

def download_file(url, destination):
    print(f"Downloading {url}...")
    try:
        destination.parent.mkdir(parents=True, exist_ok=True)
        
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
        }
        request = urllib.request.Request(url, headers=headers)
        
        with urllib.request.urlopen(request) as response:
            with open(destination, 'wb') as f:
                total_size = int(response.headers.get('content-length', 0))
                block_size = 8192
                downloaded = 0
                
                while True:
                    buffer = response.read(block_size)
                    if not buffer:
                        break
                    
                    downloaded += len(buffer)
                    f.write(buffer)
                    
                    if total_size:
                        percent = min(100, (downloaded * 100) // total_size)
                        print(f"\rProgress: {percent}% ({downloaded}/{total_size} bytes)", end="")
                
        print(f"\nβœ“ Downloaded: {destination}")
        return True
    except Exception as e:
        print(f"\nβœ— Failed to download {url}: {e}")
        return False

def install_java():
    """Download and install Java if not available"""
    print("Installing Java...")
    
    platform_key = get_platform()
    java_url = JAVA_URLS.get(platform_key)
    
    if not java_url:
        print(f"No Java download available for platform: {platform_key}")
        return False
    
    java_archive = DATA_DIR / "java.tar.gz"
    if not download_file(java_url, java_archive):
        return False
    
    print("Extracting Java...")
    try:
        import tarfile
        with tarfile.open(java_archive, 'r:gz') as tar:
            temp_dir = DATA_DIR / "java_temp"
            temp_dir.mkdir(exist_ok=True)
            tar.extractall(temp_dir)
            
            extracted_dirs = [d for d in temp_dir.iterdir() if d.is_dir() and d.name.startswith('jdk')]
            if not extracted_dirs:
                print("Could not find extracted JDK directory")
                return False
            
            jdk_dir = extracted_dirs[0]
            
            if JAVA_DIR.exists():
                shutil.rmtree(JAVA_DIR)
            shutil.move(str(jdk_dir), str(JAVA_DIR))
            
            shutil.rmtree(temp_dir)
            java_archive.unlink()
            
        print(f"βœ“ Java installed to: {JAVA_DIR}")
        
        java_bin = JAVA_DIR / "bin" / "java"
        java_bin.chmod(0o755)
        
        return True
        
    except Exception as e:
        print(f"Failed to extract Java: {e}")
        return False

def setup_minecraft_server():
    DATA_DIR.mkdir(parents=True, exist_ok=True)
    
    java_path = check_java()
    if not java_path:
        print("Java not found. Installing Java...")
        if not install_java():
            print("Failed to install Java. Cannot proceed.")
            return False
        java_path = check_java()
        if not java_path:
            print("Java installation failed or not working.")
            return False
    
    if not JAR_PATH.exists():
        print(f"Downloading Minecraft server to {JAR_PATH}")
        if not download_file(PAPER_JAR_URL, JAR_PATH):
            return False
    else:
        print(f"Server JAR already exists at {JAR_PATH}")
    
    server_properties_path = DATA_DIR / "server.properties"
    print("Creating server.properties...")
    server_properties = """# Minecraft server properties
server-port=25565
gamemode=survival
difficulty=easy
spawn-protection=0
max-players=64
online-mode=true
white-list=false
motd=Hugging Face Spaces Minecraft Server
enable-query=true
query.port=25565
"""
    with open(server_properties_path, 'w') as f:
        f.write(server_properties)
    
    # Create eula.txt (required for server to start)
    eula_path = DATA_DIR / "eula.txt"
    if not eula_path.exists():
        print("Creating eula.txt...")
        with open(eula_path, 'w') as f:
            f.write("eula=true\n")
    
    print("βœ“ Minecraft server setup complete!")
    print(f"Server files are stored in: {DATA_DIR}")
    print(f"Java path: {java_path}")
    return True

def start_server():
    """Start the Minecraft server"""
    if not JAR_PATH.exists():
        print("Server JAR not found. Run setup first.")
        return False
    
    # Get Java path
    java_path = check_java()
    if not java_path:
        print("Java not found. Run setup first.")
        return False
    
    # Change to the data directory
    os.chdir(DATA_DIR)
    
    # Start the server
    cmd = [
        java_path,
        "-Xmx2G",  # Max heap size
        "-Xms1G",  # Initial heap size
        "-jar", str(JAR_PATH),
        "--nogui"
    ]
    
    print(f"Starting Minecraft server with command: {' '.join(cmd)}")
    print(f"Working directory: {DATA_DIR}")
    
    try:
        process = subprocess.Popen(
            cmd,
            stdout=subprocess.PIPE,
            stderr=subprocess.STDOUT,
            universal_newlines=True,
            bufsize=1
        )
        
        for line in process.stdout:
            print(line.strip())
            
    except Exception as e:
        print(f"Failed to start server: {e}")
        return False

def start_server_background():
    """Start the Minecraft server in the background"""
    global server_process
    
    if not JAR_PATH.exists():
        print("Server JAR not found. Run setup first.")
        return False
    
    # Get Java path
    java_path = check_java()
    if not java_path:
        print("Java not found. Run setup first.")
        return False
    
    # Change to the data directory
    original_cwd = os.getcwd()
    os.chdir(DATA_DIR)
    
    # Start the server
    cmd = [
        java_path,
        "-Xmx2G",  # Max heap size
        "-Xms1G",  # Initial heap size
        "-jar", str(JAR_PATH),
        "--nogui"
    ]
    
    print(f"Starting Minecraft server in background with command: {' '.join(cmd)}")
    print(f"Working directory: {DATA_DIR}")
    
    try:
        server_process = subprocess.Popen(
            cmd,
            stdout=subprocess.PIPE,
            stderr=subprocess.STDOUT,
            universal_newlines=True,
            bufsize=1,
            cwd=DATA_DIR
        )
        
        # Start a thread to monitor server output
        def monitor_server():
            for line in server_process.stdout:
                print(f"[SERVER] {line.strip()}")
        
        monitor_thread = threading.Thread(target=monitor_server, daemon=True)
        monitor_thread.start()
        
        # Wait a bit for server to start
        time.sleep(5)
        
        os.chdir(original_cwd)
        print("βœ“ Minecraft server started in background")
        return True
        
    except Exception as e:
        print(f"Failed to start server: {e}")
        os.chdir(original_cwd)
        return False

def create_gradio_interface():
    """Create and return the Gradio interface"""
    global server_ip
    server = JavaServer.lookup(tunnel_url)
    status = server.status()
    latency = server.ping()
    
    with gr.Blocks() as demo:
        def get_server_status():
            server = JavaServer.lookup(tunnel_url)
            status = server.status()
            latency = server.ping() 
            return f"**Server Status:** {status.players.online} player(s) online, latency: {round(latency)} ms", f"**Server Version:** {status.version.name} ({status.version.protocol})", f"**Server Description:** {status.description}", f"**Players:** {', '.join(player.name for player in status.players.sample)}" if status.players.sample else "No players online"
        gr.Markdown("# Minecraft Server Status")
        gr.Markdown(f"**Server IP:** {tunnel_url}")
        status_markdown = gr.Markdown(f"**Server Status:** {status.players.online} player(s) online, latency: {round(latency)} ms")
        version_markdown = gr.Markdown(f"**Server Version:** {status.version.name} ({status.version.protocol})")
        description_markdown = gr.Markdown(f"**Server Description:** {status.description}")
        players_markdown = gr.Markdown(f"**Players:** {', '.join(player.name for player in status.players.sample)}" if status.players.sample else "No players online")
        btn = gr.Button("Refresh Status")
        btn.click(get_server_status, outputs=[status_markdown, version_markdown, description_markdown, players_markdown])
    return demo

def stop_server():
    """Stop the Minecraft server"""
    global server_process
    
    if server_process:
        print("Stopping Minecraft server...")
        server_process.terminate()
        try:
            server_process.wait(timeout=10)
        except subprocess.TimeoutExpired:
            server_process.kill()
        server_process = None
        print("βœ“ Minecraft server stopped")

def cleanup_ngrok():
    """Clean up ngrok tunnels on exit"""
    if PYNGROK_AVAILABLE:
        try:
            ngrok.kill()
            print("βœ“ ngrok tunnels cleaned up")
        except:
            pass

def cleanup_all():
    """Clean up all resources"""
    stop_server()
    cleanup_ngrok()

def signal_handler(signum, frame):
    """Handle shutdown signals"""
    print("\nReceived shutdown signal. Cleaning up...")
    cleanup_all()
    sys.exit(0)

if __name__ == "__main__":
    # Register signal handlers
    signal.signal(signal.SIGINT, signal_handler)
    signal.signal(signal.SIGTERM, signal_handler)
    
    print("=== Minecraft Server Setup ===")
    
    try:
        if not setup_minecraft_server():
            print("Setup failed. Cannot start server.")
            sys.exit(1)
        
        print("\n=== Setting up ngrok ===")
        if not setup_ngrok():
            print("ngrok setup failed. Server will start but won't be accessible externally.")
            tunnel = None
        else:
            print("\n=== Starting ngrok tunnel ===")
            tunnel, tunnel_url = start_ngrok_tunnel()
            if tunnel:
                ngrok_tunnel = tunnel
                # Extract IP from tunnel URL (format: tcp://x.tcp.ngrok.io:port)
                server_ip = tunnel_url.replace('tcp://', '')
                print(f"Server will be accessible at: {server_ip}")
            else:
                print("Failed to start ngrok tunnel. Server will start but won't be accessible externally.")
        
        print("\n=== Starting Minecraft Server ===")
        if not start_server_background():
            print("Failed to start server.")
            cleanup_all()
            sys.exit(1)
        
        # Wait a bit for server to fully start
        print("Waiting for server to start...")
        time.sleep(10)
        
        print("\n=== Starting Gradio Interface ===")
        demo = create_gradio_interface()
        
        # Launch Gradio interface
        demo.launch(
            server_name="0.0.0.0",
            server_port=7860,
            share=False,
            show_error=True
        )
        
    except KeyboardInterrupt:
        print("\n\nShutting down...")
        cleanup_all()
        sys.exit(0)
    except Exception as e:
        print(f"Unexpected error: {e}")
        cleanup_all()
        sys.exit(1)