From 945890ee14a5680a23bc1c9fe58095174ad89d0c Mon Sep 17 00:00:00 2001 From: DanielnetoDotCom Date: Fri, 3 Jul 2020 11:18:09 -0300 Subject: [PATCH] https://github.com/WWBN/AVideo-Storage/issues/12 --- objects/functions.php | 58 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 49 insertions(+), 9 deletions(-) diff --git a/objects/functions.php b/objects/functions.php index 173daec09007..407fe9cf44e5 100644 --- a/objects/functions.php +++ b/objects/functions.php @@ -2288,6 +2288,9 @@ function ddosProtection() { //progressive timeout-> more requests, longer timeout $active_connections = count($_SESSION['bruteForceBlock']); $timeoutReal = ($active_connections / $maxCon) < 1 ? 0 : ($active_connections / $maxCon) * $secondTimeout; + if($timeoutReal){ + _error_log("ddosProtection:: progressive timeout timeoutReal = ($timeoutReal) active_connections = ($active_connections) maxCon = ($maxCon) ", AVideoLog::$SECURITY); + } sleep($timeoutReal); //with strict mode, penalize "attacker" with sleep() above, log and then die @@ -2906,6 +2909,7 @@ function getUsageFromFilename($filename, $dir = "") { _error_log("getUsageFromFilename: start {$dir}{$filename}"); $files = glob("{$dir}{$filename}*"); session_write_close(); + $filesProcessed = array(); foreach ($files as $f) { if (is_dir($f)) { _error_log("getUsageFromFilename: {$f} is Dir"); @@ -2940,11 +2944,19 @@ function getUsageFromFilename($filename, $dir = "") { } if (!empty($urls['mp4'])) { foreach ($urls['mp4'] as $mp4) { + if(in_array($mp4, $filesProcessed)){ + continue; + } + $filesProcessed[] = $mp4; $filesize += getUsageFromURL($mp4); } } if (!empty($urls['webm'])) { foreach ($urls['webm'] as $mp4) { + if(in_array($mp4, $filesProcessed)){ + continue; + } + $filesProcessed[] = $mp4; $filesize += getUsageFromURL($mp4); } } @@ -3314,6 +3326,15 @@ function wget($url, $filename) { return false; } wgetLock($url); + if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { + $content = file_get_contents($url); + if(file_put_contents($filename, $content) > 100){ + wgetRemoveLock($url); + return true; + } + wgetRemoveLock($url); + return false; + } $cmd = "wget {$url} -O {$filename} --no-check-certificate"; //_error_log("wget Start ({$cmd}) "); //echo $cmd; @@ -3375,17 +3396,36 @@ function isWritable($dir) { return $result; } +function _isWritable($dir){ + if (!isWritable($dir)) { + return false; + } + $tmpFile = "{$dir}". uniqid(); + $bytes = file_put_contents($tmpFile, time()); + @unlink($tmpFile); + return !empty($bytes); +} + function getTmpDir($subdir = "") { global $global; - $tmpDir = sys_get_temp_dir(); - if (!isWritable($tmpDir)) { - $tmpDir = "{$global['systemRootPath']}videos/cache/"; - } - $tmpDir = rtrim($tmpDir, '/') . '/'; - $tmpDir = "{$tmpDir}{$subdir}"; - $tmpDir = rtrim($tmpDir, '/') . '/'; - if (!is_dir($tmpDir)) { - mkdir($tmpDir, 0755, true); + if(empty($_SESSION['getTmpDir'])){ + $_SESSION['getTmpDir'] = array(); + } + if(empty($_SESSION['getTmpDir'][$subdir."_"])){ + $tmpDir = sys_get_temp_dir(); + if (!_isWritable($tmpDir)) { + $tmpDir = "{$global['systemRootPath']}videos/cache/"; + } + $tmpDir = rtrim($tmpDir, '/') . '/'; + $tmpDir = "{$tmpDir}{$subdir}"; + $tmpDir = rtrim($tmpDir, '/') . '/'; + if (!is_dir($tmpDir)) { + mkdir($tmpDir, 0755, true); + } + _session_start(); + $_SESSION['getTmpDir'][$subdir."_"] = $tmpDir; + }else{ + $tmpDir = $_SESSION['getTmpDir'][$subdir."_"]; } return $tmpDir; }