PraneshJs commited on
Commit
0e477f4
·
verified ·
1 Parent(s): a7a68d9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -3
app.py CHANGED
@@ -5,6 +5,7 @@ from google.auth.transport.requests import Request
5
  from google.oauth2.credentials import Credentials
6
  from google_auth_oauthlib.flow import InstalledAppFlow
7
  import json
 
8
  import urllib
9
  import gradio as gr
10
  import time
@@ -61,22 +62,46 @@ def authorize():
61
 
62
  return gspread.authorize(creds)
63
 
 
 
 
 
 
 
64
  def client_info(request):
65
  """' | Client: ... | IP: ...' for the lookup log lines: shows who calls the app (browser or script)."""
66
  if request is None:
67
  return " | Client: unknown | IP: unknown"
68
- ua = request.headers.get("user-agent", "")
69
  kind = "browser" if ua.startswith("Mozilla/") else "script"
70
- # On HF the real client IP is the first X-Forwarded-For entry; locally it is the socket peer.
71
- ip = request.headers.get("x-forwarded-for", "").split(",")[0].strip() or (request.client.host if request.client else "unknown")
72
  return f" | Client: {kind} ({ua[:60] or 'no user-agent'}) | IP: {ip}"
73
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
  # NEW FUNCTION: Extract subjects and marks
75
  def extract_subjects_and_marks_for_gradio(roll_no, request: gr.Request = None):
76
  """
77
  Extract subjects with their redeemed points and marks for Gradio interface
78
  Uses cached studentwise_data instead of making fresh API calls
79
  """
 
 
80
  if not roll_no.strip():
81
  return "❌ Please enter a roll number"
82
 
@@ -1164,6 +1189,8 @@ print("👀 Details sheet watcher started (checks every 1 minute)")
1164
 
1165
  # Function to search student with cached data
1166
  def search_student(roll_no, request: gr.Request = None):
 
 
1167
  if not roll_no.strip():
1168
  return "❌ Please enter a roll number"
1169
 
 
5
  from google.oauth2.credentials import Credentials
6
  from google_auth_oauthlib.flow import InstalledAppFlow
7
  import json
8
+ import hashlib
9
  import urllib
10
  import gradio as gr
11
  import time
 
62
 
63
  return gspread.authorize(creds)
64
 
65
+ def _client(request):
66
+ """(user-agent, ip) of the caller. On HF the real IP is the first X-Forwarded-For entry."""
67
+ ua = request.headers.get("user-agent", "")
68
+ ip = request.headers.get("x-forwarded-for", "").split(",")[0].strip() or (request.client.host if request.client else "unknown")
69
+ return ua, ip
70
+
71
  def client_info(request):
72
  """' | Client: ... | IP: ...' for the lookup log lines: shows who calls the app (browser or script)."""
73
  if request is None:
74
  return " | Client: unknown | IP: unknown"
75
+ ua, ip = _client(request)
76
  kind = "browser" if ua.startswith("Mozilla/") else "script"
 
 
77
  return f" | Client: {kind} ({ua[:60] or 'no user-agent'}) | IP: {ip}"
78
 
79
+ # Blocked IPs, stored as sha256 of the IP text so the addresses aren't readable in the code.
80
+ # To block another: python -c "import hashlib; print(hashlib.sha256(b'1.2.3.4').hexdigest())"
81
+ BLOCKED_IP_HASHES = {
82
+ "d671d6d230c256ab2512511bec535493fbf5ee477acb82754c1921a7633ba348",
83
+ }
84
+
85
+ def is_blocked(request):
86
+ """Block every non-browser client (scripts send no browser user-agent) and the listed IPs."""
87
+ if request is None:
88
+ return False
89
+ ua, ip = _client(request)
90
+ if not ua.startswith("Mozilla/") or hashlib.sha256(ip.encode()).hexdigest() in BLOCKED_IP_HASHES:
91
+ print(f"🚫 Blocked request{client_info(request)}")
92
+ return True
93
+ return False
94
+
95
+ BLOCKED_MESSAGE = "❌ Access denied."
96
+
97
  # NEW FUNCTION: Extract subjects and marks
98
  def extract_subjects_and_marks_for_gradio(roll_no, request: gr.Request = None):
99
  """
100
  Extract subjects with their redeemed points and marks for Gradio interface
101
  Uses cached studentwise_data instead of making fresh API calls
102
  """
103
+ if is_blocked(request):
104
+ return BLOCKED_MESSAGE
105
  if not roll_no.strip():
106
  return "❌ Please enter a roll number"
107
 
 
1189
 
1190
  # Function to search student with cached data
1191
  def search_student(roll_no, request: gr.Request = None):
1192
+ if is_blocked(request):
1193
+ return BLOCKED_MESSAGE
1194
  if not roll_no.strip():
1195
  return "❌ Please enter a roll number"
1196