Class: ProxyDaemon::Worker
- Inherits:
-
Object
- Object
- ProxyDaemon::Worker
- Defined in:
- lib/proxy_daemon/worker.rb
Instance Attribute Summary collapse
-
#url ⇒ Object
Returns the value of attribute url.
Instance Method Summary collapse
- #answer(answer, data = nil) ⇒ Object
- #call(&block) ⇒ Object
- #changeProxy(proxy) ⇒ Object
-
#initialize(parser = nil, parse_method = nil) ⇒ Worker
constructor
A new instance of Worker.
- #listen ⇒ Object
- #log(msg) ⇒ Object
- #parse(body) ⇒ Object
- #process(url) ⇒ Object
Constructor Details
#initialize(parser = nil, parse_method = nil) ⇒ Worker
Returns a new instance of Worker.
10 11 12 13 14 15 |
# File 'lib/proxy_daemon/worker.rb', line 10 def initialize(parser = nil, parse_method = nil) @client = Net::HTTP.Proxy(nil, nil) @parser = parser @parse_method = parse_method @url = '' end |
Instance Attribute Details
#url ⇒ Object
Returns the value of attribute url.
8 9 10 |
# File 'lib/proxy_daemon/worker.rb', line 8 def url @url end |
Instance Method Details
#answer(answer, data = nil) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/proxy_daemon/worker.rb', line 37 def answer(answer, data = nil) begin pack = {answer: answer} pack[:data] = data if data $stdout.puts pack.to_json $stdout.flush rescue Errno::EPIPE => e log 'Broken pipe with daemon, exiting...'.yellow Kernel.exit! rescue => e log e.inspect.red end end |
#call(&block) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/proxy_daemon/worker.rb', line 112 def call(&block) $stdout.sync = true @block = block if block_given? proxy = nil loop do begin task = JSON.parse(listen) case task['command'] when 'proxy' proxy = task['proxy'] changeProxy(proxy) process(@url) unless @url.empty? when 'url' @url = task['url'] process(@url) when 'exit' exit! end rescue => e log "rescue in #{'call'.yellow}: #{e.inspect.red}" answer 'error' end end end |
#changeProxy(proxy) ⇒ Object
96 97 98 99 100 101 102 |
# File 'lib/proxy_daemon/worker.rb', line 96 def changeProxy(proxy) if proxy == 'localhost' || proxy.nil? || (proxy = proxy.split(/\s+/)).length < 2 ENV['http_proxy'] = nil else ENV['http_proxy'] = "http://#{proxy[0]}:#{proxy[1]}" end end |
#listen ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/proxy_daemon/worker.rb', line 17 def listen begin command = '' Timeout::timeout(6) { command = ($stdin.gets || String.new).strip log ('Task: ' + command.to_s.yellow) if command.empty? log "Got empty answer from daemon, exiting...".yellow raise Timeout::Error end } rescue Timeout::Error answer 'timeout' Kernel.exit! end command end |
#log(msg) ⇒ Object
139 140 141 142 |
# File 'lib/proxy_daemon/worker.rb', line 139 def log(msg) $stderr.syswrite "[child #{Process.pid}]".magenta + " #{msg}\n" $stderr.flush end |
#parse(body) ⇒ Object
104 105 106 107 108 109 110 |
# File 'lib/proxy_daemon/worker.rb', line 104 def parse(body) raise NotImplementedError if @parser.nil? && @block.nil? @block.call(self, body) if @block @parser.send(:"parse_#{@parse_method}", self, body) if @parser # instance_exec body, &@block end |
#process(url) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/proxy_daemon/worker.rb', line 52 def process(url) begin uri = URI(url) Timeout::timeout(15) { @client = Net::HTTP.new(uri.host, uri.port) @client.use_ssl = (uri.scheme == 'https') @client.verify_mode = OpenSSL::SSL::VERIFY_NONE if uri.scheme == 'https' #@client.set_debug_output($stderr) @client.start { |http| req = Net::HTTP::Get.new(uri, 'Connection' => 'keep-alive', 'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', 'Upgrade-Insecure-Requests' => '1', 'User-Agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.13 Safari/537.36', 'Accept-Language' => 'en-US,en;q=0.8,ru;q=0.6' ) @page = http.request(req) } } if (!@page.is_a?(Net::HTTPOK) || @page.body.empty?) log @page raise Net::HTTPBadResponse end res = parse(@page.body) if !!res === res || res.nil? then answer(res ? 'ok' : 'error') else answer('ok', res) end rescue Timeout::Error, Errno::ETIMEDOUT, Errno::ECONNREFUSED, Errno::EINVAL, Errno::ECONNRESET, Errno::ENETUNREACH, SocketError, EOFError, TypeError, Zlib::BufError, Net::HTTPExceptions, Net::HTTPBadResponse, OpenSSL::SSL::SSLError => e log 'proxy'.red + " in #{'process'.yellow}: #{e.inspect.red}" answer 'proxy' rescue Interrupt => e log 'Interrupted by user, exiting...'.yellow Kernel.exit! rescue Exception => e log "rescue in #{'process'.yellow}: #{e.inspect},\n#{e.backtrace.join("\n").red}" answer 'error' end end |