Created
June 16, 2016 13:04
-
-
Save bogdanRada/19673303104470527595d4615a906bcf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/rvm/wrappers/ruby-1.9.3-p194/ruby | |
require "cgi" | |
require "uri" | |
require "socket" | |
SCREEN_WIDTH = 1280 | |
SCREEN_HEIGHT = 1024 | |
cgi = CGI.new | |
uri = URI.parse(ENV["QUERY_STRING"]) | |
unless uri.is_a? URI::HTTP or uri.is_a? URI::HTTPS | |
puts cgi.header | |
puts "<b>Invalid URI</b>" | |
exit | |
end | |
if Socket.getaddrinfo(uri.host, uri.scheme).find { |_, _, hostname, ip| hostname == "localhost" or ip == "127.0.0.1" or ip == "::1" } | |
puts cgi.header | |
puts "<b>there is no way you are looking at localhost buddy</b>" | |
exit | |
end | |
xvfb, firefox = nil | |
scr = 10 | |
scr += 1 while File.exist? "/tmp/.X#{scr}-lock" | |
File.open "/tmp/cgi-bin-screenshot.lock", File::RDWR | File::CREAT do |f| | |
begin | |
f.flock File::LOCK_EX | |
xvfb = Process.spawn "Xvfb", ":#{scr}", "-screen", "0", "#{SCREEN_WIDTH}x#{SCREEN_HEIGHT}x24" | |
firefox = Process.spawn({ "DISPLAY" => "localhost:#{scr}.0" }, "firefox", uri.to_s, "-width", SCREEN_WIDTH.to_s, "-height", SCREEN_HEIGHT.to_s, "-no-remote") | |
sleep 5 # TODO - maybe increase? | |
scr_uri = "screenshots/#{uri.host}-#{rand(100000..999999)}-#{Time.now.to_s.gsub(" ", "-")}.png" | |
filename = "/var/www/charlie.bz/public/#{scr_uri}" | |
Process.wait Process.spawn({ "DISPLAY" => "localhost:#{scr}.0", "PATH" => "/usr/bin" }, "import", "-window", "root", filename) | |
print "Location: http://charlie.bz/#{scr_uri}\r\n\r\n" | |
ensure | |
Process.kill 9, firefox if firefox | |
Process.kill 9, xvfb if xvfb | |
f.flock File::LOCK_UN | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment