-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
330 lines (260 loc) · 11.5 KB
/
main.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
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
import sys
import json
import asyncio
import time
import random
from fake_useragent import UserAgent
from loguru import logger
from seleniumwire import webdriver
from undetected_chromedriver import ChromeOptions
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
logger.remove()
logger.add(sys.stdout, level="INFO", format="{time:YYYY-MM-DD HH:mm:ss} | {level} | {message}")
with open('config.json', 'r') as f:
config = json.load(f)
MAX_ATTEMPTS = 5
DELAY = 10
ACCOUNTS_FILE = config['accounts_file']
PROXIES_FILE = config['proxies_file']
SESSION_INTERVAL = config['session_interval']
EXTENSION_PATH = './extensions/gradient_extension.crx'
CHROMEDRIVER_PATH = './chromedriver-linux64/chromedriver'
def load_data(filename):
with open(filename, 'r') as f:
return [line.strip() for line in f]
def modify_headers(request):
request.headers['Accept-Language'] = 'en-US,en;q=0.9'
request.headers['Accept-Encoding'] = 'gzip, deflate, br'
def setup_driver(proxy):
chrome_options = ChromeOptions()
seleniumwire_options = {
"proxy": {
"http": proxy,
"https": proxy,
},
'disable_encoding': True,
'suppress_connection_errors': True,
'verify_ssl': False,
'connection_timeout': None,
'connection_keep_alive': True,
'no_proxy': '',
'headers': {
'User-Agent': UserAgent().random
}
}
chrome_options.add_argument('--ignore-certificate-errors')
chrome_options.add_argument('--ignore-ssl-errors')
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_argument(f'user-agent={UserAgent().random}')
chrome_options.add_argument('--disable-blink-features=AutomationControlled')
chrome_options.add_argument("--verbose")
chrome_options.add_argument("--log-level=3")
chrome_options.add_argument('--disable-software-rasterizer')
chrome_options.add_argument('--disable-webgl')
chrome_options.add_argument('--disable-webgl-extensions')
chrome_options.add_argument('--disable-accelerated-2d-canvas')
chrome_options.add_argument('--disable-accelerated-jpeg-decoding')
chrome_options.add_argument('--disable-accelerated-video-decode')
chrome_options.add_argument('--disable-gpu-sandbox')
chrome_options.add_argument('--disable-webrtc')
print(f"Loading extension from: {EXTENSION_PATH}")
chrome_options.add_extension(EXTENSION_PATH)
driver = webdriver.Chrome(seleniumwire_options=seleniumwire_options, options=chrome_options)
driver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")
driver.request_interceptor = modify_headers
driver.execute_script("""
Object.defineProperty(navigator, 'mediaDevices', {get: () => undefined});
""")
return driver
def close_popups(driver):
try:
close_button = WebDriverWait(driver, 5).until(
EC.presence_of_element_located((By.XPATH,
"//div[contains(@class, 'flex-row-center') and contains(@class, 'bg-[#fff]') and contains(@class, 'rounded-full')]"))
)
close_button.click()
logger.info("Popup 1 was closed")
except Exception:
logger.info("Popup 1 not found or unable to close")
try:
got_it_button = WebDriverWait(driver, 5).until(
EC.element_to_be_clickable(
(By.XPATH, "//button[contains(@class, 'w-full') and contains(text(), 'I got it')]"))
)
got_it_button.click()
logger.info("Popup 2 was closed")
except Exception:
logger.info("Popup 2 not found or unable to close")
def wait_for_page_load(driver):
try:
WebDriverWait(driver, 120).until(
lambda d: d.execute_script('return document.readyState') == 'complete'
)
time.sleep(15)
logger.info("Page fully loaded")
except Exception as e:
logger.error(f"Timeout waiting for page load: {str(e)}")
raise
def login_to_extension(driver, username, password):
try:
driver.get('chrome-extension://caacbgbklghmpodbdafajbgdnegacfmo/popup.html')
wait_for_page_load(driver)
window_handles = driver.window_handles
logger.info(f"Window handles: {window_handles}")
while len(driver.window_handles) < 2:
pass
driver.switch_to.window(driver.window_handles[-1])
wait_for_page_load(driver)
logger.info("Switched to the second tab")
driver.switch_to.window(window_handles[-1])
email_input = WebDriverWait(driver, 15).until(
EC.presence_of_element_located((By.XPATH, "/html/body/div[1]/div[2]/div/div/div/div[2]/div[1]/input"))
)
email_input.send_keys(username)
logger.info("Entered username")
password_input = WebDriverWait(driver, 15).until(
EC.presence_of_element_located((By.XPATH, "/html/body/div[1]/div[2]/div/div/div/div[2]/div[2]/span/input"))
)
password_input.send_keys(password)
logger.info("Entered password")
login_button = driver.find_element(By.XPATH, "/html/body/div[1]/div[2]/div/div/div/div[4]/button[1]")
login_button.click()
logger.info("Logging button clicked")
wait_for_page_load(driver)
close_popups(driver)
logger.info("Successfull login!")
driver.switch_to.window(driver.window_handles[0])
wait_for_page_load(driver)
logger.info("Switched back to extension tab")
if driver.current_url.startswith('chrome-extension://'):
logger.info("Successfully returned to extension page")
driver.refresh()
wait_for_page_load(driver)
logger.info("Extension page refreshed")
time.sleep(random.randint(1, 5))
close_popups(driver)
time.sleep(random.randint(1, 5))
return True
else:
logger.error("Failed to return to extension page")
return False
except Exception as e:
logger.error(f"Error during login: {str(e)}")
return False
async def run_session_maintenance(driver, interval, username, password, proxy):
while True:
try:
await maintain_session(driver, username, password, proxy)
await asyncio.sleep(interval)
except Exception as e:
logger.error(f"[{username}] Critical error in session maintenance: {str(e)}")
break
async def farm_points(account, proxy):
username, password = account.split(':')
logger.info(f"[{username}] Start farming")
driver = None
login_attempts = 0
maintenance_task = None
while login_attempts < MAX_ATTEMPTS:
try:
if driver:
driver.quit()
driver = setup_driver(proxy)
if login_to_extension(driver, username, password):
logger.info(f"[{username}] Successfully logged in")
if maintenance_task and not maintenance_task.done():
maintenance_task.cancel()
maintenance_task = asyncio.create_task(
run_session_maintenance(driver, SESSION_INTERVAL, username, password, proxy)
)
while True:
await asyncio.sleep(60)
else:
raise Exception("Login failed")
except Exception as e:
login_attempts += 1
logger.error(f"[{username}] Error occurred (Attempt {login_attempts}/{MAX_ATTEMPTS}): {str(e)}")
if login_attempts < MAX_ATTEMPTS:
logger.info(f"[{username}] Retrying login in {DELAY} seconds...")
await asyncio.sleep(DELAY)
else:
logger.error(f"[{username}] Max login attempts reached. Giving up.")
finally:
if maintenance_task and not maintenance_task.done():
maintenance_task.cancel()
if driver:
driver.quit()
logger.error(f"[{username}] Failed to farm points after {MAX_ATTEMPTS} attempts.")
async def maintain_session(driver, username, password, proxy):
attempts = 0
while attempts < MAX_ATTEMPTS:
try:
logger.info(f"[{username}] Starting session maintenance")
driver.switch_to.window(driver.window_handles[1])
logger.info(f"[{username}] Switched to dashboard tab")
driver.refresh()
wait_for_page_load(driver)
logger.info(f"[{username}] Dashboard refreshed")
points_element = WebDriverWait(driver, 30).until(
EC.presence_of_element_located(
(By.XPATH, "/html/body/div[1]/div[1]/div[2]/main/div/div/div[2]/div/div[1]/div[2]/div[1]")
)
)
points = points_element.text
logger.info(f"[{username}] Current points: {points}")
driver.switch_to.window(driver.window_handles[0])
driver.refresh()
wait_for_page_load(driver)
logger.info(f"[{username}] Extension tab refreshed")
connection_status = WebDriverWait(driver, 30).until(
EC.presence_of_element_located((By.XPATH, "//*/div/div[1]/div[2]/div[3]/div[2]/div/div[2]/div"))
)
connection = connection_status.text
if(connection == "Good"):
logger.info(f"[{username}] Connection status: {connection}")
else:
logger.info(f"Your connection status is {connection}. Terminating session.")
driver.quit()
driver = setup_driver(proxy)
if not login_to_extension(driver, username, password):
logger.error(f"[{username}] Login failed after restarting driver.")
raise Exception("Login failed after driver restart")
logger.info(f"[{username}] Successfully re-logged in after driver restart")
attempts = 0
await asyncio.sleep(SESSION_INTERVAL)
except Exception as e:
attempts += 1
logger.error(f"[{username}] Error during session maintenance (Attempt {attempts}/{MAX_ATTEMPTS}): {str(e)}")
if attempts >= MAX_ATTEMPTS:
logger.error(f"[{username}] Max attempts reached. Restarting driver...")
driver.quit()
driver = setup_driver(proxy)
if not login_to_extension(driver, username, password):
logger.error(f"[{username}] Login failed after restarting driver.")
raise Exception("Login failed after driver restart")
logger.info(f"[{username}] Successfully re-logged in after driver restart")
attempts = 0
await asyncio.sleep(DELAY)
async def main():
accounts = load_data(ACCOUNTS_FILE)
proxies = load_data(PROXIES_FILE)
if len(accounts) == 0:
logger.info("File accounts.txt can't be empty")
return
if len(proxies) == 0:
logger.info("File proxy.txt can't be empty")
return
if len(accounts) > len(proxies):
logger.info("You should have proxies <= than accounts.")
return
tasks = []
for i in range(min(len(accounts), len(proxies))):
tasks.append(farm_points(accounts[i], proxies[i]))
await asyncio.gather(*tasks)
if __name__ == "__main__":
asyncio.run(main())