Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -171,37 +171,67 @@ def start_ngrok_tunnel(port=25565):
|
|
171 |
|
172 |
try:
|
173 |
# Start ngrok in background
|
|
|
174 |
process = subprocess.Popen([
|
175 |
ngrok_path, "tcp", str(port), "--log", "stdout"
|
176 |
], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
177 |
|
178 |
-
# Give ngrok
|
179 |
import time
|
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 |
return process, None
|
|
|
|
|
|
|
|
|
|
|
|
|
205 |
|
206 |
except Exception as e:
|
207 |
print(f"Failed to start ngrok tunnel: {e}")
|
|
|
171 |
|
172 |
try:
|
173 |
# Start ngrok in background
|
174 |
+
print("Launching ngrok process...")
|
175 |
process = subprocess.Popen([
|
176 |
ngrok_path, "tcp", str(port), "--log", "stdout"
|
177 |
], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
178 |
|
179 |
+
# Give ngrok more time to start and wait for API to be available
|
180 |
import time
|
181 |
+
import json
|
182 |
+
import urllib.request
|
183 |
|
184 |
+
print("Waiting for ngrok to initialize...")
|
185 |
+
max_retries = 30 # Try for up to 30 seconds
|
186 |
+
retry_count = 0
|
187 |
+
|
188 |
+
while retry_count < max_retries:
|
189 |
+
time.sleep(1)
|
190 |
+
retry_count += 1
|
191 |
|
192 |
+
try:
|
193 |
+
# Check if ngrok process is still running
|
194 |
+
if process.poll() is not None:
|
195 |
+
# Process has ended, check for errors
|
196 |
+
stdout, stderr = process.communicate()
|
197 |
+
print(f"ngrok process ended unexpectedly:")
|
198 |
+
print(f"STDOUT: {stdout}")
|
199 |
+
print(f"STDERR: {stderr}")
|
200 |
+
return None, None
|
201 |
|
202 |
+
# Try to connect to ngrok API
|
203 |
+
api_url = "http://localhost:4040/api/tunnels"
|
204 |
+
with urllib.request.urlopen(api_url, timeout=2) as response:
|
205 |
+
data = json.loads(response.read().decode())
|
206 |
+
|
207 |
+
if data.get('tunnels'):
|
208 |
+
tunnel_url = data['tunnels'][0]['public_url']
|
209 |
+
print(f"✓ ngrok tunnel active: {tunnel_url}")
|
210 |
+
print(f"Players can connect to: {tunnel_url.replace('tcp://', '')}")
|
211 |
+
return process, tunnel_url
|
212 |
+
else:
|
213 |
+
print(f"Waiting for tunnel... (attempt {retry_count}/{max_retries})")
|
214 |
+
|
215 |
+
except urllib.error.URLError as e:
|
216 |
+
if retry_count % 5 == 0: # Print every 5 seconds
|
217 |
+
print(f"Waiting for ngrok API... (attempt {retry_count}/{max_retries})")
|
218 |
+
continue
|
219 |
+
except Exception as e:
|
220 |
+
print(f"Error checking ngrok status: {e}")
|
221 |
+
continue
|
222 |
+
|
223 |
+
# If we get here, we timed out
|
224 |
+
print("Timeout waiting for ngrok to start. Checking process status...")
|
225 |
+
if process.poll() is None:
|
226 |
+
print("ngrok process is still running but API is not responding.")
|
227 |
+
print("You may need to check the ngrok dashboard manually at http://localhost:4040")
|
228 |
return process, None
|
229 |
+
else:
|
230 |
+
stdout, stderr = process.communicate()
|
231 |
+
print(f"ngrok process ended during startup:")
|
232 |
+
print(f"STDOUT: {stdout}")
|
233 |
+
print(f"STDERR: {stderr}")
|
234 |
+
return None, None
|
235 |
|
236 |
except Exception as e:
|
237 |
print(f"Failed to start ngrok tunnel: {e}")
|