-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
155 lines (139 loc) · 4.7 KB
/
server.py
File metadata and controls
155 lines (139 loc) · 4.7 KB
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
import socket,sys,subprocess,base64,time
def __endConversation__():
message = 'BYE'
print('\033[31m[!] Connection closed\033[00m')
connection.sendall(message.encode())
connection.close()
exit()
def __conversationEnded__():
__clearConsole__(1)
print('\033[31m[!] Connection Closed By The Client.\033[00m')
connection.close()
exit()
def __recvFile__(file_name, file_size):
print('[!] Connection is sendign a file...')
print(f'[!] File name: {file_name}')
print(f'[!] File size: {file_size}')
getFile = input('Accept file (Y?N) ?: ')
if getFile.lower() == "y":
message = '1'
connection.send(message.encode())
__clearConsole__(1)
print('[!] Downloading File...')
file_parts = int(file_size)/1024
file=''
for j in range(0,int(file_parts)):
file+=connection.recv(1024).decode()
file+=connection.recv(1024).decode()
write_file = open(file_name, 'wb')
write_file.write(base64.b64decode(file))
sys.stdout.write("\033[4F")
sys.stdout.write("\033[J")
print('[!] File received')
return
elif getFile.lower() == 'n':
message = '0'
connection.send(message.encode())
__clearConsole__(1)
__sendMessage__()
return
def __sendFile__():
message = 'SF'
file_name = input('Enter file name: ')
whoAmI = '/home/'
whoAmI+= subprocess.check_output(['whoami'], text=True)
find_file = subprocess.check_output(['find', whoAmI.strip(), '-type', 'f', '-name', file_name], text=True)
fileList = find_file.strip().split('\n')
if len(find_file) == 0:
print('\033[31m[!] File not found\033[00m')
else:
if len(fileList) > 1:
num = 0
for file in fileList:
num+=1
print(f'[{num}] : {file}')
selectedFile = int(input('Select File Index Number : '))
find_file = fileList[selectedFile-1]
print(find_file)
else:
find_file = fileList[0]
file = open(find_file.strip(), 'rb')
file = file.read()
b64_encoded = base64.b64encode(file)
b64_string = b64_encoded.decode('ascii')
file_length = str(len(b64_string))
connection.send(message.encode())
time.sleep(0.5)
connection.send(file_name.encode())
time.sleep(0.5)
connection.send(file_length.encode())
reply = connection.recv(1024).decode()
if reply == '1':
print('[!] Sending File...')
while len(b64_string) > 1024:
file_part = b64_string[:1024]
connection.send(file_part.encode())
b64_string = b64_string[1024:]
time.sleep(0.1)
connection.send(b64_string.encode())
__clearConsole__(3)
print('[!] File Sent.')
return
elif reply == '0':
print('[!] File Transfer Denied..')
return
def __sendMessage__():
print('\033[32m',end='')
message = input('You: ')
print('\033[00m',end='')
if message == "BYE":
__endConversation__()
elif message == "SF":
__sendFile__()
else:
connection.sendall(message.encode())
print('\033[32;5;51mWait for reply.....\033[00m')
def __getMessage__():
reply = connection.recv(1024).decode()
if reply == "BYE":
__conversationEnded__()
elif reply == "SF":
file_name = connection.recv(1024).decode()
file_size = connection.recv(1024).decode()
__recvFile__(file_name, file_size)
else:
__clearConsole__(1)
print(f'Connection: {reply}')
def __clearConsole__(nLines):
sys.stdout.write("\033["+str(nLines)+"F")
sys.stdout.write("\033[J")
try:
from pyngrok import ngrok
except ImportError or ModuleNotFoundError:
print('[!] pyngrok not installed')
print('[+] Installing pyngrok')
pyNgrok = subprocess.check_output(['pip3', 'install', 'pyngrok'], text=True)
if "Successfully installed" in pyNgrok:
__clearConsole__(2)
print('[+] Successfully installed pyngrok')
from pyngrok import ngrok
else:
print('[!] error')
quit()
ngrok_tunnel = ngrok.connect(9001, "tcp")
sys.stdout.write("\033[3F")
sys.stdout.write("\033[J")
print(ngrok_tunnel)
PORT = 9001
SERVER = 'localhost'
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((SERVER, PORT))
server.listen()
print('\033[32;1;5;51m[!] Waiting for connection...\033[00m')
connection, addr = server.accept()
sys.stdout.write("\033[F")
sys.stdout.write("\033[K")
print('\033[32m[+] Connection Established\033[00m')
while True:
__sendMessage__()
__getMessage__()