-
Notifications
You must be signed in to change notification settings - Fork 531
/
pandora-get-token.py
187 lines (151 loc) · 7.36 KB
/
pandora-get-token.py
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
import sys
import requests
import os
import json
import time
pool_token = ""
api_endpoint = "http://127.0.0.1:8181/<proxy_api_prefix>"
users_file = "users.txt"
session_tokens_file = "session_tokens.txt"
tokens_file = "tokens.txt"
def print_log(msg):
print("[%s]%s"%((time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())),msg))
def make_post_request(api_endpoint, endpoint_path, payload):
url = f"{api_endpoint}{endpoint_path}"
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
try:
response = requests.post(url, headers=headers, data=payload)
return response
except Exception as e:
print_log(str(e))
return None
def read_file(file_path):
try:
with open(file_path, 'r') as file:
return [line.strip() for line in file if line.strip()]
except Exception as e:
print_log(str(e))
return []
def write_tokens(file_path, tokens):
with open(file_path, 'w') as file:
file.write('\n'.join(tokens))
def process_users(users_file, session_tokens_file):
lines = read_file(users_file)
if not lines:
print_log(users_file + ": No users found. Exiting program.")
return
session_tokens = []
for line in lines:
username, password = line.split('----')
payload = f'username={username}&password={password}'
response = make_post_request(api_endpoint, "/api/auth/login", payload)
if response.status_code == 200:
token_info = response.json()
session_tokens.append(token_info['session_token'])
print_log(f"Success for {username}")
else:
print_log(f"Failed for {username}: {response.json().get('detail')}")
write_tokens(session_tokens_file, session_tokens)
print_log(f"Successfully extracted {len(session_tokens)} session tokens, Saved to file {session_tokens_file}")
def process_session_and_register(session_token):
session_payload = f'session_token={session_token}'
session_response = make_post_request(api_endpoint, "/api/auth/session", session_payload)
if session_response.status_code != 200:
print_log(f"Failed session token: {session_response.json().get('detail')}")
return None
session_info = session_response.json()
access_token = session_info['access_token']
#show_conversations=true&show_userinfo=true 默认不隔离会话,不隐藏账号信息
register_payload = f'unique_name=pandora&access_token={access_token}&site_limit=&expires_in=0&show_conversations=true&show_userinfo=true'
register_response = make_post_request(api_endpoint, "/api/token/register", register_payload)
if register_response.status_code != 200:
print_log(f"Failed register: {register_response.json().get('detail')}")
return None
register_info = register_response.json()
return {
'share_token': register_info['token_key'],
'expire_at': register_info['expire_at'],
'session_token': session_info['session_token']
}
def process_tokens(tokens_file, session_tokens_file):
tokens = read_file(session_tokens_file)
if not tokens:
print_log(session_tokens_file + ": No tokens found. Exiting program.")
return
write_tokens(session_tokens_file + '_bak', tokens)
share_tokens = []
session_tokens = []
token_expire_dict = {}
for token in tokens:
token_info = process_session_and_register(token)
if token_info:
token_expire_dict[token_info['expire_at']] = {token_info['share_token']: token_info['session_token']}
share_tokens.append(token_info['share_token'])
session_tokens.append(token_info['session_token'])
print_log("Share Token:" + token_info['share_token'])
global pool_token
if share_tokens:
share_token_str = '%0D%0A'.join(share_tokens)
pool_payload = f'share_tokens={share_token_str}&pool_token={pool_token}'
pool_response = make_post_request(api_endpoint, "/api/pool/update", pool_payload)
if pool_response.status_code == 200:
pool_info = pool_response.json()
pool_token = pool_info['pool_token']
print_log("Count:"+str(pool_info['count'])+" Pool Token:" + pool_token)
write_tokens(tokens_file, share_tokens + [pool_token])
print_log("Saved to file " + tokens_file)
if session_tokens:
write_tokens(session_tokens_file, session_tokens)
print_log("Updated " + session_tokens_file)
while True:
current_time = int(time.time())
min_expire_time = min(token_expire_dict.keys(), default=1)
if min_expire_time == 1:
print_log("No tokens found. Exiting program.")
return
if current_time > min_expire_time:
tokens_dict = token_expire_dict.pop(min_expire_time)
share_token = next(iter(tokens_dict))
session_token = tokens_dict[share_token]
if session_token in session_tokens:
session_tokens.remove(session_token)
register_info = process_session_and_register(session_token)
if register_info:
print_log("Updated Share Token:"+ register_info['share_token'])
token_expire_dict[register_info['expire_at']] = {register_info['share_token']: register_info['session_token']}
session_tokens.append(register_info['session_token'])
else:
if share_token in share_tokens:
share_tokens.remove(share_token)
print_log("Expired. Deleted Share Token:"+ share_token)
if share_tokens:
share_token_str = '%0D%0A'.join(share_tokens)
pool_payload = f'share_tokens={share_token_str}&pool_token={pool_token}'
pool_response = make_post_request(api_endpoint, "/api/pool/update", pool_payload)
if pool_response.status_code == 200:
pool_info = pool_response.json()
pool_token = pool_info['pool_token']
print_log("Updated Pool Token. Count:"+str(pool_info['count'])+" Pool Token:" + pool_token)
write_tokens(tokens_file, share_tokens + [pool_token])
print_log("Updated " + tokens_file)
write_tokens(session_tokens_file, session_tokens)
print_log("Updated " + session_tokens_file)
else:
time_to_wait = min_expire_time - current_time + 2
print_log("Next run at "+ time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time() + time_to_wait)))
time.sleep(time_to_wait)
def get_argument_value(args, option, default=None):
try:
index = args.index(option)
if index + 1 < len(args):
return args[index + 1]
except ValueError:
pass
return default
if __name__ == "__main__":
users_file = get_argument_value(sys.argv, '-a', users_file)
pool_token = get_argument_value(sys.argv, '-p', pool_token)
if '-a' in sys.argv:
process_users(users_file, session_tokens_file)
else:
process_tokens(tokens_file, session_tokens_file)