File size: 12,441 Bytes
858b443
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22e58bb
 
 
 
 
 
 
 
 
 
 
858b443
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22e58bb
858b443
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
import requests
from bs4 import BeautifulSoup
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry
import uuid
import json
import time
import re
import hashlib
import base64
import random
import string
import os
import sys
from urllib.parse import quote, urlparse, parse_qs
import concurrent.futures
import logging

# 配置日志(仅控制台输出)
logging.basicConfig(
    level=logging.DEBUG,  # 改为DEBUG级别,获取更详细的日志
    format='%(asctime)s - %(levelname)s - %(message)s', 
    stream=sys.stdout
)

# 获取环境变量
PASTE_API_URL = os.getenv('PASTE_API_URL')
PASTE_API_PASSWORD = os.getenv('PASTE_API_PASSWORD')

# 构建上传 URL
UPLOAD_URL = PASTE_API_URL.replace('/api/paste/', '/api/admin/paste/') + '/content'

# 新的临时邮箱API
TEMP_EMAIL_API = "https://www.1secmail.com/api/v1/?action=genRandomMailbox"

# 注册 API 地址
REGISTER_API_URL = "https://spuckhogycrxcbomznwo.supabase.co/auth/v1/signup?redirect_to=https%3A%2F%2Fchat.notdiamond.ai"

# Supabase API 
SUPABASE_API_KEY = os.getenv('SUPABASE_API_KEY')

# 请求限制和重试相关配置
MAX_RETRIES = 3
RETRY_DELAY = 10  # 秒
RATE_LIMIT_DELAY = 60  # 秒

def generate_strong_password():
    """

    从环境变量获取密码,如果未设置则抛出异常

    """
    # 尝试从环境变量获取密码
    env_password = os.getenv('REGISTER_PASSWORD')
    
    if not env_password:
        logging.error("未设置 REGISTER_PASSWORD 环境变量")
        raise ValueError("必须通过 REGISTER_PASSWORD 环境变量指定密码")
    
    return env_password

def generate_code_verifier():
    return base64.urlsafe_b64encode(uuid.uuid4().bytes).decode('utf-8').rstrip('=')

def generate_code_challenge(code_verifier):
    code_challenge = hashlib.sha256(code_verifier.encode('utf-8')).digest()
    return base64.urlsafe_b64encode(code_challenge).decode('utf-8').rstrip('=')

def get_temp_email():
    session = requests.Session()
    retries = Retry(total=5, backoff_factor=1, status_forcelist=[500, 502, 503, 504])
    session.mount('https://', HTTPAdapter(max_retries=retries))
    
    try:
        response = session.get(TEMP_EMAIL_API)
        if response.status_code == 200:
            email = response.json()[0]
            logging.info(f"Generated temp email: {email}")
            return email
    except requests.exceptions.SSLError as e:
        logging.error(f"SSL Error getting temp email: {e}")
    return None

def register(email, password):
    for attempt in range(MAX_RETRIES):
        session = requests.Session()
        headers = {
            'apikey': SUPABASE_API_KEY,
            'authorization': f'Bearer {SUPABASE_API_KEY}',
            'content-type': 'application/json'
        }
        
        code_verifier = generate_code_verifier()
        code_challenge = generate_code_challenge(code_verifier)
        
        payload = {
            'email': email,
            'password': password,
            'code_challenge': code_challenge,
            'code_challenge_method': 's256',
            'data': {},
            'gotrue_meta_security': {}
        }
        
        try:
            response = session.post(REGISTER_API_URL, headers=headers, json=payload)
            
            logging.info(f"Registration response status: {response.status_code}")
            logging.info(f"Registration response content: {response.text}")
            
            if response.status_code == 200:
                logging.info(f"Registered successfully with email: {email}")
                return True
            elif response.status_code == 429:
                logging.warning(f"Rate limit reached. Waiting {RATE_LIMIT_DELAY} seconds.")
                time.sleep(RATE_LIMIT_DELAY)
            else:
                logging.error(f"Failed to register with email: {email}. Response: {response.text}")
                return False
        
        except requests.exceptions.RequestException as e:
            logging.error(f"Request error during registration: {e}")
            if attempt < MAX_RETRIES - 1:
                logging.info(f"Retrying registration in {RETRY_DELAY} seconds...")
                time.sleep(RETRY_DELAY)
            else:
                logging.error("Max registration retries reached.")
                return False
    
    return False

def save_account(email, password):
    try:
        session = requests.Session()
        
        # 首先进行认证
        auth_url = PASTE_API_URL
        auth_headers = {
            'accept': '*/*',
            'accept-language': 'zh-CN,zh;q=0.9',
            'user-agent': 'Mozilla/5.0',
            'x-password': PASTE_API_PASSWORD
        }
        
        # 获取现有内容
        logging.info(f"Authenticating with URL: {auth_url}")
        response = session.get(auth_url, headers=auth_headers)
        
        logging.info(f"Authentication response status: {response.status_code}")
        logging.info(f"Authentication response content: {response.text}")
        
        if response.status_code != 200:
            logging.error(f"Authentication failed, status code: {response.status_code}")
            return False
        
        # 从响应获取现有内容
        existing_data = response.json()
        existing_content = existing_data.get('content', '')
        
        # 构建新的内容
        if existing_content:
            new_content = f"{existing_content};{email}|{password}"
        else:
            new_content = f"{email}|{password}"
        
        logging.info(f"New content to upload: {new_content}")
        
        # 上传新内容
        upload_headers = {
            'Authorization': 'Basic emhvdWRhbjp6aG91ZGFu',
            'Content-Type': 'application/json'
        }
        
        upload_payload = {
            'content': new_content
        }
        
        logging.info(f"Uploading to URL: {UPLOAD_URL}")
        upload_response = session.put(UPLOAD_URL, headers=upload_headers, json=upload_payload)
        
        logging.info(f"Upload response status: {upload_response.status_code}")
        logging.info(f"Upload response content: {upload_response.text}")
        
        if upload_response.status_code == 200:
            logging.info(f"Successfully uploaded account {email}")
            return True
        else:
            logging.error(f"Failed to upload account {email}. Status code: {upload_response.status_code}")
            return False
    
    except Exception as e:
        logging.error(f"Comprehensive error uploading account: {e}")
        return False

def check_email(email):
    domain = email.split('@')[1]
    login = email.split('@')[0]
    
    # 获取消息ID
    mailbox_url = f"https://www.1secmail.com/api/v1/?action=getMessages&login={login}&domain={domain}"
    
    session = requests.Session()
    retries = Retry(total=5, backoff_factor=1, status_forcelist=[500, 502, 503, 504])
    session.mount('https://', HTTPAdapter(max_retries=retries))
    
    logging.info(f"Checking email for {email}")
    
    time.sleep(10)
    
    for _ in range(20):
        try:
            response = session.get(mailbox_url)
            logging.info(f"Email check response status: {response.status_code}")
            
            if response.status_code == 200 and response.json():
                for message in response.json():
                    if "Confirm Your Signup" in message.get('subject', ''):
                        # 获取完整邮件内容
                        message_url = f"https://www.1secmail.com/api/v1/?action=readMessage&login={login}&domain={domain}&id={message['id']}"
                        message_response = session.get(message_url)
                        
                        if message_response.status_code == 200:
                            full_message = message_response.json()
                            logging.info(f"Full message content found: {full_message}")
                            
                            # 提取验证链接
                            match = re.search(r'https?://chat\.notdiamond\.ai/auth/confirm\?[^\s]+', full_message.get('body', ''))
                            if match:
                                verification_link = match.group(0)
                                logging.info(f"Verification link found: {verification_link}")
                                
                                # 解析 URL 并获取参数
                                parsed_url = urlparse(verification_link)
                                query_params = parse_qs(parsed_url.query)
                                
                                # 使用 Supabase API 进行验证
                                verify_url = "https://spuckhogycrxcbomznwo.supabase.co/auth/v1/verify"
                                headers = {
                                    'apikey': SUPABASE_API_KEY,
                                    'authorization': f'Bearer {SUPABASE_API_KEY}',
                                    'content-type': 'application/json'
                                }
                                
                                payload = {
                                    'token_hash': query_params['token_hash'][0],
                                    'type': 'signup'  # 硬编码为 'signup'
                                }
                                
                                verify_response = session.post(verify_url, headers=headers, json=payload)
                                
                                logging.info(f"Verification response status: {verify_response.status_code}")
                                
                                if verify_response.status_code == 200:
                                    logging.info(f"Email verified for {email}")
                                    return True
                                else:
                                    logging.error(f"Failed to verify email for {email}: {verify_response.text}")
                                    return False
                            else:
                                logging.error(f"Failed to extract verification link from email for {email}")
                                return False
            time.sleep(5)
        except Exception as e:
            logging.error(f"Error during email verification: {e}")
    
    logging.warning(f"No verification email found for {email}")
    return False

def register_and_verify(num_accounts=1):
    successful_accounts = []
    with concurrent.futures.ThreadPoolExecutor(max_workers=2) as executor:
        futures = []
        for _ in range(num_accounts):
            email = get_temp_email()
            if email:
                futures.append(executor.submit(process_account, email))
                time.sleep(RETRY_DELAY)
        
        for future in concurrent.futures.as_completed(futures):
            result = future.result()
            if result:
                successful_accounts.append(result)
    
    return successful_accounts

def process_account(email):
    try:
        # 使用环境变量获取密码
        password = generate_strong_password()
        
        if register(email, password):
            save_result = save_account(email, password)
            if not save_result:
                logging.error(f"Failed to save account {email}")
                return None
            
            if check_email(email):
                logging.info(f"Account fully processed: {email}")
                return {'email': email, 'password': password}
    except Exception as e:
        logging.error(f"Error processing account {email}: {e}")
    return None

def main():
    num_accounts = 5  # 可以根据需要修改注册数量
    successful_accounts = register_and_verify(num_accounts)
    
    # 仅输出到控制台
    print(f"Successfully registered {len(successful_accounts)} accounts")
    for account in successful_accounts:
        print(f"Email: {account['email']}, Password: {account['password']}")

if __name__ == "__main__":
    main()