Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -102,44 +102,52 @@ class AuthManager:
|
|
102 |
self.logger.info(f"\033[92mRefresh Token: {self.refresh_token}\033[0m")
|
103 |
|
104 |
def fetch_apikey(self) -> str:
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
try:
|
110 |
-
url = "https://chat.notdiamond.ai/login"
|
111 |
-
headers = {
|
112 |
-
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36'
|
113 |
-
}
|
114 |
-
response = self.session.get(url, headers=headers)
|
115 |
-
response.raise_for_status()
|
116 |
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
|
|
|
|
|
|
121 |
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
if api_key_match:
|
128 |
-
self.api_key = api_key_match.group(1)
|
129 |
-
return self.api_key
|
130 |
-
else:
|
131 |
-
self.logger.error("未能匹配API key")
|
132 |
-
return ""
|
133 |
|
134 |
-
|
135 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
136 |
return ""
|
137 |
|
|
|
|
|
|
|
|
|
138 |
def login(self) -> None:
|
139 |
"""使用电子邮件和密码进行用户登录,并获取用户信息。"""
|
|
|
|
|
|
|
|
|
|
|
140 |
url = "https://spuckhogycrxcbomznwo.supabase.co/auth/v1/token?grant_type=password"
|
141 |
headers = {
|
142 |
-
'apikey':
|
143 |
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36',
|
144 |
'Content-Type': 'application/json'
|
145 |
}
|
@@ -148,7 +156,7 @@ class AuthManager:
|
|
148 |
"password": self.password,
|
149 |
"gotrue_meta_security": {}
|
150 |
}
|
151 |
-
|
152 |
try:
|
153 |
response = self.session.post(url, headers=headers, json=data)
|
154 |
response.raise_for_status()
|
@@ -159,8 +167,10 @@ class AuthManager:
|
|
159 |
cache_key = f"{self.email}|{self.password}"
|
160 |
refresh_token_cache[cache_key] = self.refresh_token
|
161 |
|
|
|
|
|
162 |
except requests.RequestException as e:
|
163 |
-
self.logger.error(f"
|
164 |
|
165 |
def refresh_user_token(self) -> None:
|
166 |
"""使用刷新令牌来请求一个新的访问令牌并更新实例变量。"""
|
@@ -362,14 +372,21 @@ def generate_stream_response(response, model, prompt_tokens):
|
|
362 |
def get_auth_credentials():
|
363 |
"""从请求头中获取认证凭据"""
|
364 |
auth_header = request.headers.get('Authorization')
|
365 |
-
if not auth_header
|
|
|
|
|
|
|
|
|
|
|
366 |
return None, None
|
367 |
|
368 |
try:
|
369 |
credentials = auth_header.split('Bearer ')[1]
|
370 |
email, password = credentials.split('|')
|
|
|
371 |
return email.strip(), password.strip()
|
372 |
-
except:
|
|
|
373 |
return None, None
|
374 |
|
375 |
@app.before_request
|
|
|
102 |
self.logger.info(f"\033[92mRefresh Token: {self.refresh_token}\033[0m")
|
103 |
|
104 |
def fetch_apikey(self) -> str:
|
105 |
+
"""获取API密钥。"""
|
106 |
+
if self.api_key:
|
107 |
+
return self.api_key
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
108 |
|
109 |
+
try:
|
110 |
+
url = "https://chat.notdiamond.ai/login"
|
111 |
+
headers = {
|
112 |
+
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36'
|
113 |
+
}
|
114 |
+
response = self.session.get(url, headers=headers)
|
115 |
+
response.raise_for_status()
|
116 |
|
117 |
+
# 匹配 <script> 标签中的 JS 文件路径
|
118 |
+
match = re.search(r'<script src="(/_next/static/chunks/app/layout-[^"]+\.js)"', response.text)
|
119 |
+
if not match:
|
120 |
+
self.logger.warning("未找到匹配的脚本标签")
|
121 |
+
return ""
|
|
|
|
|
|
|
|
|
|
|
|
|
122 |
|
123 |
+
js_url = f"https://chat.notdiamond.ai{match.group(1)}"
|
124 |
+
js_response = self.session.get(js_url, headers=headers)
|
125 |
+
js_response.raise_for_status()
|
126 |
+
|
127 |
+
# 匹配 API key
|
128 |
+
api_key_match = re.search(r'\("https://spuckhogycrxcbomznwo\.supabase\.co","([^"]+)"\)', js_response.text)
|
129 |
+
if api_key_match:
|
130 |
+
self.api_key = api_key_match.group(1)
|
131 |
+
logger.info(f"Extracted API key: {self.api_key}")
|
132 |
+
return self.api_key
|
133 |
+
else:
|
134 |
+
self.logger.error("未能匹配API key")
|
135 |
return ""
|
136 |
|
137 |
+
except requests.RequestException as e:
|
138 |
+
self.logger.error(f"请求JS文件时发生错误: {e}")
|
139 |
+
return ""
|
140 |
+
|
141 |
def login(self) -> None:
|
142 |
"""使用电子邮件和密码进行用户登录,并获取用户信息。"""
|
143 |
+
api_key = self.fetch_apikey()
|
144 |
+
if not api_key:
|
145 |
+
self.logger.error("API key is missing, cannot proceed with login.")
|
146 |
+
return
|
147 |
+
|
148 |
url = "https://spuckhogycrxcbomznwo.supabase.co/auth/v1/token?grant_type=password"
|
149 |
headers = {
|
150 |
+
'apikey': api_key,
|
151 |
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36',
|
152 |
'Content-Type': 'application/json'
|
153 |
}
|
|
|
156 |
"password": self.password,
|
157 |
"gotrue_meta_security": {}
|
158 |
}
|
159 |
+
|
160 |
try:
|
161 |
response = self.session.post(url, headers=headers, json=data)
|
162 |
response.raise_for_status()
|
|
|
167 |
cache_key = f"{self.email}|{self.password}"
|
168 |
refresh_token_cache[cache_key] = self.refresh_token
|
169 |
|
170 |
+
logger.info(f"Login successful for email: {self.email}")
|
171 |
+
|
172 |
except requests.RequestException as e:
|
173 |
+
self.logger.error(f"登录请求错误: {e}")
|
174 |
|
175 |
def refresh_user_token(self) -> None:
|
176 |
"""使用刷新令牌来请求一个新的访问令牌并更新实例变量。"""
|
|
|
372 |
def get_auth_credentials():
|
373 |
"""从请求头中获取认证凭据"""
|
374 |
auth_header = request.headers.get('Authorization')
|
375 |
+
if not auth_header:
|
376 |
+
logger.error("Authorization header is missing")
|
377 |
+
return None, None
|
378 |
+
|
379 |
+
if not auth_header.startswith('Bearer '):
|
380 |
+
logger.error(f"Authorization header format is incorrect: {auth_header}")
|
381 |
return None, None
|
382 |
|
383 |
try:
|
384 |
credentials = auth_header.split('Bearer ')[1]
|
385 |
email, password = credentials.split('|')
|
386 |
+
logger.info(f"Extracted email: {email}, password: {'*' * len(password)}")
|
387 |
return email.strip(), password.strip()
|
388 |
+
except Exception as e:
|
389 |
+
logger.error(f"Error parsing Authorization header: {e}")
|
390 |
return None, None
|
391 |
|
392 |
@app.before_request
|