<?php 
header("Content-Type: application/json; charset=UTF-8"); 
define('CACHE_FILE', 'cache.json'); // فایل کش
define('INDEX_FILE', 'index.txt'); // فایل شمارنده
define('CACHE_TIME', 136); // زمان کش به ثانیه

function decrypt($encrypted_config) { 
    $decoded = base64_decode($encrypted_config); 
    $decrypted = openssl_decrypt($decoded,'AES-256-CBC',"c2fa1ed96a8d762e2b594b3b0475a2bb",OPENSSL_RAW_DATA,"126a04ada77963fb"); 
    return $decrypted; 
} 

function swap($i10, $i11, $str) { 
  $charAtI10 = $str[$i10]; 
  $str[$i10] = $str[$i11]; 
  $str[$i11] = $charAtI10; 
  return $str; 
} 

// تابع برای گرفتن داده‌ها از API و ذخیره در کش
function fetchDataFromAPI() {
    $curl = curl_init();
    curl_setopt_array($curl, [
        CURLOPT_URL => 'https://api3.pn01.fun/servers/public/101oopaliaajppprrr',
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => '',
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 30,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => 'POST',
        CURLOPT_POSTFIELDS => '{"connect_state":"rw5efcXvxVtTVO14lGXQuE1vHsVLDymJF1o1NrMszYs\u003d","connected":false,"current_version":10,"ip":"5.213.37.209","isp":"WIFI|Mobile Communication Company of Iran|Mobile Communication Company","languageCode":"en","server_number":"7b611bdaa2e2d18c7b611bdaa2e2d18c55673890p4rl4s456j4u8rh4d4d456009rsjr6dddnedb7rdh67poc78ehrh990908jn","type":"official"}',
        CURLOPT_HTTPHEADER => [
            'authorization: Bearer null',
            'type: official',
            'deactiveserver: true',
            'checkapp: VmZdwHBDj2ODQF2sp8/NMQJhNxfG8fTcv0CVNaJoIPos1CFacoX/dDHNwleWEBq9',
            'check-time: PIUSZHcPNqe3g1tvgJOUZ1SAwU7aVPSGrxJGHYgc7hRLeWH+FAK55+rsGvtB7jL7CE4D13n/kM62uT3bJd3eHg==',
            'content-type: application/json; charset=UTF-8',
        ],
    ]);
    
    $response = curl_exec($curl);
    curl_close($curl);
    
    return json_decode($response, true);
}

// چک کردن اعتبار کش
if (file_exists(CACHE_FILE) && (time() - filemtime(CACHE_FILE)) < CACHE_TIME) {
    // خواندن داده‌ها از کش
    $response = json_decode(file_get_contents(CACHE_FILE), true);
} else {
    // گرفتن داده‌ها از API و ذخیره در کش
    $response = fetchDataFromAPI();
    if ($response) {
        file_put_contents(CACHE_FILE, json_encode($response));
    }
}

// اطمینان از وجود داده‌های مورد نیاز در پاسخ
if ($response && isset($response['result']['gghhzzss']['vvff'])) {
    // دریافت مقدار شمارنده
    $index = file_exists(INDEX_FILE) ? (int)file_get_contents(INDEX_FILE) : 0;
    
    $configs = $response['result']['gghhzzss']['vvff'];
    $totalConfigs = count($configs);
    
    // اطمینان از اینکه شمارنده از تعداد کانفیگ‌ها بیشتر نشود
    $index = $index % $totalConfigs;
    $config = $configs[$index];
    
    // چاپ کانفیگ به صورت رمزگشایی و اصلاح‌شده
    $url = decrypt($config['jkdfidiodooo']);
    if ($url !== false) {
        echo swap($config['jhvxjchvb'], $config['dfkvjdfkj'], $url) . "\n\n";
    }
    
    // افزایش شمارنده و ذخیره آن در فایل
    $index = ($index + 1) % $totalConfigs;
    file_put_contents(INDEX_FILE, $index);
}
