-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathremotePykib.py
264 lines (230 loc) · 11.2 KB
/
remotePykib.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
#!/usr/bin/env python3
# pykib - A PyQt5 based kiosk browser with a minimum set of functionality
# Copyright (C) 2021 Tobias Wintrich
#
# This file is part of pykib.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import sys
import os
import logging
import random
import string
import tempfile
import pykib_base.ui
import pykib_base.arguments
import pykib_base.mainWindow
import pykib_base.remotePykibWebsocketServer
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication, QSystemTrayIcon, QMenu, QAction
#Workaround for Problem with relative File Paths
class RemotePykib():
def __init__(self, args, dirname):
self.args = args
self.dirname = dirname
self.app = QApplication(sys.argv)
self.pykibInstances = {}
self.startRemotePykib()
def startRemotePykib(self):
logging.info("Pykib Remote Browser Daemon Mode")
#Create Temp Session Token if Option is set
if(self.args.useTemporarySessionToken):
logging.info("Temporary Session Token is used:")
characters = string.ascii_letters + string.digits
self.args.remoteBrowserSessionToken = ''.join(random.choice(characters) for i in range(128))
logging.info(" Token: " + self.args.remoteBrowserSessionToken)
token_path = tempfile.gettempdir()
if (self.args.temporarySessionTokenPath):
token_path = self.args.temporarySessionTokenPath
token_path = token_path.replace("\\", "/")+"/.pykibTemporarySessionToken"
with open(token_path, "w") as text_file:
text_file.write(self.args.remoteBrowserSessionToken)
logging.info(" Storing Token under: " + token_path)
self.app.setQuitOnLastWindowClosed(False)
# Creating a Tray App
icon = QIcon(os.path.join(self.dirname, 'icons/pykib.png'))
tray = QSystemTrayIcon()
tray.setIcon(icon)
tray.setVisible(True)
tray.setToolTip("Rangee Remote Browser Daemon")
# Create the menu
menu = QMenu()
# Add a Quit option to the menu.
quit = QAction("Close Rangee Remote Browser Daemon")
quit.triggered.connect(sys.exit)
menu.addAction(quit)
# Build Configuration Array fpr Plugin
config = {
"remotingList": self.args.remotingList.split(" "),
"allowUserBasedRemoting": self.args.allowUserBasedRemoting,
"remoteBrowserMoveInterval": self.args.remoteBrowserMoveInterval,
"remoteDaemonProtocolVersion": self.args.remoteDaemonProtocolVersion,
}
# Create an Start the Websocket Server Thread
websocketServer = pykib_base.remotePykibWebsocketServer.RemotePykibWebsocketServer(config,
self.args.remoteBrowserSessionToken,
self.args.remoteBrowserPort)
websocketServer.daemon = True # Daemonize thread
websocketServer.configureInstance.connect(self.configureInstance)
websocketServer.closeInstance.connect(self.closeInstance)
websocketServer.activateInstance.connect(self.activateInstance)
websocketServer.moveInstance.connect(self.moveInstance)
websocketServer.changeTabWindow.connect(self.changeTabWindow)
websocketServer.start()
# Add the menu to the tray
tray.setContextMenu(menu)
sys.exit(self.app.exec_())
def configureInstance(self, tabId, windowId, url):
logging.info("RemotePykib:")
#Create Window Array if not exist
try:
self.pykibInstances[windowId]
except:
self.pykibInstances[windowId] = {}
if(tabId in self.pykibInstances[windowId] and self.pykibInstances[windowId][tabId].web):
logging.info(" Tab found, set as CurrentView:"+str(tabId))
logging.info(" TabID: " + str(tabId))
logging.info(" WindowID: " + str(windowId))
print(self.pykibInstances[windowId][tabId])
currentView = self.pykibInstances[windowId][tabId]
try:
currentView.web.load(url)
except:
logging.info(" Tab should be availeable but is not. May be closed manually. creating new: " + str(tabId))
self.args.url = url
currentView = pykib_base.mainWindow.MainWindow(self.args, self.dirname)
self.pykibInstances[windowId][tabId] = currentView
else:
logging.info(" Tab not found, create new an set as CurrentView:" + str(tabId))
logging.info(" TabID: " + str(tabId))
logging.info(" WindowID: " + str(windowId))
self.args.url = url
currentView = pykib_base.mainWindow.MainWindow(self.args, self.dirname)
self.pykibInstances[windowId][tabId] = currentView
for key in self.pykibInstances[windowId]:
if(key != tabId):
logging.info(" Hiding Tabs with:")
logging.info(" TabID: " + str(key))
logging.info(" WindowID: " + str(windowId))
currentView.hide()
logging.info(" Showing CurrenView")
logging.info("------------------------------------------------------------")
currentView.show()
#currentView.activateWindow()
def closeInstance(self, tabId, windowId):
logging.info("RemotePykib:")
try:
if(tabId == 0 and windowId == 0):
logging.info(" Closing All Tabs")
try:
for window in self.pykibInstances:
try:
for tab in self.pykibInstances[window]:
self.pykibInstances[window][tab].web.close()
self.pykibInstances[window][tab].close()
except Exception as i:
logging.warning(i)
except Exception as e:
logging.warning(e)
self.pykibInstances = {}
elif(tabId == 0):
logging.info(" Closing All Tabs:")
logging.info(" WindowID: " + str(windowId))
try:
for tab in self.pykibInstances[windowId]:
logging.info(" Tab closed: " + str(tab))
self.pykibInstances[windowId][tab].web.close()
self.pykibInstances[windowId][tab].close()
except Exception as e:
logging.warning(e)
elif (tabId in self.pykibInstances[windowId]):
logging.info(" Closing Tabs:")
logging.info(" TabID: " + str(tabId))
logging.info(" WindowID: " + str(windowId))
self.pykibInstances[windowId][tabId].web.close()
self.pykibInstances[windowId][tabId].close()
del self.pykibInstances[windowId][tabId]
except Exception as e:
logging.warning(e)
return False
logging.info("------------------------------------------------------------")
def activateInstance(self, tabId, windowId):
logging.info("RemotePykib:")
logging.info(" Bringing Tab in Front")
logging.info(" TabID: " + str(tabId))
logging.info(" WindowID: " + str(windowId))
try:
if (tabId in self.pykibInstances[windowId]):
logging.info(" Tab found. Set as CurrentView")
self.pykibInstances[windowId][tabId].show()
#self.pykibInstances[windowId][tabId].activateWindow()
else:
logging.info(" Tab not found - Nothing to bring in Front.")
for tab in self.pykibInstances[windowId]:
if (tab != tabId):
logging.info(" Hiding Tabs with:")
logging.info(" TabID: " + str(tab))
logging.info(" WindowID: " + str(windowId))
try:
self.pykibInstances[windowId][tab].hide()
except Exception as e:
logging.info(" Error, Tab not found")
except Exception as e:
logging.info(" Error, WindowID not configured")
logging.info("------------------------------------------------------------")
def moveInstance(self, tabId, windowId, geometry, zoomFactor = 1):
logging.debug("RemotePykib:")
logging.debug(" Moving/Resizing Tab")
logging.debug(" TabID: " + str(tabId))
logging.debug(" WindowID: " + str(windowId))
try:
if(tabId in self.pykibInstances[windowId]):
logging.debug(" Tab found. Moving/Resizing")
self.args.setZoomFactor = zoomFactor * 100
#self.pykibInstances[windowId][tabId].web.setZoomFactor(zoomFactor)
logging.debug(" 1")
logging.debug(geometry)
logging.debug(self.args.screenOffsetLeft)
self.pykibInstances[windowId][tabId].setGeometry(int(geometry[0] + self.args.screenOffsetLeft), int(geometry[1]), int(geometry[2]), int(geometry[3]))
logging.debug(" 2")
self.pykibInstances[windowId][tabId].show()
logging.debug(" 3")
#self.pykibInstances[windowId][tabId].activateWindow()
else:
logging.debug(" Tab not found.")
except Exception as e:
logging.info(e)
logging.info(" Error, WindowID not found")
def changeTabWindow(self, tabId, oldWindowId, newWindowId):
logging.info("RemotePykib:")
logging.info(" Change Tab Window")
logging.info(" TabID: " + str(tabId))
logging.info(" oldWindowId: " + str(oldWindowId))
logging.info(" newWindowId: " + str(newWindowId))
try:
if(tabId in self.pykibInstances[oldWindowId]):
logging.debug(" Tab found. Move to new Window")
try:
# Array for newWindowId found
self.pykibInstances[newWindowId]
except:
# No Array for newWindowId exist, creating one
self.pykibInstances[newWindowId] = {}
#Move Tab from old to new Window Id
self.pykibInstances[newWindowId][tabId] = self.pykibInstances[oldWindowId][tabId]
del self.pykibInstances[oldWindowId][tabId]
else:
logging.info(" Tab not found.")
except Exception as e:
logging.info(e)