Skip to content

Instantly share code, notes, and snippets.

@Eleeleth
Created June 16, 2013 23:31
Show Gist options
  • Save Eleeleth/5793836 to your computer and use it in GitHub Desktop.
Save Eleeleth/5793836 to your computer and use it in GitHub Desktop.
Resolver + get forwarded ip if not resolved.
#create a new server if none exists already
require 'socket'
def resolver(server)
puts "Trying to resolve #{server} to IP..."
#Create our socket.
host = 'game.toribash.com'
port = 22000
lobbySock = TCPSocket.new host, port
#loop over lobby output
while line = lobbySock.gets
if line =~ /^SERVER/
line = line.split('; ')[1]
line = line.split ' '
if server == line[1]
ip = line[0]
puts " => resolved #{server} to #{ip}"
break
end
end
end
if !ip
puts " => could not resolve #{server} to an IP"
end
lobbySock.close
#return ip
ip
end
server = 'nonexisting'
#If server doesn't exist, let's get an ip to connect to
if !resolver server
host = 'game.toribash.com'
port = 22001 #Apparently port 22001 is what redirects me when I try to join a room
sock = TCPSocket.new host, port
sock.puts "join #{server}"
while line = sock.gets
if line =~ /^FORWARD/
ip = line.split(';')[1]
puts " => got forwarded to #{ip}"
end
end
sock.close
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment