Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
|
@@ -10,103 +10,126 @@ import uvicorn
|
|
| 10 |
app = FastAPI()
|
| 11 |
|
| 12 |
# --- Configurations ---
|
| 13 |
-
ACCESS_KEY = os.getenv("ACCESS_KEY", "786969")
|
| 14 |
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY", "")
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
@app.get("/")
|
| 17 |
def index():
|
| 18 |
return {"message": "Proxy is Online!", "usage": "/download?url=[LINK]&key=[YOUR_KEY]"}
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
def get_direct_url(url):
|
|
|
|
|
|
|
|
|
|
| 21 |
if "drive.google.com" in url:
|
| 22 |
fid = re.search(r'/(?:d|file/d|open\?id=)/([a-zA-Z0-9_-]+)', url)
|
| 23 |
if fid:
|
| 24 |
file_id = fid.group(1)
|
| 25 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
if GOOGLE_API_KEY and GOOGLE_API_KEY != "YOUR_GOOGLE_API_KEY_HERE":
|
| 27 |
-
return f"https://www.googleapis.com/drive/v3/files/{file_id}?alt=media&key={GOOGLE_API_KEY}"
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
elif "mediafire.com" in url:
|
| 34 |
try:
|
| 35 |
r = requests.get(url, headers={'User-Agent': 'Mozilla/5.0'}, timeout=10)
|
| 36 |
soup = BeautifulSoup(r.text, 'html.parser')
|
| 37 |
btn = soup.find('a', {'id': 'downloadButton'})
|
| 38 |
-
return btn.get('href') if btn else None
|
| 39 |
-
except:
|
| 40 |
-
return None
|
| 41 |
|
| 42 |
elif "dropbox.com" in url:
|
| 43 |
-
return url.replace("?dl=0", "").split("?")[0] + "?dl=1"
|
| 44 |
|
| 45 |
elif "1drv.ms" in url or "onedrive.live.com" in url:
|
| 46 |
encoded_url = base64.b64encode(bytes(url, 'utf-8')).decode('utf-8').replace('=', '').replace('/', '_').replace('+', '-')
|
| 47 |
-
return f"https://api.onedrive.com/v1.0/shares/u!{encoded_url}/root/content"
|
| 48 |
|
| 49 |
-
return 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 |
-
|
|
|
|
| 57 |
if not target_link:
|
| 58 |
raise HTTPException(status_code=400, detail="Invalid Link")
|
| 59 |
|
| 60 |
-
#
|
| 61 |
-
range_header = request.headers.get('range')
|
| 62 |
headers = {}
|
|
|
|
| 63 |
if range_header:
|
| 64 |
headers['Range'] = range_header
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
-
# Session ���သုံးပြုခြင်းဖြင့် GDrive ၏ Cookie များကို သိမ်းဆည်းနိုင်သည်
|
| 67 |
session = requests.Session()
|
| 68 |
-
|
| 69 |
-
# Target Server ဆီကို Range Header နဲ့ လှမ်းတောင်းခြင်း
|
| 70 |
r = session.get(target_link, headers=headers, stream=True, allow_redirects=True)
|
| 71 |
|
| 72 |
# ---------------------------------------------------------
|
| 73 |
-
# GDrive
|
| 74 |
# ---------------------------------------------------------
|
| 75 |
-
if "drive.google.com" in url and
|
| 76 |
-
# Content-Type သည် HTML ဖြစ်နေမှသာ (Virus Warning Page ဖြစ်နိုင်ခြေရှိမှသာ) r.text ကို ဖတ်မည်
|
| 77 |
content_type = r.headers.get('Content-Type', '').lower()
|
| 78 |
if 'text/html' in content_type:
|
| 79 |
-
|
| 80 |
-
if "Google Drive - Virus scan warning" in
|
| 81 |
confirm_token = None
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
confirm_token = cookie_value
|
| 86 |
break
|
| 87 |
-
|
| 88 |
if confirm_token:
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
r = session.get(new_target_link, headers=headers, stream=True, allow_redirects=True)
|
| 92 |
|
| 93 |
-
# Header များကို ပြန်
|
| 94 |
-
|
| 95 |
-
response_headers = {
|
| 96 |
-
|
| 97 |
-
# Video ကျော်ကြည့်နိုင်ရန် Accept-Ranges နှင့် Content-Range ထည့်ပေးရပါမည်
|
| 98 |
response_headers['Accept-Ranges'] = 'bytes'
|
| 99 |
|
| 100 |
if 'Content-Length' in r.headers:
|
| 101 |
response_headers['Content-Length'] = r.headers['Content-Length']
|
| 102 |
-
|
| 103 |
if 'Content-Range' in r.headers:
|
| 104 |
response_headers['Content-Range'] = r.headers['Content-Range']
|
| 105 |
|
| 106 |
def iterfile():
|
| 107 |
-
for chunk in r.iter_content(chunk_size=1024*1024):
|
| 108 |
-
if chunk:
|
| 109 |
-
yield chunk
|
| 110 |
|
| 111 |
return StreamingResponse(
|
| 112 |
iterfile(),
|
|
|
|
| 10 |
app = FastAPI()
|
| 11 |
|
| 12 |
# --- Configurations ---
|
| 13 |
+
ACCESS_KEY = os.getenv("ACCESS_KEY", "786969")
|
| 14 |
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY", "")
|
| 15 |
|
| 16 |
+
# OAuth Credentials (Hugging Face Secrets ထဲမှာ ထည့်ထားပေးပါ)
|
| 17 |
+
CLIENT_ID = os.getenv("GOOGLE_CLIENT_ID")
|
| 18 |
+
CLIENT_SECRET = os.getenv("GOOGLE_CLIENT_SECRET")
|
| 19 |
+
REFRESH_TOKEN = os.getenv("GOOGLE_REFRESH_TOKEN")
|
| 20 |
+
|
| 21 |
@app.get("/")
|
| 22 |
def index():
|
| 23 |
return {"message": "Proxy is Online!", "usage": "/download?url=[LINK]&key=[YOUR_KEY]"}
|
| 24 |
|
| 25 |
+
def get_google_access_token():
|
| 26 |
+
"""Refresh Token ကိုသုံးပြီး Access Token အသစ်တောင်းယူခြင်း"""
|
| 27 |
+
if not (CLIENT_ID and CLIENT_SECRET and REFRESH_TOKEN):
|
| 28 |
+
return None
|
| 29 |
+
try:
|
| 30 |
+
url = "https://oauth2.googleapis.com/token"
|
| 31 |
+
data = {
|
| 32 |
+
"client_id": CLIENT_ID,
|
| 33 |
+
"client_secret": CLIENT_SECRET,
|
| 34 |
+
"refresh_token": REFRESH_TOKEN,
|
| 35 |
+
"grant_type": "refresh_token",
|
| 36 |
+
}
|
| 37 |
+
r = requests.post(url, data=data, timeout=10)
|
| 38 |
+
return r.json().get("access_token")
|
| 39 |
+
except:
|
| 40 |
+
return None
|
| 41 |
+
|
| 42 |
def get_direct_url(url):
|
| 43 |
+
"""Link အမျိုးအစားအလိုက် Direct Link နှင့် လိုအပ်ပါက Bearer Token ပြန်ပေးခြင်း"""
|
| 44 |
+
token = None
|
| 45 |
+
|
| 46 |
if "drive.google.com" in url:
|
| 47 |
fid = re.search(r'/(?:d|file/d|open\?id=)/([a-zA-Z0-9_-]+)', url)
|
| 48 |
if fid:
|
| 49 |
file_id = fid.group(1)
|
| 50 |
+
# OAuth သုံးနိုင်လျှင် (Private Files အတွက် အကောင်းဆုံး)
|
| 51 |
+
access_token = get_google_access_token()
|
| 52 |
+
if access_token:
|
| 53 |
+
return f"https://www.googleapis.com/drive/v3/files/{file_id}?alt=media", access_token
|
| 54 |
+
|
| 55 |
+
# API Key ရှိလျှင် (Public Files အတွက်)
|
| 56 |
if GOOGLE_API_KEY and GOOGLE_API_KEY != "YOUR_GOOGLE_API_KEY_HERE":
|
| 57 |
+
return f"https://www.googleapis.com/drive/v3/files/{file_id}?alt=media&key={GOOGLE_API_KEY}", None
|
| 58 |
+
|
| 59 |
+
# API Key မရှိလျှင် ရိုးရိုး Direct Link
|
| 60 |
+
return f"https://drive.google.com/uc?export=download&id={file_id}", None
|
| 61 |
+
|
|
|
|
| 62 |
elif "mediafire.com" in url:
|
| 63 |
try:
|
| 64 |
r = requests.get(url, headers={'User-Agent': 'Mozilla/5.0'}, timeout=10)
|
| 65 |
soup = BeautifulSoup(r.text, 'html.parser')
|
| 66 |
btn = soup.find('a', {'id': 'downloadButton'})
|
| 67 |
+
return btn.get('href') if btn else None, None
|
| 68 |
+
except: return None, None
|
|
|
|
| 69 |
|
| 70 |
elif "dropbox.com" in url:
|
| 71 |
+
return url.replace("?dl=0", "").split("?")[0] + "?dl=1", None
|
| 72 |
|
| 73 |
elif "1drv.ms" in url or "onedrive.live.com" in url:
|
| 74 |
encoded_url = base64.b64encode(bytes(url, 'utf-8')).decode('utf-8').replace('=', '').replace('/', '_').replace('+', '-')
|
| 75 |
+
return f"https://api.onedrive.com/v1.0/shares/u!{encoded_url}/root/content", None
|
| 76 |
|
| 77 |
+
return None, None
|
| 78 |
|
| 79 |
@app.get("/download")
|
| 80 |
async def download_proxy(request: Request, url: str, key: str = None):
|
| 81 |
+
# Proxy Password စစ်ဆေးခြင်း
|
| 82 |
if key != ACCESS_KEY:
|
| 83 |
raise HTTPException(status_code=403, detail="Access Denied")
|
| 84 |
|
| 85 |
+
# Direct Link နှင့် Token ရယူခြင်း
|
| 86 |
+
target_link, token = get_direct_url(url)
|
| 87 |
if not target_link:
|
| 88 |
raise HTTPException(status_code=400, detail="Invalid Link")
|
| 89 |
|
| 90 |
+
# Header များ ပြင်ဆင်ခြင်း
|
|
|
|
| 91 |
headers = {}
|
| 92 |
+
range_header = request.headers.get('range')
|
| 93 |
if range_header:
|
| 94 |
headers['Range'] = range_header
|
| 95 |
+
|
| 96 |
+
# OAuth Token ရှိလျှင် Authorization Header ထည့်မည်
|
| 97 |
+
if token:
|
| 98 |
+
headers['Authorization'] = f"Bearer {token}"
|
| 99 |
|
|
|
|
| 100 |
session = requests.Session()
|
|
|
|
|
|
|
| 101 |
r = session.get(target_link, headers=headers, stream=True, allow_redirects=True)
|
| 102 |
|
| 103 |
# ---------------------------------------------------------
|
| 104 |
+
# GDrive Virus Scan Bypass (OAuth မသုံးထားသော Public Links များအတွက်)
|
| 105 |
# ---------------------------------------------------------
|
| 106 |
+
if "drive.google.com" in url and not token:
|
|
|
|
| 107 |
content_type = r.headers.get('Content-Type', '').lower()
|
| 108 |
if 'text/html' in content_type:
|
| 109 |
+
# HTML ဖြစ်မှသာ r.text ကိုဖတ်မည် (RAM မပြည့်စေရန်)
|
| 110 |
+
if "Google Drive - Virus scan warning" in r.text or "confirm=" not in target_link:
|
| 111 |
confirm_token = None
|
| 112 |
+
for c_key, c_val in session.cookies.items():
|
| 113 |
+
if c_key.startswith("download_warning"):
|
| 114 |
+
confirm_token = c_val
|
|
|
|
| 115 |
break
|
|
|
|
| 116 |
if confirm_token:
|
| 117 |
+
new_link = f"{target_link}&confirm={confirm_token}"
|
| 118 |
+
r = session.get(new_link, headers=headers, stream=True, allow_redirects=True)
|
|
|
|
| 119 |
|
| 120 |
+
# Header များကို Proxy လုပ်၍ ပြန်ပို့ခြင်း
|
| 121 |
+
excluded = ['content-encoding', 'content-length', 'transfer-encoding', 'connection']
|
| 122 |
+
response_headers = {n: v for n, v in r.headers.items() if n.lower() not in excluded}
|
|
|
|
|
|
|
| 123 |
response_headers['Accept-Ranges'] = 'bytes'
|
| 124 |
|
| 125 |
if 'Content-Length' in r.headers:
|
| 126 |
response_headers['Content-Length'] = r.headers['Content-Length']
|
|
|
|
| 127 |
if 'Content-Range' in r.headers:
|
| 128 |
response_headers['Content-Range'] = r.headers['Content-Range']
|
| 129 |
|
| 130 |
def iterfile():
|
| 131 |
+
for chunk in r.iter_content(chunk_size=1024*1024):
|
| 132 |
+
if chunk: yield chunk
|
|
|
|
| 133 |
|
| 134 |
return StreamingResponse(
|
| 135 |
iterfile(),
|