class Hula::HttpProxyUpstreamSocks

Attributes

http_host[R]
http_port[R]
polipo_bin[R]
socks_proxy_host[R]
socks_proxy_port[R]

Public Class Methods

new( polipo_bin: 'polipo', socks_proxy:, http_host: 'localhost', http_port: free_port ) click to toggle source
# File lib/hula/http_proxy_upstream_socks.rb, line 17
def initialize(
  polipo_bin: 'polipo',
  socks_proxy:,
  http_host: 'localhost',
  http_port: free_port
)
  @socks_proxy_host = socks_proxy.socks_host
  @socks_proxy_port = socks_proxy.socks_port
  @http_host = http_host
  @http_port = http_port
  @polipo_bin = polipo_bin

  check_polipo_bin!
end

Public Instance Methods

start() click to toggle source
# File lib/hula/http_proxy_upstream_socks.rb, line 34
def start
  @process ||= start_polipo_process
end
stop() click to toggle source
# File lib/hula/http_proxy_upstream_socks.rb, line 38
def stop
  return unless @process

  Process.kill('TERM', @process) rescue Errno::ESRCH
  Process.wait(@process)         rescue Errno::ECHILD
  @process = nil
end

Private Instance Methods

check_polipo_bin!() click to toggle source
# File lib/hula/http_proxy_upstream_socks.rb, line 66
def check_polipo_bin!
  unless system("which #{polipo_bin} > /dev/null 2>&1")
    raise "Could not run polipo (#{polipo_bin}). Please install, or put in PATH"
  end
end
polipo_command() click to toggle source
# File lib/hula/http_proxy_upstream_socks.rb, line 59
def polipo_command
  "#{polipo_bin} diskCacheRoot='' \
    proxyPort=#{http_port} \
    socksParentProxy=#{socks_proxy_host}:#{socks_proxy_port} \
    socksProxyType=socks4a"
end
start_polipo_process() click to toggle source
# File lib/hula/http_proxy_upstream_socks.rb, line 51
def start_polipo_process
  pid = Process.spawn(polipo_command)
  at_exit { stop }
  wait_for_port(host: http_host, port: http_port)
  Process.detach(pid)
  pid
end