ruby-rewrite-proxy/proxy.rb

52 lines
1.6 KiB
Ruby

#!/usr/bin/env ruby
require "webrick"
require "net/http"
require "uri"
require "json"
# Load configuration from file
CONFIG = JSON.parse(File.read(File.join(__dir__, 'config.json')))
$replacements = CONFIG['replacements']
class MyProxy < WEBrick::HTTPServlet::AbstractServlet
HOST = CONFIG['host'] # set host to proxy duh
def do_GET(request, response)
uri = request.unparsed_uri
proxy_request(request, response, Net::HTTP::Get.new(uri, {"User-Agent" => "amongus happy meal guys this is crazy i ordered an amogus happy meal at 3am hugy wugy came and sucked my dick this is so scary"}))
end
def do_POST(request, response)
uri = request.unparsed_uri
proxy_request(request, response, Net::HTTP::Post.new(uri, {"User-Agent" => "amongus happy meal guys this is crazy i ordered an amogus happy meal at 3am hugy wugy came and sucked my dick this is so scary"}), request.body)
end
def proxy_request(request, response, http_request, body = nil)
http = Net::HTTP.new(HOST, 443)
http.use_ssl = true
http_request.body = body if body
resp = http.request(http_request)
body = resp.body
response.content_type = resp["content-type"]
$replacements.each { |k,v|
body = body.gsub(k,v)
}
response.body = body
end
end
# Redirect stdout and stderr to a log file
$stdout.reopen(File.join(__dir__, 'ruby-proxy.log'), 'a')
$stderr.reopen($stdout)
server = WEBrick::HTTPServer.new(:Port => CONFIG['port'] || 8000) # define port on which the proxy runs ... so this would be localhost:8000
server.mount "/", MyProxy
trap("INT"){ server.shutdown }
server.start