This repository has been archived by the owner on Feb 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathaccounts.py
395 lines (332 loc) · 14.8 KB
/
accounts.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
from PyQt5.QtWidgets import (
QStackedWidget,
QMainWindow,
QPushButton,
QVBoxLayout,
QInputDialog,
QHBoxLayout,
QScrollArea,
QMessageBox,
QWidget
)
from PyQt5.QtWebEngineWidgets import (QWebEngineView, QWebEngineProfile, QWebEnginePage)
from PyQt5.QtWebEngineCore import (QWebEngineUrlRequestInterceptor)
from PyQt5.QtWebChannel import QWebChannel
from PyQt5.QtGui import (QIcon, QCursor)
from PyQt5.QtCore import (QUrl, Qt)
from controller import Controller
import shutil
import json
import os
SIGNALS = None
# sobrescreve as funções de log javascript para que, todas as saídas geradas sejam escritas no arquivo de log do maturador
class LogCapturingPage(QWebEnginePage):
def consoleMessage(self, level, message, lineNumber, sourceID):
try:
log_message = f"{level} - {message}"
with open("MaturadorLogs.txt", "a", encoding="utf-8") as log_file:
log_file.write(log_message + "\n")
except:
pass
def javaScriptConsoleMessage(self, level, message, lineNumber, sourceID):
try:
log_message = f"{level} - {message}"
with open("MaturadorLogs.txt", "a", encoding="utf-8") as log_file:
log_file.write(log_message + "\n")
except:
pass
# interceptar as requisições que vem do WhatsApp web para filtrar
# as que são feitas para enviar dados ao maturador, isso é necessário
# pois o WhatsApp web bloqueia requisições feitas para domínios
# diferente do deles através do content security policy (CSP).
class RequestInterceptor(QWebEngineUrlRequestInterceptor):
def interceptRequest(self, info):
# novo numero adicionado
if "/maturador/api/account-added" in info.requestUrl().path():
url_path = info.requestUrl().path()
json_string = url_path[url_path.find("{"):url_path.find("}") + 1]
data = json.loads(json_string)
global SIGNALS
SIGNALS.new_phone_number.emit(data)
info.block(True)
# conta bloqueada
elif "/maturador/api/account-blocked" in info.requestUrl().path():
url_path = info.requestUrl().path()
json_string = url_path[url_path.find("{"):url_path.find("}") + 1]
data = json.loads(json_string)
SIGNALS.account_blocked.emit(data)
info.block(True)
class MainWindow(QMainWindow):
def __init__(self):
self.controller:Controller = None
self._is_open = False
super().__init__()
self.webs_engine = []
self.setWindowTitle("Maturador de chips - Contas conectadas")
self.setWindowIcon(QIcon("pages/assets/medias/icon.ico"))
self.setFixedSize(1060, 650)
with open(file="user-agent", mode="r", encoding="utf8") as f: self.USER_AGENT = f.read()
self.sessions_waiting_to_be_disconnected = []
self.webviews:QWebEngineView = []
self.buttons_layout = QHBoxLayout()
self.stacked_widget = QStackedWidget()
self.buttons = {}
self.selected_button = None
self.load_webviews()
# criar o QStackedWidget para alternar entre os webviews
for webview in self.webviews:
self.stacked_widget.addWidget(webview)
self.buttons_widget = QWidget()
self.buttons_widget.setFixedHeight(50)
# configura o layout dos botoes de alternar entre instancias
buttons_scroll_area = QScrollArea()
buttons_scroll_area.setWidgetResizable(True)
buttons_scroll_area.setFixedHeight(50)
buttons_container = QWidget()
buttons_container.setLayout(self.buttons_layout)
buttons_scroll_area.setWidget(buttons_container)
#o layout principal como QVBoxLayout
main_layout = QVBoxLayout()
# configurar widget central da janela
central_widget = QWidget()
central_widget.setLayout(main_layout)
self.setCentralWidget(central_widget)
# adicionar o QScrollArea dentro layout principal
main_layout.addWidget(buttons_scroll_area)
# adicionar o QStackedWidget ao layout principal
main_layout.addWidget(self.stacked_widget)
# botões "adicionar" e "remover" layout
buttons_container = QWidget()
buttons_container.setLayout(main_layout)
button_layout = QHBoxLayout()
add_instance_button = QPushButton("Adicionar")
add_instance_button.clicked.connect(self.create_session)
remove_instance_button = QPushButton("Remover")
remove_instance_button.clicked.connect(self.delete_session)
# personaliza os dois botões "adicionar" e "remover"
add_instance_button.setCursor(QCursor(Qt.PointingHandCursor))
remove_instance_button.setCursor(QCursor(Qt.PointingHandCursor))
add_instance_button.setStyleSheet(
'''
QPushButton {
background-color: #f56642;
color: white;
font-size: 13px;
width: 23px;
height: 23px;
margin-right: 0x;
border-radius: 0px;
}
QPushButton:hover {
background-color: #fc4f23;
}
'''
)
remove_instance_button.setStyleSheet(
'''
QPushButton {
background-color: #ed4a47;
color: white;
font-size: 13px;
width: 23px;
height: 23px;
margin-right: 0x;
border-radius: 0px;
}
QPushButton:hover {
background-color: #fa2420;
}
'''
)
button_layout.addWidget(add_instance_button)
button_layout.addWidget(remove_instance_button)
buttons_container.layout().addLayout(button_layout)
# configurar o widget central da janela
central_widget = QWidget()
central_widget.setLayout(main_layout)
self.setCentralWidget(central_widget)
def closeEvent(self, event):
self._is_open = False
event.accept()
# criar nova sessão/webview
def create_session(self):
while True:
session_name, want_continue = QInputDialog.getText(self, "Maturador de chips" ,"qual o nome você quer definir para essa instância?")
if not want_continue:
return
elif not session_name or session_name.isspace():
QMessageBox.critical(self, "Maturador de chips","o nome da instância não pode estar vazio")
elif len(session_name) > 11:
QMessageBox.critical(self, "Maturador de chips","o nome da instância não pode ter mais de 11 dígitos")
elif os.path.exists(os.path.join("sessions", f"@{session_name}")):
QMessageBox.critical(self, "Maturador de chips", f"já existe uma instância com o nome {session_name}, escolha outro.")
else:
session_name = "@" + session_name
break
webview = QWebEngineView()
profile = QWebEngineProfile(f"storage{session_name}", webview)
session_dir = os.getcwd() + f"/sessions/{session_name}"
profile.setCachePath(session_dir)
profile.setPersistentStoragePath(session_dir)
profile.setDownloadPath(session_dir)
profile.setPersistentCookiesPolicy(QWebEngineProfile.AllowPersistentCookies)
profile.setHttpUserAgent(self.USER_AGENT)
profile.setHttpAcceptLanguage("pt-br")
interceptor = RequestInterceptor(webview)
profile.setUrlRequestInterceptor(interceptor)
engine = LogCapturingPage(profile, webview)
webview.setPage(engine)
self.webs_engine.append(engine)
webview.load(QUrl("https://web.whatsapp.com/"))
self.stacked_widget.addWidget(webview)
self.webviews.append(webview)
self.create_button(session_name).click()
webview.page().loadFinished.connect(lambda ok, sessionname=session_name : self.run_script(sessionname))
# carregar sessões de webviews já existentes
def load_webviews(self):
try:
sessions = [session for session in os.listdir("sessions") if session.startswith("@")]
except FileNotFoundError: # a pasta de sessões ainda não existe
return
for sessionname in sessions:
webview = QWebEngineView()
profile = QWebEngineProfile(f"storage{sessionname}", webview)
session_dir = os.getcwd() + f"/sessions/{sessionname}"
# apaga Service Worker porque está causando problema com a sessão
service_Worker_path = os.path.join(session_dir, "Service Worker")
if os.path.exists(path=service_Worker_path):
shutil.rmtree(path=service_Worker_path)
profile.setCachePath(session_dir)
profile.setPersistentStoragePath(session_dir)
profile.setDownloadPath(session_dir)
profile.setPersistentCookiesPolicy(QWebEngineProfile.AllowPersistentCookies)
profile.setHttpUserAgent(self.USER_AGENT)
profile.setHttpAcceptLanguage("pt-br")
interceptor = RequestInterceptor(webview)
profile.setUrlRequestInterceptor(interceptor)
engine = LogCapturingPage(profile, webview)
webview.setPage(engine)
self.webs_engine.append(engine)
webview.load(QUrl("https://web.whatsapp.com/"))
self.create_button(sessionname)
self.webviews.append(webview)
webview.page().loadFinished.connect(lambda ok, session_name=sessionname : self.run_script(session_name))
# injeta script login.js e script do webchannel
def run_script(self, session_name:str):
for idx, key in enumerate(start=0, iterable=self.buttons.keys()):
if key == session_name:
script = open(
file="scripts/login.js",
mode="r",
encoding="utf8"
)
webview:QWebEngineView = self.webviews[idx]
webview.page().runJavaScript(script.read().replace('@INSTANCE', str(idx) ).replace("@SESSIONNAME", session_name))
script.close()
# criar botão para alternar entre o webview
def create_button(self, session_name:str) -> QPushButton:
button = QPushButton(session_name.replace('@', ''))
button.setMinimumWidth(000)
button.setStyleSheet('''
QPushButton {
background-color: #6ab8b8;
color: white;
font-size: 13px;
width: 50px;
height: 20px;
margin-right: 5px;
border-radius: 10px;
}
QPushButton:hover {
background-color: #45a049;
}
''')
button.setCursor(QCursor(Qt.PointingHandCursor))
button.clicked.connect(lambda _, name=session_name: self.switch_tab(name))
self.buttons_layout.addWidget(button)
self.buttons.update(
{
session_name:button
}
)
if len(self.buttons) == 1:
button.click()
return button
# troca o webview padrão
def switch_tab(self, session_name):
for idx, key in enumerate(start=0, iterable=self.buttons.keys()):
if session_name == key:
self.selected_button = str(session_name)
self.stacked_widget.setCurrentIndex(idx)
self.highlight_current_button(idx)
# destaca o botão do webview atual
def highlight_current_button(self, idx):
for i, button in enumerate(self.buttons):
button = self.buttons[button]
if i == idx:
button.setStyleSheet('''
QPushButton {
background-color: #4f6b6b;
color: white;
font-size: 13px;
width: 50px;
height: 20px;
margin-right: 5px;
border-radius: 10px;
}
''')
else:
button.setStyleSheet('''
QPushButton {
background-color: #6ab8b8;
color: white;
font-size: 13px;
width: 50px;
height: 20px;
margin-right: 5px;
border-radius: 10px;
}
QPushButton:hover {
background-color: #45a049;
}
''')
# apagar uma instancia
def delete_session(self):
if not self.selected_button:
return QMessageBox.critical(self, "Maturador de chips","nenhuma instância aberta, impossível apagar.")
confirm = QMessageBox.question(
self,
"Maturador de chips",
f"""você tem certeza que quer apagar a instância "{self.selected_button.replace("@", "")}" ?""",
QMessageBox.StandardButton.Apply,
QMessageBox.StandardButton.Abort
)
if confirm == QMessageBox.StandardButton.Abort:
return
for idx, key in enumerate(start=0, iterable=self.buttons.keys()):
if key == self.selected_button:
self.buttons_layout.removeWidget(self.buttons[key])
self.stacked_widget.removeWidget(self.webviews[idx])
try:
self.switch_tab(
list(self.buttons.keys())[idx - 1]
)
except (KeyError, IndexError):
self.selected_button = None
self.webviews.pop(idx)
self.webs_engine.pop(idx)
self.buttons.pop(key)
self.sessions_waiting_to_be_disconnected.append(
os.path.join(
"sessions",
key
)
)
try:
self.controller.connected_numbers.pop(key)
SIGNALS.new_phone_number.emit({})
except KeyError:
pass
break
if not self.buttons:
self.selected_button = None