def rsendBye(self, ua_toSend, ua_genred, Ip_Client, Port_Client, line): """ Metodo que reenvia un BYE de un cliente a un servidor. """ if ua_toSend in self.diccRegister: ServAttr = self.findPort(ua_toSend) my_socket = self.conectServer(ServAttr[1]) try: # Enviamos el INVITE y recibimos respuesta my_socket.send(bytes(line, 'utf-8') + b'\r\n') received = my_socket.recv(1024).decode('utf-8') print("-----------------------------") print ("Received and reSend to " + ua_genred) print(received.split("\r\n")[0]) # Se lo enviamos respues a UAclient q origino el INVITE if received.split("\r\n")[0] == "SIP/2.0 200 OK": self.wfile.write(bytes(received.split("\r\n")[0], 'utf-8')) mess = received.split("\r\n")[0] messlog(LOG_PATH, " Sent to ", ServAttr[0], ServAttr[0], mess) except socket.error: Answer = "Error: Server is not listening" print(Answer) my_socket.close() messlog(LOG_PATH, " Cant conect ", ServAttr[0], ServAttr[0], " make server") else: Answer = "SIP/2.0 404 User Not Found\r\n" self.wfile.write(bytes(Answer, 'utf-8') + b'\r\n') print("-----------------------------") print("Sending... \r\n" + Answer) mess = Answer.split("\r\n")[0] messlog(LOG_PATH, " Sent to ", Ip_Client, Port_Client, mess)
def handle(self): while 1: Ip_Client = str(self.client_address[0]) Port_Client = str(self.client_address[1]) # Leyendo línea a línea lo que nos envía el cliente line = self.rfile.read().decode('utf-8') if line != "": print("Received...\r\n" + line) mess = line.split("\r\n")[0] # Si no hay más líneas salimos del bucle infinito if not line: break # Observamos el metodo. METODO = line.split(" ")[0] if METODO == "INVITE": ua_genred = line.split("\r\n")[0].split(":")[1].split(" ")[0] ua_origin = line.split("\r\n")[4].split("=")[1].split(" ")[0] RTP_IP = line.split("\r\n")[4].split("=")[1].split(" ")[1] RTP_PORT = line.split("\r\n")[7].split(" ")[1] self.diccResend[ua_origin] = [RTP_IP, RTP_PORT] Answer = "SIP/2.0 100 Trying\r\n" Answer += "SIP/2.0 180 Ring\r\n" Answer += "SIP/2.0 200 OK\r\n" Answer += "Content-Type: application/sdpr\n" Answer += "\r\nv=0\r\n" Answer += "o=" + ua_genred + " " + SER_IP + "\r\n" Answer += "s=misesion\r\n" + "t=0\r\n" Answer += "m=audio " + AUDIO_PORT + " RTP\r\n" self.wfile.write(bytes(Answer, 'utf-8')) print("-----------------------------") print ("Sending...\r\n" + Answer) mess = Answer.split("\r\n")[0] messlog(LOG_PATH, " Sent to ", PROX_IP, PROX_PORT, mess + "...") if METODO == "ACK": for usuario in self.diccResend: Answer = "to " + self.diccResend[usuario][0] Answer += ":" + self.diccResend[usuario][1] print("-----------------------------") print("Sending audio... \r\n" + Answer) aEjecutar = './mp32rtp -i ' + self.diccResend[usuario][0] aEjecutar += ' -p ' + self.diccResend[usuario][1] aEjecutar += ' < ' + AUDIO_FILE print("-----------------------------") print("Vamos a ejecutar", aEjecutar) os.system(aEjecutar) messlog(LOG_PATH, " Sent to ", self.diccResend[usuario][0], self.diccResend[usuario][1], " RTP AUDIO...") if METODO == "BYE": Answer = "SIP/2.0 200 OK\r\n" self.wfile.write(bytes(Answer, 'utf-8')) print("-----------------------------") print ("Sending...\r\n" + Answer) messlog(LOG_PATH, " Sent to ", Ip_Client, Port_Client, " RTP AUDIO...")
def handle(self): while 1: # Mirar si ya ha expirado para eliminar del diccRegister self.eliminarDicc() self.register2database(DATABASE_PATH) line = self.rfile.read().decode('utf-8') if line != "": print("-----------------------------") print ("Received...\r\n" + line) # Si no hay más líneas salimos del bucle infinito if not line: break # Observamos el METODO. METODO = line.split(" ")[0] Ip_Client = str(self.client_address[0]) Port_Client = str(self.client_address[1]) mess = line.split("\r\n")[0] messlog(LOG_PATH, " Received from ", Ip_Client, Port_Client, mess) if METODO == "REGISTER": USER_NAME = line.split(":")[1] SER_PORT = line.split(":")[2].split(" ")[0] EXPIRES = line.split(":")[3][1:5].split("\r\n\r\n")[0] # Distiguimos si llega un register con NONCE longi = line.split(" ") NONCE = str(1234) if len(longi) < 6: if int(EXPIRES) == 0: if USER_NAME in self.diccRegister: print ("Expired from", USER_NAME, "out") print ("Deleting: " + USER_NAME) # eliminamos de la lista de registrados del self.diccRegister[USER_NAME] Answer = "SIP/2.0 200 OK" print("-----------------------------") print("Sending... \r\n" + Answer) self.wfile.write(bytes(Answer+"\r\n\r\n", 'utf-8')) else: print (USER_NAME + " not registered") mess = "Expired= 0" messlog(LOG_PATH, " Deleting ", Ip_Client, Port_Client, mess) elif int(EXPIRES) > 0: Answer = "SIP/2.0 401 UNAUTHORIZED\r\n" Answer += "WWW Authenticate: nonce=" + NONCE AAA = Answer.split("\r\n") print("-----------------------------") print("Sending... \r\n" + Answer) self.wfile.write(bytes(Answer+"\r\n\r\n", 'utf-8')) mess = Answer.split("\r\n")[0] messlog(LOG_PATH, " Sent to ", Ip_Client, Port_Client, mess) elif len(longi) == 6: EXPIRES = line.split("\r\n")[1].split(":")[1] fichero = open(PASS_PATH, 'r') listUsers = fichero.readlines() fichero.close() for users in listUsers: user = users.split(":")[0] if user == USER_NAME: PASSW = users.split(":")[1].split("\n")[0] break response = line.split("\r\n")[2].split(":")[2] m = hashlib.md5() m.update(bytes(PASSW, 'utf-8') + bytes(NONCE, 'utf-8')) check = m.hexdigest() if check == response: Answer = "SIP/2.0 200 OK" print("-----------------------------") print("Sending... \r\n" + Answer) self.wfile.write(bytes(Answer+"\r\n\r\n", 'utf-8')) valor = int(EXPIRES) + int(time()) nowTime = strftime("%Y%m%d%H%M%S", gmtime(time())) termTime = strftime("%Y%m%d%H%M%S", gmtime(valor)) # Añadimos a la lista de registrados. self.diccRegister[USER_NAME] = [nowTime, "127.0.0.1", SER_PORT, termTime] # Guardos en el DATABASE_PATH self.register2database(DATABASE_PATH) messlog(LOG_PATH, " Sent to ", Ip_Client, Port_Client, Answer) elif METODO == "BYE": ua_genred = line.split(":")[1] PORT = line.split(":")[2].split(" ")[0] ua_toSend = line.split(" ")[3].split("\r\n")[0] self.rsendBye(ua_toSend, ua_genred, Ip_Client, Port_Client, line) elif METODO == "INVITE": ua_toSend = line.split("\r\n")[0].split(":")[1].split(" ")[0] ua_genred = line.split("\r\n")[4].split("=")[1].split(" ")[0] if ua_toSend in self.diccRegister: # Obtenemos puerto del UAserver # a quien va dirigido el INVITE ServAttr = self.findPort(ua_toSend) # Nos conectamos al UAserver my_socket = self.conectServer(ServAttr[1]) try: # Enviamos el INVITE y recibimos respuesta my_socket.send(bytes(line, 'utf-8') + b'\r\n') received = my_socket.recv(1024).decode('utf-8') print("-----------------------------") print ("Received and reSend to " + ua_genred) print(received) # Se lo enviamos respues a UAclient q origino el INVITE self.wfile.write(bytes(received, 'utf-8')) messlog(LOG_PATH, " Sent to ", ServAttr[0], ServAttr[0], mess) except socket.error: Answer = "Error: Server is not listening" print(Answer) my_socket.close() messlog(LOG_PATH, " Cant conect ", ServAttr[0], ServAttr[0], " make server") else: Answer = "SIP/2.0 404 User Not Found\r\n" self.wfile.write(bytes(Answer, 'utf-8') + b'\r\n') print("-----------------------------") print("Sending... \r\n" + Answer) mess = Answer.split("\r\n")[0] messlog(LOG_PATH, " Sent to ", Ip_Client, Port_Client, mess) elif METODO == "ACK": # Enviamos mensaje a los demas servidores excepto el nuertro ua_toSend = line.split(":")[1].split(" ")[0] if ua_toSend in self.diccRegister: ServAttr = self.findPort(ua_toSend) # Nos conectamos al UAserver my_socket = self.conectServer(ServAttr[1]) try: my_socket.send(bytes(line, 'utf-8') + b'\r\n') print("-----------------------------") print("Sending...\r\n" + line) ServAttr = self.findPort(ua_toSend) messlog(LOG_PATH, " Sent to ", ServAttr[0], ServAttr[1], mess) except socket.error: Answer = "Error: Server is not listening" print(Answer) my_socket.close() if line.split("\r\n")[0] == "SIP/2.0 405 Method Not Allowed": Answer = "SIP/2.0 405 Method Not Allowed" print("-----------------------------") print("Sending...\r\n" + Answer) self.wfile.write(bytes(Answer+"\r\n\r\n", 'utf-8')) messlog(LOG_PATH, " Sent to ", Ip_Client, Port_Client, Answer) elif line.split("\r\n")[0] == "SIP/2.0 400 Bad Request": Answer = "SIP/2.0 400 Bad Request" print("-----------------------------") print("Sending...\r\n" + Answer) self.wfile.write(bytes(Answer+"\r\n\r\n", 'utf-8')) messlog(LOG_PATH, " Sent to ", Ip_Client, Port_Client, Answer)