Spaces:
Sleeping
Sleeping
| set -e | |
| # ------------------------------------------------------------------------------ | |
| # 1. Start sshd on port 22 (internal) | |
| # ------------------------------------------------------------------------------ | |
| echo "[*] Starting SSH server..." | |
| /usr/sbin/sshd | |
| # Verify sshd is running | |
| sleep 1 | |
| if ! pgrep sshd > /dev/null; then | |
| echo "[!] sshd failed to start. Check /etc/ssh/sshd_config" | |
| exit 1 | |
| fi | |
| echo "[*] sshd running on port 22" | |
| # ------------------------------------------------------------------------------ | |
| # 2. Start gsocket to expose sshd over the gsocket network | |
| # Connect to it from anywhere with: | |
| # gsocket -s mypass ssh user@gsocket | |
| # ------------------------------------------------------------------------------ | |
| echo "[*] Starting gsocket tunnel for SSH (passkey: mypass)..." | |
| gs-netcat -s mypass -l -e /usr/sbin/sshd & | |
| GSOCKET_PID=$! | |
| # ------------------------------------------------------------------------------ | |
| # 3. Start code-server (VS Code) on port 8080 (internal, optional) | |
| # ------------------------------------------------------------------------------ | |
| echo "[*] Starting code-server on port 8080..." | |
| code-server --bind-addr 0.0.0.0:8080 --auth none /app & | |
| # ------------------------------------------------------------------------------ | |
| # 4. CRITICAL: Serve HTTP on port 7860 | |
| # Hugging Face Spaces REQUIRES port 7860 to respond with HTTP. | |
| # Without this the Space shows as "unhealthy" and gets killed. | |
| # This page also shows connection instructions. | |
| # ------------------------------------------------------------------------------ | |
| echo "[*] Starting HTTP status page on port 7860..." | |
| python3 - <<'PYEOF' & | |
| import http.server, socketserver, os | |
| HTML = """<!DOCTYPE html> | |
| <html> | |
| <head><title>Dev Space</title> | |
| <style> | |
| body { font-family: monospace; background: #1e1e2e; color: #cdd6f4; padding: 2em; } | |
| h1 { color: #89b4fa; } | |
| code { background: #313244; padding: 2px 8px; border-radius: 4px; } | |
| pre { background: #313244; padding: 1em; border-radius: 8px; } | |
| </style> | |
| </head> | |
| <body> | |
| <h1>🟢 Dev Space is Running</h1> | |
| <p>Connect via gsocket SSH from any machine:</p> | |
| <pre>gsocket -s mypass ssh user@localhost</pre> | |
| <p>Or connect to VS Code:</p> | |
| <pre>code-server is on internal port 8080</pre> | |
| </body> | |
| </html>""" | |
| class Handler(http.server.BaseHTTPRequestHandler): | |
| def do_GET(self): | |
| self.send_response(200) | |
| self.send_header("Content-Type", "text/html") | |
| self.end_headers() | |
| self.wfile.write(HTML.encode()) | |
| def log_message(self, *args): | |
| pass # suppress access logs | |
| with socketserver.TCPServer(("", 7860), Handler) as httpd: | |
| print("[*] HTTP status page serving on port 7860") | |
| httpd.serve_forever() | |
| PYEOF | |
| HTTP_PID=$! | |
| # ------------------------------------------------------------------------------ | |
| # 5. Keep the container alive — wait on gsocket | |
| # ------------------------------------------------------------------------------ | |
| echo "[*] All services started. Space is ready." | |
| echo " - SSH via gsocket: gsocket -s mypass ssh user@localhost" | |
| echo " - HTTP status page: http://<your-space>.hf.space" | |
| wait $GSOCKET_PID |