This commit is contained in:
Lanta 2025-04-15 17:12:33 +02:00
parent 1be9732279
commit 01aa843d0b
18 changed files with 4122 additions and 2393 deletions

View file

@ -1,52 +1,80 @@
function new-lock {
# Functions for creating and removing a simple lock file to prevent concurrent script execution.
function New-Lock {
[CmdletBinding()]
param (
[Parameter()]
[string]
$by
[string]$by # Optional identifier for who/what created the lock
)
Write-Output 'Setting lock'
$timeout = 15
$i = 0
Write-Verbose "Attempting to acquire lock..."
$timeoutSeconds = 15 * 15 # Increased timeout to ~4 minutes (15 attempts * 15 seconds)
$sleepSeconds = 15
$startTime = Get-Date
# Determine lock file path based on OS
if ($IsWindows) {
$lockFilePath = Join-Path -Path $env:TEMP -ChildPath 'pubg_stats.lock'
} elseif ($IsLinux -or $IsMacOS) {
$lockFilePath = '/tmp/pubg_stats.lock'
} else {
Write-Error "Unsupported operating system for lock file path determination."
throw "Unsupported OS for locking." # Throw to ensure script stops
}
Write-Verbose "Using lock file path: $lockFilePath"
while ($true) {
if ($env:temp) {
$lockFile = Join-Path -Path $env:temp -ChildPath 'lockfile_pubg.lock'
}
else {
$lockFile = "/tmp/lockfile_pubg.lock"
if (Test-Path -Path $lockFilePath -PathType Leaf) {
$lockContent = Get-Content -Path $lockFilePath -Raw -ErrorAction SilentlyContinue
Write-Warning ("Lock file '$lockFilePath' already exists (Content: '$lockContent'). Waiting $sleepSeconds seconds...")
Start-Sleep -Seconds $sleepSeconds
} else {
try {
# Attempt to create the lock file
$lockCreatorInfo = if ($by) { "Locked by '$by' at $(Get-Date)" } else { "Locked at $(Get-Date)" }
New-Item -ItemType File -Path $lockFilePath -Value $lockCreatorInfo -Force -ErrorAction Stop | Out-Null
Write-Output "Lock file created successfully at '$lockFilePath'."
return # Exit the loop and function on success
} catch {
# This catch might occur if another process creates the file between Test-Path and New-Item (race condition)
$errorMessage = $_.Exception.Message
Write-Warning "Failed to create lock file (possible race condition?): $errorMessage. Retrying..."
Start-Sleep -Seconds 1 # Short sleep before retry on creation failure
}
}
if (Test-Path -Path $lockFile) {
Write-Host "Job is already running. Lock file found at $lockFile. Sleeping 15 seconds."
Start-Sleep -Seconds 15
# Check for timeout
if (((Get-Date) - $startTime).TotalSeconds -ge $timeoutSeconds) {
Write-Error "Timed out after $timeoutSeconds seconds waiting for lock file '$lockFilePath'."
throw "Lock acquisition timed out." # Throw to ensure script stops
}
else {
try {
$content = if ($by) { $by } else { "" }
New-Item -ItemType File -Path $lockFile -Value $content -Force
Write-Host "Lock file created at $lockFile."
break
}
catch {
Write-Output "Unable to create lockfile, error: $_. Resuming lock loop."
}
}
if ($i -ge $timeout) {
Write-Output "Timed out after $timeout attempts."
exit
}
$i++
}
}
function remove-lock {
Write-Output 'Removing lock'
if ($env:temp) {
$lockFile = Join-Path -Path $env:temp -ChildPath 'lockfile_pubg.lock'
function Remove-Lock {
Write-Verbose "Attempting to remove lock..."
# Determine lock file path (consistent with New-Lock)
if ($IsWindows) {
$lockFilePath = Join-Path -Path $env:TEMP -ChildPath 'pubg_stats.lock'
} elseif ($IsLinux -or $IsMacOS) {
$lockFilePath = '/tmp/pubg_stats.lock'
} else {
Write-Warning "Unsupported operating system for lock file path determination. Cannot remove lock."
return
}
else {
$lockFile = "/tmp/lockfile_pubg.lock"
Write-Verbose "Target lock file path: $lockFilePath"
if (Test-Path -Path $lockFilePath -PathType Leaf) {
try {
Remove-Item -Path $lockFilePath -Force -ErrorAction Stop
Write-Output "Lock file '$lockFilePath' removed successfully."
} catch {
$errorMessage = $_.Exception.Message
Write-Error "Failed to remove lock file '$lockFilePath'. Manual removal might be required. Error: $errorMessage"
# Consider if this should be a fatal error depending on script requirements
}
} else {
Write-Warning "Lock file '$lockFilePath' not found. Assuming already removed."
}
Remove-Item -Path $lockFile
}

View file

@ -22,20 +22,46 @@ foreach ($_SESSION['access_times'] as $key => $timestamp) {
if (count($_SESSION['access_times']) > $allowed_refreshes) {
die('
<!DOCTYPE html>
<html lang="en">
// Set HTTP status code for rate limiting
http_response_code(429); // Too Many Requests
?>
<!DOCTYPE html>
<html lang="nl">
<head>
<meta charset="UTF-8">
<meta http-equiv="refresh" content="3">
<title>RustAGHH</title>
<meta http-equiv="refresh" content="5;url=<?php echo htmlspecialchars($_SERVER['REQUEST_URI']); ?>">
<title>Te Veel Verzoeken</title>
<style>
body { font-family: sans-serif; padding: 20px; background-color: #f8f8f8; color: #333; }
.container { max-width: 600px; margin: 50px auto; background-color: #fff; padding: 30px; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); text-align: center; }
h1 { color: #d9534f; }
p { line-height: 1.6; }
.timer { font-weight: bold; }
</style>
</head>
<body>
<p>RUSTAAAGGHHHH je mag de pagina niet vaker dan ' . $allowed_refreshes . 'x per ' . $allowed_time . ' seconde refreshen. Over een paar seconden wordt je weer teruggeleid.</p>
<div class="container">
<h1>Rustâââgh!</h1>
<p>Je probeert de pagina te vaak te vernieuwen (meer dan <?php echo $allowed_refreshes; ?> keer per <?php echo $allowed_time; ?> seconden).</p>
<p>Wacht even, je wordt over <span id="countdown" class="timer">5</span> seconden automatisch teruggestuurd.</p>
</div>
<script>
let seconds = 5;
const countdownElement = document.getElementById('countdown');
const interval = setInterval(() => {
seconds--;
countdownElement.textContent = seconds;
if (seconds <= 0) {
clearInterval(interval);
// Optional: You could redirect here as well, but meta refresh handles it
// window.location.href = "<?php echo htmlspecialchars($_SERVER['REQUEST_URI']); ?>";
}
}, 1000);
</script>
</body>
</html>
');
<?php
exit; // Use exit instead of die for clarity
}
?>