Spaces:
Running
Running
ffreemt
commited on
Commit
·
dabcedb
1
Parent(s):
085f33d
Update files
Browse files- Dockerfile +19 -1
- Dockerfile- +1 -0
- auth_utils.py +214 -0
- docker-compose.yml +11 -0
- requirements.txt +4 -0
Dockerfile
CHANGED
@@ -1 +1,19 @@
|
|
1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# 使用Python 3.9作为基础镜像
|
2 |
+
FROM python:3.9-slim
|
3 |
+
|
4 |
+
# 设置工作目录
|
5 |
+
WORKDIR /app
|
6 |
+
|
7 |
+
# 复制依赖文件
|
8 |
+
COPY requirements.txt .
|
9 |
+
|
10 |
+
# 安装依赖
|
11 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
12 |
+
|
13 |
+
# 复制应用代码
|
14 |
+
COPY . .
|
15 |
+
|
16 |
+
# 暴露端口
|
17 |
+
EXPOSE 3000
|
18 |
+
|
19 |
+
CMD flask run --host=0.0.0.0 --port=${PORT}
|
Dockerfile-
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
FROM fjiabinc/notdiamond2api
|
auth_utils.py
ADDED
@@ -0,0 +1,214 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
import json
|
3 |
+
import base64
|
4 |
+
import re
|
5 |
+
import logging
|
6 |
+
|
7 |
+
class AuthManager:
|
8 |
+
"""
|
9 |
+
AuthManager类用于管理身份验证过程,包括获取API密钥、用户信息和处理刷新令牌等操作。
|
10 |
+
|
11 |
+
Attributes:
|
12 |
+
session (requests.Session): 一个session对象,用于保持与服务器之间的连接。
|
13 |
+
api_key (str): API密钥,用于身份验证。
|
14 |
+
user_info (dict): 用户信息字典。
|
15 |
+
refresh_token (str): 用于刷新的令牌。
|
16 |
+
user_id (str): 用户唯一标识符。
|
17 |
+
next_action (str): 下一步操作的标识符。
|
18 |
+
logger (logging.Logger): 日志记录器。
|
19 |
+
|
20 |
+
Methods:
|
21 |
+
log_values(): 记录关键信息到日志。
|
22 |
+
get_next_action(): 获取下一步的操作。
|
23 |
+
fetch_apikey(): 获取并存储API密钥。
|
24 |
+
login(email, password): 使用email和密码进行登录。
|
25 |
+
refresh_user_token(): 刷新用户的身份验证令牌。
|
26 |
+
base64_encode(data): 以JSON格式编码然后转换为Base64。
|
27 |
+
get_cookie_value(): 获取cookie值用于会话验证。
|
28 |
+
"""
|
29 |
+
|
30 |
+
email = ""
|
31 |
+
password = ""
|
32 |
+
|
33 |
+
def __init__(self, email, password):
|
34 |
+
self.email = email
|
35 |
+
self.password = password
|
36 |
+
self.session = requests.Session()
|
37 |
+
self.api_key = ""
|
38 |
+
self.user_info = {}
|
39 |
+
self.refresh_token = ""
|
40 |
+
self.user_id = ""
|
41 |
+
self.next_action = ""
|
42 |
+
|
43 |
+
self.logger = logging.getLogger(__name__)
|
44 |
+
logging.basicConfig(level=logging.INFO)
|
45 |
+
self.login()
|
46 |
+
self.fetch_apikey()
|
47 |
+
self.get_next_action()
|
48 |
+
|
49 |
+
self.log_values()
|
50 |
+
|
51 |
+
def log_values(self):
|
52 |
+
"""
|
53 |
+
记录刷新令牌、用户ID和下一步操作到日志中。
|
54 |
+
"""
|
55 |
+
self.logger.info(f"\033[92mRefresh Token: {self.refresh_token}\033[0m")
|
56 |
+
self.logger.info(f"\033[94mUser ID: {self.user_id}\033[0m")
|
57 |
+
self.logger.info(f"\033[96mNext Action: {self.next_action}\033[0m")
|
58 |
+
|
59 |
+
def get_next_action(self):
|
60 |
+
"""
|
61 |
+
尝试从服务器获取下一步操作标识符,并更新实例变量。
|
62 |
+
|
63 |
+
Returns:
|
64 |
+
str: 下一步的操作标识符,如果未找到则返回空字符串。
|
65 |
+
"""
|
66 |
+
try:
|
67 |
+
url = "https://chat.notdiamond.ai/"
|
68 |
+
headers = {
|
69 |
+
'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',
|
70 |
+
'cookie': self.get_cookie_value()
|
71 |
+
}
|
72 |
+
response = self.session.get(url, headers=headers)
|
73 |
+
response.raise_for_status()
|
74 |
+
script_tags = re.findall(r'<script[^>]*>(.*?)<\/script>', response.text, re.DOTALL)
|
75 |
+
matches = []
|
76 |
+
for script_content in script_tags:
|
77 |
+
if "static/chunks/app/(chat)/page-" in script_content:
|
78 |
+
matches.extend(re.findall(r'static/chunks/[a-zA-Z0-9]+-[a-zA-Z0-9]+\.js', script_content))
|
79 |
+
if not matches:
|
80 |
+
self.logger.warning("未找到匹配的脚本标签")
|
81 |
+
else:
|
82 |
+
full_script_urls = [f"https://chat.notdiamond.ai/_next/{match}" for match in matches]
|
83 |
+
for full_url in full_script_urls:
|
84 |
+
try:
|
85 |
+
script_response = self.session.get(full_url, headers=headers)
|
86 |
+
script_response.raise_for_status()
|
87 |
+
|
88 |
+
match = re.search(r'v=\(0,s.\$\)\("([^"]+)"\)', script_response.text)
|
89 |
+
if match:
|
90 |
+
desired_value = match.group(1)
|
91 |
+
self.next_action = desired_value
|
92 |
+
return self.next_action
|
93 |
+
except requests.RequestException as e:
|
94 |
+
self.logger.error(f"请求脚本URL时发生错误 {full_url}: {e}")
|
95 |
+
return ""
|
96 |
+
return ""
|
97 |
+
except requests.RequestException as e:
|
98 |
+
return ""
|
99 |
+
|
100 |
+
def fetch_apikey(self):
|
101 |
+
"""
|
102 |
+
获取API密钥并将其存储在本地文件中,以备后用。
|
103 |
+
|
104 |
+
Returns:
|
105 |
+
str: 获取的API密钥。
|
106 |
+
"""
|
107 |
+
if self.api_key:
|
108 |
+
return self.api_key
|
109 |
+
|
110 |
+
try:
|
111 |
+
url = "https://chat.notdiamond.ai/login"
|
112 |
+
headers = {
|
113 |
+
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) '
|
114 |
+
'AppleWebKit/537.36 (KHTML, like Gecko) '
|
115 |
+
'Chrome/128.0.0.0 Safari/537.36'
|
116 |
+
}
|
117 |
+
response = self.session.get(url, headers=headers)
|
118 |
+
response.raise_for_status()
|
119 |
+
|
120 |
+
match = re.search(r'<script src="(/_next/static/chunks/app/layout-[^"]+\.js)"', response.text)
|
121 |
+
if not match:
|
122 |
+
self.logger.warning("未找到匹配的脚本标签")
|
123 |
+
return ""
|
124 |
+
|
125 |
+
js_url = f"https://chat.notdiamond.ai{match.group(1)}"
|
126 |
+
response = self.session.get(js_url, headers=headers)
|
127 |
+
response.raise_for_status()
|
128 |
+
match = re.search(r'\("https://spuckhogycrxcbomznwo\.supabase\.co","([^"]+)"\)', response.text)
|
129 |
+
if match:
|
130 |
+
self.api_key = match.group(1)
|
131 |
+
|
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):
|
142 |
+
"""
|
143 |
+
使用类成员中的电子邮件和密码进行用户登录,并获取用户信息。
|
144 |
+
"""
|
145 |
+
url = "https://spuckhogycrxcbomznwo.supabase.co/auth/v1/token?grant_type=password"
|
146 |
+
headers = {
|
147 |
+
'apikey': self.fetch_apikey(),
|
148 |
+
'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',
|
149 |
+
'Content-Type': 'application/json'
|
150 |
+
}
|
151 |
+
data = {
|
152 |
+
"email": self.email,
|
153 |
+
"password": self.password,
|
154 |
+
"gotrue_meta_security": {}
|
155 |
+
}
|
156 |
+
|
157 |
+
try:
|
158 |
+
response = self.session.post(url, headers=headers, json=data)
|
159 |
+
response.raise_for_status()
|
160 |
+
self.user_info = response.json()
|
161 |
+
self.refresh_token = self.user_info.get('refresh_token', '')
|
162 |
+
self.user_id = self.user_info.get('user', {}).get('id', '')
|
163 |
+
|
164 |
+
except requests.RequestException as e:
|
165 |
+
self.logger.error(f"\033[91m登录请求错误: {e}\033[0m")
|
166 |
+
return ""
|
167 |
+
|
168 |
+
def refresh_user_token(self):
|
169 |
+
"""
|
170 |
+
使用刷新令牌来请求一个新的访问令牌并更新实例变量。
|
171 |
+
"""
|
172 |
+
url = "https://spuckhogycrxcbomznwo.supabase.co/auth/v1/token?grant_type=refresh_token"
|
173 |
+
headers = {
|
174 |
+
'apikey': self.fetch_apikey(),
|
175 |
+
'content-type': 'application/json;charset=UTF-8',
|
176 |
+
'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'
|
177 |
+
}
|
178 |
+
data = {
|
179 |
+
"refresh_token": self.refresh_token
|
180 |
+
}
|
181 |
+
|
182 |
+
try:
|
183 |
+
response = self.session.post(url, headers=headers, json=data)
|
184 |
+
response.raise_for_status()
|
185 |
+
self.user_info = response.json()
|
186 |
+
self.refresh_token = self.user_info.get('refresh_token', '')
|
187 |
+
self.user_id = self.user_info.get('user', {}).get('id', '')
|
188 |
+
|
189 |
+
except requests.RequestException as e:
|
190 |
+
self.logger.error(f"刷新令牌请求错误: {e}")
|
191 |
+
return ""
|
192 |
+
|
193 |
+
def base64_encode(self, data):
|
194 |
+
"""
|
195 |
+
将输入数据转换为JSON字符串并编码为Base64格式。
|
196 |
+
|
197 |
+
Parameters:
|
198 |
+
data (any): 需要编码的数据。
|
199 |
+
|
200 |
+
Returns:
|
201 |
+
str: 编码后的Base64字符串。
|
202 |
+
"""
|
203 |
+
json_str = json.dumps(data, ensure_ascii=False)
|
204 |
+
return base64.b64encode(json_str.encode('utf-8')).decode('utf-8')
|
205 |
+
|
206 |
+
def get_cookie_value(self):
|
207 |
+
"""
|
208 |
+
生成并返回用于会话验证的cookie值。
|
209 |
+
|
210 |
+
Returns:
|
211 |
+
str: 用于验证的cookie字符串。
|
212 |
+
"""
|
213 |
+
encoded_user_info = self.base64_encode(self.user_info)
|
214 |
+
return f"sb-spuckhogycrxcbomznwo-auth-token=base64-{encoded_user_info}"
|
docker-compose.yml
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
version: '3.8'
|
2 |
+
|
3 |
+
services:
|
4 |
+
flask-app:
|
5 |
+
build: .
|
6 |
+
ports:
|
7 |
+
- "3000:3000"
|
8 |
+
environment:
|
9 |
+
- PORT=3000
|
10 |
+
- AUTH_EMAIL=your_email@example.com
|
11 |
+
- AUTH_PASSWORD=your_password
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Flask==2.3.2
|
2 |
+
requests==2.31.0
|
3 |
+
Flask-CORS==4.0.0
|
4 |
+
tiktoken==0.7.0
|