aldigobbler commited on
Commit
cd56b8b
·
verified ·
1 Parent(s): 5ba1f2c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -22
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 a moment to start
179
  import time
180
- time.sleep(3)
 
181
 
182
- # Try to get the tunnel URL from ngrok API
183
- try:
184
- import json
185
- import urllib.request
 
 
 
186
 
187
- # ngrok exposes a local API on port 4040
188
- api_url = "http://localhost:4040/api/tunnels"
189
- with urllib.request.urlopen(api_url) as response:
190
- data = json.loads(response.read().decode())
 
 
 
 
 
191
 
192
- if data.get('tunnels'):
193
- tunnel_url = data['tunnels'][0]['public_url']
194
- print(f"✓ ngrok tunnel active: {tunnel_url}")
195
- print(f"Players can connect to: {tunnel_url.replace('tcp://', '')}")
196
- return process, tunnel_url
197
- else:
198
- print("No active tunnels found")
199
- return process, None
200
-
201
- except Exception as e:
202
- print(f"Could not retrieve tunnel URL: {e}")
203
- print("Check ngrok dashboard at http://localhost:4040")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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}")