From 46e77541cfd88ad8ea4dfd46075cfd1bc015abdc Mon Sep 17 00:00:00 2001 From: Dominika Liberda Date: Tue, 1 Jun 2021 21:14:03 +0200 Subject: [PATCH] + new proxying system --- http.sh | 6 +++--- src/response/proxy.sh | 30 +++++++++++++++++++++++++++--- src/server.sh | 7 ++++--- 3 files changed, 34 insertions(+), 9 deletions(-) diff --git a/http.sh b/http.sh index ce26216..b60282f 100755 --- a/http.sh +++ b/http.sh @@ -36,9 +36,9 @@ cfg[python_enabled]=false # enable Python script evalutaion (requires Python) cfg[log]='log' # filename -# proxy functionality is very WiP -cfg[proxy]=false -cfg[proxy_url]='http://example.com/' +cfg[proxy]=false # you probably want to configure this per-url +cfg[proxy_url]='' # regexp matching valid URLs to proxy +cfg[proxy_param]='url' # /proxy?url=... # mail handler config cfg[mail]="" diff --git a/src/response/proxy.sh b/src/response/proxy.sh index 764237b..59cfb91 100755 --- a/src/response/proxy.sh +++ b/src/response/proxy.sh @@ -1,4 +1,28 @@ -printf "HTTP/1.0 200 OK -${cfg[extra_headers]}\r\n\r\n" +#!/bin/bash +url="$(url_decode "$(url_decode "$(sed -E 's/\?/��Lun4_iS_CuTe�/;s/^(.*)��Lun4_iS_CuTe�//;s/'"${cfg[proxy_param]}"'=//g' <<< "${r[url]}")")")" -curl ${cfg[proxy_url]}${r[url]} \ No newline at end of file +if [[ $(grep -Poh "${cfg[proxy_url]}" <<< "$url") == '' ]]; then + exit 1 +fi + +host="$(sed -E 's@http(s|)://@@;s@/.*@@' <<< "$url")" +headers="$(tr '\r' '\n' <<< "${r[req_headers]}")" +headers+=$'\n' + +while read line; do + if [[ "$line" == "GET"* ]]; then + if [[ "$url" == *"$host" ]]; then + echo "GET / HTTP/1.1" + else + echo "GET /$(sed -E 's@http(s|)://@@;s@/@��Lun4_iS_CuTe�@;s@.*��Lun4_iS_CuTe�@@' <<< "$url") HTTP/1.1" + fi + elif [[ "$line" == *"Host"* ]]; then + echo "Host: $url" | sed -E 's@http(s|)://@@;s@/.*@@' + else + echo "$line" + fi +done <<< "$headers" | if [[ "$url" == "https"* ]]; then + nc $host 443 --ssl -C -i 0.1 --no-shutdown +else + nc $host 80 -C -i 0.1 --no-shutdown +fi diff --git a/src/server.sh b/src/server.sh index ef0903d..190bcb7 100755 --- a/src/server.sh +++ b/src/server.sh @@ -16,9 +16,11 @@ declare -A post_data # all POST params declare -A params # parsed router data r[status]=210 # Mommy always said that I was special +r[req_headers]='' post_length=0 while read -r param; do + r[req_headers]+="$param" param_l="${param,,}" # lowercase name='' value='' @@ -160,6 +162,8 @@ fi echo "${r[url]}" >&2 +# the app config gets loaded a second time to allow for path-specific config modification +[[ -f "${cfg[namespace]}/config.sh" ]] && source "${cfg[namespace]}/config.sh" if [[ "${cfg[auth_required]}" == true && "${r[authorized]}" != true ]]; then echo "Auth failed." >> ${cfg[log_misc]} @@ -213,9 +217,6 @@ if [[ "${r[post]}" == true && "${r[status]}" == 200 ]]; then fi fi -# the app config gets loaded a second time to allow for path-specific config modification -[[ -f "${cfg[namespace]}/config.sh" ]] && source "${cfg[namespace]}/config.sh" - if [[ ${r[status]} == 210 && ${cfg[autoindex]} == true ]]; then source "src/response/listing.sh" elif [[ ${r[status]} == 211 ]]; then