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
|
# File 'lib/git_game_show/cli.rb', line 72
def host
begin
repo = Git.open(options[:repo_path])
password = options[:password] || generate_random_password
server = GameServer.new(
port: options[:port],
password: password,
rounds: options[:rounds],
repo: repo
)
local_ip = `hostname -I 2>/dev/null || ipconfig getifaddr en0 2>/dev/null`.strip
puts "Detecting your external IP address... (this will only take a second)"
begin
external_ip = `curl -s --connect-timeout 3 https://api.ipify.org || curl -s --connect-timeout 3 https://ifconfig.me || curl -s --connect-timeout 3 https://icanhazip.com`.strip
external_ip = nil if external_ip.empty? || external_ip.length > 45 rescue
external_ip = nil
end
display_ggs
prompt = TTY::Prompt.new
ip_choices = []
ip_choices << {name: "Local IP (#{local_ip})", value: {:type => :local, :ip => local_ip}} if !local_ip.empty?
ip_choices << {name: "External IP (#{external_ip})", value: {:type => :external, :ip => external_ip}} if external_ip
ip_choices << {name: "Automatic tunneling", value: {:type => :tunnel}}
ip_choices << {name: "Custom IP or hostname", value: {:type => :custom}}
display_info_box(:info, "Network Setup", [
" • Local IP: Only for players on the same network",
" • External IP: For internet players",
" - requires router port forwarding",
" • Automatic tunneling: For internet players",
" - requires free account & authorization through ngrok",
" • Custom IP: For advanced users with specific network setup"
])
ip_choice = prompt.select("How should players connect to your game?", ip_choices)
external_port = options[:port] || GitGameShow::DEFAULT_CONFIG[:internal_port]
case ip_choice[:type]
when :local, :external
ip = ip_choice[:ip]
when :custom
ip = prompt.ask("Enter your IP address or hostname:", required: true)
when :tunnel
display_ggs
display_info_box(:info, "Ngrok Tunnel Setup", [
" The automatic tunneling option uses ngrok, a secure tunneling service.",
" This will allow players to connect from anywhere without port forwarding.",
" ",
" If you do not already have ngrok set up:",
" • The ngrok client will be downloaded and started automatically",
" • Instructions will be provided to set up a required free account on ngrok",
" • The tunnel will be established and the game will start"
])
begin
ngrok_available = system("which ngrok > /dev/null 2>&1") || system("where ngrok > /dev/null 2>&1")
unless ngrok_available
prompt = TTY::Prompt.new
install_ngrok = prompt.yes?("Ngrok is required for tunneling but wasn't found. Would you like to install it now?")
if install_ngrok
puts "Installing ngrok..."
os = RbConfig::CONFIG['host_os']
arch = RbConfig::CONFIG['host_cpu']
arch_suffix = arch =~ /64|amd64/ ? '64' : '32'
download_url = if os =~ /darwin/i
"https://bin.equinox.io/c/bNyj1mQVY4c/ngrok-v3-stable-darwin-#{arch =~ /arm|aarch64/ ? 'arm64' : 'amd64'}.zip"
elsif os =~ /linux/i
"https://bin.equinox.io/c/bNyj1mQVY4c/ngrok-v3-stable-linux-#{arch =~ /arm|aarch64/ ? 'arm64' : 'amd64'}.zip"
elsif os =~ /mswin|mingw|cygwin/i
"https://bin.equinox.io/c/bNyj1mQVY4c/ngrok-v3-stable-windows-#{arch_suffix}.zip"
else
nil
end
if download_url.nil?
puts "Could not determine your system type. Please install ngrok manually from https://ngrok.com/download"
exit(1)
end
user_home = ENV['HOME'] || ENV['USERPROFILE']
install_dir = File.join(user_home, '.git_game_show')
FileUtils.mkdir_p(install_dir) unless Dir.exist?(install_dir)
puts "Downloading ngrok from #{download_url}..."
require 'open-uri'
require 'tempfile'
temp_zip = Tempfile.new(['ngrok', '.zip'])
temp_zip.binmode
begin
URI.open(download_url) do |remote_file|
temp_zip.write(remote_file.read)
end
temp_zip.close
require 'zip'
puts "Extracting ngrok..."
Zip::File.open(temp_zip.path) do |zip_file|
zip_file.each do |entry|
entry_path = File.join(install_dir, entry.name)
entry.(entry_path) { true } FileUtils.chmod(0755, entry_path) if entry.name == 'ngrok' || entry.name == 'ngrok.exe'
end
end
ENV['PATH'] = "#{install_dir}:#{ENV['PATH']}"
ngrok_path = File.join(install_dir, os =~ /mswin|mingw|cygwin/i ? 'ngrok.exe' : 'ngrok')
if File.exist?(ngrok_path)
puts "Ngrok installed successfully to #{install_dir}"
ngrok_available = true
puts "\nTIP: To use ngrok in other terminal sessions, add this to your shell profile:"
puts "export PATH=\"#{install_dir}:$PATH\""
puts "\nPress Enter to continue..."
gets
else
puts "Failed to install ngrok. Please install manually from https://ngrok.com/download"
exit(1)
end
rescue => e
puts "Error installing ngrok: #{e.message}"
puts "Please install manually from https://ngrok.com/download"
exit(1)
ensure
temp_zip.unlink
end
else
puts "Ngrok installation declined. Please choose a different connection option."
exit(1)
end
end
puts "Starting tunnel service... (this may take a few moments)"
require 'open3'
require 'json'
system("pkill -f ngrok > /dev/null 2>&1 || taskkill /F /IM ngrok.exe > /dev/null 2>&1")
puts "Checking for existing ngrok sessions..."
api_available = system("curl -s http://localhost:4040/api/tunnels > /dev/null 2>&1")
if api_available
puts "Found existing ngrok session. Attempting to use it or restart if needed..."
system("pkill -f ngrok > /dev/null 2>&1 || taskkill /F /IM ngrok.exe > /dev/null 2>&1")
sleep(1)
end
puts "Checking ngrok authentication status..."
auth_check = `ngrok config check 2>&1`
auth_needed = auth_check.include?("auth") || auth_check.include?("authtoken") || auth_check.include?("ERR") || auth_check.include?("error")
if auth_needed
display_ggs
display_info_box(:info, 'Ngrok Authorization Required', [
" Starting with ngrok v3, you need to create a free account and authorize ",
" to use TCP tunnels. This is a one-time setup. ",
" ",
" Steps to authorize ngrok: ",
" 1. Create a free account at https://ngrok.com/signup ",
" 2. Get an authtoken https://dashboard.ngrok.com/get-started/your-authtoken ",
" 3. Enter your authtoken below "
])
puts ""
prompt = TTY::Prompt.new
token = prompt.mask("Enter your ngrok authtoken:")
if token && !token.empty?
puts "Setting up ngrok authentication..."
auth_result = system("ngrok config add-authtoken #{token}")
if !auth_result
puts "Failed to set ngrok authtoken. Please try again manually with:"
puts " ngrok config add-authtoken YOUR_TOKEN"
puts ""
puts "Press Enter to continue with local IP instead..."
gets
ip = local_ip
return
else
puts "Successfully authenticated with ngrok!"
end
else
puts "No token provided. Falling back to local IP."
puts "Press Enter to continue..."
gets
ip = local_ip
return
end
end
puts "Starting ngrok tunnel for port #{options[:port]}..."
stdin, stdout, stderr, wait_thr = Open3.popen3("ngrok tcp #{options[:port]} --log=stdout")
err_thread = Thread.new do
while (error_line = stderr.gets)
if error_line.include?("ERR") || error_line.include?("error")
if error_line.include?("auth") || error_line.include?("authtoken")
puts "\nAuthentication error detected: #{error_line.strip}"
elsif error_line.include?("connection")
puts "\nConnection error detected: #{error_line.strip}"
elsif error_line.include?("bind") || error_line.include?("address already in use")
puts "\nPort binding error detected: #{error_line.strip}"
else
puts "\nngrok error: #{error_line.strip}"
end
end
end
end
puts "Waiting for tunnel to be established (this may take up to 30 seconds)..."
tunnel_url = nil
30.times do |attempt|
print "." if attempt > 0 && attempt % 5 == 0
status = Open3.capture2("curl -s http://localhost:4040/api/tunnels")
if status[1].success? tunnels = JSON.parse(status[0])['tunnels']
if tunnels && !tunnels.empty?
tunnel = tunnels.first
public_url = tunnel['public_url']
if public_url && public_url.start_with?('tcp://')
public_url = public_url.sub('tcp://', '')
host, port = public_url.split(':')
ip = host
external_port = port.to_i
tunnel_url = public_url
at_exit do
system("pkill -f ngrok > /dev/null 2>&1 || taskkill /F /IM ngrok.exe > /dev/null 2>&1")
end
display_ggs
content = [
" Your game is now accessible over the internet",
" The ngrok tunnel is running in the background:",
" ",
" ngrok ip: #{ip.ljust(57)}".colorize(:light_blue),
" ngrok port: #{external_port.to_s.ljust(57)}".colorize(:light_blue),
" ngrok public URL: #{public_url.ljust(57)}".colorize(:light_blue),
" ",
" DO NOT close the terminal window until your game is finished",
" The tunnel will automatically close when you exit the game"
]
display_info_box(:success, 'Ngrok tunnel established successfully!', content)
puts ""
puts "Press Enter to continue..."
gets
break
end
end
end
sleep 1
end
unless tunnel_url
display_ggs
display_info_box(:error, 'Tunnel setup failed!', [
" ngrok tunnel could not be established",
" Most common reason: Missing or invalid ngrok authentication token",
" Falling back to local IP (players will only be able to join locally)",
" ",
" Common solutions:",
" 1. Create a free account at https://ngrok.com/signup",
" 2. Get an authtoken https://dashboard.ngrok.com/get-started/your-authtoken",
" 3. Run this command: ngrok config add-authtoken <YOUR_TOKEN>",
" 4. Then restart the game and try tunneling again"
])
puts "Press Enter to continue with local IP..."
gets
ip = local_ip
end
rescue => e
display_ggs
display_info_box(:error, 'Error setting up ngrok tunnel!', [
" An error occurred while trying to set up the ngrok tunnel",
" This is likely an authentication issue with ngrok",
" Falling back to local IP (players will only be able to join locally)"
])
puts "Error details: #{e.message}"
puts ""
puts "Press Enter to continue with local IP..."
gets
ip = local_ip
end
end
unless ip_choice[:type] == :tunnel
internal_port = prompt.ask("Which port would you like to use?",
convert: :int,
default: GitGameShow::DEFAULT_CONFIG[:internal_port])
server.instance_variable_set(:@port, internal_port)
end
secure_link = "gitgame://#{ip}:#{external_port}/#{URI.encode_www_form_component(password)}"
server.start_with_ui(secure_link)
rescue Git::GitExecuteError
puts "Error: Not a valid Git repository at #{options[:repo_path]}".colorize(:red)
rescue => e
puts "Error: #{e.message}".colorize(:red)
end
end
|