coollsd commited on
Commit
c0e61b6
·
verified ·
1 Parent(s): 885522c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -62
app.py CHANGED
@@ -1,14 +1,23 @@
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
6
  from typing import Dict
7
  import base64
8
  import secrets
 
 
9
 
10
  app = FastAPI()
11
 
 
 
 
 
 
 
 
12
  HTML_CONTENT = """
13
  <!DOCTYPE html>
14
  <html lang="en">
@@ -866,54 +875,30 @@ async def handle_video_stream(path: str, request: Request):
866
 
867
  return StreamingResponse(generate(), status_code=response.status_code, headers=headers)
868
 
869
- @app.get("/embed")
870
- async def embed_video(url: str, thumbnail: str):
871
- html = f'''
872
- <html>
873
- <head>
874
- <meta property="og:type" content="video.other">
875
- <meta property="og:video" content="{url}">
876
- <meta property="og:video:url" content="{url}">
877
- <meta property="og:video:secure_url" content="{url}">
878
- <meta property="og:video:type" content="video/mp4">
879
- <meta property="og:video:width" content="1280">
880
- <meta property="og:video:height" content="720">
881
- <meta property="og:image" content="{thumbnail}">
882
- <meta property="og:image:secure_url" content="{thumbnail}">
883
- <meta property="og:image:width" content="1280">
884
- <meta property="og:image:height" content="720">
885
- <meta property="og:image:type" content="image/png">
886
- <style>
887
- body, html {{ margin: 0; padding: 0; height: 100%; background: #000; }}
888
- #thumbnail {{ width: 100%; height: 100%; object-fit: contain; cursor: pointer; }}
889
- #video {{ display: none; width: 100%; height: 100%; object-fit: contain; }}
890
- </style>
891
- </head>
892
- <body>
893
- <img id="thumbnail" src="{thumbnail}" onclick="playVideo()">
894
- <video id="video" controls autoplay>
895
- <source src="{url}" type="video/mp4">
896
- Your browser does not support the video tag.
897
- </video>
898
- <script>
899
- function playVideo() {{
900
- document.getElementById('thumbnail').style.display = 'none';
901
- document.getElementById('video').style.display = 'block';
902
- document.getElementById('video').play();
903
- }}
904
- </script>
905
- </body>
906
- </html>
907
- '''
908
- return HTMLResponse(content=html)
909
 
910
  @app.get("/encrypted/{encrypted_url}")
911
  async def handle_encrypted_url(encrypted_url: str, request: Request):
912
- decrypted_url = decrypt_url(encrypted_url)
913
- if '/pbxt/' not in decrypted_url:
914
- return JSONResponse(content={"error": "Invalid encrypted URL"}, status_code=400)
915
- path = decrypted_url.split('/pbxt/')[1]
916
- return await handle_video_stream(path, request)
 
 
 
 
 
 
 
 
 
917
 
918
  async def get_cookies() -> Dict[str, str]:
919
  try:
@@ -970,20 +955,6 @@ async def retry_upload(upload_url: str, file_content: bytes, content_type: str,
970
 
971
  return False
972
 
973
- # Generate a secure secret key
974
- SECRET_KEY = secrets.token_urlsafe(32)
975
-
976
- def encrypt_url(url: str) -> str:
977
- encoded = base64.urlsafe_b64encode(url.encode()).decode()
978
- return encoded[::-1] # Simple reversal for obfuscation
979
-
980
- def decrypt_url(encrypted_url: str) -> str:
981
- try:
982
- decoded = base64.urlsafe_b64decode(encrypted_url[::-1]).decode()
983
- return decoded
984
- except:
985
- return ""
986
-
987
  if __name__ == "__main__":
988
  import uvicorn
989
- uvicorn.run(app, host="0.0.0.0", port=7860)
 
1
+ from fastapi import FastAPI, Request, HTTPException
2
+ from fastapi.responses import JSONResponse, StreamingResponse
3
  import requests
4
  import time
5
  import asyncio
6
  from typing import Dict
7
  import base64
8
  import secrets
9
+ from cryptography.fernet import Fernet
10
+ from starlette.middleware.sessions import SessionMiddleware
11
 
12
  app = FastAPI()
13
 
14
+ # Add SessionMiddleware to the application
15
+ app.add_middleware(SessionMiddleware, secret_key=secrets.token_urlsafe(32))
16
+
17
+ # Generate a secret key for Fernet encryption
18
+ FERNET_KEY = Fernet.generate_key()
19
+ fernet = Fernet(FERNET_KEY)
20
+
21
  HTML_CONTENT = """
22
  <!DOCTYPE html>
23
  <html lang="en">
 
875
 
876
  return StreamingResponse(generate(), status_code=response.status_code, headers=headers)
877
 
878
+ @app.get("/encrypt")
879
+ async def encrypt_url(request: Request, url: str):
880
+ session = request.session
881
+ session_id = secrets.token_urlsafe(16)
882
+ session[session_id] = url
883
+ encrypted_url = fernet.encrypt(f"{session_id}:{url}".encode()).decode()
884
+ return JSONResponse(content={"encrypted_url": f"/encrypted/{encrypted_url}"})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
885
 
886
  @app.get("/encrypted/{encrypted_url}")
887
  async def handle_encrypted_url(encrypted_url: str, request: Request):
888
+ try:
889
+ decrypted = fernet.decrypt(encrypted_url.encode()).decode()
890
+ session_id, original_url = decrypted.split(':', 1)
891
+
892
+ if session_id not in request.session or request.session[session_id] != original_url:
893
+ raise HTTPException(status_code=403, detail="Invalid or expired encrypted URL")
894
+
895
+ if '/pbxt/' not in original_url:
896
+ raise HTTPException(status_code=400, detail="Invalid URL format")
897
+
898
+ path = original_url.split('/pbxt/')[1]
899
+ return await handle_video_stream(path, request)
900
+ except Exception as e:
901
+ raise HTTPException(status_code=400, detail="Invalid encrypted URL")
902
 
903
  async def get_cookies() -> Dict[str, str]:
904
  try:
 
955
 
956
  return False
957
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
958
  if __name__ == "__main__":
959
  import uvicorn
960
+ uvicorn.run(app, host="0.0.0.0", port=8000)