-
Notifications
You must be signed in to change notification settings - Fork 0
/
UDP_Server.py
181 lines (157 loc) · 9.81 KB
/
UDP_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
import newSocket as socketLib
import time
import datetime
#import Users
class UDP_Server(object):
def __init__(self,IP=socketLib.findByDomain("T3"),port=80):
while True:
self.run_the_chat(IP,port)
def run_the_chat(self,IP,port):
socket, AF_INET, SOCK_DGRAM, timeout = socketLib.socket, socketLib.AF_INET, socketLib.SOCK_DGRAM, socketLib.timeout
with socket(AF_INET, SOCK_DGRAM) as self.sock:
self.sock.bind((IP,port))
self.sock.settimeout(2.0) # 2 second timeout
self.Users={}
self.MsgList=[] #List of all messages on server
self.AdminList={} #List of Admins (added 2/25/2014} Stores Address and T/F
self.BannedList={} #Store Address and T/F
self.Times={} #Stores index for each message based on timestamp
self.password='ADMIN' #Current Admin password
self.MsgCount=0 #Index of most recent message on server
self.msgHelp='/ADMIN {password} '+ '/ADMININFO '+ '/HELP '+'/RESTART ' #List of Client Commands
self.msgAdminHelp='/BAN {user}' #List of Admin Commands
print ("UDP Server started on IP Address {}, port {}".format(IP,port))
self.logon=True
self.allOtherMsg=[]
while self.logon:
try:
# print('hit try')
if self.allOtherMsg != []:
# print('passed through if')
for i in range(len(allOtherMsg)):
self.decodeMsg(allOtherMsg[i])
time.sleep(.1)
else:
# print('hit else')
bytearray_msg, address = self.sock.recvfrom(1024)
# print('gotmsg')
self.decodeMsg(bytearray_msg, address)
# print('decoded')
time.sleep(.1)
except Exception as e: ## Handles timeout with the server
time.sleep(.1)
print (e)
continue
def decodeMsg(self, bytearray_msg, address):
source_IP, source_port = address
ts=time.time()
st=datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
test=address in self.Users #Returns True is user is logged on to the server, False if not
if test==False: #Requests user to logon for the first time
self.Users[address]=st
self.MsgList+=['']
self.Times[st]=self.MsgCount
self.MsgCount+=1
bytearray_message = bytearray('In.' ,encoding="UTF-8")
bytes_sent = self.sock.sendto(bytearray_message, address)
## self.waitForAck(Oaddress=address,OMsg=bytearray_message)
elif test==True:
if source_IP not in self.BannedList:
print ("\nMessage received from IP address {}, port {}:".format(
source_IP,source_port))
if (bytearray_msg.decode("UTF-8"))[0] == '/':#Tests to see if the message starts with a / and is therefore a command
command = bytearray_msg.decode("UTF-8")
print (source_IP+': '+command)
if command == "/HELP": #Tests for /help command
bytearray_message = bytearray("List of avaliable commands are: "+self.msgHelp,encoding="UTF-8")
bytes_sent = self.sock.sendto(bytearray_message, address)
## self.waitForAck(Oaddress=address,OMsg=bytearray_message)
#New command checks added (2/25/2014)
#Note - commands need to be checked (sorry!)
elif command == "/ADMININFO":
if address in self.AdminList:
bytearray_message = bytearray("As Admin, You can use: "+self.msgAdminHelp+self.msgHelp ,encoding="UTF-8")
else:
bytearray_message = bytearray("If you are an admin, type /admin [password] to logon",encoding="UTF-8")
bytes_sent = self.sock.sendto(bytearray_message, address)
## self.waitForAck(Oaddress=address,OMsg=bytearray_message)
elif "/ADMIN" in command:
## if len(command)<7:
## print('there was no password')
## bytearray_message = bytearray("Admin permissions require the correct password to logon!",encoding="UTF-8")
## bytes_sent = self.sock.sendto(bytearray_message, address)
if command[7:] == self.password: #Tests is Admin password is provided
bytearray_message = bytearray("You are now Admin!",encoding="UTF-8")
bytes_sent = self.sock.sendto(bytearray_message, address)
self.AdminList[address] = True #Adds user to Admin list
print ("{} is now an Admin".format(source_IP))
else: #Triggers if wrong/no password is provided
print('there was a wrong password')
bytearray_message = bytearray("Admin permissions require the correct password to logon!",encoding="UTF-8")
bytes_sent = self.sock.sendto(bytearray_message, address)
## self.waitForAck(Oaddress=address,OMsg=bytearray_message)
print('bypassed everything...')
elif command == "/RESTART":
bytearray_message = bytearray("BYE - you will now be logged off",encoding="UTF-8")
bytes_sent = self.sock.sendto(bytearray_message, address)
self.logon=False
elif "/BAN" in command:
if address in self.AdminList:
who = command[5:]
if who=='':
bytearray_message = bytearray("No user specified to ban",encoding="UTF-8")
bytes_sent = self.sock.sendto(bytearray_message, address)
else:
print (who)
self.BannedList[who] = True
bytearray_message = bytearray("Your ban on "+who+" is now active",encoding="UTF-8")
bytes_sent = self.sock.sendto(bytearray_message, address)
## self.waitForAck(Oaddress=address,OMsg=bytearray_message)
else:
bytearray_message = bytearray("You must be Admin to use this command.",encoding="UTF-8")
bytes_sent = self.sock.sendto(bytearray_message, address)
## self.waitForAck(Oaddress=address,OMsg=bytearray_message)
else:
bytearray_message = bytearray("The command you entered is not recognized.",encoding="UTF-8")
bytes_sent = self.sock.sendto(bytearray_message, address)
## self.waitForAck(Oaddress=address,OMsg=bytearray_message)
##Code below this point has not been modified at all
else:
self.LastMsg=self.Users[address] #Reads the last message index provided by the Server to the Client since logon
str_message =[source_IP+': '+(bytearray_msg.decode("UTF-8"))]
print(str_message) #Prints the message out to the server
self.Times[st]=self.MsgCount #Uses timestamp to access current message index
self.MsgList+=str_message #Adds latest message to the list of all messages sinse server startup
for i in range(self.Times[self.LastMsg]+1,self.Times[st]+1): #Transmitts all messages from last message index to current message index.
bytearray_message = bytearray(self.MsgList[i],encoding="UTF-8") #Back-Translate into UTF-8
bytes_sent = self.sock.sendto(bytearray_message, address) #Transmit message
self.MsgCount+=1
self.Users[address]=st
##Added with ban Code
else:
bytearray_message=bytearray("You are on the 'banned' list - Contact an Admin to have the ban lifted",encoding="UTF-8")
bytes_sent = self.sock.sendto(bytearray_message, address)
## self.waitForAck(Oaddress=address,OMsg=bytearray_message)
##Nothing changed below
def waitForAck(self,Oaddress,timeout=10,OMsg=''):
finaltime=time.time()+timeout
while noAck:
try:
if time.time()>finaltime:
bytes_sent = sock.sendto(oMsg, Oaddress)
else:
bytearray_msg, address = sock.recvfrom(1024)
source_IP, source_port = address
if address == Oaddress:
if 'ACK' == bytearray_msg.decode("UTF-8"):
noAck=False
else:
self.allOtherMsg.append(bytearray_msg.decode("UTF-8"),address)
else:
self.allOtherMsg.append(bytearray_msg.decode("UTF-8"),address)
except Exception:
continue
if __name__ == "__main__":
print("udp server will start in 3 seconds")
time.sleep(3)
UDP_Server()