ruby-rewrite-proxy/proxy.rb

52 lines
1.6 KiB
Ruby
Raw Normal View History

2023-07-05 20:42:05 +00:00
#!/usr/bin/env ruby
require "webrick"
require "net/http"
require "uri"
2023-07-07 08:51:57 +00:00
require "json"
2023-07-05 20:42:05 +00:00
# Load configuration from file
2023-07-07 08:51:57 +00:00
CONFIG = JSON.parse(File.read(File.join(__dir__, 'config.json')))
2023-07-05 20:42:05 +00:00
2023-07-07 08:51:57 +00:00
$replacements = CONFIG['replacements']
2023-07-05 20:42:05 +00:00
class MyProxy < WEBrick::HTTPServlet::AbstractServlet
2023-07-07 08:51:57 +00:00
HOST = CONFIG['host'] # set host to proxy duh
2023-07-05 20:42:05 +00:00
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)
2023-07-07 08:51:57 +00:00
server = WEBrick::HTTPServer.new(:Port => CONFIG['port'] || 8000) # define port on which the proxy runs ... so this would be localhost:8000
2023-07-05 20:42:05 +00:00
server.mount "/", MyProxy
trap("INT"){ server.shutdown }
server.start