Class: Zokor::ProxyMagic
- Inherits:
-
Object
- Object
- Zokor::ProxyMagic
- Defined in:
- lib/zokor/proxy_magic.rb
Instance Attribute Summary collapse
-
#opts ⇒ Object
readonly
Returns the value of attribute opts.
-
#remote_host ⇒ Object
readonly
Returns the value of attribute remote_host.
-
#remote_port ⇒ Object
readonly
Returns the value of attribute remote_port.
-
#server ⇒ Object
readonly
Returns the value of attribute server.
Instance Method Summary collapse
-
#initialize(local_host, local_port, remote_host, remote_port, opts = {}) ⇒ ProxyMagic
constructor
A new instance of ProxyMagic.
- #log ⇒ Object
- #run_loop ⇒ Object
- #serve(local_host, local_port) ⇒ Object
Constructor Details
#initialize(local_host, local_port, remote_host, remote_port, opts = {}) ⇒ ProxyMagic
Returns a new instance of ProxyMagic.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/zokor/proxy_magic.rb', line 10 def initialize(local_host, local_port, remote_host, remote_port, opts={}) @remote_host = remote_host @remote_port = remote_port @opts = opts @server = serve(local_host, local_port) if @opts[:proxy_url] log.info "Intermediate proxy: #{@opts.fetch(:proxy_url)}" else log.info "Intermediate proxy: none" end if @opts[:use_ssl] ssl_msg = '[SSL]' else ssl_msg = '[no SSL]' end log.info "External proxy: #{ssl_msg} #{remote_host}:#{remote_port}" end |
Instance Attribute Details
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
8 9 10 |
# File 'lib/zokor/proxy_magic.rb', line 8 def opts @opts end |
#remote_host ⇒ Object (readonly)
Returns the value of attribute remote_host.
8 9 10 |
# File 'lib/zokor/proxy_magic.rb', line 8 def remote_host @remote_host end |
#remote_port ⇒ Object (readonly)
Returns the value of attribute remote_port.
8 9 10 |
# File 'lib/zokor/proxy_magic.rb', line 8 def remote_port @remote_port end |
#server ⇒ Object (readonly)
Returns the value of attribute server.
8 9 10 |
# File 'lib/zokor/proxy_magic.rb', line 8 def server @server end |
Instance Method Details
#log ⇒ Object
60 61 62 |
# File 'lib/zokor/proxy_magic.rb', line 60 def log @log ||= Zokor::ProgLogger.new('zokor') end |
#run_loop ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/zokor/proxy_magic.rb', line 42 def run_loop log.info "Starting main loop..." # Add thread to process Ctrl-C on Windows _sleep_thread = Thread.new { loop { sleep 1 } } @threads = [] while true @threads.select!(&:alive?) log.debug "#{@threads.length} open connections" unless @threads.empty? @threads << Thread.start(server.accept) {|sock| c = Zokor::ProxyConnection.new(sock, remote_host, remote_port, opts) c.connect } end end |
#serve(local_host, local_port) ⇒ Object
33 34 35 36 37 38 39 40 |
# File 'lib/zokor/proxy_magic.rb', line 33 def serve(local_host, local_port) server = TCPServer.open(local_host, local_port) port = server.addr[1] addrs = server.addr[2..-1].uniq log.info "Listening on #{addrs.collect{|a|"#{a}:#{port}"}.join(' ')}" server end |