|
3 | 3 | import json |
4 | 4 | import uuid |
5 | 5 | import time |
6 | | -import shutil |
| 6 | +import threading |
7 | 7 | import logging |
8 | | -import zipfile |
9 | 8 | from main import get_symbol_address, patch_address, copy_file_to_src, zip_src_files |
10 | 9 |
|
11 | 10 | app = Flask(__name__) |
@@ -44,21 +43,8 @@ def format(self, record): |
44 | 43 | handler.setFormatter(ColoredFormatter('%(asctime)s - %(levelname)s - %(message)s')) |
45 | 44 |
|
46 | 45 | def save_patch_info(permalink_id, file_path): |
47 | | - patch_info = { |
48 | | - 'permalink_id': permalink_id, |
49 | | - 'file_path': file_path, |
50 | | - 'timestamp': time.time() |
51 | | - } |
52 | | - if os.path.exists(PATCHES_JSON): |
53 | | - with open(PATCHES_JSON, 'r') as f: |
54 | | - patches = json.load(f) |
55 | | - else: |
56 | | - patches = [] |
57 | | - |
58 | | - patches.append(patch_info) |
59 | | - |
60 | | - with open(PATCHES_JSON, 'w') as f: |
61 | | - json.dump(patches, f, indent=4) |
| 46 | + # Dummy function to maintain logs |
| 47 | + logger.info(f"Patch info saved for permalink_id: {permalink_id}, file_path: {file_path}") |
62 | 48 |
|
63 | 49 | @app.route('/') |
64 | 50 | def index(): |
@@ -282,30 +268,22 @@ def patch(): |
282 | 268 | logger.error(f"Error patching file: {str(e)}") |
283 | 269 | return jsonify({"error": f"Error patching file: {str(e)}"}), 500 |
284 | 270 |
|
285 | | - # Create a zip file containing the patched .so |
286 | | - try: |
287 | | - zip_filename = f"patched_{library_name}.zip" |
288 | | - zip_path = os.path.join('uploads', zip_filename) |
289 | | - with zipfile.ZipFile(zip_path, 'w') as zipf: |
290 | | - zipf.write(file_path, arcname=library_name) |
291 | | - except Exception as e: |
292 | | - logger.error(f"Error creating zip file: {str(e)}") |
293 | | - return jsonify({"error": f"Error creating zip file: {str(e)}"}), 500 |
| 271 | + # Create permalink |
| 272 | + permalink_id = str(uuid.uuid4()) |
| 273 | + PATCHED_LIBRARIES[permalink_id] = { |
| 274 | + 'file_path': file_path, |
| 275 | + 'library_name': library_name, |
| 276 | + 'timestamp': time.time() |
| 277 | + } |
294 | 278 |
|
295 | | - # Send the zip file |
296 | | - try: |
297 | | - return send_file( |
298 | | - zip_path, |
299 | | - mimetype='application/zip', |
300 | | - as_attachment=True, |
301 | | - download_name=zip_filename |
302 | | - ) |
303 | | - except Exception as e: |
304 | | - logger.error(f"Error sending zip file: {str(e)}") |
305 | | - return jsonify({"error": f"Error sending zip file: {str(e)}"}), 500 |
306 | | - finally: |
307 | | - os.remove(file_path) |
308 | | - os.remove(zip_path) |
| 279 | + # Save patch info |
| 280 | + save_patch_info(permalink_id, file_path) |
| 281 | + |
| 282 | + # Schedule deletion |
| 283 | + threading.Timer(PERMALINK_EXPIRY, delete_expired_permalink, args=[permalink_id]).start() |
| 284 | + logger.info(f"Permalink {permalink_id} created, will expire in {PERMALINK_EXPIRY} seconds") |
| 285 | + |
| 286 | + return jsonify({'permalink': f'/download/{permalink_id}'}) |
309 | 287 |
|
310 | 288 | @app.route('/api', methods=['POST']) |
311 | 289 | def api(): |
@@ -336,30 +314,38 @@ def api(): |
336 | 314 | logger.error(f"Error patching file: {str(e)}") |
337 | 315 | return jsonify({"error": f"Error patching file: {str(e)}"}), 500 |
338 | 316 |
|
339 | | - # Send the patched .so file directly |
340 | | - patched_filename = f"patched_{library_name}" |
341 | | - patched_file_path = os.path.join('uploads', patched_filename) |
342 | | - shutil.copy(file_path, patched_file_path) |
| 317 | + # Log patch info |
| 318 | + permalink_id = str(uuid.uuid4()) |
| 319 | + save_patch_info(permalink_id, file_path) |
| 320 | + |
| 321 | + # Return the patched file directly |
| 322 | + try: |
| 323 | + return send_file(file_path, as_attachment=True, attachment_filename=library_name) |
| 324 | + except Exception as e: |
| 325 | + logger.error(f"Error sending file: {str(e)}") |
| 326 | + return jsonify({"error": f"Error sending file: {str(e)}"}), 500 |
| 327 | + |
| 328 | +@app.route('/download/<permalink_id>', methods=['GET']) |
| 329 | +def download(permalink_id): |
| 330 | + if permalink_id not in PATCHED_LIBRARIES: |
| 331 | + return "Permalink expired or invalid", 404 |
| 332 | + |
| 333 | + file_path = PATCHED_LIBRARIES[permalink_id]['file_path'] |
| 334 | + library_name = PATCHED_LIBRARIES[permalink_id]['library_name'] |
| 335 | + if not os.path.exists(file_path): |
| 336 | + return "File not found", 404 |
343 | 337 |
|
344 | 338 | try: |
345 | | - resp = make_response(send_file( |
346 | | - patched_file_path, |
347 | | - mimetype='application/octet-stream', |
348 | | - as_attachment=True, |
349 | | - download_name=patched_filename |
350 | | - )) |
351 | | - os.remove(file_path) |
352 | | - os.remove(patched_file_path) |
353 | | - return resp |
354 | | - |
| 339 | + copy_file_to_src(file_path, library_name) |
| 340 | + zip_src_files() |
355 | 341 | except Exception as e: |
356 | | - logger.error(f"Error sending patched file: {str(e)}") |
357 | | - |
358 | | - os.remove(file_path) |
359 | | - os.remove(patched_file_path) |
360 | | - |
361 | | - return jsonify({"error": f"Error sending patched file: {str(e)}"}), 500 |
362 | | - |
| 342 | + logger.error(f"Error preparing download: {str(e)}") |
| 343 | + return f"Error preparing download: {str(e)}", 500 |
| 344 | + |
| 345 | + resp = make_response(send_file('btl2capfix.zip', as_attachment=True)) |
| 346 | + resp.headers['Content-Disposition'] = f'attachment; filename=btl2capfix.zip' |
| 347 | + return resp |
| 348 | + |
363 | 349 | def delete_expired_permalink(permalink_id): |
364 | 350 | if permalink_id in PATCHED_LIBRARIES: |
365 | 351 | file_path = PATCHED_LIBRARIES[permalink_id]['file_path'] |
|
0 commit comments