Created
January 8, 2012 12:44
-
-
Save Milly/1578248 to your computer and use it in GitHub Desktop.
iRemocon Proxy
This file contains hidden or 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
#!/usr/bin/env ruby | |
# iRemocon Proxy | |
# | |
# Author: Milly | |
# Last update: Jan 9 2012 | |
require "socket" | |
@remote_host = ARGV[0] | |
unless @remote_host | |
puts "usage: #{$0} <server-address>" | |
puts "require server address." | |
exit 1 | |
end | |
@remote_port = 51013 | |
@listen_port = 51013 | |
@max_threads = 5 | |
@latest_client_socket = nil | |
@remote_thread = nil | |
@remote_socket = nil | |
def main | |
puts "starting server" | |
@server = TCPServer.new("", @listen_port) | |
client_threads = [] | |
while true | |
client_threads << client_connection | |
client_threads = client_threads.select { |t| t.alive? ? true : (t.join; false) } | |
while @max_threads <= client_threads.size | |
sleep 1 | |
client_threads = client_threads.select { |t| t.alive? ? true : (t.join; false) } | |
end | |
end | |
end | |
def remote_connection | |
puts "connecting to remote #{@remote_host}:#{@remote_port}" | |
@remote_socket = TCPSocket.new(@remote_host, @remote_port) | |
Thread.new do | |
begin | |
while true | |
socket = [ IO.select([@remote_socket]) ].flatten.first | |
begin | |
data = socket.readpartial(4096) | |
puts "server #{Thread.current}: server -> client #{data.inspect}" | |
rescue EOFError | |
break | |
end | |
begin | |
client_socket = @latest_client_socket | |
if client_socket | |
client_socket.write data | |
client_socket.flush | |
end | |
rescue StandardError => e | |
puts "#{Thread.current}: got exception #{e.inspect}" | |
end | |
end | |
rescue StandardError => e | |
puts "server #{Thread.current}: got exception #{e.inspect}" | |
end | |
puts "server #{Thread.current}: closing the connections" | |
@remote_socket.close rescue StandardError | |
end | |
end | |
def client_connection | |
puts "waiting for connections" | |
Thread.new(@server.accept) do |client_socket| | |
begin | |
puts "client #{Thread.current}: got a client connection" | |
@latest_client_socket = client_socket | |
if @remote_thread.nil? or not @remote_thread.alive? | |
@remote_thread = remote_connection | |
end | |
while true | |
socket = [ IO.select([client_socket]) ].flatten.first | |
@latest_client_socket = socket | |
begin | |
data = socket.readpartial(4096) | |
puts "client #{Thread.current}: client -> server #{data.inspect}" | |
@remote_socket.write data | |
@remote_socket.flush | |
rescue EOFError | |
break | |
end | |
end | |
rescue StandardError => e | |
puts "client #{Thread.current}: got exception #{e.inspect}" | |
end | |
puts "client #{Thread.current}: closing the connections" | |
client_socket.close rescue StandardError | |
end | |
end | |
main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment