-
Notifications
You must be signed in to change notification settings - Fork 7
/
bot.py
214 lines (175 loc) · 7.21 KB
/
bot.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
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
import asyncio
import cloudscraper
import json
import time
from loguru import logger
import requests
from concurrent.futures import ThreadPoolExecutor
PING_INTERVAL = 60
RETRIES = 60
MAX_PROXY_PER_TOKEN = 3 # Misalnya batas maksimum proxy per token
DOMAIN_API = {
"SESSION": "http://api.nodepay.ai/api/auth/session",
"PING": [
"https://nw.nodepay.org/api/network/ping"
]
}
CONNECTION_STATES = {
"CONNECTED": 1,
"DISCONNECTED": 2,
"NONE_CONNECTION": 3
}
BASE_PROXY = "ONLY BASE PROXY"
class AccountInfo:
def __init__(self, token):
self.token = token
self.proxies = [BASE_PROXY] # Selalu gunakan BASE_PROXY
self.status_connect = CONNECTION_STATES["NONE_CONNECTION"]
self.account_data = {}
self.retries = 0
self.last_ping_status = 'Waiting...'
self.browser_id = {
'ping_count': 0,
'successful_pings': 0,
'score': 0,
'start_time': time.time(),
'last_ping_time': None
}
def reset(self):
self.status_connect = CONNECTION_STATES["NONE_CONNECTION"]
self.account_data = {}
self.retries = 3
def get_proxy(self):
"""Mengambil proxy yang akan digunakan (selalu BASE_PROXY)"""
return self.proxies[0] # Selalu gunakan proxy yang sama
# Cloudscraper instance
scraper = cloudscraper.create_scraper(
browser={
'browser': 'chrome',
'platform': 'windows',
'desktop': True
}
)
async def load_tokens():
try:
with open('Token.txt', 'r') as file:
tokens = []
for line in file:
# Menghapus spasi di awal dan akhir, lalu memeriksa apakah baris bukan komentar
line = line.strip()
if line and not line.startswith('#'): # Mengabaikan baris yang kosong atau diawali dengan '#'
tokens.append(line)
return tokens
except Exception as e:
logger.error(f"Failed to load tokens: {e}")
raise SystemExit("Exiting due to failure in loading tokens")
async def call_api(url, data, account_info, proxy):
headers = {
"Authorization": f"Bearer {account_info.token}",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36",
"Accept-Language": "en-US,en;q=0.9",
"Referer": "https://app.nodepay.ai/",
"Accept": "*/*",
"Content-Type": "application/json",
"Origin": "chrome-extension://lgmpfmgeabnnlemejacfljbmonaomfmm",
}
proxy_config = {
"http": proxy,
"https": proxy
}
try:
response = scraper.post(url, json=data, headers=headers, proxies=proxy_config, timeout=60)
response.raise_for_status()
except Exception as e:
logger.error(f"Error during API call for token {account_info.token} with proxy {proxy}: {e}")
raise ValueError(f"Failed API call to {url}")
return response.json()
async def render_profile_info(account_info):
try:
for proxy in account_info.proxies:
try:
response = await call_api(DOMAIN_API["SESSION"], {}, account_info, proxy)
if response.get("code") == 0:
account_info.account_data = response["data"]
if account_info.account_data.get("uid"):
await start_ping(account_info)
return
else:
logger.warning(f"Session failed for token {account_info.token} using proxy {proxy}")
except Exception as e:
logger.error(f"Failed to render profile info for token {account_info.token} using proxy {proxy}: {e}")
logger.error(f"All proxies failed for token {account_info.token}")
except Exception as e:
logger.error(f"Error in render_profile_info for token {account_info.token}: {e}")
async def start_ping(account_info):
try:
logger.info(f"Starting ping for token {account_info.token}")
while True:
for proxy in account_info.proxies:
try:
await asyncio.sleep(PING_INTERVAL)
await ping(account_info, proxy)
except Exception as e:
logger.error(f"Ping failed for token {account_info.token} using proxy {proxy}: {e}")
except asyncio.CancelledError:
logger.info(f"Ping task for token {account_info.token} was cancelled")
except Exception as e:
logger.error(f"Error in start_ping for token {account_info.token}: {e}")
async def ping(account_info, proxy):
for url in DOMAIN_API["PING"]:
try:
data = {
"id": account_info.account_data.get("uid"),
"browser_id": account_info.browser_id,
"timestamp": int(time.time())
}
response = await call_api(url, data, account_info, proxy)
# Periksa apakah response adalah dictionary dan memiliki key 'code'
if isinstance(response, dict) and "code" in response:
if response["code"] == 0:
logger.info(f"Ping successful for token {account_info.token} using proxy {proxy}")
return
else:
logger.error(f"Ping failed for token {account_info.token} with code {response.get('code')} using proxy {proxy}")
else:
logger.error(f"Unexpected response format for token {account_info.token} using proxy {proxy}: {response}")
except Exception as e:
logger.error(f"Ping failed for token {account_info.token} using URL {url} and proxy {proxy}: {e}")
handle_ping_fail(account_info, e)
def handle_ping_fail(account_info, response):
global RETRIES
RETRIES += 1
# Cek apakah response error dengan code 403 atau masalah lain
if isinstance(response, dict) and response.get("code") == 403:
handle_logout(account_info)
elif RETRIES < 2:
account_info.status_connect = CONNECTION_STATES["DISCONNECTED"]
else:
account_info.status_connect = CONNECTION_STATES["DISCONNECTED"]
logger.warning(f"Retry limit exceeded for account {account_info.token}, continuing with the same proxy.")
def handle_logout(proxy):
global status_connect, account_info
status_connect = CONNECTION_STATES["NONE_CONNECTION"]
account_info = {}
save_status(proxy, None)
logger.error(f"Logged out and cleared session info for proxy {proxy}")
def process_account(token):
"""
Process a single account: Initialize proxies and start asyncio event loop for this account.
"""
account_info = AccountInfo(token)
asyncio.run(render_profile_info(account_info))
async def main():
tokens = await load_tokens()
with ThreadPoolExecutor(max_workers=1000) as executor:
futures = []
for token in tokens:
futures.append(executor.submit(process_account, token))
# Wait for all tasks to complete
for future in futures:
future.result() # Block until each future is done
if __name__ == '__main__':
try:
asyncio.run(main())
except (KeyboardInterrupt, SystemExit):
logger.info("Program terminated by user.")