Merge pull request #1690 from unraid/proxy-via-localprepend

Proxy via local_prepend.php
This commit is contained in:
tom mortensen
2024-04-03 15:43:52 -07:00
committed by GitHub
2 changed files with 24 additions and 9 deletions

View File

@@ -39,4 +39,8 @@ if ($_SERVER['SCRIPT_NAME'] != '/login.php' && $_SERVER['SCRIPT_NAME'] != '/auth
if ($var['csrf_token'] != $_POST['csrf_token']) csrf_terminate("wrong");
unset($_POST['csrf_token']);
}
$proxy_cfg = (array)@parse_ini_file('/var/local/emhttp/proxy.ini',true);
putenv('http_proxy='.((array_key_exists('http_proxy', $proxy_cfg)) ? $proxy_cfg['http_proxy'] : ''));
putenv('https_proxy='.((array_key_exists('https_proxy', $proxy_cfg)) ? $proxy_cfg['https_proxy'] : ''));
putenv('no_proxy='.((array_key_exists('http_proxy', $proxy_cfg)) ? $proxy_cfg['no_proxy'] : ''));
?>

View File

@@ -11,6 +11,7 @@ CFG=/boot/config/proxy.cfg
# these files are generated by this script based on the data in proxy.cfg
PROXY_SH=/etc/profile.d/proxy.sh
PROXY_INI=/usr/local/emhttp/state/proxy.ini
# random file extension for atomic writes
RND=$RANDOM
@@ -53,19 +54,29 @@ EOF
mv "${FILE}.${RND}" "${FILE}"
}
# if running, restart_phpfpm when the environment variables change
restart_phpfpm () {
if /etc/rc.d/rc.php-fpm status > /dev/null; then
/etc/rc.d/rc.php-fpm restart > /dev/null
fi
set_proxy_ini() {
local FILE
FILE="${PROXY_INI}"
cat <<EOF >"${FILE}.${RND}"
http_proxy="${proxy_url}"
https_proxy="${proxy_url}"
no_proxy="${no_proxy}"
EOF
chmod 644 "${FILE}.${RND}"
mv "${FILE}.${RND}" "${FILE}"
}
# clear /usr/local/emhttp/state/proxy.ini
unset_proxy_ini() {
# vars are empty now, call set_proxy_ini to add empty vars to proxy.ini
set_proxy_ini
}
# generate proxy files
add_proxy_to_generated_files_and_exit() {
echo "generating proxy files"
set_proxy_sh
. "${PROXY_SH}"
restart_phpfpm
set_proxy_ini
[[ -n "${VERBOSE}" ]] && display_generated_files
exit 0
}
@@ -74,8 +85,7 @@ add_proxy_to_generated_files_and_exit() {
remove_proxy_from_generated_files_and_exit() {
echo "removing proxy info from generated files"
unset_proxy_sh
. "${PROXY_SH}"
restart_phpfpm
unset_proxy_ini
[[ -n "${VERBOSE}" ]] && display_generated_files
exit 0
}
@@ -84,6 +94,7 @@ remove_proxy_from_generated_files_and_exit() {
display_generated_files() {
echo
display_generated_file "${PROXY_SH}"
display_generated_file "${PROXY_INI}"
}
# when verbose mode enabled