mirror of
https://github.com/unraid/webgui.git
synced 2026-01-28 20:49:04 -06:00
286 lines
7.1 KiB
PHP
Executable File
286 lines
7.1 KiB
PHP
Executable File
#!/usr/bin/php
|
|
<?php
|
|
/**
|
|
* script: set_proxy
|
|
*
|
|
* Copyright 2005-2024, Lime Technology
|
|
*
|
|
* Call this script (/usr/local/sbin/set_proxy.php) to set up proxy files.
|
|
*/
|
|
|
|
$opmPlugin = "dynamix";
|
|
require_once("plugins/".$opmPlugin."/include/OutgoingProxyLib.php");
|
|
|
|
/* Files 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 = rand();
|
|
|
|
/* Comments to beginning of the proxy_sh file. */
|
|
$comments = "#!/bin/bash\n"."# Do not edit. This file is autogenerated by /usr/local/sbin/set_proxy.\n";
|
|
|
|
/* Set verbose if command line switch is set. */
|
|
$verbose = false;
|
|
if (isset($argv[1]) && ($argv[1] == '-v')) {
|
|
$verbose = true;
|
|
}
|
|
|
|
/* Initialize global variables. */
|
|
$proxy_active = "";
|
|
$proxy_url = "";
|
|
$no_proxy = "";
|
|
|
|
/* Write comments and content to a file. */
|
|
function write_file($file, $content, $show_comments = false) {
|
|
global $rnd, $comments;
|
|
|
|
$tmpFile = "{$file}.{$rnd}";
|
|
file_put_contents($tmpFile, ($show_comments ? $comments : "").$content);
|
|
chmod($tmpFile, 0755);
|
|
rename($tmpFile, $file);
|
|
}
|
|
|
|
/* Write environment variables to /etc/profile.d/proxy.sh. */
|
|
function set_proxy_sh() {
|
|
global $proxy_sh, $proxy_url, $no_proxy;
|
|
|
|
$content = "export http_proxy=".$proxy_url."\n";
|
|
$content .= "export https_proxy=".$proxy_url."\n";
|
|
$content .= "export no_proxy=".$no_proxy."\n";
|
|
|
|
write_file($proxy_sh, $content, true);
|
|
}
|
|
|
|
/* Clear environment variables from /etc/profile.d/proxy.sh. */
|
|
function unset_proxy_sh() {
|
|
global $proxy_sh;
|
|
|
|
$content = "unset http_proxy\n";
|
|
$content .= "unset https_proxy\n";
|
|
$content .= "unset no_proxy\n";
|
|
|
|
write_file($proxy_sh, $content, true);
|
|
}
|
|
|
|
/* Write proxy information to /usr/local/emhttp/state/proxy.ini. */
|
|
function set_proxy_ini() {
|
|
global $proxy_ini, $proxy_url, $no_proxy;
|
|
|
|
$content = "http_proxy=".$proxy_url."\n";
|
|
$content .= "https_proxy=".$proxy_url."\n";
|
|
$content .= "no_proxy=".$no_proxy."\n";
|
|
|
|
write_file($proxy_ini, $content);
|
|
}
|
|
|
|
/* Clear /usr/local/emhttp/state/proxy.ini. */
|
|
function unset_proxy_ini() {
|
|
/* Vars are empty now, call set_proxy_ini to add empty vars to proxy.ini. */
|
|
set_proxy_ini();
|
|
}
|
|
|
|
/* Generate proxy files. */
|
|
function add_proxy_to_generated_files() {
|
|
global $verbose;
|
|
|
|
echo("generating proxy files\n");
|
|
set_proxy_sh();
|
|
set_proxy_ini();
|
|
if ($verbose) {
|
|
display_generated_files();
|
|
}
|
|
}
|
|
|
|
/* Remove proxy info from all generated files. */
|
|
function remove_proxy_from_generated_files() {
|
|
global $verbose;
|
|
|
|
echo("removing proxy info from generated files\n");
|
|
unset_proxy_sh();
|
|
unset_proxy_ini();
|
|
if ($verbose) {
|
|
display_generated_files();
|
|
}
|
|
}
|
|
|
|
/* When verbose mode is enabled. */
|
|
function display_generated_files() {
|
|
global $proxy_sh, $proxy_ini;
|
|
|
|
echo("\n");
|
|
display_generated_file($proxy_sh);
|
|
echo("\n");
|
|
display_generated_file($proxy_ini);
|
|
}
|
|
|
|
/* When verbose mode is enabled. */
|
|
function display_generated_file($file) {
|
|
echo("file: ".$file."\n");
|
|
if (file_exists($file)) {
|
|
echo(file_get_contents($file));
|
|
} else {
|
|
echo("file does not exist\n");
|
|
}
|
|
}
|
|
|
|
/* Read current proxy information from Outgoing Proxy Manager config file. */
|
|
$config = parse_plugin_config();
|
|
|
|
/* Get the current active proxy. */
|
|
$proxy_active = $config['proxy_active'];
|
|
$proxy_url = "";
|
|
|
|
/* Only build full url for an active proxy. */
|
|
if ($proxy_active != "0") {
|
|
/* Get the active proxy url, user, and pass. */
|
|
$proxy_url = "proxy_url_".$proxy_active;
|
|
$proxy_user = "proxy_user_".$proxy_active;
|
|
$proxy_pass = "proxy_pass_".$proxy_active;
|
|
|
|
/* Get the full url for the active proxy. */
|
|
$url_array = get_proxy_info($config[$proxy_url] ?? "", $config[$proxy_user] ?? "", $config[$proxy_pass] ?? "");
|
|
$proxy_url = $proxy_active ? $url_array['full_url'] : "";
|
|
|
|
/* If the active proxy url is defined, create the generated files. */
|
|
if ($proxy_url) {
|
|
/* Proxies are defined, write generated files. */
|
|
$no_proxy = "127.0.0.1,localhost";
|
|
add_proxy_to_generated_files();
|
|
}
|
|
} else {
|
|
/* If no active proxies, remove proxy info from all generated files. */
|
|
remove_proxy_from_generated_files();
|
|
}
|
|
?>
|
|
=======
|
|
#!/bin/bash
|
|
#
|
|
# script: set_proxy
|
|
#
|
|
# Copyright 2005-2024, Lime Technology
|
|
#
|
|
# call this script (/usr/local/sbin/set_proxy) when /boot/config/proxy.cfg changes
|
|
|
|
# proxy.cfg is the source of all proxy information
|
|
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
|
|
|
|
VERBOSE=
|
|
[[ "$1" == "-v" ]] && VERBOSE=1
|
|
|
|
# global vars defined later
|
|
proxy_active=
|
|
proxy_url=
|
|
no_proxy=
|
|
|
|
# write environment variables to /etc/profile.d/proxy.sh
|
|
set_proxy_sh() {
|
|
local FILE
|
|
FILE="${PROXY_SH}"
|
|
cat <<EOF >"${FILE}.${RND}"
|
|
#!/bin/bash
|
|
# Do not edit. This file is autogenerated by /usr/local/sbin/set_proxy
|
|
export http_proxy="${proxy_url}"
|
|
export https_proxy="${proxy_url}"
|
|
export no_proxy="${no_proxy}"
|
|
EOF
|
|
chmod 755 "${FILE}.${RND}"
|
|
mv "${FILE}.${RND}" "${FILE}"
|
|
}
|
|
|
|
# clear environment variables from /etc/profile.d/proxy.sh
|
|
unset_proxy_sh() {
|
|
local FILE
|
|
FILE="${PROXY_SH}"
|
|
cat <<EOF >"${FILE}.${RND}"
|
|
#!/bin/bash
|
|
# Do not edit. This file is autogenerated by /usr/local/sbin/set_proxy
|
|
unset http_proxy
|
|
unset https_proxy
|
|
unset no_proxy
|
|
EOF
|
|
chmod 755 "${FILE}.${RND}"
|
|
mv "${FILE}.${RND}" "${FILE}"
|
|
}
|
|
|
|
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
|
|
set_proxy_ini
|
|
[[ -n "${VERBOSE}" ]] && display_generated_files
|
|
exit 0
|
|
}
|
|
|
|
# remove proxy info from all generated files and exit
|
|
remove_proxy_from_generated_files_and_exit() {
|
|
echo "removing proxy info from generated files"
|
|
unset_proxy_sh
|
|
unset_proxy_ini
|
|
[[ -n "${VERBOSE}" ]] && display_generated_files
|
|
exit 0
|
|
}
|
|
|
|
# when verbose mode enabled
|
|
display_generated_files() {
|
|
echo
|
|
display_generated_file "${PROXY_SH}"
|
|
display_generated_file "${PROXY_INI}"
|
|
}
|
|
|
|
# when verbose mode enabled
|
|
display_generated_file() {
|
|
local FILE
|
|
FILE=$1
|
|
echo "${FILE}"
|
|
[[ -f "${FILE}" ]] && cat "${FILE}" || echo "file does not exist"
|
|
echo
|
|
}
|
|
|
|
# if no proxy config, remove proxy info from all generated files and exit
|
|
[[ ! -f "${CFG}" ]] && remove_proxy_from_generated_files_and_exit
|
|
|
|
# read current proxy information from /boot/config/proxy.cfg
|
|
# shellcheck source=/dev/null
|
|
. <(/usr/bin/fromdos <"${CFG}")
|
|
|
|
# determine proxy information
|
|
proxy_url_var="proxy_url_${proxy_active:=}"
|
|
proxy_url="${!proxy_url_var}"
|
|
|
|
# if no active proxies, remove proxy info from all generated files and exit
|
|
if [[ "${proxy_active:=0}" == "0" || "${proxy_url}" == "" ]]; then
|
|
remove_proxy_from_generated_files_and_exit
|
|
fi
|
|
|
|
# proxies are defined, write generated files
|
|
no_proxy="127.0.0.1,localhost"
|
|
add_proxy_to_generated_files_and_exit
|
|
|