#!/usr/bin/env ruby require 'thin' require 'fileutils' require 'timeout' require 'stringio' require 'time' require 'forwardable' require 'rack' require 'daemons' module PhantomJSProxy CONFIG = File.expand_path(File.dirname(__FILE__))+"/../lib/phantom_proxy/config.ru" end require 'phantom_proxy' # Become a daemon options = { :app_name => "phantom_proxy", :backtrace => true, :ontop => true, :log_output => true } #Daemons.daemonize(options) PARAMETERS = Array.new hmac_key = nil phantom = false last_arg = nil ARGV.each { |arg| if !/-hmac/.match(arg) && !/-hmac/.match(last_arg) && !/-self/.match(arg) PARAMETERS << arg end phantom = true if /-self/.match(arg) hmac_key = arg if /-hmac/.match(last_arg) last_arg = arg } if hmac_key if !File.directory?("/tmp/phantom_proxy") Dir.mkdir("/tmp/phantom_proxy") end File.open("/tmp/phantom_proxy/key", 'w+') {|f| f.write(hmac_key) } else begin File.delete("/tmp/phantom_proxy/key") rescue end end if !phantom startoptions = ["start", "-R", PhantomJSProxy::CONFIG, "-P", "/tmp/pids/phantom_proxy.pid", "--tag", "phantom_proxy"]+PARAMETERS runner = Thin::Runner.new(startoptions).run! else Thin::Server.start(PhantomJSProxy::PhantomJSServer.new, ARGV[0], ARGV[1], ARGV[2]) end