Class: SocketHarness::Proxy

Inherits:
Object
  • Object
show all
Defined in:
lib/socket_harness/proxy.rb

Constant Summary collapse

BLOCK_SIZE =
1024

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(remote_host, remote_port, options = {}) ⇒ Proxy

Returns a new instance of Proxy.



13
14
15
16
17
# File 'lib/socket_harness/proxy.rb', line 13

def initialize(remote_host, remote_port, options={})
  @remote_host = remote_host
  @remote_port = remote_port
  @options = self.class.default_options.merge(options)
end

Class Method Details

.default_optionsObject



5
6
7
8
9
10
11
# File 'lib/socket_harness/proxy.rb', line 5

def self.default_options
  {
    :local_port => 13012,
    :block_size => 1024,
    :delay => 0,
  }
end

Instance Method Details

#runObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/socket_harness/proxy.rb', line 19

def run
  puts "*** target #{@remote_host}:#{@remote_port}"
  server = TCPServer.open(@options[:local_host], @options[:local_port].to_i)

  port = server.addr[1]
  addrs = server.addr[2..-1].uniq

  puts "*** listening on #{addrs.collect{|a|"#{a}:#{port}"}.join(' ')}"

  # abort on exceptions, otherwise threads will be silently killed in case
  # of unhandled exceptions
  Thread.abort_on_exception = true

  # have a thread just to process Ctrl-C events on Windows
  # (although Ctrl-Break always works)
  Thread.new { loop { sleep 1 } }
  loop do
    # whenever server.accept returns a new connection, start
    # a handler thread for that connection
    Thread.start(server.accept) { |local| conn_thread(local) }
  end
end