-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
191 lines (140 loc) · 6.27 KB
/
client.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
__author__ = "Alon & K9"
from constants import Constants
from parser import Parser
from utils import NetworkUtils, Connection, DataType
from terminal import Colors, Terminal
from events import Events
import global_utils
import req
import socket
import time
import res
def wait():
while global_utils.get_requests() != 0:
time.sleep(0.1)
def wait_for_rsa():
while not global_utils.get_is_rsa():
time.sleep(0.1)
def main():
Terminal.clear()
Terminal.logo()
Terminal.info("enter the server's IP address:")
ip = Terminal.get_input("$ ", color=Colors.CYAN)
Terminal.info("\nconnecting to server...")
connection = None
try:
connection = Connection(socket.SOCK_STREAM, (ip, Constants.PRIMARY_CONNECTION_PORT))
except Exception as e:
Terminal.error(f"exiting, couldn't connect to server: {e}.")
return
NetworkUtils.set_primary_connection(connection)
NetworkUtils.add_listener(Events.PublicKeyTransfer, res.PublicKeyTransferEvent, DataType.Raw)
Terminal.info("establishing an encrypted connection...")
wait_for_rsa()
Terminal.success("key exchange completed, the connection is now end-to-end encrypted.")
NetworkUtils.add_listener(Events.ScreenshotDone_Response, res.ScreenshotDoneEvent)
NetworkUtils.add_listener(Events.AcceptScreenControl_Response, res.ScreenControlAcceptedEvent, DataType.Raw)
NetworkUtils.add_listener(Events.AcceptScreenWatch_Response, res.ScreenWatchAcceptedEvent, DataType.Raw)
NetworkUtils.add_listener(Events.FileDownload_Response, res.FileDownloadEvent, DataType.Raw)
NetworkUtils.add_listener(Events.FileList_Response, res.FileListEvent, DataType.Raw)
NetworkUtils.add_listener(Events.CommandRun_Response, res.CommandRunEvent)
time.sleep(0.25)
running = True
while running:
try:
wait()
print()
command = Terminal.get_input("$ ", color=Colors.CYAN)
if command == "exit":
connection.send_event(Events.ConnectionClosed, [])
connection.close()
break
parsed = Parser.parse_command(command)
if parsed is None:
continue
cmd, args = parsed
match cmd:
case "echo":
Terminal.out(f"{' '.join(args)}")
continue
case "mv":
if len(args) != 2:
Terminal.error("mv requires 2 arguments: source and destination.")
continue
Terminal.info(f"moving [{args[0]}] to [{args[1]}]...")
req.MoveFileRequest(connection).handle(args[0], args[1])
continue
case "cp":
if len(args) != 2:
Terminal.error("cp requires 2 arguments: source and destination.")
continue
Terminal.info(f"copying [{args[0]}] to [{args[1]}]...")
req.CopyFileRequest(connection).handle(args[0], args[1])
continue
case "rm":
if len(args) != 1:
Terminal.error("rm requires 1 path argument.")
continue
Terminal.info(f"removing [{args[0]}]...")
req.RemoveFileRequest(connection).handle(args[0])
continue
case "upload":
if len(args) != 2:
Terminal.error("upload requires 2 path arguments, source and destination.")
continue
Terminal.info(f"uploading [{args[0]}] to [{args[1]}]...")
req.FileUploadRequest(connection).handle(args[0], args[1])
continue
case "fetch":
if len(args) != 1:
Terminal.error("fetch requires 1 path argument.")
continue
Terminal.info(f"fetching from [{args[0]}]...")
req.FileContentRequest(connection).handle(args[0])
continue
case "ss":
Terminal.info("taking screenshot...")
req.ScreenshotRequest(connection).handle()
continue
case "sc":
Terminal.info("starting screen control...")
req.ScreenControlRequest(connection).handle()
continue
case "sw":
Terminal.info("starting screen watch...")
req.ScreenWatchRequest(connection).handle()
continue
case "ls":
Terminal.info("listing directory contents...")
req.FileListRequest(connection).handle(args[0] if args else ".")
continue
case "run":
if len(args) == 0:
Terminal.error("run requires the command to run.")
continue
Terminal.info(f"running [{' '.join(args)}]...")
req.RunCommandRequest(connection).handle(" ".join(args))
case "clear":
Terminal.clear()
continue
case "cls":
Terminal.clear()
continue
case "help":
Terminal.out("commands: echo, mv, cp, rm, upload, fetch, ss, sc, sw, ls, run, clear, cls, help, exit.")
case _:
Terminal.error(f"unknown command: {cmd}.")
continue
except KeyboardInterrupt:
running = False
except Exception as e:
Terminal.error(f"an unexpected error occurred at main loop: {e}.")
running = False
connection.send_event(Events.ConnectionClosed, [])
if __name__ == "__main__":
try:
main()
except Exception as e:
Terminal.error(f"exiting, an unexpected error occurred outside of main loop: {e}.")
finally:
global_utils.exit()