-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.py
executable file
Β·198 lines (154 loc) Β· 6.45 KB
/
app.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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
__author__ = "Md. Minhazul Haque"
__license__ = "GPLv3"
from PyQt5.QtWidgets import QWidget, QLabel, QLineEdit, QPushButton, QApplication, QGridLayout, QDialog, QMessageBox
from PyQt5.QtCore import QProcess, Qt, QUrl, QSharedMemory
from PyQt5.QtGui import QIcon, QDesktopServices, QPixmap
from urllib.parse import urlparse
from deepdiff import DeepDiff
import sys
import yaml
import shutil
import time
import glob
import os
import requests
from tunnel import Ui_Tunnel
from tunnelconfig import Ui_TunnelConfig
from vars import CONF_FILE, LANG, KEYS, ICONS, CMDS
import icons
class TunnelConfig(QDialog):
def __init__(self, parent, data):
super(TunnelConfig, self).__init__(parent)
self.ui = Ui_TunnelConfig()
self.ui.setupUi(self)
self.ui.remote_address.setText(data.get(KEYS.REMOTE_ADDRESS))
self.ui.proxy_host.setText(data.get(KEYS.PROXY_HOST))
self.ui.browser_open.setText(data.get(KEYS.BROWSER_OPEN))
self.ui.local_port.setValue(data.get(KEYS.LOCAL_PORT))
self.ui.remote_address.textChanged.connect(self.render_ssh_command)
self.ui.proxy_host.textChanged.connect(self.render_ssh_command)
self.ui.local_port.valueChanged.connect(self.render_ssh_command)
self.ui.copy.clicked.connect(self.do_copy_ssh_command)
self.render_ssh_command()
def render_ssh_command(self):
ssh_command = F"ssh -L 127.0.0.1:{self.ui.local_port.value()}:{self.ui.remote_address.text()} {self.ui.proxy_host.text()}"
self.ui.ssh_command.setText(ssh_command)
def do_copy_ssh_command(self):
cb = QApplication.clipboard()
cb.clear(mode=cb.Clipboard)
cb.setText(self.ui.ssh_command.text(), mode=cb.Clipboard)
def as_dict(self):
return {
KEYS.REMOTE_ADDRESS: self.ui.remote_address.text(),
KEYS.PROXY_HOST: self.ui.proxy_host.text(),
KEYS.BROWSER_OPEN: self.ui.browser_open.text(),
KEYS.LOCAL_PORT: self.ui.local_port.value(),
}
class Tunnel(QWidget):
def __init__(self, name, data):
super(Tunnel, self).__init__()
self.ui = Ui_Tunnel()
self.ui.setupUi(self)
self.tunnelconfig = TunnelConfig(self, data)
self.tunnelconfig.setWindowTitle(name)
self.tunnelconfig.setModal(True)
self.ui.name.setText(name)
self.tunnelconfig.icon = F"./icons/{name}.png"
if not os.path.exists(self.tunnelconfig.icon):
self.tunnelconfig.icon = ICONS.TUNNEL
self.ui.icon.setPixmap(QPixmap(self.tunnelconfig.icon))
self.ui.action_tunnel.clicked.connect(self.do_tunnel)
self.ui.action_settings.clicked.connect(self.tunnelconfig.show)
self.ui.action_open.clicked.connect(self.do_open_browser)
self.process = None
def do_open_browser(self):
browser_open = self.tunnelconfig.ui.browser_open.text()
if browser_open:
urlobj = urlparse(browser_open)
local_port = self.tunnelconfig.ui.local_port.value()
new_url = urlobj._replace(netloc=F"{urlobj.hostname}:{local_port}").geturl()
QDesktopServices.openUrl(QUrl(new_url))
def do_tunnel(self):
if self.process:
self.stop_tunnel()
else:
self.start_tunnel()
def start_tunnel(self):
params = self.tunnelconfig.ui.ssh_command.text().split(" ")
self.process = QProcess()
self.process.start(params[0], params[1:])
self.ui.action_tunnel.setIcon(QIcon(ICONS.STOP))
self.do_open_browser()
def stop_tunnel(self):
try:
self.process.kill()
self.process = None
except:
pass
self.ui.action_tunnel.setIcon(QIcon(ICONS.START))
class TunnelManager(QWidget):
def __init__(self):
super().__init__()
with open(CONF_FILE, "r") as fp:
self.data = yaml.load(fp, Loader=yaml.FullLoader)
self.grid = QGridLayout(self)
self.tunnels = []
for i, name in enumerate(sorted(self.data.keys())):
tunnel = Tunnel(name, self.data[name])
self.tunnels.append(tunnel)
self.grid.addWidget(tunnel, i, 0)
self.kill_button = QPushButton(LANG.KILL_SSH)
self.kill_button.setIcon(QIcon(ICONS.KILL_SSH))
self.kill_button.setFocusPolicy(Qt.NoFocus)
self.kill_button.clicked.connect(self.do_killall_ssh)
self.grid.addWidget(self.kill_button, i+1, 0)
self.setLayout(self.grid)
self.resize(10, 10)
self.setWindowTitle(LANG.TITLE)
self.setWindowIcon(QIcon(ICONS.TUNNEL))
self.show()
def do_killall_ssh(self):
for tunnel in self.tunnels:
tunnel.stop_tunnel()
if os.name == 'nt':
os.system(CMDS.SSH_KILL_WIN)
else:
os.system(CMDS.SSH_KILL_NIX)
def closeEvent(self, event):
data = {}
for tunnel in self.tunnels:
name = tunnel.ui.name.text()
data[name] = tunnel.tunnelconfig.as_dict()
changed = DeepDiff(self.data, data, ignore_order=True)
if changed:
timestamp = int(time.time())
shutil.copy(CONF_FILE, F"{CONF_FILE}-{timestamp}")
with open(CONF_FILE, "w") as fp:
yaml.dump(data, fp)
backup_configs = glob.glob(F"{CONF_FILE}-*")
if len(backup_configs) > 10:
for config in sorted(backup_configs, reverse=True)[10:]:
os.remove(config)
event.accept()
if __name__ == '__main__':
app = QApplication(sys.argv)
sm = QSharedMemory("3866273d-f4d5-4bf3-b27b-772ca7915a61")
if not sm.create(1):
mb = QMessageBox()
mb.setIcon(QMessageBox.Information)
mb.setText(LANG.ALREADY_RUNNING)
mb.setWindowTitle(LANG.OOPS)
mb.setStandardButtons(QMessageBox.Close)
mb.show()
elif not os.path.exists(CONF_FILE):
mb = QMessageBox()
mb.setIcon(QMessageBox.Information)
mb.setText(LANG.CONF_NOT_FOUND)
mb.setWindowTitle(LANG.OOPS)
mb.setStandardButtons(QMessageBox.Close)
mb.show()
else:
tm = TunnelManager()
sys.exit(app.exec_())