Skip to content

Instantly share code, notes, and snippets.

@andredublin
Created September 19, 2012 20:22
Show Gist options
  • Save andredublin/3752002 to your computer and use it in GitHub Desktop.
Save andredublin/3752002 to your computer and use it in GitHub Desktop.
Skype-Style Firewall Busting with Ruby and UDP
# From
# http://www.rubyinside.com/skype-style-firewall-busting-with-ruby-and-udp-399.html
require 'socket'
remote_host = ARGV.first
# Punches hole in firewall
punch = UDPSocket.new
punch.bind('', 6311)
punch.send('', 0, remote_host, 6311)
punch.close
# Bind for receiving
udp_in = UDPSocket.new
udp_in.bind('0.0.0.0', 6311)
puts "Binding to local port 6311"
loop do
# Receive data or time out after 5 seconds
if IO.select([udp_in], nil, nil, rand(4))
data = udp_in.recvfrom(1024)
remote_port = data[1][1]
remote_addr = data[1][3]
puts "Response from #{remote_addr}:#{remote_port} is #{data[0]}"
else
puts "Sending a little something.."
udp_in.send(Time.now.to_s, 0, remote_host, 6311)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment