commit fcf5e9ac65f31c136d48ecde262528ce398699c7 Author: Roxy Date: Wed Jul 5 22:42:05 2023 +0200 silly diff --git a/README.md b/README.md new file mode 100644 index 0000000..277f269 --- /dev/null +++ b/README.md @@ -0,0 +1,39 @@ +# ruby-rewrite-proxy + +This project sets up a simple HTTP proxy using Ruby and Nginx. It's designed to forward requests to a specified host, with the option to replace certain strings in the response. + +## Installation + +To install the necessary dependencies and set up the proxy, run the following command: + +```bash +bash installer.sh +``` + +This will: + + Install Ruby and the 'webrick' gem. + Install Nginx and enable it to start on boot. + Create a directory in the current user's home directory called 'ruby-proxy'. + Clone this repository into the 'ruby-proxy' directory. + Prompt you to enter values for 'server_name' and 'proxy_set_header'. + Create an Nginx configuration file using the provided template and your input. + Enable the new Nginx site. + Reload Nginx to apply the changes. + +After running the script, don't forget to adjust config.yml to your likings. + +## Configuration +The proxy.rb script reads its configuration from config.yml. This file specifies the host to proxy requests to, the port to listen on, and a set of string replacements to apply to the response. + +## Logging +The proxy.rb script logs its output to ruby-proxy.log in the same directory. + +## Usage +To start the proxy, run the following command: + +```bash +ruby proxy.rb +``` + +This will start the proxy, listening on the port specified in config.yml. diff --git a/config.yml b/config.yml new file mode 100644 index 0000000..8d5db9d --- /dev/null +++ b/config.yml @@ -0,0 +1,7 @@ +replacements: + example.com: example.mydomain.com + ">example<": "example @ mydomain<" + https:: http: +host: example.com +port: 8000 + diff --git a/installer.sh b/installer.sh new file mode 100644 index 0000000..fe90b7d --- /dev/null +++ b/installer.sh @@ -0,0 +1,88 @@ +#!/bin/bash + +set -e + +# Install Ruby and the Ruby dependency 'webrick' +if ! command -v ruby &> /dev/null; then + echo "Installing Ruby..." + sudo apt-get update + sudo apt-get install -y ruby-full +fi + +if ! gem list webrick -i > /dev/null; then + echo "Installing webrick..." + gem install webrick +fi + +# Install Nginx and enable the service +if ! command -v nginx &> /dev/null; then + echo "Installing Nginx..." + sudo apt-get install -y nginx +fi + +echo "Enabling Nginx service..." +sudo systemctl enable nginx +sudo systemctl start nginx + +# Make a directory in the current user's home folder called 'ruby-proxy' +if [ ! -d ~/ruby-proxy ]; then + echo "Creating directory 'ruby-proxy'..." + mkdir ~/ruby-proxy +fi + +# Clone the contents of the specified git repository into that folder +if [ ! -d ~/ruby-proxy/ruby-rewrite-proxy ]; then + echo "Cloning repository..." + git clone https://adastra7.net/git/crt/ruby-rewrite-proxy.git ~/ruby-proxy +fi + +# Ask the user to set server_name and proxy_set_header +read -p "Enter server_name (the URL from which you will access the Website): " server_name +read -p "Enter proxy_set_header (the host header for the website you wanna modify/proxy): " proxy_set_header + +# Create the Nginx config file +if [ ! -f /etc/nginx/sites-available/ruby-proxy ]; then + echo "Creating Nginx configuration file..." + cat << EOF > /etc/nginx/sites-available/ruby-proxy +resolver 8.8.8.8; + +server { + listen 80; + server_name $server_name; + + location / { + proxy_pass http://localhost:8000/; + proxy_set_header Host $proxy_set_header; + proxy_set_header X-Real-IP \$remote_addr; + proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for; + proxy_ssl_server_name on; + proxy_ssl_verify off; + } + + location /favicon.ico { + return 204; + access_log off; + log_not_found off; + } + + location /robots.txt { + return 200 "User-agent: *\nDisallow: /"; + access_log off; + log_not_found off; + } +} +EOF +fi + +# Enable the site +if [ ! -f /etc/nginx/sites-enabled/ruby-proxy ]; then + echo "Enabling the site..." + sudo ln -s /etc/nginx/sites-available/ruby-proxy /etc/nginx/sites-enabled/ +fi + +# Reload Nginx to apply the changes +echo "Reloading Nginx..." +sudo systemctl reload nginx + +echo "The script has completed. Please edit the config.yml in ~/ruby-proxy/config.yml to your liking now before running proxy.rb to start the proxy" + diff --git a/proxy.rb b/proxy.rb new file mode 100644 index 0000000..6be2cc5 --- /dev/null +++ b/proxy.rb @@ -0,0 +1,51 @@ +#!/usr/bin/env ruby + +require "webrick" +require "net/http" +require "uri" +require "yaml" + +# Load configuration from file +config = YAML.load_file(File.join(__dir__, 'config.yml')) + +$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 +