# 🔒 Ashoor TV - Apache Configuration

# تفعيل محرك إعادة الكتابة
RewriteEngine On

# إعادة توجيه favicon
RewriteCond %{REQUEST_URI} ^/favicon\.ico$ [NC]
RewriteRule ^(.*)$ https://static.ct8.pl/favicon.ico [L,R=301]

# السماح بالوصول المباشر للملفات الموجودة
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

# السماح بالوصول المباشر للمجلدات الموجودة
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

# توجيه طلبات البث المباشر (نسخة Xtream الجديدة)
# ⚡ أولاً: توجيه طلبات البروكسي (تحتوي http في المسار)
# نمط: /live/ID/http://server/path... (بروكسي لروابط خارجية)
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^live/([^/]+)/(https?):/(.*)$ get_video.php?id=$1&path=$2:/$3 [QSA,L,B]

RewriteRule ^live/([^/]+)/(https?):(.*)$ get_video.php?id=$1&path=$2:$3 [QSA,L,B]

# نمط: /live/username/password/stream_id.m3u8
RewriteRule ^live/([^/]+)/([^/]+)/([^/.]+)\.([^/.]+)$ get_video.php?username=$1&password=$2&id=$3&output=$4 [QSA,L]

# نمط: /live/username/password/stream_id/segment.ts (للبروكسي)
RewriteRule ^live/([^/]+)/([^/]+)/([^/.]+)/(.*)$ get_video.php?username=$1&password=$2&id=$3&path=$4 [QSA,L]

# توجيه طلبات البث المباشر (النمط القديم بالـ ID فقط)
RewriteRule ^live/([^/]+)/(.*)$ get_video.php?id=$1&path=$2 [QSA,L]

# توجيه الطلبات بالتوكن
RewriteRule ^stream/(.*)$ get_video.php?token=$1 [QSA,L]

# Headers للأمان والأداء
<IfModule mod_headers.c>
    # السماح بـ CORS من جميع المصادر
    Header set Access-Control-Allow-Origin "*"
    Header set Access-Control-Allow-Methods "GET, POST, OPTIONS"
    Header set Access-Control-Allow-Headers "Content-Type, Authorization, X-Requested-With"
    
    # منع التخزين المؤقت للبث المباشر
    <FilesMatch "\.(m3u8|mpd|ts)$">
        Header set Cache-Control "no-cache, no-store, must-revalidate"
        Header set Pragma "no-cache"
        Header set Expires "0"
    </FilesMatch>
    
    # إضافة Content-Type الصحيح لملفات HLS
    <FilesMatch "\.m3u8$">
        Header set Content-Type "application/vnd.apple.mpegurl"
    </FilesMatch>
    
    <FilesMatch "\.ts$">
        Header set Content-Type "video/MP2T"
    </FilesMatch>
    
    <FilesMatch "\.mpd$">
        Header set Content-Type "application/dash+xml"
    </FilesMatch>
</IfModule>

# تعطيل عرض قائمة الملفات
Options -Indexes

# حماية الملفات الحساسة (Config, DB, Security)
<FilesMatch "^(config\.php|db_helper\.php|security\.php|\.env|links_db\.json|xtream_db\.json|dns_cache\.json)$">
    Order allow,deny
    Deny from all
</FilesMatch>

# 🛡️ حماية صفحات الإدارة بمفتاح سري
# لن يتمكن أي شخص من الوصول لـ encrypt_tool.php إلا بإضافة ?access_key=YOUR_SECRET_KEY
# غيّر المفتاح أدناه لمفتاح خاص بك
RewriteCond %{REQUEST_URI} (encrypt_tool|check_access|debug_xtream|diag_api|diagnostic|proxy_debug|stream_diagnostics|stream_test|test_advanced|test_stream|test_whitelist|test_xtream|test_vod_api)\.php [NC]
RewriteCond %{QUERY_STRING} !access_key=AshoorAdmin2026SecretGate [NC]
RewriteRule ^ - [F,L]

# 🛡️ حماية مجلد admin
RewriteCond %{REQUEST_URI} ^/alammna/admin/ [NC]
RewriteCond %{QUERY_STRING} !access_key=AshoorAdmin2026SecretGate [NC]
RewriteRule ^ - [F,L]

# 🛡️ حماية ملف الـ debug.log من الوصول المباشر
<FilesMatch "^debug\.log$">
    Order allow,deny
    Deny from all
</FilesMatch>

# 🛡️ منع أدوات السحب والسكان الشائعة
RewriteCond %{HTTP_USER_AGENT} (nikto|sqlmap|nmap|masscan|dirbuster|gobuster|wfuzz|ffuf|nuclei|acunetix|nessus|burp|owasp|zaproxy|httrack|wget|curl\/|python-requests|scrapy|httpie) [NC]
RewriteRule ^ - [F,L]

# السماح بملفات .well-known (لشهادات SSL)
RewriteCond %{REQUEST_URI} !^/\.well-known/
