-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathpe_session.py
More file actions
222 lines (149 loc) · 5.54 KB
/
pe_session.py
File metadata and controls
222 lines (149 loc) · 5.54 KB
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
import traceback
from requests import TooManyRedirects
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.support import expected_conditions as EC
import faulthandler
from anticaptchaofficial.imagecaptcha import *
from PIL import Image
import json
import pe_api
import phone_api
from rich.console import Console
from pe_global_objects import log
import os
console = Console()
MAX_TRIES = 3
CAPTCHA_KEY = None
PROFILE_NAME = None
PHPSESS_NAME = "__Host-PHPSESSID"
def session_setup(captcha: str, profile: str) -> None:
global CAPTCHA_KEY, PROFILE_NAME
CAPTCHA_KEY = captcha
PROFILE_NAME = profile
def get_captcha(driver, element, path):
location = element.location
size = element.size
driver.save_screenshot(path)
image = Image.open(path)
left = location['x']
top = location['y']
right = location['x'] + size['width']
bottom = location['y'] + size['height']
image = image.crop((left, top, right, bottom))
image.save(path)
def solve(image_name: str, human: bool = False):
if human:
return input("Human CAPTCHA: ")
solver = imagecaptcha()
solver.set_key(CAPTCHA_KEY)
captcha_text = solver.solve_and_return_solution(image_name)
phone_api.bot_info("Consumed a CAPTCHA token")
return str(captcha_text)
def try_fetching_cookies(human: bool = False):
url = "https://projecteuler.net/sign_in"
filename = "web_utils/current-captcha.png"
if 'web_utils' not in os.listdir('.'):
os.mkdir('web_utils')
service = Service(executable_path='/usr/local/bin/geckodriver')
options = webdriver.FirefoxOptions()
options.add_argument("-headless")
driver = None
try:
driver = webdriver.Firefox(service=service, options=options)
driver.set_page_load_timeout(30)
driver.implicitly_wait(10)
driver.set_window_size(1080, 720)
driver.get(url)
captcha = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "captcha_image"))
)
get_captcha(driver, captcha, filename)
captcha_result = solve(filename, human)
log.info(f"[-] Tried to guess {captcha_result} as Captcha")
driver.find_element("xpath",
"//input[@id='username' and @name='username']"
).send_keys("EulerCommunity")
driver.find_element("xpath",
"//input[@id='password' and @name='password']"
).send_keys("IncredibleBoy")
driver.find_element("xpath",
"//input[@id='captcha' and @name='captcha']"
).send_keys(captcha_result)
driver.find_element("xpath",
"//input[@id='remember_me' and @name='remember_me']"
).click()
driver.find_element("xpath",
"//input[@name='sign_in' and @type='submit']"
).click()
cookies = driver.get_cookies()
return cookies
except Exception as e:
# logging.error(f"[!] Error during browser automation: {e}")
# traceback.print_exc()
log.exception(e)
return []
finally:
if driver:
try:
driver.quit()
except Exception as e:
log.exception(e)
# console.log(f"[!] Error closing driver: {e}")
def refresh_tokens():
faulthandler.enable()
human = False
current_tries = 0
found_keepalive = False
values = {PHPSESS_NAME: None, "keep_alive": None} # [PHPSESSID, keep_alive]
while not found_keepalive and current_tries < MAX_TRIES:
cookies = try_fetching_cookies(human)
current_tries += 1
log.info(f"Making try #{current_tries} to refresh cookies")
for cookie in cookies:
log.info(f'{cookie["name"]}, {cookie["value"]}')
if cookie["name"] == PHPSESS_NAME:
values[PHPSESS_NAME] = cookie["value"]
if cookie["name"] == "keep_alive":
found_keepalive = True
values["keep_alive"] = cookie["value"]
if values["keep_alive"] is not None:
phone_api.bot_info("Token refreshed automatically")
log.info(f"Token refreshed automatically {values[PHPSESS_NAME]}")
else:
phone_api.bot_crashed("Failed to refresh token")
log.error("Failed to refresh token")
with open(PROFILE_NAME, "r") as f:
data = json.load(f)
data["session_keys"] = values
with open(PROFILE_NAME, "w") as f:
json.dump(data, f, indent=4)
pe_api.COOKIES = values
return values
def is_connected() -> bool:
try:
pe_request = pe_api.ProjectEulerRequest("https://projecteuler.net/archives", True)
except TooManyRedirects as exc:
pe_api.console.log(exc, traceback.format_exc())
log.exception(exc)
return False
except pe_api.EulerRequestFail:
return False
if pe_request.status != 200:
return False
return "Logged in as" in pe_request.response
def is_website_active() -> bool:
try:
pe_request = pe_api.ProjectEulerRequest("https://projecteuler.net/", False)
except pe_api.EulerRequestFail as _:
return False
return pe_request.status == 200
if __name__ == "__main__":
profile_name = "profiles/authentic.json"
with open(profile_name, "r") as f:
data = json.load(f)
session_setup(data["captcha_key"], profile_name)
print(is_connected())
print(refresh_tokens())