Cacti / cacti

Cacti ™
http://www.cacti.net
GNU General Public License v2.0
1.64k stars 405 forks source link

Proxy settings\hook for plugins #2437

Open jpobeda opened 5 years ago

jpobeda commented 5 years ago

I've opened a Feature request for Mactrack and @cigamit mentioned that it would also be useful for other plugins.

I'd like an option to create proxy servers that could later on be used on different plugins such as WebSeer, Mactrack, etc. Some of these plugins need to reach URLs for one thing or another, either internally or at internet.

I couldn't find any practical alternative to replace the proxy's function.

Mactrack has an option to import or update MAC OUI Database from internet BUT, at least on my case, most of the servers allowed to go out to internet must use a proxy.

image

Let me know if you need further details.

netniV commented 5 years ago

It would depend on what is being used. For raw PHP we can use the following if all connections should be proxied:

stream_context_set_default([
    'http'=>[
        'proxy'=>'proxy-host:proxy-port', 
        'header'=>'Proxy-Authorization: Basic ' . 
           base64_encode('your-username:your-password')
    ]
]);

For curl:

function getUrl($url)
{
    $ch = curl_init(); 
    $timeout = 5; // set to zero for no timeout 
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); 
    curl_setopt ($ch, CURLOPT_URL, $url); 
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_PROXY, "http://proxy.example.com"); //your proxy url
    curl_setopt($ch, CURLOPT_PROXYPORT, "8080"); // your proxy port number 
    curl_setopt($ch, CURLOPT_PROXYUSERPWD, "username:pass"); //username:pass 
    $file_contents = curl_exec($ch); 
    curl_close($ch); 
    return $file_contents;
}

An example of the first option is showing in this gist (which i've copied below in case it gets removed): https://gist.github.com/ebuildy/381f116e9cd18216a69188ce0230708d

$proxy = getenv('http_proxy');
if (!empty($proxy)) {
    $proxy = str_replace('http://', 'tcp://', $proxy);
    echo "Found a proxy " . $proxy . PHP_EOL;
    $context = array(
        'http' => array(
            'proxy' => $proxy,
            'request_fulluri' => true,
            'verify_peer'      => false,
            'verify_peer_name' => false,
        ),
        "ssl"=>array(
        "verify_peer"=>false,
        "verify_peer_name"=>false
        )
    );
    stream_context_set_default($context);
} else {
    echo "Proxy not found" . PHP_EOL;
}

This would not cover everything though, since we also call scripts which could be using SSH/SCP/SFTP or some other protocol. Currently, the poller uses the stream_context when connecting to a remote poller but i don't think that has proxy settings yet.

JorisFRST commented 4 years ago

Hi, I already have been adding the curl proxy manually to some of the plugins on my cacti install. For instance mactrack for the oui update and flowview for the arin lookups.

Having this as a cacti global setting or a per plugin setting would be very helpfull.

netniV commented 4 years ago

We can look at introducing base settings, and then tackle those places that are missed via issue trackers later.

TheWitness commented 4 years ago

Yea, that's what I'm thinking. Need a user/password/proxy/port. Add it to the 'Mail/Reporting/DNS' page.

netniV commented 4 years ago

The system options have been added to 1.3, but no functionality has been added yet.