-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
190 lines (170 loc) · 8.38 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
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
import socket
import threading
import sys
from Crypto import Random
class Peer:
def __init__(self,socket,ip,port):
self.socket=socket
self.ip=ip
self.port=port
self.isloggedin=False
self.user_name=''
self.password=''
self.rollno=None
class Group:
def __init__(self,name):
self.name=name
self.members=[]
self.nonce=Random.get_random_bytes(4)
connected_peers={}
groups={}
class Server:
IP = socket.gethostbyname(socket.gethostname())
def __init__(self):
self.server_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
self.server_socket.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
self.conn_list = []
def handle_connection(self,client_socket,client_address,peer_details):
while True:
data=client_socket.recv(4096)
if not data:
break
data=data.decode()
tokens=data.split()
command=tokens[0].lower()
print(tokens)
if command=='create_user':
# print('hello')
if len(tokens)==4:
if tokens[1] in connected_peers:
peer_details.socket.send('Username already exists'.encode())
else:
connected_peers[tokens[1]]=peer_details
peer_details.user_name=tokens[1]
peer_details.password=tokens[2]
peer_details.rollno = tokens[3]
peer_details.socket.send('User id created successfully'.encode())
else:
peer_details.socket.send('invalid command'.encode())
elif command=='login':
if len(tokens)==3:
if tokens[1] in connected_peers:
if tokens[2]==connected_peers[tokens[1]].password:
peer_details.isloggedin=True
peer_details.socket.send('you are now logged in.'.encode())
else:
peer_details.socket.send('Wrong password'.encode())
else:
peer_details.socket.send('This user id does not exist'.encode())
else:
peer_details.socket.send('invalid command'.encode())
elif command=='send':
if peer_details.isloggedin:
if tokens[1] in connected_peers:
if connected_peers[tokens[1]].isloggedin:
message='send '+connected_peers[tokens[1]].ip+':'+str(connected_peers[tokens[1]].port)
peer_details.socket.send(message.encode())
else:
peer_details.socket.send((tokens[1]+' is not logged in.').encode())
else:
peer_details.socket.send(('User '+tokens[1]+' does not exist.').encode())
else:
peer_details.socket.send('you need to log in first'.encode())
elif command=='send_file':
if peer_details.isloggedin:
if tokens[1] in connected_peers:
if connected_peers[tokens[1]].isloggedin:
message='send_file '+connected_peers[tokens[1]].ip+':'+str(connected_peers[tokens[1]].port)
peer_details.socket.send(message.encode())
else:
peer_details.socket.send((tokens[1]+' is not logged in.').encode())
else:
peer_details.socket.send(('User '+tokens[1]+' does not exist.').encode())
else:
peer_details.socket.send('you need to log in first'.encode())
elif command=='group_send_file':
if peer_details.isloggedin:
groupname=tokens[1]
if groupname in groups:
if peer_details in groups[groupname].members:
reply='group_send_file '
for i in groups[groupname].members:
if i!=peer_details:
reply+=(i.ip+':'+str(i.port)+';')
reply+=('<'+groupname+'>')
peer_details.socket.send(reply.encode())
else:
peer_details.socket.send('You are not a member of this group'.encode())
else:
peer_details.socket.send('group name does not exist'.encode())
else:
peer_details.socket.send('you need to log in first'.encode())
elif command=='group_send':
if peer_details.isloggedin:
groupname=tokens[1]
if groupname in groups:
if peer_details in groups[groupname].members:
reply='group_send '
for i in groups[groupname].members:
if i!=peer_details:
reply+=(i.ip+':'+str(i.port)+';')
reply+=('<'+groupname+'>')
# reply=reply[:-1]
peer_details.socket.send(reply.encode())
else:
peer_details.socket.send('You are not a member of this group'.encode())
else:
peer_details.socket.send('group name does not exist'.encode())
else:
peer_details.socket.send('you need to log in first'.encode())
elif command=='create_group':
if peer_details.isloggedin:
groupname=tokens[1]
if groupname in groups:
peer_details.socket.send('Groupname already exists'.encode())
else:
group=Group(groupname)
group.members.append(peer_details)
groups[groupname]=group
peer_details.socket.send('Group created successfully'.encode())
else:
peer_details.socket.send('You need to log in first'.encode())
elif command=='join':
if peer_details.isloggedin:
groupname=tokens[1]
if groupname in groups:
groups[groupname].members.append(peer_details)
peer_details.socket.send('Group joined successfully'.encode())
else:
peer_details.socket.send('Groupname does not exist'.encode())
else:
peer_details.socket.send('You need to log in first'.encode())
elif command=='list_groups':
reply=''
for i in groups:
reply+=(i+' '+str(len(groups[i].members))+'\n')
reply=reply[:-1]
peer_details.socket.send(reply.encode())
if not data:
print("{}:{} disconnected.....".format(client_address[0],client_address[1]))
self.conn_list.remove(client_socket)
client_socket.close()
break
def run(self,PORT=18000):
self.server_socket.bind((self.IP,PORT))
self.server_socket.listen(20)
print("Listening at {}".format(self.server_socket.getsockname()))
while True:
client_socket,client_address = self.server_socket.accept()
print(client_address[0],client_address[1],' connected')
serveratpeerip, seerveratpeerport=client_socket.recv(4096).decode().split(':')
print(serveratpeerip,seerveratpeerport)
# self.conn_list.append(client_socket)
client_details=Peer(client_socket,serveratpeerip,seerveratpeerport)
# peers.append(client_details)
th=threading.Thread(target=self.handle_connection,args=(client_socket,client_address,client_details))
th.daemon = True
th.start()
if __name__ == "__main__":
server = Server()
server.run()