Add password encryption and create_proxy.sh script to create the proxy.cfg file needed by set_proxy.

This commit is contained in:
dlandon
2024-04-04 13:05:36 -05:00
parent 3790217cec
commit a1d486f112
4 changed files with 183 additions and 53 deletions

View File

@@ -12,7 +12,7 @@
$opmPlugin = "dynamix";
/* UI config file location. */
$plg_config_file = "/boot/config/proxy.cfg";
$plg_config_file = "/boot/config/plugins/".$opmPlugin."/proxy.cfg";
/* Output config file location for set_proxy script. */
$proxy_config_file = "/boot/config/proxy.cfg";
@@ -39,6 +39,21 @@ function parse_plugin_config() {
return($cfg);
}
/* Write values to plugin config file. */
function write_plugin_config($config) {
global $plg_config_file;
/* Rewrite config file. */
/* Convert the array to an INI string. */
$iniString = '';
foreach ($config as $key => $value) {
$iniString .= "$key=\"$value\"\n";
}
/* Write the INI string to a file. */
file_put_contents($plg_config_file, $iniString);
}
/* Check to see if the proxy is online and available. */
function proxy_online($proxyUrl) {
@@ -70,15 +85,25 @@ function proxy_online($proxyUrl) {
}
/* Get the URL with the user and password parsed from the url. */
function get_url($cfg_url) {
function get_proxy_info($cfg_url, $cfg_user = "", $cfg_pass = "") {
/* Passed in values:
cfg_url - can be with or without credentials (user and password).
cfg_user - user from config file.
cfg_pass - encrypted password from the config file.
*/
/* An array is returned with the following values. */
$return = [
'url' => '',
'user' => '',
'pass' => '',
'url' => '', /* URL without credentials. */
'user' => '', /* User. */
'pass' => '', /* Unencrypted password. */
'full_url' => '', /* Full URL with credentials urlencoded. */
];
if ($cfg_url) {
/* Decrypt password. */
$cfg_pass = decrypt_data($cfg_pass);
/* Parse the URL by removing the user and password. */
$urlComponents = parse_url($cfg_url);
@@ -94,15 +119,76 @@ function get_url($cfg_url) {
/* Extract the credentials. */
if (strpos($cfg_url, '%') !== false) {
/* The credentials are urlencoded. */
$return['user'] = urldecode($user);
$return['pass'] = urldecode($pass);
$return['user'] = $user ? urldecode($user) : $cfg_user;
$return['pass'] = $pass ? urldecode($pass) : $cfg_pass;
} else {
/* The credentials are not urlencoded. */
$return['user'] = $user;
$return['pass'] = $pass;
$return['user'] = $user ? $user : $cfg_user;
$return['pass'] = $pass ? $pass : $cfg_pass;
}
/* Put together the full url. */
if (($return['user']) && ($return['pass'])) {
$return['full_url'] = "http://".urlencode($return['user']).":".urlencode($return['pass'])."@".$host.":".$port;
} else {
$return['full_url'] = $return['url'];
}
}
return($return);
}
/* Get configuration parameter. */
function get_config($variable) {
$config = parse_plugin_config();
return $config[$variable] ?? "";
}
/* Set configuration parameter. */
function set_config($variable, $value) {
$config = parse_plugin_config();
$config[$variable] = $value;
write_plugin_config($config);
}
/* Encrypt data. */
function encrypt_data($data) {
$key = get_config("key");
if ((! $key) || strlen($key) != 32) {
$key = substr(base64_encode(openssl_random_pseudo_bytes(32)), 0, 32);
set_config("key", $key);
}
$iv = get_config("iv");
if ((! $iv) || strlen($iv) != 16) {
$iv = substr(base64_encode(openssl_random_pseudo_bytes(16)), 0, 16);
set_config("iv", $iv);
}
/* Encrypt the data using aes256. */
$value = trim(openssl_encrypt($data, 'aes256', $key, $options=0, $iv));
return $value;
}
/* Decrypt data. */
function decrypt_data($data) {
$key = get_config("key");
$iv = get_config("iv");
/* Decrypt the data using aes256. */
$value = openssl_decrypt($data, 'aes256', $key, $options=0, $iv);
/* Make sure the data is UTF-8 encoded. */
if (! mb_check_encoding($value, 'UTF-8')) {
outgoingproxy_log("Warning: Data is not UTF-8 encoded");
$value = "";
}
return $value;
}
?>