Class: Zokor::ProxyMagic

Inherits:
Object
  • Object
show all
Defined in:
lib/zokor/proxy_magic.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#optsObject (readonly)

Returns the value of attribute opts.



8
9
10
# File 'lib/zokor/proxy_magic.rb', line 8

def opts
  @opts
end

#remote_hostObject (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_portObject (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

#serverObject (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

#logObject



60
61
62
# File 'lib/zokor/proxy_magic.rb', line 60

def log
  @log ||= Zokor::ProgLogger.new('zokor')
end

#run_loopObject



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