Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import os
|
| 2 |
import re
|
|
|
|
| 3 |
import base64
|
| 4 |
import requests
|
| 5 |
from bs4 import BeautifulSoup
|
|
@@ -7,74 +8,113 @@ from fastapi import FastAPI, HTTPException, Request
|
|
| 7 |
from fastapi.responses import StreamingResponse
|
| 8 |
import uvicorn
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
app = FastAPI()
|
| 11 |
|
| 12 |
# --- Configurations ---
|
| 13 |
ACCESS_KEY = os.getenv("ACCESS_KEY", "786969")
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
def get_google_access_token():
|
| 19 |
-
"""
|
| 20 |
-
if not
|
|
|
|
| 21 |
return None
|
|
|
|
| 22 |
try:
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
return None
|
| 34 |
|
| 35 |
def get_direct_url(url):
|
| 36 |
if "drive.google.com" in url:
|
| 37 |
fid = re.search(r'/(?:d|file/d|open\?id=)/([a-zA-Z0-9_-]+)', url)
|
| 38 |
-
if fid:
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
return None, None
|
| 50 |
|
| 51 |
@app.get("/download")
|
| 52 |
async def download_proxy(request: Request, url: str, key: str = None):
|
|
|
|
| 53 |
if key != ACCESS_KEY:
|
| 54 |
raise HTTPException(status_code=403, detail="Access Denied")
|
| 55 |
|
| 56 |
target_link, token = get_direct_url(url)
|
| 57 |
if not target_link:
|
| 58 |
-
raise HTTPException(status_code=400, detail="Invalid Link")
|
| 59 |
|
|
|
|
| 60 |
headers = {}
|
| 61 |
range_header = request.headers.get('range')
|
| 62 |
if range_header:
|
| 63 |
headers['Range'] = range_header
|
| 64 |
|
| 65 |
-
# Token ရှိ
|
| 66 |
if token:
|
| 67 |
headers['Authorization'] = f"Bearer {token}"
|
| 68 |
|
| 69 |
session = requests.Session()
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
| 74 |
if "text/html" in r.headers.get("Content-Type", "").lower() and "google.com" in target_link:
|
| 75 |
-
|
| 76 |
-
raise HTTPException(status_code=401, detail="Google Authentication Failed. Please check your OAuth Credentials.")
|
| 77 |
|
|
|
|
| 78 |
excluded = ['content-encoding', 'content-length', 'transfer-encoding', 'connection']
|
| 79 |
response_headers = {n: v for n, v in r.headers.items() if n.lower() not in excluded}
|
| 80 |
response_headers['Accept-Ranges'] = 'bytes'
|
|
|
|
| 1 |
import os
|
| 2 |
import re
|
| 3 |
+
import json
|
| 4 |
import base64
|
| 5 |
import requests
|
| 6 |
from bs4 import BeautifulSoup
|
|
|
|
| 8 |
from fastapi.responses import StreamingResponse
|
| 9 |
import uvicorn
|
| 10 |
|
| 11 |
+
# Google Auth Libraries များကို Import လုပ်ခြင်း
|
| 12 |
+
from google.oauth2 import service_account
|
| 13 |
+
import google.auth.transport.requests
|
| 14 |
+
|
| 15 |
app = FastAPI()
|
| 16 |
|
| 17 |
# --- Configurations ---
|
| 18 |
ACCESS_KEY = os.getenv("ACCESS_KEY", "786969")
|
| 19 |
+
# Service Account ရဲ့ JSON ဖိုင်ထဲက စာသားအကုန်လုံးကို Copy ကူးပြီး Secret ထဲမှာ ထည့်ရပါမည်
|
| 20 |
+
SERVICE_ACCOUNT_JSON_STR = os.getenv("GOOGLE_SERVICE_ACCOUNT_JSON")
|
| 21 |
+
|
| 22 |
+
@app.get("/")
|
| 23 |
+
def index():
|
| 24 |
+
return {"message": "Proxy is Online!", "usage": "/download?url=[LINK]&key=[YOUR_KEY]"}
|
| 25 |
|
| 26 |
def get_google_access_token():
|
| 27 |
+
"""Service Account JSON ကို အသုံးပြု၍ Access Token ရယူခြင်း"""
|
| 28 |
+
if not SERVICE_ACCOUNT_JSON_STR:
|
| 29 |
+
print("Missing GOOGLE_SERVICE_ACCOUNT_JSON credential!")
|
| 30 |
return None
|
| 31 |
+
|
| 32 |
try:
|
| 33 |
+
# Secret ထဲမှ JSON string ကို Dictionary အဖြစ် ပြောင်းလဲခြင်း
|
| 34 |
+
creds_dict = json.loads(SERVICE_ACCOUNT_JSON_STR)
|
| 35 |
+
|
| 36 |
+
# Scopes သတ်မှတ်ခြင်း (Drive ကို ဖတ်ရန်)
|
| 37 |
+
scopes = ['https://www.googleapis.com/auth/drive.readonly']
|
| 38 |
+
|
| 39 |
+
# Service Account Credentials တည်ဆောက်ခြင်း
|
| 40 |
+
creds = service_account.Credentials.from_service_account_info(
|
| 41 |
+
creds_dict,
|
| 42 |
+
scopes=scopes
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
# Token အသစ်တောင်းခံခြင်း
|
| 46 |
+
auth_req = google.auth.transport.requests.Request()
|
| 47 |
+
creds.refresh(auth_req)
|
| 48 |
+
|
| 49 |
+
return creds.token
|
| 50 |
+
except Exception as e:
|
| 51 |
+
print(f"Service Account Auth Error: {e}")
|
| 52 |
return None
|
| 53 |
|
| 54 |
def get_direct_url(url):
|
| 55 |
if "drive.google.com" in url:
|
| 56 |
fid = re.search(r'/(?:d|file/d|open\?id=)/([a-zA-Z0-9_-]+)', url)
|
| 57 |
+
if not fid:
|
| 58 |
+
return None, None
|
| 59 |
+
file_id = fid.group(1)
|
| 60 |
+
|
| 61 |
+
access_token = get_google_access_token()
|
| 62 |
+
if access_token:
|
| 63 |
+
# Service Account မှ Token ရပါက API v3 Link ဖြင့် ပြန်ပေးမည်
|
| 64 |
+
return f"https://www.googleapis.com/drive/v3/files/{file_id}?alt=media", access_token
|
| 65 |
+
|
| 66 |
+
return None, None
|
| 67 |
+
|
| 68 |
+
elif "mediafire.com" in url:
|
| 69 |
+
try:
|
| 70 |
+
r = requests.get(url, headers={'User-Agent': 'Mozilla/5.0'}, timeout=10)
|
| 71 |
+
soup = BeautifulSoup(r.text, 'html.parser')
|
| 72 |
+
btn = soup.find('a', {'id': 'downloadButton'})
|
| 73 |
+
return btn.get('href') if btn else None, None
|
| 74 |
+
except:
|
| 75 |
+
return None, None
|
| 76 |
+
|
| 77 |
+
elif "dropbox.com" in url:
|
| 78 |
+
return url.replace("?dl=0", "").split("?")[0] + "?dl=1", None
|
| 79 |
+
|
| 80 |
+
elif "1drv.ms" in url or "onedrive.live.com" in url:
|
| 81 |
+
encoded_url = base64.b64encode(bytes(url, 'utf-8')).decode('utf-8').replace('=', '').replace('/', '_').replace('+', '-')
|
| 82 |
+
return f"https://api.onedrive.com/v1.0/shares/u!{encoded_url}/root/content", None
|
| 83 |
+
|
| 84 |
return None, None
|
| 85 |
|
| 86 |
@app.get("/download")
|
| 87 |
async def download_proxy(request: Request, url: str, key: str = None):
|
| 88 |
+
# Proxy Password စစ်ဆေးခြင်း
|
| 89 |
if key != ACCESS_KEY:
|
| 90 |
raise HTTPException(status_code=403, detail="Access Denied")
|
| 91 |
|
| 92 |
target_link, token = get_direct_url(url)
|
| 93 |
if not target_link:
|
| 94 |
+
raise HTTPException(status_code=400, detail="Invalid Link or Auth Failed")
|
| 95 |
|
| 96 |
+
# Header ပြင်ဆင်ခြင်း
|
| 97 |
headers = {}
|
| 98 |
range_header = request.headers.get('range')
|
| 99 |
if range_header:
|
| 100 |
headers['Range'] = range_header
|
| 101 |
|
| 102 |
+
# Token ရှိပါက Bearer Token ထည့်ပေးခြင်း
|
| 103 |
if token:
|
| 104 |
headers['Authorization'] = f"Bearer {token}"
|
| 105 |
|
| 106 |
session = requests.Session()
|
| 107 |
+
try:
|
| 108 |
+
r = session.get(target_link, headers=headers, stream=True, allow_redirects=True)
|
| 109 |
+
r.raise_for_status() # 403 / 404 ဖြစ်ပါက တန်းပြီး Error ဖမ်းမည်
|
| 110 |
+
except requests.exceptions.RequestException as e:
|
| 111 |
+
raise HTTPException(status_code=400, detail=f"Failed to fetch file: {str(e)}")
|
| 112 |
+
|
| 113 |
+
# Sign-in page သို့မဟုတ် HTML Error Page တက်လာပါက
|
| 114 |
if "text/html" in r.headers.get("Content-Type", "").lower() and "google.com" in target_link:
|
| 115 |
+
raise HTTPException(status_code=401, detail="Google Authentication Failed. Token may be invalid.")
|
|
|
|
| 116 |
|
| 117 |
+
# Header များကို ပြန်လည်ပေးပို့ရန် စစ်ထုတ်ခြင်း
|
| 118 |
excluded = ['content-encoding', 'content-length', 'transfer-encoding', 'connection']
|
| 119 |
response_headers = {n: v for n, v in r.headers.items() if n.lower() not in excluded}
|
| 120 |
response_headers['Accept-Ranges'] = 'bytes'
|