File size: 12,190 Bytes
d615b83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0831d2d
 
 
e8c35e6
 
d615b83
 
 
 
 
e8c35e6
d615b83
 
 
 
 
 
 
 
 
0831d2d
e8c35e6
0831d2d
 
 
 
e8c35e6
0831d2d
 
 
e8c35e6
 
 
 
0831d2d
e8c35e6
 
0831d2d
e8c35e6
 
 
 
d615b83
 
 
 
 
 
 
 
 
 
 
 
 
 
e8c35e6
d615b83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e8c35e6
d615b83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
import os
import time
import subprocess
import threading
import random
import shutil
import logging
import requests
import gc
import json
from flask import Flask, request, jsonify
from huggingface_hub import HfApi

app = Flask(__name__)
logging.basicConfig(level=logging.INFO)

DATASET_REPO = os.environ.get("DATASET_REPO")
HF_TOKEN = os.environ.get("HF_TOKEN")
RENDER_URL = os.environ.get("RENDER_URL") 
ACCOUNTS_FILE = "accounts.txt"
WARP_PORT = 40000

api = HfApi(token=HF_TOKEN)

# --- YARDIMCI KOMUTLAR ---
def run_command(cmd):
    try:
        return subprocess.run(cmd, shell=True, capture_output=True, text=True)
    except Exception as e:
        print(f"⚠️ Komut Hatası: {e}", flush=True)
        return None

def get_accounts():
    if not os.path.exists(ACCOUNTS_FILE): return []
    with open(ACCOUNTS_FILE, 'r') as f:
        return [line.strip() for line in f if ':' in line]

def report_to_render(status, message, task_id, download_url=None):
    print(f"📡 Rapor: {status} - {message}", flush=True)
    if not RENDER_URL: return
    try:
        requests.post(f"{RENDER_URL}/webhook", json={
            'status': status, 'message': message, 'task_id': task_id, 'download_url': download_url
        }, timeout=10)
    except: pass

# --- GÜVENLİ WARP YÖNETİMİ ---
def rotate_warp_ip():
    """WARP IP'sini güvenli bir şekilde değiştirir (Çökmeden)"""
    print("\n♻️ IP ROTASYONU BAŞLATILIYOR...", flush=True)
    try:
        os.system("pkill wireproxy")
        os.system("pkill wgcf")
        time.sleep(2)
        
        for f in ["wgcf-account.toml", "wgcf-profile.conf", "wireproxy.conf"]:
            if os.path.exists(f): 
                try: os.remove(f)
                except: pass
        
        print("⚡ Cloudflare ile görüşülüyor...", flush=True)
        subprocess.run(["wgcf", "register", "--accept-tos"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, timeout=30)
        subprocess.run(["wgcf", "generate"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, timeout=30)
        
        if not os.path.exists("wgcf-profile.conf"):
            print("❌ WARP Config alınamadı!", flush=True)
            return False
            
        with open("wgcf-profile.conf", "r") as f:
            content = f.read()
            
        private_key = content.split("PrivateKey = ")[1].split("\n")[0].strip()
        address = content.split("Address = ")[1].split("\n")[0].strip()
        
        wp_conf = f"""[Interface]
PrivateKey = {private_key}
Address = {address}
DNS = 1.1.1.1
[Peer]
PublicKey = bmXOC+F1FxEMF9dyiK2H5/1SUtzH0JuVo51h2wPfgyo=
AllowedIPs = 0.0.0.0/0
Endpoint = engage.cloudflareclient.com:2408
[Socks5]
BindAddress = 127.0.0.1:{WARP_PORT}"""

        with open("wireproxy.conf", "w") as f:
            f.write(wp_conf)
            
        print(f"🚀 Tünel Açılıyor...", flush=True)
        subprocess.Popen(["wireproxy", "-c", "wireproxy.conf"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
        time.sleep(5)
        return True
    except Exception as e:
        print(f"❌ IP Rotasyonunda Hata: {e}", flush=True)
        return False

def safe_mega_reset():
    """MegaCMD'yi güvenli sıfırlar"""
    try:
        os.system("pkill mega-cmd")
        time.sleep(2)
        subprocess.Popen(["nohup", "mega-cmd-server"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
        time.sleep(5)
    except: pass

def login_with_proxy(account_str):
    try:
        email, password = account_str.split(":", 1)
        proxy_url = f"socks5://127.0.0.1:{WARP_PORT}"
        subprocess.run(["mega-proxy", proxy_url], capture_output=True)
        
        print(f"🔑 Giriş Deneniyor: {email[:3]}***", flush=True)
        res = subprocess.run(["mega-login", email, password], capture_output=True, text=True, timeout=45)
        if res.returncode == 0:
            print("✅ Giriş Başarılı.", flush=True)
            return True
        else:
            print(f"❌ Giriş Başarısız: {res.stderr}", flush=True)
            return False
    except Exception as e:
        return False

def get_mega_details():
    try:
        res = subprocess.run(["mega-transfers"], capture_output=True, text=True)
        return res.stdout.strip()
    except: return ""

def download_engine(link, task_id):
    try:
        print(f"\n⚙️ WORKER BAŞLADI: {task_id}", flush=True)
        base_dir = "downloads"
        abs_base = os.path.abspath(base_dir)
        download_folder = f"{abs_base}/{task_id}"
        zip_file = f"{abs_base}/{task_id}.zip"
        
        os.makedirs(download_folder, exist_ok=True)
        accounts = get_accounts()
        random.shuffle(accounts)
        
        if not accounts: return
        is_completed = False
        
        safe_mega_reset()
        rotate_warp_ip()
        
        for i, account in enumerate(accounts):
            if is_completed: break
            
            print(f"\n🔄 --- HESAP {i+1} / {len(accounts)} ---", flush=True)
            if not login_with_proxy(account):
                rotate_warp_ip()
                safe_mega_reset()
                if not login_with_proxy(account): continue
                
            print(f"📥 İNDİRME BAŞLATILIYOR...", flush=True)
            cmd = f'nohup mega-get "{link}" "{download_folder}" > /dev/null 2>&1 &'
            os.system(cmd)
            
            no_transfer_count = 0
            loop_counter = 0
            
           # 🔴 ZOMBİ KORUMASI V2 (Kesin Çözüm - Sadece Yüzdelere Bakar)
            import re
            previous_percents = []
            stuck_counter = 0
            
            while True:
                time.sleep(5)
                output = get_mega_details()
                lower_output = output.lower()
                
                # 1. Klasik Limit ve Ban Kontrolü
                if "bandwidth quota exceeded" in lower_output or "paused" in lower_output or "retrying" in lower_output:
                    print("\n🚨 KOTA/IP BAN TESPİT EDİLDİ! Değiştiriliyor...", flush=True)
                    os.system("pkill mega-get")
                    if rotate_warp_ip():
                        safe_mega_reset()
                    else:
                        time.sleep(60)
                    break 
                    
                # 2. Zombi Koruması (Yüzde Avcısı)
                if "active" in lower_output:
                    # Logun içindeki tüm "%X.XX" rakamlarını cımbızla çeker
                    current_percents = re.findall(r'(\d+\.\d+)%', output)
                    
                    if current_percents and current_percents == previous_percents:
                        stuck_counter += 1
                        print(f"⚠️ İlerleme Durdu! Yüzdeler sabit kaldı. (Şoka son {6 - stuck_counter} adım)", flush=True)
                        if stuck_counter >= 6: # 30 Saniye milim ilerlemezse
                            print("\n🧟 ZOMBİ DOSYA VURULDU! Mega tıkandı, IP'ye şok veriliyor...", flush=True)
                            os.system("pkill mega-get")
                            rotate_warp_ip()
                            safe_mega_reset()
                            stuck_counter = 0
                            break
                    else:
                        stuck_counter = 0
                        previous_percents = current_percents
                else:
                    stuck_counter = 0
                    
                # 3. İndirme Bitiş Kontrolü
                if "no active transfers" in lower_output:
                    loop_counter += 1
                    if loop_counter > 3: 
                        no_transfer_count += 1
                        if no_transfer_count >= 2:
                            if len(os.listdir(download_folder)) > 0:
                                print("\n✅ İNDİRME TAMAMLANDI!", flush=True)
                                is_completed = True
                                break
                            else:
                                break 
                else:
                    no_transfer_count = 0
                
                # Terminale Temiz Log Basma
                lines = output.split('\n')
                for line in lines:
                    if "TRANSFERRING" in line or "%" in line:
                        print(f"📊 {line.strip()}", flush=True)
                loop_counter += 1
                
            if is_completed: break
            time.sleep(2)
            
        if not is_completed: return

        print("📦 ZİPLENİYOR...", flush=True)
        shutil.make_archive(f"{abs_base}/{task_id}", 'zip', download_folder)
        
        print("📤 YÜKLENİYOR...", flush=True)
        try:
            api.upload_file(
                path_or_fileobj=zip_file, repo_id=DATASET_REPO, repo_type="dataset", path_in_repo=f"uploads/{task_id}.zip"
            )
            print("✅ HUGGING FACE YÜKLEMESİ BAŞARILI!")
        except Exception as e:
            print(f"❌ Upload Hatası: {e}", flush=True)
            
        try:
            shutil.rmtree(download_folder, ignore_errors=True)
            os.remove(zip_file)
        except: pass
        gc.collect()
        
    except Exception as e:
        print(f"🔥 KRİTİK HATA: {e}", flush=True)
        
@app.route('/process', methods=['POST'])
def process():
    data = request.json
    threading.Thread(target=download_engine, args=(data['link'], data.get('task_id', 'mega_task'))).start()
    return jsonify({'status': 'started'})

@app.route('/')
def ui():
    return """
    <html>
        <body style="background-color: #1e1e1e; color: white; font-family: sans-serif; text-align: center; padding-top: 50px;">
            <h2>Yael Mega-Worker Paneli 🚀</h2>
            <div style="margin: 20px;">
                <input id="mega_link" type="text" placeholder="Mega.nz Linkini Buraya Yapıştır..." style="width: 80%; max-width: 500px; padding: 15px; border-radius: 8px; border: none; margin-bottom: 10px;"/><br>
                <input id="task_id" type="text" placeholder="Görev Adı (Örn: arsiv_1 - Boşluk Bırakma)" style="width: 80%; max-width: 500px; padding: 15px; border-radius: 8px; border: none; margin-bottom: 20px;"/><br>
                <button onclick="tetikle()" style="background-color: #ff9900; color: #1e1e1e; font-weight: bold; padding: 15px 30px; border: none; border-radius: 8px; cursor: pointer; font-size: 16px;">İndirmeyi Başlat</button>
            </div>
            <p id="sonuc" style="color: #00ff00; font-size: 18px; margin-top: 20px;"></p>

            <script>
                function tetikle() {
                    let link = document.getElementById('mega_link').value;
                    let task = document.getElementById('task_id').value;
                    let sonucText = document.getElementById('sonuc');
                    
                    if(!link || !task) {
                        sonucText.innerText = "⚠️ Hata: Link ve Görev Adı boş olamaz!";
                        sonucText.style.color = "red";
                        return;
                    }

                    sonucText.innerText = "⏳ İstek sunucuya iletiliyor, bekle...";
                    sonucText.style.color = "yellow";

                    fetch('/process', {
                        method: 'POST',
                        headers: {'Content-Type': 'application/json'},
                        body: JSON.stringify({link: link, task_id: task})
                    })
                    .then(res => res.json())
                    .then(data => {
                        sonucText.innerText = "✅ İndirme motoru ateşlendi! Hugging Face loglarından takip edebilirsin.";
                        sonucText.style.color = "#00ff00";
                    })
                    .catch(err => {
                        sonucText.innerText = "❌ Bir hata oluştu: " + err;
                        sonucText.style.color = "red";
                    });
                }
            </script>
        </body>
    </html>
    """

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=7860)