Skydata001 commited on
Commit
06a33e4
·
verified ·
1 Parent(s): 5aa5791

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -10
app.py CHANGED
@@ -15,21 +15,27 @@ st.set_page_config(
15
  layout="wide"
16
  )
17
 
18
- # --- <<< جديد: خطوة المصادقة والتحقق من النطاق ---
19
 
20
  # تهيئة متغير "المصادقة" في حالة الجلسة
21
  if 'authenticated' not in st.session_state:
22
  st.session_state.authenticated = False
23
 
 
24
  # كود HTML/JavaScript الذي سيتم حقنه في Streamlit "للاستماع"
25
  auth_listener_html = """
26
  <script>
27
  // الاستماع للرسائل القادمة من النافذة "الأب"
28
  window.addEventListener('message', (event) => {
29
 
30
- // هام جداً: التحقق من مصدر الرسالة
31
- // اسمح فقط بالرسائل القادمة من نطاقك
32
- if (event.origin !== "https://www.skydata.kozow.com") {
 
 
 
 
 
33
  console.warn("تم رفض رسالة من نطاق غير مصرح به:", event.origin);
34
  return;
35
  }
@@ -37,8 +43,14 @@ window.addEventListener('message', (event) => {
37
  try {
38
  const data = JSON.parse(event.data);
39
 
 
 
 
 
 
 
40
  // التحقق من محتوى الرسالة (لزيادة الأمان)
41
- if (data.source === 'skydata-auth' && data.domain === 'www.skydata.kozow.com') {
42
 
43
  // إذا نجحت المصادقة، أرسل "True" إلى Streamlit
44
  Streamlit.setComponentValue({ "authenticated": true });
@@ -49,12 +61,12 @@ window.addEventListener('message', (event) => {
49
  }, false);
50
  </script>
51
  """
 
52
 
53
  # تشغيل "المستمع" كعنصر HTML غير مرئي
54
  auth_result = components.html(auth_listener_html, height=0, width=0)
55
 
56
- # --- <<< هذا هو السطر الذي تم تصحيحه (السطر 60) ---
57
- # إذا أرسل المستمع نتيجة إيجابية، قم بتحديث حالة الجلسة
58
  if auth_result and isinstance(auth_result, dict) and auth_result.get("authenticated") == True:
59
  st.session_state.authenticated = True
60
  # --- نهاية خطوة المصادقة ---
@@ -200,7 +212,7 @@ if st.session_state.authenticated:
200
  loc = localization[lang_code]
201
 
202
  # --- 7. واجهة التطبيق الرئيسية (Main App) ---
203
- LOGO_URL = "https://i.ibb.co/v4vwvcGq/skydatafull.webp"
204
  col1, col2 = st.columns([1, 6])
205
  with col1:
206
  st.image(LOGO_URL, width=80)
@@ -211,6 +223,7 @@ if st.session_state.authenticated:
211
  selected_tab = st.sidebar.radio(loc['input_method'], [loc['tab_upload'], loc['tab_url'], loc['tab_file']])
212
 
213
  # --- 8. منطق التبويبات ---
 
214
  if selected_tab == loc['tab_upload']:
215
  uploaded_file = st.file_uploader(loc['upload_prompt'], type=["jpg", "jpeg", "png"])
216
  if uploaded_file is not None:
@@ -264,10 +277,10 @@ if st.session_state.authenticated:
264
  except Exception as e:
265
  st.error(f"{loc['generic_error']} {e}")
266
 
 
267
  # --- إذا فشلت المصادقة ---
268
  else:
269
- # إذا لم تنجح المصادقة (st.session_state.authenticated بقيت False)
270
- # سيعرض رسالة الخطأ هذه بدلاً من التطبيق
271
  st.error("🔒 الوصول مرفوض.")
272
  st.error("لا يمكن تشغيل هذه الأداة إلا من خلال النطاق المعتمد: https://www.skydata.kozow.com/")
273
  st.warning("Access Denied. This tool can only be run when embedded on https://www.skydata.kozow.com/")
 
15
  layout="wide"
16
  )
17
 
18
+ # --- <<< خطوة المصادقة والتحقق من النطاق ---
19
 
20
  # تهيئة متغير "المصادقة" في حالة الجلسة
21
  if 'authenticated' not in st.session_state:
22
  st.session_state.authenticated = False
23
 
24
+ # --- <<< هذا هو الجزء الذي تم تعديله ---
25
  # كود HTML/JavaScript الذي سيتم حقنه في Streamlit "للاستماع"
26
  auth_listener_html = """
27
  <script>
28
  // الاستماع للرسائل القادمة من النافذة "الأب"
29
  window.addEventListener('message', (event) => {
30
 
31
+ // --- تعديل 1: قائمة النطاقات المسموحة (مع وبدون www) ---
32
+ const allowedOrigins = [
33
+ "https://www.skydata.kozow.com",
34
+ "https://skydata.kozow.com"
35
+ ];
36
+
37
+ // التحقق إذا كان مصدر الرسالة ضمن القائمة المسموحة
38
+ if (!allowedOrigins.includes(event.origin)) {
39
  console.warn("تم رفض رسالة من نطاق غير مصرح به:", event.origin);
40
  return;
41
  }
 
43
  try {
44
  const data = JSON.parse(event.data);
45
 
46
+ // --- تعديل 2: قائمة أسماء النطاقات المسموحة (للتأكيد) ---
47
+ const allowedDomains = [
48
+ "www.skydata.kozow.com",
49
+ "skydata.kozow.com"
50
+ ];
51
+
52
  // التحقق من محتوى الرسالة (لزيادة الأمان)
53
+ if (data.source === 'skydata-auth' && allowedDomains.includes(data.domain)) {
54
 
55
  // إذا نجحت المصادقة، أرسل "True" إلى Streamlit
56
  Streamlit.setComponentValue({ "authenticated": true });
 
61
  }, false);
62
  </script>
63
  """
64
+ # --- <<< نهاية الجزء المعدل ---
65
 
66
  # تشغيل "المستمع" كعنصر HTML غير مرئي
67
  auth_result = components.html(auth_listener_html, height=0, width=0)
68
 
69
+ # (الكود الذي أصلحناه سابقاً - يبقى كما هو)
 
70
  if auth_result and isinstance(auth_result, dict) and auth_result.get("authenticated") == True:
71
  st.session_state.authenticated = True
72
  # --- نهاية خطوة المصادقة ---
 
212
  loc = localization[lang_code]
213
 
214
  # --- 7. واجهة التطبيق الرئيسية (Main App) ---
215
+ LOGO_URL = "https://i.ibb.co/v4vwvcGq/skydata.webp" # تم تغيير الرابط لإصلاح خطأ سابق محتمل
216
  col1, col2 = st.columns([1, 6])
217
  with col1:
218
  st.image(LOGO_URL, width=80)
 
223
  selected_tab = st.sidebar.radio(loc['input_method'], [loc['tab_upload'], loc['tab_url'], loc['tab_file']])
224
 
225
  # --- 8. منطق التبويبات ---
226
+ # ... (باقي كود التبويبات يبقى كما هو تماماً) ...
227
  if selected_tab == loc['tab_upload']:
228
  uploaded_file = st.file_uploader(loc['upload_prompt'], type=["jpg", "jpeg", "png"])
229
  if uploaded_file is not None:
 
277
  except Exception as e:
278
  st.error(f"{loc['generic_error']} {e}")
279
 
280
+
281
  # --- إذا فشلت المصادقة ---
282
  else:
283
+ # (هذا الكود سليم)
 
284
  st.error("🔒 الوصول مرفوض.")
285
  st.error("لا يمكن تشغيل هذه الأداة إلا من خلال النطاق المعتمد: https://www.skydata.kozow.com/")
286
  st.warning("Access Denied. This tool can only be run when embedded on https://www.skydata.kozow.com/")