set :bind, "0.0.0.0" set :port, 5300 cdir = Dir.pwd //dir.pwd返回当前路劲 get '/'do str = "welcome to the automatic resource inliner, we inline all images" str << " go to /example.com to get an inlined version of example.com" str << " flag is in /flag" str << " source is in /source" str end
get '/source' do IO.read "/home/optiproxy/optiproxy.rb" end
get '/flag' do str = "I mean, /flag on the file system... If you're looking here, I question" str << " your skills" str end
get '/:url' do url = params[:url] main_dir = Dir.pwd //返回当前路径 temp_dir = "" dir = Dir.mktmpdir "inliner"//在tmp下创建一个inliner+随机数的文件夹 Dir.chdir dir //改变当前目录 temp_dir = dir //复制给temp_dir exec = "timeout 5 wget -T 2 --page-requisites #{Shellwords.shellescape url}"//shellwords.shellescape对url进行转义 `#{exec}` //执行命令行wget my_dir = Dir.glob ("**/") //匹配所有文件夹,以数组的方式返回 Dir.chdir my_dir[0]//改变当前目录为第一个(也就是wget回来的) index_file = "index.html" html_file = IO.read index_file //返回index.html的源码 doc = Nokogiri::HTML(open(index_file)) doc.xpath('//img').each do |img| //遍历index.html的所有img标签 header = img.xpath('preceding::h2[1]').text image = img['src'] img_data = "" uri_scheme = URI(image).scheme begin// try if (uri_scheme == "http"or uri_scheme == "https") //拼接url url = image else url = "http://#{url}/#{image}" end img_data = open(url).read b64d = "data:image/png;base64," + Base64.strict_encode64(img_data) img['src'] = b64d rescue //相当于catch # gotta catch 'em all puts "lole" next end end puts dir FileUtils.rm_rf dir //删掉所有内容。 Dir.chdir main_dir doc.to_html end