Created
October 7, 2026 22:20
-
-
Save JVT038/f03c94414729f73ca277d0ed17af3e32 to your computer and use it in GitHub Desktop.
Nextcloud configuration to automatically get the latest Bunny CDN IP addresses and add them to the trusted_proxies config value. It even supports caching for 1 week!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| # Make sure to put this file in the same place as the regular config.php. | |
| # The trusted_proxies value here will override the value in config.php | |
| # Change $trustedProxiesFilepath to wherever you want to temporarily store/cache the IP addresses from the Bunny server. | |
| $trustedProxiesFilepath = "/config/www/nextcloud/config/trusted_proxies.json"; | |
| function download_page($path){ | |
| $ch = curl_init(); | |
| curl_setopt($ch, CURLOPT_URL,$path); | |
| curl_setopt($ch, CURLOPT_FAILONERROR,1); | |
| curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1); | |
| curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); | |
| curl_setopt($ch, CURLOPT_TIMEOUT, 15); | |
| $retValue = curl_exec($ch); | |
| return $retValue; | |
| } | |
| function getBunnyIPs() : array { | |
| $edgeServerListV4 = (string) str_replace("\"", "", download_page("https://api.bunny.net/system/edgeserverlist")); | |
| $edgeServerListV6 = (string) str_replace("\"", "", download_page("https://api.bunny.net/system/edgeserverlist/ipv6")); | |
| $ipv4List = explode(",", substr($edgeServerListV4, 1, strlen($edgeServerListV4) - 2)); | |
| $ipv6List = explode(",", substr($edgeServerListV6, 1, strlen($edgeServerListV6) - 2)); | |
| return array_merge($ipv4List, $ipv6List); | |
| } | |
| function getCachedIps(string $trustedProxiesFilepath) : array { | |
| if (!file_exists($trustedProxiesFilepath)) { | |
| return []; | |
| } | |
| $fileContents = file_get_contents($trustedProxiesFilepath); | |
| if (empty($fileContents)) { | |
| return []; | |
| } | |
| $jsonContents = json_decode($fileContents, true); | |
| if (array_key_exists("lastUpdated", $jsonContents) && array_key_exists("ipAddresses", $jsonContents)) { | |
| return $jsonContents; | |
| } | |
| return []; | |
| } | |
| function updateCache(string $trustedProxiesFilepath) : array { | |
| $bunnyIps = getBunnyIPs(); | |
| $jsonContents = ["lastUpdated" => strtotime("now"), "ipAddresses" => $bunnyIps]; | |
| file_put_contents($trustedProxiesFilepath, json_encode($jsonContents, JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_LINE_TERMINATORS|JSON_UNESCAPED_UNICODE)); | |
| return $bunnyIps; | |
| } | |
| function getTrustedProxies(string $trustedProxiesFilepath) : array { | |
| $default = ['192.168.80.0/24', "127.0.0.1"]; | |
| $cachedIps = getCachedIps($trustedProxiesFilepath); | |
| if (empty($cachedIps)) { | |
| $newBunnyIps = updateCache($trustedProxiesFilepath); | |
| return array_merge($default, $newBunnyIps); | |
| } | |
| $lastUpdated = new DateTime(); | |
| $lastUpdated->setTimestamp($cachedIps["lastUpdated"]); | |
| $now = new DateTime(); | |
| $difference = $lastUpdated->diff($now)->days; | |
| if ($difference > 7) { | |
| $newBunnyIps = updateCache($trustedProxiesFilepath); | |
| return array_merge($default, $newBunnyIps); | |
| } | |
| return array_merge($default, $cachedIps["ipAddresses"]); | |
| } | |
| $CONFIG = [ | |
| 'trusted_proxies' => getTrustedProxies($trustedProxiesFilepath), | |
| ]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment