Created
October 20, 2010 02:05
-
-
Save DylanLukes/635620 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'socket' | |
local_port = 45788 | |
# Quick check | |
if ARGV.length() < 2 then | |
puts "Syntax: 'ruby InventProxy.rb [dest_host] [dest_port]'" | |
exit() | |
end | |
# Collect the destination host & port | |
dst_host = ARGV[0] | |
dst_port = ARGV[1] | |
# Connect to the MC Client | |
client_receiver = TCPServer.open(local_port) | |
client_receiver.setsockopt(Socket::SOL_SOCKET, Socket::SO_REUSEADDR, true) | |
puts "Connect your client to 127.0.0.1:#{local_port}" | |
client = client_receiver.accept() | |
# Connect to the MC Server | |
puts "Connecting to server..." | |
server = TCPSocket.open(dst_host, dst_port) | |
puts "Connected." | |
while true | |
(fds, dummy, dummy) = IO.select([client, server, STDIN]) | |
begin | |
fds.each do |fd| | |
data = fd.readpartial(4096) | |
if fd == client | |
# Client --> Server | |
server.write(data) | |
server.flush() | |
elsif fd == server | |
# Server --> Client | |
client.write(data) | |
client.flush() | |
elsif fd = STDIN | |
# Parse commands (perhaps plugins here) | |
if data =~ /!give\s([0-9]+)\s([0-9]+)/ | |
# Give function: | |
bytearray = [0x11, $1.to_i, $2.to_i, 0x00] | |
packet = bytearray.pack("cncn") | |
client.write(packet) | |
end | |
end | |
end | |
rescue EOFError, Interrupt | |
break | |
end | |
end | |
# Cleanup | |
puts "\nShutting down" | |
server.close() | |
client.close() | |
client_receiver.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment