-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServer.py
133 lines (126 loc) · 5.39 KB
/
Server.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
import socket
import os
# import sys
import time
# import threading
os.system('color 0A')
PORT=5678
SERVER=socket.gethostbyname(socket.gethostname())
# SERVER='192.168.1.16'
ADDR=(SERVER,PORT)
server=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(ADDR)
def handle_client(conn, addr):
def delete_line(original_file, line_number):
say = 0
""" Delete a line from a file at the given line number """
is_skipped = False
current_index = 0
dummy_file = original_file + '.bak'
# Open original file in read only mode and dummy file in write mode
with open(original_file, 'rb') as read_obj, open(dummy_file, 'wb') as write_obj:
# Line by line copy data from original file to dummy file
for line in read_obj:
# If current line number matches the given line number then skip copying
if current_index != line_number:
write_obj.write(line)
say = say + 1
else:
is_skipped = True
current_index += 1
if is_skipped:
os.remove(original_file)
os.rename(dummy_file, original_file)
else:
os.remove(dummy_file)
# sys.stdout = open("server_log.txt", "a")
print(f'\n[NEW CONNECTION] {addr} connected.\n')
# sys.stdout.close()
with conn:
host_key = conn.recv(1024).decode('utf-8')
msg, host, key = host_key.split(': ')
hostandkey = host + ": " + key
# sys.stdout = open("server_log.txt", "a")
print(f'[{msg}]')
# sys.stdout.close()
if msg == 'Decrypter':
if os.path.exists("encrypted_hosts.txt"):
pass
else:
with open("encrypted_hosts.txt", "w") as file1:
pass
with open("encrypted_hosts.txt", "r") as file1:
# setting flag and index to 0
flag = 0
index = (-1)
# Loop through the file line by line
for line in file1:
index += 1
read_line = line
read_line = read_line.strip()
try:
date, hot, rkey = read_line.split(': ')
rockey = hot + ": " + rkey
# checking string is present in line or not
if rockey == hostandkey:
flag = 1
break
except:
pass
# checking condition for string found or not
if flag == 0:
# sys.stdout = open("server_log.txt", "a")
print(f"[{time.asctime(time.localtime(time.time()))}] The key entered by {host}: {key} didn't match any encrypted host!")
# sys.stdout.close()
conn.send(bytes("no", 'utf-8'))
# break
# print('String', string1, 'Not Found')
else:
conn.send(bytes("yes", 'utf-8'))
delete_line("encrypted_hosts.txt", index)
with open('decrypted_hosts.txt', 'a') as su:
su.write(f'[{time.asctime(time.localtime(time.time()))}]: {hostandkey}' + '\n')
# sys.stdout = open("server_log.txt", "a")
print(f'[{time.asctime(time.localtime(time.time()))}] {host} successfully decrypted with key: {key}')
# sys.stdout.close()
elif msg == 'Encrypter':
if os.path.exists("encrypted_hosts.txt"):
pass
else:
with open("encrypted_hosts.txt", "w") as file1:
pass
with open('encrypted_hosts.txt', 'r') as f:
readdata = f.read()
if host in readdata:
conn.send(bytes("no", 'utf-8'))
# sys.stdout = open("server_log.txt", "a")
print(f"[{time.asctime(time.localtime(time.time()))}] {host} already encrypted with key: {key}")
# sys.stdout.close()
f.close()
else:
conn.send(bytes("yes", 'utf-8'))
with open('encrypted_hosts.txt', 'a') as f:
f.write(f'[{time.asctime(time.localtime(time.time()))}]: {hostandkey}' + '\n')
# sys.stdout = open("server_log.txt", "a")
print(f'[{time.asctime(time.localtime(time.time()))}] {host} successfully encrypted with key: {key}')
# sys.stdout.close()
f.close()
# sys.stdout = open("server_log.txt", "a")
print(f"\n[DROPPED CONNECTION] {addr} disconnected.")
print(f'\n[Listening] Server is listening for connections on [{SERVER}]')
# sys.stdout.close()
def start():
server.listen(5)
# sys.stdout = open("server_log.txt", "a")
print(f'\n[Listening] Server is listening for connections on [{SERVER}]')
# sys.stdout.close()
while True:
conn, addr=server.accept()
handle_client(conn,addr)
# thread= threading.Thread(target=handle_client,args=(conn,addr))
# thread.start()
# print(f'[ACTIVE CONNECTIONS] {threading.activeCount()-1}\n')
# sys.stdout = open("server_log.txt", "a")
print(f"[{time.asctime(time.localtime(time.time()))}] Starting server...")
# sys.stdout.close()
start()