Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,9 @@
|
|
| 1 |
-
from flask import Flask, request,
|
| 2 |
-
import requests
|
| 3 |
import base64
|
| 4 |
|
| 5 |
app = Flask(__name__)
|
| 6 |
|
| 7 |
-
#
|
| 8 |
FRONTEND_HTML = """
|
| 9 |
<!DOCTYPE html>
|
| 10 |
<html lang="he" dir="rtl">
|
|
@@ -19,40 +18,16 @@ FRONTEND_HTML = """
|
|
| 19 |
input[type="url"] { width: 80%; padding: 10px; border: 1px solid #ddd; border-radius: 4px; font-size: 16px; }
|
| 20 |
button { padding: 10px 20px; border: none; background-color: #007bff; color: white; font-size: 16px; border-radius: 4px; cursor: pointer; margin-right: 10px;}
|
| 21 |
button:hover { background-color: #0056b3; }
|
| 22 |
-
#content-frame { width: 100%; height: 60vh; margin-top: 20px; border: 1px solid #ddd; }
|
| 23 |
</style>
|
| 24 |
</head>
|
| 25 |
<body>
|
| 26 |
<div class="container">
|
| 27 |
<h1>פרוקסי אישי</h1>
|
| 28 |
-
<form id="proxy-form"
|
| 29 |
-
<input type="url" id="url-input" name="
|
| 30 |
<button type="submit">טען</button>
|
| 31 |
</form>
|
| 32 |
-
<iframe id="content-frame" name="content-frame" sandbox="allow-forms allow-modals allow-pointer-lock allow-popups allow-same-origin allow-scripts" referrerpolicy="no-referrer"></iframe>
|
| 33 |
</div>
|
| 34 |
-
<script>
|
| 35 |
-
// שינוי קטן כדי שיהיה יותר פשוט - נשלח את ה-URL גלוי לשרת שלנו,
|
| 36 |
-
// והוא כבר יטפל בהכל. זה מפשט את הקוד.
|
| 37 |
-
document.getElementById('proxy-form').addEventListener('submit', function(event) {
|
| 38 |
-
const form = event.target;
|
| 39 |
-
const urlInput = document.getElementById('url-input');
|
| 40 |
-
|
| 41 |
-
// Create a hidden input for the base64 encoded URL
|
| 42 |
-
let hiddenInput = document.getElementById('url-encoded');
|
| 43 |
-
if (!hiddenInput) {
|
| 44 |
-
hiddenInput = document.createElement('input');
|
| 45 |
-
hiddenInput.type = 'hidden';
|
| 46 |
-
hiddenInput.name = 'url';
|
| 47 |
-
hiddenInput.id = 'url-encoded';
|
| 48 |
-
form.appendChild(hiddenInput);
|
| 49 |
-
}
|
| 50 |
-
// Encode the URL to Base64 and set it to the hidden input
|
| 51 |
-
hiddenInput.value = btoa(urlInput.value);
|
| 52 |
-
// Clear the plain URL so it's not sent
|
| 53 |
-
urlInput.name = '';
|
| 54 |
-
});
|
| 55 |
-
</script>
|
| 56 |
</body>
|
| 57 |
</html>
|
| 58 |
"""
|
|
@@ -60,37 +35,25 @@ FRONTEND_HTML = """
|
|
| 60 |
@app.route('/')
|
| 61 |
def home():
|
| 62 |
"""מגיש את דף הפרונטאנד."""
|
| 63 |
-
return FRONTEND_HTML
|
| 64 |
|
| 65 |
@app.route('/proxy')
|
| 66 |
def proxy():
|
| 67 |
-
"""
|
| 68 |
-
|
| 69 |
-
if not
|
| 70 |
return "שגיאה: פרמטר URL חסר.", 400
|
| 71 |
|
| 72 |
try:
|
| 73 |
-
# שלב 1:
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
# שלב 2: בניית ה-URL של גוגל תרגום
|
| 78 |
-
# אנחנו "מתרגמים" מאנגלית לאנגלית (sl=en, tl=en)
|
| 79 |
google_translate_url = f"https://translate.google.com/translate?sl=auto&tl=en&u={original_url}"
|
| 80 |
|
| 81 |
-
# שלב 3:
|
| 82 |
-
|
| 83 |
-
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
|
| 84 |
-
}
|
| 85 |
-
resp = requests.get(google_translate_url, headers=headers, stream=True, timeout=20)
|
| 86 |
-
|
| 87 |
-
# שלב 4: החזרת התוכן למשתמש
|
| 88 |
-
excluded_headers = ['content-encoding', 'content-length', 'transfer-encoding', 'connection']
|
| 89 |
-
response_headers = [(name, value) for (name, value) in resp.raw.headers.items() if name.lower() not in excluded_headers]
|
| 90 |
-
|
| 91 |
-
return Response(resp.content, resp.status_code, response_headers)
|
| 92 |
|
| 93 |
-
except
|
| 94 |
-
return f"שגיאה
|
| 95 |
-
except requests.exceptions.RequestException as e:
|
| 96 |
-
return f"שגיאה בגישה לכתובת דרך המתווך: {e}", 500
|
|
|
|
| 1 |
+
from flask import Flask, request, redirect, render_template_string
|
|
|
|
| 2 |
import base64
|
| 3 |
|
| 4 |
app = Flask(__name__)
|
| 5 |
|
| 6 |
+
# פרונטאנד פשוט יותר - רק טופס, בלי אייפרעים או ג'אווהסקריפט מורכב
|
| 7 |
FRONTEND_HTML = """
|
| 8 |
<!DOCTYPE html>
|
| 9 |
<html lang="he" dir="rtl">
|
|
|
|
| 18 |
input[type="url"] { width: 80%; padding: 10px; border: 1px solid #ddd; border-radius: 4px; font-size: 16px; }
|
| 19 |
button { padding: 10px 20px; border: none; background-color: #007bff; color: white; font-size: 16px; border-radius: 4px; cursor: pointer; margin-right: 10px;}
|
| 20 |
button:hover { background-color: #0056b3; }
|
|
|
|
| 21 |
</style>
|
| 22 |
</head>
|
| 23 |
<body>
|
| 24 |
<div class="container">
|
| 25 |
<h1>פרוקסי אישי</h1>
|
| 26 |
+
<form id="proxy-form" action="/proxy" method="GET">
|
| 27 |
+
<input type="url" id="url-input" name="url" placeholder="הכנס כתובת אתר..." required>
|
| 28 |
<button type="submit">טען</button>
|
| 29 |
</form>
|
|
|
|
| 30 |
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
</body>
|
| 32 |
</html>
|
| 33 |
"""
|
|
|
|
| 35 |
@app.route('/')
|
| 36 |
def home():
|
| 37 |
"""מגיש את דף הפרונטאנד."""
|
| 38 |
+
return render_template_string(FRONTEND_HTML)
|
| 39 |
|
| 40 |
@app.route('/proxy')
|
| 41 |
def proxy():
|
| 42 |
+
"""מקבל את ה-URL, מקודד אותו, ומפנה לגוגל תרגום."""
|
| 43 |
+
original_url = request.args.get('url')
|
| 44 |
+
if not original_url:
|
| 45 |
return "שגיאה: פרמטר URL חסר.", 400
|
| 46 |
|
| 47 |
try:
|
| 48 |
+
# שלב 1: קידוד ה-URL ל-Base64 כדי להסתיר אותו מנטפרי בבקשה הראשונית
|
| 49 |
+
# (זה בעצם כבר לא הכרחי בגישה הזו, אבל נשאיר את זה כהגנה נוספת)
|
| 50 |
+
encoded_url_for_google = base64.urlsafe_b64encode(original_url.encode()).decode()
|
| 51 |
+
|
| 52 |
# שלב 2: בניית ה-URL של גוגל תרגום
|
|
|
|
| 53 |
google_translate_url = f"https://translate.google.com/translate?sl=auto&tl=en&u={original_url}"
|
| 54 |
|
| 55 |
+
# שלב 3: הפניה מחדש (Redirect) של הדפדפן לכתובת של גוגל
|
| 56 |
+
return redirect(google_translate_url)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
+
except Exception as e:
|
| 59 |
+
return f"אירעה שגיאה: {e}", 500
|
|
|
|
|
|