Skip to content

Commit

Permalink
https://github.com/WWBN/AVideo-Storage/issues/12
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielnetoDotCom committed Jul 3, 2020
1 parent 4aa70f3 commit 945890e
Showing 1 changed file with 49 additions and 9 deletions.
58 changes: 49 additions & 9 deletions objects/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 945890e

Please sign in to comment.