-
Notifications
You must be signed in to change notification settings - Fork 0
/
launcher.py
409 lines (356 loc) · 13.9 KB
/
launcher.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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
import enclib as enc
from rsa import newkeys, PublicKey, decrypt
from os import path, mkdir, listdir, remove, rename
from socket import socket
from subprocess import Popen, call
from zipfile import ZipFile
from time import sleep, perf_counter
from threading import Thread
from kivy.clock import Clock
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import ObjectProperty
from kivy.properties import StringProperty
from kivy.lang import Builder
from kivy.core.window import Window
from kivy.config import Config
# server class containing connection algorithm and data transfer functions
class Server:
def __init__(self):
self.s, self.enc_key = socket(), None
if path.exists("app/server_ip"):
with open(f"app/server_ip", "rb") as f:
self.ip = f.read().decode().split(":")
else:
self.ip = None
def connect(self):
try:
self.s.connect((self.ip[0], int(self.ip[1])))
print("Connected to server")
l_ip, l_port = str(self.s).split("laddr=")[1].split("raddr=")[0][2:-3].split("', ")
s_ip, s_port = str(self.s).split("raddr=")[1][2:-2].split("', ")
print(f" << Server connected via {l_ip}:{l_port} -> {s_ip}:{s_port}")
pub_key, pri_key = newkeys(512)
try:
self.s.send(PublicKey.save_pkcs1(pub_key))
except ConnectionResetError:
return False
print(" >> Public RSA key sent")
enc_seed = decrypt(self.s.recv(128), pri_key).decode()
self.enc_key = enc.pass_to_key(enc_seed[:18], enc_seed[18:], 100000)
print(" << Client enc_seed and enc_salt received and loaded\n -- RSA Enc bootstrap complete")
return True
except ConnectionRefusedError:
print("Connection refused")
return False
def send_e(self, text): # encrypt and send data to server
try:
self.s.send(enc.enc_from_key(text, self.enc_key))
except ConnectionResetError:
print("CONNECTION_LOST, reconnecting...")
if s.ip and s.connect():
self.s.send(enc.enc_from_key(text, self.enc_key))
else:
print("Failed to reconnect")
def recv_d(self, buf_lim): # receive and decrypt data from server
try:
return enc.dec_from_key(self.s.recv(buf_lim), self.enc_key)
except ConnectionResetError:
print("CONNECTION_LOST, reconnecting...")
if s.ip and s.connect():
return enc.dec_from_key(self.s.recv(buf_lim), self.enc_key)
else:
print("Failed to reconnect")
s = Server()
# detects if any version of the app is already present
def load():
if not path.exists("app"): # if no app present
mkdir("app")
sm.switch_to(ChooseDistro())
elif path.exists("app/code/rdisc.py"): # if code version present
call("launch.bat")
sleep(2)
App.get_running_app().stop()
else: # if app version present
try:
[file for file in listdir('app') if file.endswith('.exe')][-1]
sm.switch_to(AttemptConnection())
except IndexError:
sm.switch_to(ChooseDistro())
# runs load()
class Loading(Screen):
def __init__(self, **kwargs):
super(Loading, self).__init__(**kwargs)
Clock.schedule_once(lambda dt: load(), 0.1)
# screen for choosing between code and app version
class ChooseDistro(Screen):
def exe(self):
sm.switch_to(AttemptConnection())
def dev(self):
sm.switch_to(CreateDev())
# screen for while attempting to connect to server
class AttemptConnection(Screen):
def on_enter(self, *args):
if s.ip and s.connect():
print("Connected to server")
with open(f"app/server_ip", "wb") as f:
f.write(f"{s.ip[0]}:{s.ip[1]}".encode())
sm.switch_to(Update())
else:
sm.switch_to(IpSet(), direction="left")
# screen for setting server ip and port
class IpSet(Screen):
ip_address = ObjectProperty(None)
def try_connect(self):
if self.ip_address.text == "":
print("No input provided")
else:
try:
server_ip, server_port = self.ip_address.text.split(":")
server_port = int(server_port)
except ValueError or NameError:
print("\n[COL-RED] Invalid input")
else:
if server_port < 1 or server_port > 65535:
print("\n[COL-RED] Port number must be between 1 and 65535")
else:
try:
ip_1, ip_2, ip_3, ip_4 = server_ip.split(".")
if all(i.isdigit() and 0 <= int(i) <= 255 for i in [ip_1, ip_2, ip_3, ip_4]):
s.ip = [server_ip, server_port]
sm.switch_to(AttemptConnection(), direction="left")
else:
print("\n[COL-RED] IP address must have integers between 0 and 255")
except ValueError or NameError:
print("\n[COL-RED] IP address must be in the format 'xxx.xxx.xxx.xxx'")
# screen for launching/updating and showing progress
class Update(Screen):
update_text = StringProperty()
def update_system(self):
self.update_text = "Checking for updates..."
file_name = None
try:
app_location = [file for file in listdir('app') if file.endswith('.exe')][-1]
app_hash = enc.hash_a_file(f"app/{app_location}")
s.send_e(f"UPD:{app_hash}")
update_data = s.recv_d(1024)
if not update_data == "V":
file_name, update_size = update_data.split("")
except IndexError:
s.send_e("UPD:N")
file_name, update_size = s.recv_d(1024).split("")
if file_name:
update_size = int(update_size)
self.update_text = f"Downloading version {file_name[:-4].replace('rdisc', 'Rdisc')}..."
all_bytes = b""
start = perf_counter()
while True: # receive update from server
bytes_read = s.s.recv(32768)
if b"_BREAK_" in bytes_read:
all_bytes += bytes_read[:-7]
break
if perf_counter()-start > 0.25:
start = perf_counter()
self.update_text = f"Downloading version {file_name[:-4].replace('rdisc', 'Rdisc')} " \
f"({round((len(all_bytes)/update_size)*100, 2)}%)"
all_bytes += bytes_read
with open(f"app/{file_name}", "wb") as f:
f.write(all_bytes)
self.update_text = "Update downloaded, unpacking..."
sleep(1)
with ZipFile(f"app/{file_name}", 'r') as zip_:
zip_.extractall("app")
self.update_text = f"Successfully updated to {file_name[:-4].replace('rdisc', 'Rdisc')}."
if path.exists(f"old_rdisc.kv"):
remove(f"old_rdisc.kv")
if path.exists("rdisc.kv"):
rename("rdisc.kv", "old_rdisc.kv")
remove(f"app/{file_name}")
for file in listdir("app"):
if file.endswith(".exe") and file != f"{file_name[:-4]}.exe":
remove(f"app/{file}")
self.update_text = "Launching..."
Popen(f"app/{[file for file in listdir('app') if file.endswith('.exe')][-1]}")
sleep(1)
App.get_running_app().stop()
def on_enter(self, *args):
Thread(target=self.update_system, daemon=True).start()
# screen for creating/downloading dev environment
class CreateDev(Screen):
create_text = StringProperty()
def create(self):
self.create_text = "Detecting Git..."
try:
call("git")
except FileNotFoundError:
self.create_text = "Git not installed, installing..."
call("winget install --id Git.Git -e --source winget")
self.create_text = "Loading repo installer..."
with open("install.bat", "w") as f:
f.write("""echo Setting up repository
git clone --filter=blob:none --no-checkout --depth 1 --sparse https://github.com/rapidslayer101/Rdisc-Kivy app/code
cd app/code
git config core.sparsecheckout true
echo rdisc.py > .git/info/sparse-checkout
echo rdisc_kv.py >> .git/info/sparse-checkout
echo enclib.py >> .git/info/sparse-checkout
echo venv.zip >> .git/info/sparse-checkout
git checkout
tar -xf venv.zip
)""")
self.create_text = "Running repo Installer..."
call("install.bat")
self.create_text = "Installing Dependency (kivy) 1/2..."
call("app/code/venv/Scripts/python -m pip install kivy")
self.create_text = "Installing Dependency (rsa) 2/2..."
call("app/code/venv/Scripts/python -m pip install rsa")
self.create_text = "Launching..."
with open("launch.bat", "w") as f:
f.write("""cd app/code
echo Checking for updates
git reset --hard
git pull origin master
echo Launching client
start venv/Scripts/python.exe rdisc.py""")
call("launch.bat")
sleep(2)
App.get_running_app().stop()
def on_enter(self, *args):
Thread(target=self.create, daemon=True).start()
class WindowManager(ScreenManager):
pass
# Kivy UI code for launcher
Builder.load_string("""
#:set rdisc_purple (104/255, 84/255, 252/255,1)
#:set rdisc_purple_dark (104/255, 73/255, 160/255,1)
#:set rdisc_cyan (37/255, 190/255, 150/255,1)
#:set bk_grey_1 (50/255, 50/255, 50/255,1)
<GreyFloatLayout@FloatLayout>:
canvas.before:
Color:
rgba: bk_grey_1
Rectangle:
pos: self.pos
size: self.size
<RoundedButton@Button>
background_color: 0,0,0,0
size_hint: 0.3, 0.1
canvas.before:
Color:
rgba: rdisc_purple if self.state == 'normal' else rdisc_purple_dark
RoundedRectangle:
size: self.size
pos: self.pos
radius: [10]
<RoundedTextInput@TextInput>:
font_size: '16dp'
multiline: False
halign: "center"
size_hint: 0.3, 0.1
padding: [0, self.height/2.0-(self.line_height/2.0)*len(self._lines), 0, 0]
write_tab: False
background_color: 0,0,0,0
cursor_color: rdisc_cyan
canvas.before:
Color:
rgba: rdisc_cyan
canvas.after:
Color:
rgba: 0,0,0,0
Ellipse:
angle_start:180
angle_end:360
pos:(self.pos[0]-self.size[1]/2.0, self.pos[1])
size: (self.size[1], self.size[1])
Ellipse:
angle_start:360
angle_end:540
pos: (self.size[0]+self.pos[0]-self.size[1]/2.0, self.pos[1])
size: (self.size[1], self.size[1])
Color:
rgba: rdisc_purple
Line:
points: self.pos[0], self.pos[1], self.pos[0]+self.size[0], self.pos[1]
Line:
points: self.pos[0], self.pos[1]+self.size[1], self.pos[0]+self.size[0], self.pos[1]+self.size[1]
Line:
ellipse: self.pos[0]-self.size[1]/2.0, self.pos[1], self.size[1], self.size[1], 180, 360
Line:
ellipse: self.size[0]+self.pos[0]-self.size[1]/2.0, self.pos[1], self.size[1], self.size[1], 360, 540
<SizeLabel@Label>:
size_hint: 0.1, 0.05
<Loading>:
GreyFloatLayout:
Label:
text: "Loading..."
font_size: '18dp'
size_hint: 0.3, 0.1
pos_hint: {"x": 0.35, "top": 0.6}
<ChooseDistro>:
GreyFloatLayout:
SizeLabel:
text: "Choose Distro:"
font_size: '18dp'
pos_hint: {"x": 0.45, "top": 0.75}
SizeLabel:
text: "For normal users:"
pos_hint: {"x": 0.25, "top": 0.58}
RoundedButton:
text: "Rdisc (exe)"
pos_hint: {"x": 0.5, "top": 0.6}
on_press: root.exe()
SizeLabel:
text: "For coders/devs:"
pos_hint: {"x": 0.25, "top": 0.38}
RoundedButton:
text: "Rdisc Dev (py)"
pos_hint: {"x": 0.5, "top": 0.4}
on_press: root.dev()
<AttemptConnection>:
GreyFloatLayout:
Label:
text: "Attempting to connect to server..."
font_size: '18dp'
size_hint: 0.3, 0.1
pos_hint: {"x": 0.35, "top": 0.6}
<IpSet>:
ip_address: ip_address
GreyFloatLayout:
SizeLabel:
text: "Enter server IP address"
pos_hint: {"x": 0.45, "top": 0.8}
RoundedTextInput:
id: ip_address
pos_hint: {"x": 0.35, "top": 0.65}
on_text_validate: root.try_connect()
RoundedButton:
text: "Connect"
pos_hint: {"x": 0.35, "top": 0.45}
on_press: root.try_connect()
<Update>:
GreyFloatLayout:
Label:
text: root.update_text
font_size: '18dp'
size_hint: 0.3, 0.1
pos_hint: {"x": 0.35, "top": 0.6}
<CreateDev>:
GreyFloatLayout:
Label:
text: root.create_text
font_size: '18dp'
size_hint: 0.3, 0.1
pos_hint: {"x": 0.35, "top": 0.6}
""")
sm = WindowManager()
[sm.add_widget(screen) for screen in [Loading(), ChooseDistro(), AttemptConnection(), IpSet(), Update(), CreateDev()]]
# app class
class RdiscLauncher(App):
def build(self):
self.title = f"Rdisc Launcher"
Window.size = (500, 600)
Config.set('input', 'mouse', 'mouse,disable_multitouch')
Config.set('kivy', 'exit_on_escape', '0')
return sm
if __name__ == "__main__":
RdiscLauncher().run()