-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.py
193 lines (161 loc) · 7.02 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
import os
import distro
import platform
import subprocess
import random
import time
import logging
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException, TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
def setup_logging():
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
def connection_status(driver):
if wait_for_element_exists(driver, By.XPATH, "//*[text()='Connected']"):
logging.info("Status: Connected!")
elif wait_for_element_exists(driver, By.XPATH, "//*[text()='Disconnected']"):
logging.warning("Status: Disconnected!")
else:
logging.warning("Status: Unknown!")
def check_active_element(driver):
try:
wait_for_element(driver, By.XPATH, "//*[text()='Activated']")
driver.find_element(By.XPATH, "//*[text()='Activated']")
logging.info("Extension is activated!")
except NoSuchElementException:
logging.error("Failed to find 'Activated' element. Extension activation failed.")
def wait_for_element_exists(driver, by, value, timeout=10):
try:
WebDriverWait(driver, timeout).until(EC.presence_of_element_located((by, value)))
return True
except TimeoutException:
return False
def wait_for_element(driver, by, value, timeout=10):
try:
element = WebDriverWait(driver, timeout).until(EC.presence_of_element_located((by, value)))
return element
except TimeoutException as e:
logging.error(f"Error waiting for element {value}: {e}")
raise
def set_local_storage_item(driver, key, value):
driver.execute_script(f"localStorage.setItem('{key}', '{value}');")
result = driver.execute_script(f"return localStorage.getItem('{key}');")
return result
def add_cookie_to_local_storage(driver, cookie_value):
keys = ['np_webapp_token', 'np_token']
for key in keys:
result = set_local_storage_item(driver, key, cookie_value)
logging.info(f"Added {key} with value {result[:8]}...{result[-8:]} to local storage.")
logging.info("!!!!! Your token can be used to login for 7 days !!!!!")
def get_chromedriver_version():
try:
result = subprocess.run(['chromedriver', '--version'], capture_output=True, text=True)
return result.stdout.strip()
except Exception as e:
logging.error(f"Could not get ChromeDriver version: {e}")
return "Unknown version"
def get_os_info():
try:
os_info = {
'System': platform.system(),
'Version': platform.version()
}
if os_info['System'] == 'Linux':
os_info.update({
'System': distro.name(pretty=True),
'Version': distro.version(pretty=True, best=True)
})
return os_info
except Exception as e:
logging.error(f"Could not get OS information: {e}")
return "Unknown OS"
def run():
setup_logging()
branch = ''
version = '1.0.9' + branch
secUntilRestart = 60
logging.info(f"Started the script {version}")
try:
os_info = get_os_info()
logging.info(f'OS Info: {os_info}')
# Read variables from the OS env
cookie = os.getenv('NP_COOKIE')
extension_id = os.getenv('EXTENSION_ID')
extension_url = os.getenv('EXTENSION_URL')
# Check if credentials are provided
if not cookie:
logging.error('No cookie provided. Please set the NP_COOKIE environment variable.')
return # Exit the script if credentials are not provided
chrome_options = Options()
chrome_options.add_extension(f'./{extension_id}.crx')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--headless=new')
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_argument("--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36 Edg/121.0.0.0")
# Initialize the WebDriver
chromedriver_version = get_chromedriver_version()
logging.info(f'Using {chromedriver_version}')
driver = webdriver.Chrome(options=chrome_options)
except Exception as e:
logging.error(f'An error occurred: {e}')
logging.error(f'Restarting in 60 seconds...')
time.sleep(secUntilRestart)
run()
try:
# NodePass checks for width less than 1024p
driver.set_window_size(1024, driver.get_window_size()['height'])
# Navigate to a webpage
logging.info(f'Navigating to {extension_url} website...')
driver.get(extension_url)
time.sleep(random.randint(3,7))
add_cookie_to_local_storage(driver, cookie)
# Check successful login
while not wait_for_element_exists(driver, By.XPATH, "//*[text()='Dashboard']"):
logging.info(f'Refreshing in {secUntilRestart} seconds to check login (If stuck, verify your token)...')
driver.get(extension_url)
logging.info('Logged in successfully!')
time.sleep(random.randint(10,50))
logging.info('Accessing extension settings page...')
driver.get(f'chrome-extension://{extension_id}/index.html')
time.sleep(random.randint(3,7))
# Refresh until the "Login" button disappears
while wait_for_element_exists(driver, By.XPATH, "//*[text()='Login']"):
logging.info('Clicking the extension login button...')
login = driver.find_element(By.XPATH, "//*[text()='Login']")
login.click()
time.sleep(10)
# Refresh the page
driver.refresh()
# Check for the "Activated" element
check_active_element(driver)
# Get handles for all windows
all_windows = driver.window_handles
# Get the handle of the active window
active_window = driver.current_window_handle
# Close all windows except the active one
for window in all_windows:
if window != active_window:
driver.switch_to.window(window)
driver.close()
# Switch back to the active window
driver.switch_to.window(active_window)
connection_status(driver)
except Exception as e:
logging.error(f'An error occurred: {e}')
logging.error(f'Restarting in {secUntilRestart} seconds...')
driver.quit()
time.sleep(secUntilRestart)
run()
while True:
try:
time.sleep(3600)
driver.refresh()
connection_status(driver)
except KeyboardInterrupt:
logging.info('Stopping the script...')
driver.quit()
break
run()