coollsd commited on
Commit
9f76556
·
verified ·
1 Parent(s): 7077ef0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -15
app.py CHANGED
@@ -1,5 +1,5 @@
1
- from fastapi import FastAPI, File, UploadFile, Request
2
- from fastapi.responses import HTMLResponse, JSONResponse, StreamingResponse
3
  import requests
4
  import time
5
  import asyncio
@@ -821,16 +821,12 @@ HTML_CONTENT = """
821
  }
822
 
823
  function encryptUrl(url) {
824
- const key = generateEncryptionKey();
 
825
  const encryptedData = CryptoJS.AES.encrypt(url, key).toString();
826
- const encryptedUrl = `${window.location.origin}/decrypt?data=${encodeURIComponent(encryptedData)}&key=${encodeURIComponent(key)}`;
827
  return encryptedUrl;
828
  }
829
-
830
- function generateEncryptionKey() {
831
- const browserInfo = navigator.userAgent + navigator.language + screen.width + screen.height + new Date().getTimezoneOffset();
832
- return CryptoJS.SHA256(browserInfo).toString();
833
- }
834
  </script>
835
  <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/crypto-js.min.js"></script>
836
  </body>
@@ -927,12 +923,15 @@ async def embed_video(url: str, thumbnail: str):
927
  return HTMLResponse(content=html)
928
 
929
  @app.get("/decrypt")
930
- async def decrypt_url(data: str, key: str):
931
  try:
932
- decrypted_url = Fernet(key.encode()).decrypt(data.encode()).decode()
 
 
 
933
  return RedirectResponse(url=decrypted_url)
934
- except:
935
- return HTMLResponse(content="Invalid or expired link", status_code=400)
936
 
937
  async def get_cookies() -> Dict[str, str]:
938
  try:
@@ -975,7 +974,7 @@ async def upload_file(upload_url: str, file_content: bytes, content_type: str) -
975
  return False
976
 
977
  async def retry_upload(upload_url: str, file_content: bytes, content_type: str, max_retries: int = 5, delay: int = 1) -> bool:
978
- while True:
979
  try:
980
  success = await upload_file(upload_url, file_content, content_type)
981
  if success:
@@ -985,7 +984,7 @@ async def retry_upload(upload_url: str, file_content: bytes, content_type: str,
985
  print(f"Error during upload: {e}")
986
 
987
  await asyncio.sleep(delay)
988
- delay = min(delay * 2, 60)
989
 
990
  return False
991
 
 
1
+ from fastapi import FastAPI, File, UploadFile, Request, HTTPException
2
+ from fastapi.responses import HTMLResponse, JSONResponse, StreamingResponse, RedirectResponse
3
  import requests
4
  import time
5
  import asyncio
 
821
  }
822
 
823
  function encryptUrl(url) {
824
+ const browserInfo = navigator.userAgent + navigator.language + screen.width + screen.height + new Date().getTimezoneOffset();
825
+ const key = CryptoJS.SHA256(browserInfo).toString();
826
  const encryptedData = CryptoJS.AES.encrypt(url, key).toString();
827
+ const encryptedUrl = `${window.location.origin}/decrypt?data=${encodeURIComponent(encryptedData)}`;
828
  return encryptedUrl;
829
  }
 
 
 
 
 
830
  </script>
831
  <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/crypto-js.min.js"></script>
832
  </body>
 
923
  return HTMLResponse(content=html)
924
 
925
  @app.get("/decrypt")
926
+ async def decrypt_url(data: str, request: Request):
927
  try:
928
+ browser_info = request.headers.get('User-Agent', '') + request.headers.get('Accept-Language', '') + str(request.client.host)
929
+ key = hashlib.sha256(browser_info.encode()).hexdigest()
930
+ fernet = Fernet(base64.urlsafe_b64encode(key[:32].encode()))
931
+ decrypted_url = fernet.decrypt(data.encode()).decode()
932
  return RedirectResponse(url=decrypted_url)
933
+ except Exception as e:
934
+ raise HTTPException(status_code=400, detail="Invalid or expired link")
935
 
936
  async def get_cookies() -> Dict[str, str]:
937
  try:
 
974
  return False
975
 
976
  async def retry_upload(upload_url: str, file_content: bytes, content_type: str, max_retries: int = 5, delay: int = 1) -> bool:
977
+ for _ in range(max_retries):
978
  try:
979
  success = await upload_file(upload_url, file_content, content_type)
980
  if success:
 
984
  print(f"Error during upload: {e}")
985
 
986
  await asyncio.sleep(delay)
987
+ delay = min(delay * 2, 60)
988
 
989
  return False
990