Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS 

Getting a client's IP address with EventMachine

This is from, and by: http://nhw.pl/wp/2007/12/07/eventmachine-how-to-get-clients-ip-address/ .. I just wanted a more permanent reference as it's cool code.

   1  
   2  EventMachine::run {
   3     EventMachine::open_datagram_socket $conf[:address],
   4                                   $conf[:udp_port], CustomServer
   5  }
   6  
   7  module CustomServer
   8     def receive_data d
   9        pp get_peername[2,6].unpack "nC4"
  10     end
  11  end

[Ruby] Script/bot to send MediaWiki Recent Changes over UDP to a Campfire room

Requires the Tinder gem, and PHP on the server running MediaWiki must be compiled with --enable-sockets. The computer running the bot will also have to be open to the web - it can't be behind a firewall.

First, add this code to your MediaWiki LocalSettings.php, replacing the IP address with the IP of the server/computer running the bot, and the port to whatever you want to use (probably something above 40,000, but it's up to you):
   1  
   2  $wgRC2UDPAddress = '69.178.6.244';
   3  $wgRC2UDPPort = '41895';
   4  $wgRC2UDPPrefix = "";


Now, you can run the bot! This is extremely hackish, because the retarded collective-mailing-list-coding system of the MediaWiki repos has somehow managed to code IRC color codes directly into their exported strings (at least with MediaWiki 1.2alpha, which is what I'm running right now).
You'll need to replace the new tinder definition with your subdomain on Campfire, the username and password with ones for the bot account you've created on your campfire room, and the room name with the room you want the updates to be sent to.
   1  
   2  require 'rubygems'
   3  require 'tinder'
   4  require 'socket'
   5  puts 'Dependencies loaded...'
   6  
   7  campfire = Tinder::Campfire.new 'subdomain'
   8  campfire.login 'email@domain.com', 'password'
   9  exit unless room = campfire.find_room_by_name('Case Sensitive Room Name')
  10  puts 'Campfire logged in...'
  11  
  12  server = UDPSocket.new
  13  exit unless server.bind('0.0.0.0', 41895)
  14  puts 'Socket listening...'
  15  
  16  loop do
  17    msg = server.recv(2048).gsub(/\003[\d]{0,2}/,'').chomp
  18    print "sending to room: #{msg.inspect}..."
  19    exit unless room.speak msg.to_s
  20    puts ' sent!'
  21  end

Send custom UDP packets in Ruby


From: http://www.ruby-forum.com/topic/124159
Author: Bill Kelly

For yet another nifty UDP snippet see Skype-Style Firewall Busting with Ruby and UDP.


   1  
   2  
   3  require 'socket'
   4  
   5  #abort "Usage: server_addr, server_port, cmd_str" unless ARGV.length == 3
   6  
   7  UDP_RECV_TIMEOUT = 3  # seconds
   8  
   9  def q2cmd(server_addr, server_port, cmd_str)
  10    resp, sock = nil, nil
  11    begin
  12     cmd = "\377\377\377\377#{cmd_str}\0"
  13      sock = UDPSocket.open
  14      sock.send(cmd, 0, server_addr, server_port)
  15      resp = if select([sock], nil, nil, UDP_RECV_TIMEOUT)
  16        sock.recvfrom(65536)
  17      end
  18      if resp
  19        resp[0] = resp[0][4..-1]  # trim leading 0xffffffff
  20      end
  21    rescue IOError, SystemCallError
  22    ensure
  23      sock.close if sock
  24    end
  25    resp ? resp[0] : nil
  26  end
  27  
  28  # your firewall has to allow communication with IP address 67.19.248.74 (port 27912)
  29  #server, port, cmd = *ARGV
  30  server = "tastyspleen.net"
  31  port = 27912
  32  cmd = "status"
  33  
  34  result = q2cmd(server, port, cmd)
  35  puts result
  36  

« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS