AbuAlone09 commited on
Commit
51b0060
·
verified ·
1 Parent(s): 6570177

Update licensing_client.py

Browse files
Files changed (1) hide show
  1. licensing_client.py +153 -84
licensing_client.py CHANGED
@@ -1,6 +1,7 @@
1
  # =========================================================
2
  # MODULE: licensing_client.py
3
  # SYSTEM: ADVANCED RESOURCE & CONCURRENCY THREAD MANAGER
 
4
  # AUTHOR: Abu Alone © 2026
5
  # =========================================================
6
 
@@ -13,33 +14,70 @@ import subprocess
13
  from datetime import datetime
14
  from loguru import logger
15
 
16
- # Cấu hình đường dẫn lưu trữ thông tin Server cổng thanh toán dịch vụ
17
  STORAGE_FILE = "Hugging AbuAlone09/AI-Video-Engine-storage"
 
 
18
 
19
- # ĐỌC BIẾN MÔI TRƯỜNG AN TOÀN TỪ SECRET CỦA HUGGING FACE
20
  SERVER_URL = os.getenv("LICENSIFY_SERVER_URL", "https://abualone09-my-licensify-server.hf.space").strip()
21
  SECRET_API_KEY = os.getenv("SECRET_API_KEY", "YOUR_SECRET_API_KEY_HERE").strip()
22
 
23
- # Khởi tạo cấu trúc lưu trữ nếu chưa tồn tại trên thiết bị di động
24
  os.makedirs(os.path.dirname(STORAGE_FILE), exist_ok=True)
25
  if not os.path.exists(STORAGE_FILE):
26
  with open(STORAGE_FILE, "w", encoding="utf-8") as f:
27
  json.dump({"keys": {}, "free_devices": {}}, f, indent=4)
28
 
29
- # --- THÀNH PHẦN QUAN TRỌNG: QUẢN LÝ TRẠNG THÁI 6 LUỒNG TOÀN CỤC (MEMORY LOCK) ---
30
- THREAD_POOL = {
31
- 1: {"status": "idle", "key": None, "type": None, "pid": None, "device": None},
32
- 2: {"status": "idle", "key": None, "type": None, "pid": None, "device": None},
33
- 3: {"status": "idle", "key": None, "type": None, "pid": None, "device": None},
34
- 4: {"status": "idle", "key": None, "type": None, "pid": None, "device": None},
35
- 5: {"status": "idle", "key": None, "type": None, "pid": None, "device": None},
36
- 6: {"status": "idle", "key": None, "type": None, "pid": None, "device": None},
37
- }
38
-
39
- # --- BỔ SUNG HÀM ĐỂ BÊN RUN_APP.PY GỌI ĐỒNG BỘ REALTIME THEO GIÂY KHÔNG BỊ LỖI ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
  def get_active_threads_count() -> int:
41
- """Trả về tổng số luồng thực tế đang bận render trong hệ thống cluster"""
42
- return sum(1 for t in THREAD_POOL.values() if t["status"] == "rendering")
 
43
 
44
  # =========================================================
45
  # CORE 1: XỬ LÝ ĐỌC / GHI & XÁC THỰC API KEY TỪ XA TỚI SERVER
@@ -54,7 +92,6 @@ def _save_storage(data):
54
  json.dump(data, f, indent=4)
55
 
56
  def clean_expired_keys():
57
- """Tự động quét và xóa sạch các Key đã hết hạn so với thời gian thực của hệ thống"""
58
  data = _load_storage()
59
  now_ts = time.time()
60
  changed = False
@@ -76,16 +113,12 @@ def clean_expired_keys():
76
  _save_storage(data)
77
 
78
  def verify_and_get_license_info(key_input: str, device_id: str) -> tuple:
79
- """Kiểm tra cục bộ qua Hugging Face Secret trước, nếu không khớp gọi API sang Server đối soát"""
80
  clean_expired_keys()
81
-
82
  token = key_input.strip()
83
 
84
- # LẤY VIP KEY VÀ ADMIN KEY AN TOÀN TRỰC TIẾP TỪ MỤC SECRET CỦA HUGGING FACE
85
- admin_secret = os.getenv("ADMIN_KEY").strip()
86
- vip_secret = os.getenv("VIP_KEY").strip()
87
 
88
- # 1. Cơ chế đăng nhập bằng mã Admin tối cao lấy từ Secret (Không giới hạn, mở Panel Test)
89
  if token == admin_secret:
90
  return True, {
91
  "tier": "ADMIN",
@@ -94,13 +127,12 @@ def verify_and_get_license_info(key_input: str, device_id: str) -> tuple:
94
  "tx_date": datetime.now().strftime("%Y-%m-%d"),
95
  "expiry": "Permanent Access",
96
  "days_left": 9999,
97
- "msg": "📋 SYSTEM STATUS: Admin Master Active. Full hardware diagnostic test layer unlocked.",
98
  "show_test_panel": True,
99
  "remove_watermark": True,
100
  "bypass_limits": True
101
  }
102
 
103
- # 2. Cơ chế đặc quyền ưu đãi dành cho VIP Key lấy từ Secret (Bypass giới hạn, không hiện Panel Test)
104
  if token == vip_secret:
105
  return True, {
106
  "tier": "VIP",
@@ -109,33 +141,27 @@ def verify_and_get_license_info(key_input: str, device_id: str) -> tuple:
109
  "tx_date": datetime.now().strftime("%Y-%m-%d"),
110
  "expiry": "2026-12-31",
111
  "days_left": 200,
112
- "msg": "👑 VIP STATUS: Promo Key Verified. Watermarks and daily rendering limits removed.",
113
  "show_test_panel": False,
114
  "remove_watermark": True,
115
  "bypass_limits": True
116
  }
117
 
118
  data = _load_storage()
119
-
120
- # 3. Tra cứu kho lưu trữ cục bộ trước để tăng tốc độ phản hồi cho giao diện điện thoại
121
  if token in data["keys"]:
122
  info = data["keys"][token]
123
  days_left = int((info["expiry_timestamp"] - time.time()) / 86400)
124
  return True, {
125
  "tier": "VIP", "tx_name": info["tx_name"], "amount": info["amount"],
126
  "tx_date": info["tx_date"], "expiry": info["expiry"], "days_left": max(0, days_left),
127
- "msg": f"👑 VIP ACTIVE | User: {info['tx_name']} | {max(0, days_left)} days remaining.",
128
  "show_test_panel": False,
129
  "remove_watermark": True,
130
  "bypass_limits": False
131
  }
132
 
133
- # 4. Giao tiếp API an toàn từ xa kết nối thẳng tới Kho lưu trữ của Cổng thanh toán dịch vụ công khai
134
  try:
135
- headers = {
136
- "X-API-Key": SECRET_API_KEY,
137
- "Content-Type": "application/json"
138
- }
139
  payload = {"key": token, "hwid": device_id}
140
  response = requests.post(f"{SERVER_URL}/api/verify-key", json=payload, headers=headers, timeout=10)
141
 
@@ -159,53 +185,65 @@ def verify_and_get_license_info(key_input: str, device_id: str) -> tuple:
159
  return True, {
160
  "tier": "VIP", "tx_name": tx_info.get("buyer_name"), "amount": tx_info.get("amount_paid"),
161
  "tx_date": tx_info.get("payment_date"), "expiry": expiry_str, "days_left": max(0, days_left),
162
- "msg": f"👑 VIP KEY VERIFIED | Owner: {tx_info.get('buyer_name')} | Expiry: {expiry_str}.",
163
  "show_test_panel": False,
164
  "remove_watermark": True,
165
  "bypass_limits": False
166
  }
167
- return False, {"msg": "❌ Invalid Access Key or communication failure with payment gateway!", "show_test_panel": False, "remove_watermark": False, "tier": "FREE"}
168
  except Exception as e:
169
- return False, {"msg": f"⚠️ Connection error to Licensify Server! Info: {str(e)}", "show_test_panel": False, "remove_watermark": False, "tier": "FREE"}
170
 
171
  # =========================================================
172
- # CORE 2: CƠ CHẾ ĐIỀU PHỐI 6 LUỒNG ĐỘNG & BIÊN TRỰC VIP / FREE
173
  # =========================================================
174
 
175
- def get_thread_status_json():
176
- busy_count = get_active_threads_count()
177
- vip_count = sum(1 for t in THREAD_POOL.values() if t["type"] == "VIP")
178
- free_count = sum(1 for t in THREAD_POOL.values() if t["type"] == "FREE")
 
 
 
 
 
179
  return {
180
  "busy_channels": f"{busy_count}/6",
181
  "vip_active": vip_count,
182
  "free_active": free_count,
183
- "pool": THREAD_POOL
184
  }
185
 
186
  def allocate_render_thread(key_input: str, device_id: str, is_vip: bool) -> tuple:
187
- global THREAD_POOL
188
-
189
- for slot, thread in THREAD_POOL.items():
 
 
 
190
  if thread["status"] == "rendering":
191
- if is_vip and thread["key"] == key_input.strip():
192
  return False, "❌ This VIP Key is already running a rendering process! Multi-thread allocation denied."
193
  if not is_vip and thread["device"] == device_id:
194
  return False, "❌ Your device is currently rendering a video. Please wait until it completes!"
195
 
196
  target_slot = None
197
-
198
  if is_vip:
 
199
  for i in range(1, 6):
200
- if THREAD_POOL[i]["status"] == "idle":
201
  target_slot = i
202
  break
203
 
 
204
  if not target_slot:
205
- if THREAD_POOL[6]["status"] == "idle":
206
  target_slot = 6
207
- elif THREAD_POOL[6]["type"] == "FREE":
208
- free_pid = THREAD_POOL[6]["pid"]
 
 
209
  logger.warning(f"👑 VIP Eviction Triggered: Terminating Free PID {free_pid} at Slot 6.")
210
  try:
211
  if free_pid:
@@ -213,40 +251,59 @@ def allocate_render_thread(key_input: str, device_id: str, is_vip: bool) -> tupl
213
  except ProcessLookupError:
214
  pass
215
 
 
 
 
 
 
 
 
 
 
 
 
216
  target_slot = 6
217
- THREAD_POOL[6] = {"status": "idle", "key": None, "type": None, "pid": None, "device": None}
218
  else:
219
- if THREAD_POOL[6]["status"] == "idle":
 
220
  target_slot = 6
221
 
222
  if not target_slot:
223
  return False, "⚠️ All rendering channels are currently full. Please try again in a few moments!"
224
 
225
- return True, target_slot
226
 
227
  def register_process_to_slot(slot: int, key_input: str, device_id: str, is_vip: bool, pid: int):
228
- THREAD_POOL[slot] = {
 
229
  "status": "rendering",
230
  "key": key_input.strip() if is_vip else None,
231
  "type": "VIP" if is_vip else "FREE",
232
  "pid": pid,
233
- "device": device_id
 
234
  }
 
235
 
236
  def release_thread_slot(slot: int):
237
- if slot in THREAD_POOL:
238
- THREAD_POOL[slot] = {"status": "idle", "key": None, "type": None, "pid": None, "device": None}
 
 
239
 
240
  # =========================================================
241
- # CORE 3: GIÁM SÁT HẠN MỨC CHẶN & BỘ ĐẾM (VIP VS FREE)
242
  # =========================================================
243
 
244
  def check_generation_limits(key_input: str, device_id: str, is_vip: bool) -> tuple:
 
245
  data = _load_storage()
246
  today_str = datetime.now().strftime("%Y-%m-%d")
247
  now_ts = time.time()
248
 
249
  if is_vip:
 
250
  if key_input not in data["keys"]:
251
  return False, "License error: Key missing from verified list."
252
 
@@ -264,14 +321,9 @@ def check_generation_limits(key_input: str, device_id: str, is_vip: bool) -> tup
264
  if vip_data.get("daily_batches", 0) >= 5:
265
  return False, "❌ You have exhausted your daily limit of 5 batches. Please return tomorrow!"
266
 
267
- remaining_batches = 5 - vip_data.get("daily_batches", 0)
268
- return True, {
269
- "videos_created": vip_data.get("videos_this_batch", 0),
270
- "batches_created": vip_data.get("daily_batches", 0),
271
- "remaining_batches": remaining_batches,
272
- "status_str": f"VIP Usage: Batch {vip_data.get('daily_batches', 0)}/5 | Batch Videos: {vip_data.get('videos_this_batch', 0)}/2"
273
- }
274
  else:
 
275
  if device_id not in data["free_devices"]:
276
  data["free_devices"][device_id] = {
277
  "last_date": today_str,
@@ -286,19 +338,13 @@ def check_generation_limits(key_input: str, device_id: str, is_vip: bool) -> tup
286
  free_data["cooldown_until"] = 0
287
 
288
  if now_ts < free_data.get("cooldown_until", 0):
289
- wait_hours = int((free_data["cooldown_until"] - now_ts) / 3600)
290
- return False, f"⏱️ Free Tier Cooldown: Please wait {wait_hours + 1} hours before generating again, or upgrade to VIP Premium!"
291
 
292
  if free_data.get("daily_batches", 0) >= 3:
293
  return False, "❌ Free Tier Exhausted! Maximum 3 daily videos reached."
294
 
295
- remaining_free = 3 - free_data.get("daily_batches", 0)
296
- return True, {
297
- "videos_created": 0,
298
- "batches_created": free_data.get("daily_batches", 0),
299
- "remaining_batches": remaining_free,
300
- "status_str": f"Free Tier Usage: {free_data.get('daily_batches', 0)}/3 Videos generated today."
301
- }
302
 
303
  def commit_generation_success(key_input: str, device_id: str, is_vip: bool):
304
  data = _load_storage()
@@ -316,27 +362,50 @@ def commit_generation_success(key_input: str, device_id: str, is_vip: bool):
316
  if device_id in data["free_devices"]:
317
  free_data = data["free_devices"][device_id]
318
  free_data["daily_batches"] = free_data.get("daily_batches", 0) + 1
319
- free_data["cooldown_until"] = now_ts + (3 * 3600)
320
 
321
  _save_storage(data)
322
 
323
  # =========================================================
324
- # CORE 4: CHẾ TRỰC TIẾP KÍCH HOẠT TRẢ BÁO CÁO FILE TESTER.PY
325
  # =========================================================
326
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
327
  def execute_admin_diagnostic_test() -> str:
328
- """Khi Admin nhấn nút Test trên run_app.py, hàm này gọi ngầm file tester.py để hứng dữ liệu báo cáo trả lên UI"""
329
  test_script = "tester.py"
330
  if not os.path.exists(test_script):
331
- return f"❌ FILE MISSING: Tệp '{test_script}' không tồn tại trong thư mục chạy hệ thống!"
332
-
333
  try:
334
  result = subprocess.run(["python", test_script], capture_output=True, text=True, timeout=15)
335
  if result.returncode == 0:
336
  return f"✅ [TESTER REPORT SUCCESS]:\n{result.stdout.strip()}"
337
  else:
338
- return f"❌ [TESTER REPORT CRASHED WITH EXIT CODE {result.returncode}]:\n{result.stderr.strip()}"
339
  except subprocess.TimeoutExpired:
340
- return "⚠️ [TESTER TIMEOUT]: Tiến trình kiểm thử vượt ngưỡng 15 giây!"
341
  except Exception as e:
342
- return f"❌ [SYSTEM CRASH]: Không thể kích hoạt được tiến trình kiểm thử. Lỗi: {str(e)}"
 
 
1
  # =========================================================
2
  # MODULE: licensing_client.py
3
  # SYSTEM: ADVANCED RESOURCE & CONCURRENCY THREAD MANAGER
4
+ # FIX: PERSISTENT FILE-LOCK THREADS & COOLDOWN RE-SYNC (ANTI-RESET)
5
  # AUTHOR: Abu Alone © 2026
6
  # =========================================================
7
 
 
14
  from datetime import datetime
15
  from loguru import logger
16
 
17
+ # Cấu hình đường dẫn lưu trữ thông tin cấu hình trạng thái luồng thời gian thực
18
  STORAGE_FILE = "Hugging AbuAlone09/AI-Video-Engine-storage"
19
+ # File map trạng thái 6 luồng toàn cục để chống việc tải lại trang bị mất dấu tiến trình
20
+ THREAD_STATE_FILE = "Hugging AbuAlone09/AI-Video-Engine-threads"
21
 
 
22
  SERVER_URL = os.getenv("LICENSIFY_SERVER_URL", "https://abualone09-my-licensify-server.hf.space").strip()
23
  SECRET_API_KEY = os.getenv("SECRET_API_KEY", "YOUR_SECRET_API_KEY_HERE").strip()
24
 
25
+ # Khởi tạo cấu trúc thư mục lưu trữ an toàn trên thiết bị di động
26
  os.makedirs(os.path.dirname(STORAGE_FILE), exist_ok=True)
27
  if not os.path.exists(STORAGE_FILE):
28
  with open(STORAGE_FILE, "w", encoding="utf-8") as f:
29
  json.dump({"keys": {}, "free_devices": {}}, f, indent=4)
30
 
31
+ # Khởi tạo tệp tin đồng bộ 6 luồng vật nếu chưa
32
+ if not os.path.exists(THREAD_STATE_FILE):
33
+ initial_pool = {str(i): {"status": "idle", "key": None, "type": None, "pid": None, "device": None, "start_time": 0} for i in range(1, 7)}
34
+ with open(THREAD_STATE_FILE, "w", encoding="utf-8") as f:
35
+ json.dump(initial_pool, f, indent=4)
36
+
37
+ # --- KHỞI TẠO BỘ NHỚ ĐỆM ĐỂ TRÁNH LỖI IMPORT TỪ RUN_APP ---
38
+ THREAD_POOL = {i: {"status": "idle", "key": None, "type": None, "pid": None, "device": None} for i in range(1, 7)}
39
+
40
+ # =========================================================
41
+ # TIỆN ÍCH ĐỒNG BỘ ĐA LUỒNG FILE-PERSISTENCE (FIX THỰC TẾ 100%)
42
+ # =========================================================
43
+
44
+ def _read_threads_from_disk() -> dict:
45
+ """Đọc trạng thái thực của 6 luồng từ đĩa cứng, quét dọn ngay nếu PID chết ngầm"""
46
+ try:
47
+ with open(THREAD_STATE_FILE, "r", encoding="utf-8") as f:
48
+ pool = json.load(f)
49
+ except Exception:
50
+ pool = {str(i): {"status": "idle", "key": None, "type": None, "pid": None, "device": None, "start_time": 0} for i in range(1, 7)}
51
+
52
+ # Cơ chế tự động quét dọn (Garbage Collection): Nếu tiến trình bị sập đột ngột, giải phóng luồng ngay
53
+ changed = False
54
+ for slot_str, thread in pool.items():
55
+ if thread["status"] == "rendering" and thread["pid"]:
56
+ # Kiểm tra xem PID có thực sự đang chạy trên OS Linux không
57
+ try:
58
+ os.kill(thread["pid"], 0)
59
+ except (ProcessLookupError, OSError):
60
+ # PID đã chết, reset luồng về idle lập tức để tránh nghẽn mạch
61
+ pool[slot_str] = {"status": "idle", "key": None, "type": None, "pid": None, "device": None, "start_time": 0}
62
+ changed = True
63
+ if changed:
64
+ with open(THREAD_STATE_FILE, "w", encoding="utf-8") as f:
65
+ json.dump(pool, f, indent=4)
66
+
67
+ return pool
68
+
69
+ def _write_threads_to_disk(pool: dict):
70
+ """Ghi đè cấu trúc luồng mới xuống đĩa cứng để toàn bộ worker FastAPI nhận diện đồng bộ"""
71
+ try:
72
+ with open(THREAD_STATE_FILE, "w", encoding="utf-8") as f:
73
+ json.dump(pool, f, indent=4)
74
+ except Exception as e:
75
+ logger.error(f"Error writing thread state: {e}")
76
+
77
  def get_active_threads_count() -> int:
78
+ """Trả về tổng số luồng thực tế đang bận render - Đọc từ file giúp nhảy số chuẩn xác từng giây"""
79
+ pool = _read_threads_from_disk()
80
+ return sum(1 for t in pool.values() if t["status"] == "rendering")
81
 
82
  # =========================================================
83
  # CORE 1: XỬ LÝ ĐỌC / GHI & XÁC THỰC API KEY TỪ XA TỚI SERVER
 
92
  json.dump(data, f, indent=4)
93
 
94
  def clean_expired_keys():
 
95
  data = _load_storage()
96
  now_ts = time.time()
97
  changed = False
 
113
  _save_storage(data)
114
 
115
  def verify_and_get_license_info(key_input: str, device_id: str) -> tuple:
 
116
  clean_expired_keys()
 
117
  token = key_input.strip()
118
 
119
+ admin_secret = os.getenv("ADMIN_KEY", "ADMIN_SECRET_MOCK").strip()
120
+ vip_secret = os.getenv("VIP_KEY", "VIP_SECRET_MOCK").strip()
 
121
 
 
122
  if token == admin_secret:
123
  return True, {
124
  "tier": "ADMIN",
 
127
  "tx_date": datetime.now().strftime("%Y-%m-%d"),
128
  "expiry": "Permanent Access",
129
  "days_left": 9999,
130
+ "msg": "📋 SYSTEM STATUS: Admin Master Active.",
131
  "show_test_panel": True,
132
  "remove_watermark": True,
133
  "bypass_limits": True
134
  }
135
 
 
136
  if token == vip_secret:
137
  return True, {
138
  "tier": "VIP",
 
141
  "tx_date": datetime.now().strftime("%Y-%m-%d"),
142
  "expiry": "2026-12-31",
143
  "days_left": 200,
144
+ "msg": "👑 VIP STATUS: Promo Key Verified.",
145
  "show_test_panel": False,
146
  "remove_watermark": True,
147
  "bypass_limits": True
148
  }
149
 
150
  data = _load_storage()
 
 
151
  if token in data["keys"]:
152
  info = data["keys"][token]
153
  days_left = int((info["expiry_timestamp"] - time.time()) / 86400)
154
  return True, {
155
  "tier": "VIP", "tx_name": info["tx_name"], "amount": info["amount"],
156
  "tx_date": info["tx_date"], "expiry": info["expiry"], "days_left": max(0, days_left),
157
+ "msg": f"👑 VIP ACTIVE | User: {info['tx_name']}",
158
  "show_test_panel": False,
159
  "remove_watermark": True,
160
  "bypass_limits": False
161
  }
162
 
 
163
  try:
164
+ headers = {"X-API-Key": SECRET_API_KEY, "Content-Type": "application/json"}
 
 
 
165
  payload = {"key": token, "hwid": device_id}
166
  response = requests.post(f"{SERVER_URL}/api/verify-key", json=payload, headers=headers, timeout=10)
167
 
 
185
  return True, {
186
  "tier": "VIP", "tx_name": tx_info.get("buyer_name"), "amount": tx_info.get("amount_paid"),
187
  "tx_date": tx_info.get("payment_date"), "expiry": expiry_str, "days_left": max(0, days_left),
188
+ "msg": f"👑 VIP KEY VERIFIED.",
189
  "show_test_panel": False,
190
  "remove_watermark": True,
191
  "bypass_limits": False
192
  }
193
+ return False, {"msg": "❌ Invalid Access Key", "show_test_panel": False, "remove_watermark": False, "tier": "FREE"}
194
  except Exception as e:
195
+ return False, {"msg": f"⚠️ Connection error: {str(e)}", "show_test_panel": False, "remove_watermark": False, "tier": "FREE"}
196
 
197
  # =========================================================
198
+ # CORE 2: ĐIỀU PHỐI 6 LUỒNG BIÊN TRỰC (VIP LẤY LUỒNG - TRỤC XUẤT FREE)
199
  # =========================================================
200
 
201
+ def get_thread_status_json() -> dict:
202
+ """Hàm này nhảy số từng giây, kiểm tra chéo xem user hiện tại có đang chạy ngầm hay không để hồi phục UI khi refresh trang"""
203
+ pool = _read_threads_from_disk()
204
+ busy_count = sum(1 for t in pool.values() if t["status"] == "rendering")
205
+ vip_count = sum(1 for t in pool.values() if t["type"] == "VIP")
206
+ free_count = sum(1 for t in pool.values() if t["type"] == "FREE")
207
+
208
+ # Chuyển đổi định dạng key thành kiểu số để tương thích ngược hoàn toàn với run_app.py
209
+ compatible_pool = {int(k): v for k, v in pool.items()}
210
  return {
211
  "busy_channels": f"{busy_count}/6",
212
  "vip_active": vip_count,
213
  "free_active": free_count,
214
+ "pool": compatible_pool
215
  }
216
 
217
  def allocate_render_thread(key_input: str, device_id: str, is_vip: bool) -> tuple:
218
+ """Phân bổ kênh: Khóa chặt không cho user tự nhân bản luồng và kích hoạt trục xuất nếu VIP cần kênh"""
219
+ pool = _read_threads_from_disk()
220
+ token = key_input.strip()
221
+
222
+ # Kiểm tra trùng lặp: Nếu thực thể này đang chạy thì không cấp thêm luồng thứ 2
223
+ for slot_str, thread in pool.items():
224
  if thread["status"] == "rendering":
225
+ if is_vip and thread["key"] == token:
226
  return False, "❌ This VIP Key is already running a rendering process! Multi-thread allocation denied."
227
  if not is_vip and thread["device"] == device_id:
228
  return False, "❌ Your device is currently rendering a video. Please wait until it completes!"
229
 
230
  target_slot = None
231
+
232
  if is_vip:
233
+ # VIP tìm từ luồng 1 đến luồng 5 trước
234
  for i in range(1, 6):
235
+ if pool[str(i)]["status"] == "idle":
236
  target_slot = i
237
  break
238
 
239
+ # Nếu luồng 1-5 bận, kiểm tra luồng số 6
240
  if not target_slot:
241
+ if pool["6"]["status"] == "idle":
242
  target_slot = 6
243
+ elif pool["6"]["type"] == "FREE":
244
+ # CHẾ TRỤC XUẤT: Luồng 6 của user thường bị VIP đá văng khẩn cấp lập tức
245
+ free_pid = pool["6"]["pid"]
246
+ free_device = pool["6"]["device"]
247
  logger.warning(f"👑 VIP Eviction Triggered: Terminating Free PID {free_pid} at Slot 6.")
248
  try:
249
  if free_pid:
 
251
  except ProcessLookupError:
252
  pass
253
 
254
+ # Trả lại lượt tạo cho người dùng thường bị đá văng để không làm mất oan lượt của họ
255
+ try:
256
+ data = _load_storage()
257
+ if free_device in data["free_devices"]:
258
+ # Giảm bộ đếm đi 1 đơn vị, xóa cooldown vì tiến trình bị hủy giữa chừng do nhường kênh VIP
259
+ data["free_devices"][free_device]["daily_batches"] = max(0, data["free_devices"][free_device]["daily_batches"] - 1)
260
+ data["free_devices"][free_device]["cooldown_until"] = 0
261
+ _save_storage(data)
262
+ except Exception as ex:
263
+ logger.error(f"Failed to restore evicted user limits: {ex}")
264
+
265
  target_slot = 6
266
+ pool["6"] = {"status": "idle", "key": None, "type": None, "pid": None, "device": None, "start_time": 0}
267
  else:
268
+ # Người dùng miễn phí CHỈ ĐƯỢC PHÉP CHẠY tại luồng số 6 nếu nó trống hoàn toàn
269
+ if pool["6"]["status"] == "idle":
270
  target_slot = 6
271
 
272
  if not target_slot:
273
  return False, "⚠️ All rendering channels are currently full. Please try again in a few moments!"
274
 
275
+ return True, int(target_slot)
276
 
277
  def register_process_to_slot(slot: int, key_input: str, device_id: str, is_vip: bool, pid: int):
278
+ pool = _read_threads_from_disk()
279
+ pool[str(slot)] = {
280
  "status": "rendering",
281
  "key": key_input.strip() if is_vip else None,
282
  "type": "VIP" if is_vip else "FREE",
283
  "pid": pid,
284
+ "device": device_id,
285
+ "start_time": time.time()
286
  }
287
+ _write_threads_to_disk(pool)
288
 
289
  def release_thread_slot(slot: int):
290
+ pool = _read_threads_from_disk()
291
+ if str(slot) in pool:
292
+ pool[str(slot)] = {"status": "idle", "key": None, "type": None, "pid": None, "device": None, "start_time": 0}
293
+ _write_threads_to_disk(pool)
294
 
295
  # =========================================================
296
+ # CORE 3: GIÁM SÁT HẠN MỨC CHẶN CHỐNG BYPASS PHÁ MÁY CHỦ
297
  # =========================================================
298
 
299
  def check_generation_limits(key_input: str, device_id: str, is_vip: bool) -> tuple:
300
+ """Kiểm tra nghiêm ngặt hạn mức, không cho phép bỏ qua bước kiểm tra này"""
301
  data = _load_storage()
302
  today_str = datetime.now().strftime("%Y-%m-%d")
303
  now_ts = time.time()
304
 
305
  if is_vip:
306
+ # Nếu là VIP Key mua từ cổng thanh toán, giới hạn 5 batches / ngày
307
  if key_input not in data["keys"]:
308
  return False, "License error: Key missing from verified list."
309
 
 
321
  if vip_data.get("daily_batches", 0) >= 5:
322
  return False, "❌ You have exhausted your daily limit of 5 batches. Please return tomorrow!"
323
 
324
+ return True, "VIP limit checked successfully."
 
 
 
 
 
 
325
  else:
326
+ # Người dùng miễn phí: Khóa chặt 3 videos/ngày, cooldown 3 tiếng
327
  if device_id not in data["free_devices"]:
328
  data["free_devices"][device_id] = {
329
  "last_date": today_str,
 
338
  free_data["cooldown_until"] = 0
339
 
340
  if now_ts < free_data.get("cooldown_until", 0):
341
+ wait_hours = int((free_data["cooldown_until"] - now_ts) / 3600) + 1
342
+ return False, f"⏱️ Free Tier Cooldown: Please wait {wait_hours} hours before generating again, or upgrade to VIP Premium!"
343
 
344
  if free_data.get("daily_batches", 0) >= 3:
345
  return False, "❌ Free Tier Exhausted! Maximum 3 daily videos reached."
346
 
347
+ return True, "Free limit checked successfully."
 
 
 
 
 
 
348
 
349
  def commit_generation_success(key_input: str, device_id: str, is_vip: bool):
350
  data = _load_storage()
 
362
  if device_id in data["free_devices"]:
363
  free_data = data["free_devices"][device_id]
364
  free_data["daily_batches"] = free_data.get("daily_batches", 0) + 1
365
+ free_data["cooldown_until"] = now_ts + (3 * 3600) # Đóng băng 3 tiếng chuẩn chỉ
366
 
367
  _save_storage(data)
368
 
369
  # =========================================================
370
+ # CORE 4: KHÔI PHỤC TIẾN TRÌNH KHI USER LÀM MỚI TRANG TRÊN ĐIỆN THOẠI
371
  # =========================================================
372
 
373
+ def force_abort_user_session(token: str, device_id: str) -> bool:
374
+ """Hủy khẩn cấp tiến trình khi nhấn nút STOP trên giao diện"""
375
+ pool = _read_threads_from_disk()
376
+ token_str = token.strip()
377
+ released = False
378
+
379
+ for slot_str, thread in pool.items():
380
+ if thread["status"] == "rendering":
381
+ # Kiểm tra xem luồng này có thuộc về key hoặc thiết bị yêu cầu stop hay không
382
+ if (thread["key"] and thread["key"] == token_str) or (thread["device"] == device_id):
383
+ pid = thread["pid"]
384
+ logger.warning(f"Force stopping active worker PID {pid} at slot {slot_str}")
385
+ try:
386
+ if pid:
387
+ os.kill(pid, signal.SIGKILL)
388
+ except ProcessLookupError:
389
+ pass
390
+ pool[slot_str] = {"status": "idle", "key": None, "type": None, "pid": None, "device": None, "start_time": 0}
391
+ released = True
392
+
393
+ if released:
394
+ _write_threads_to_disk(pool)
395
+ return released
396
+
397
  def execute_admin_diagnostic_test() -> str:
 
398
  test_script = "tester.py"
399
  if not os.path.exists(test_script):
400
+ return f"❌ FILE MISSING: Tệp '{test_script}' không tồn tại!"
 
401
  try:
402
  result = subprocess.run(["python", test_script], capture_output=True, text=True, timeout=15)
403
  if result.returncode == 0:
404
  return f"✅ [TESTER REPORT SUCCESS]:\n{result.stdout.strip()}"
405
  else:
406
+ return f"❌ [TESTER REPORT CRASHED]:\n{result.stderr.strip()}"
407
  except subprocess.TimeoutExpired:
408
+ return "⚠️ [TESTER TIMEOUT]: Vượt ngưỡng 15 giây!"
409
  except Exception as e:
410
+ return f"❌ [SYSTEM CRASH]: Lỗi: {str(e)}"
411
+