def handle(self): DATOS = [] for line in self.rfile: DATOS.append(line.decode('utf-8')) datx = DATOS[0].split(' ') if ('sip:' not in datx[1] or '@' not in datx[1] or datx[2] != 'SIP/2.0\r\n'): msend = 'SIP/2.0 400 Bad Request\r\n\r\n' uaclient.CLog( 'Sent to ' + self.dicdb[datx[1].split(':')][1] + ':' + int(self.dicdb[datx[1].split(':')][2]) + ': ' + msend + '\r\n', datosxml[2]['path']) self.wfile.write(bytes(msend, 'utf-8')) else: if DATOS[0].split(' ')[0] == 'REGISTER': self.Register(DATOS) elif DATOS[0].split(' ')[0] == 'INVITE': self.Invite(DATOS) elif DATOS[0].split(' ')[0] == 'ACK': self.Ack(DATOS) elif DATOS[0].split(' ')[0] == 'BYE': self.Bye(DATOS) else: msend = 'SIP/2.0 405 Method Not Allowed\r\n\r\n' uaclient.CLog( 'Sent to ' + self.dicdb[username][1] + ':' + int(self.dicdb[username][2]) + ': ' + msend + '\r\n', datosxml[2]['path']) self.wfile.write(bytes(msend, 'utf-8'))
def Bye(self, data): ''' Funcion para reenviar el Bye al server y reeenvio el 200 ''' datosxml = proxy_parser_xml(sys.argv[1]) user = data[0].split(':')[1].split(' ')[0] if user in self.dicdb: with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as my_socket: my_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) my_socket.connect( (self.dicdb[user][1], int(self.dicdb[user][2]))) msend = ''.join(data) uaclient.CLog( 'Sent to ' + self.dicdb[user][1] + ':' + str(self.dicdb[user][2]) + ': ' + msend + '\r\n', datosxml[2]['path']) my_socket.send(bytes(msend, 'utf-8') + b'\r\n') msend = my_socket.recv(1024).decode('utf-8') uaclient.CLog( 'Received from 127.0.0.1:6003: ' + msend + '\r\n', datosxml[2]['path']) self.wfile.write(bytes(msend, 'utf-8')) else: # 404 User not found msend = 'SIP/2.0 404 User Not Found' + '\r\n' uaclient.CLog( 'Sent to ' + self.dicdb[user][1] + ':' + str(self.dicdb[user][2]) + ': ' + msend + '\r\n', datosxml[2]['path']) self.wfile.write(bytes(msend, 'utf-8'))
def handle(self): DATOS = [] DATAXML = uaclient.parser_xml(sys.argv[1]) DATAXML[1]['ip'] = '127.0.0.1' for line in self.rfile: DATOS.append(line.decode('utf-8')) uaclient.CLog( 'Received from ' + DATAXML[3]['ip'] + ':' + DATAXML[3]['puerto'] + ': ' + '\r\n'.join(DATOS), DATAXML[4]['path']) if DATOS[0].split(' ')[0] == 'INVITE': msend = ('SIP/2.0 100 Trying\r\n\r\n' + 'SIP/2.0 180 Ringing\r\n\r\n' + 'SIP/2.0 200 OK\r\n\r\n' + 'Content-Type: application/sdp\r\n\r\n' + 'v=0\r\n' + 'o=' + DATAXML[0]['username'] + ' ' + DATAXML[1]['ip'] + '\r\n' + 's=mysession\r\n' + 't=0\r\n' + 'm=audio ' + str(DATAXML[2]['puerto']) + ' RTP\r\n') uaclient.CLog( 'Sent to ' + DATAXML[3]['ip'] + ':' + DATAXML[3]['puerto'] + ': ' + msend, DATAXML[4]['path']) self.wfile.write(bytes(msend, 'utf-8')) elif DATOS[0].split(' ')[0] == 'ACK': os.system('./mp32rtp -i ' + DATAXML[1]['ip'] + ' -p ' + DATAXML[2]['puerto'] + ' < ' + DATAXML[5]['path']) elif DATOS[0].split(' ')[0] == 'BYE': msend = 'SIP/2.0 200 OK\r\n\r\n' uaclient.CLog( 'Sent to ' + DATAXML[3]['ip'] + ':' + DATAXML[3]['puerto'] + ': ' + msend, DATAXML[4]['path']) self.wfile.write(bytes(msend, 'utf-8'))
def Register(self, data): ''' Funcion para el register ''' nonce = '767676' user = data[0].split(':')[1] userip = self.client_address[0] userport = data[0].split(' ')[1].split(':')[2] userdate = time.time() userexp = data[1].split(':')[1][1:] uslist = [user, userip, userport, userdate, userexp] if user in self.dicdb: # usuario ya está en el diccionario msend = 'SIP/2.0 200 OK\r\n\r\n' uaclient.CLog('Sent to ' + userip + ':' + userport + ': ' + msend, datosxml[2]['path']) self.wfile.write(bytes(msend, 'utf-8')) self.dicdb[user][3] = userdate self.dicdb[user][4] = userexp UserDatabase(self.dicdb, self.dbpath) else: try: if data[3].split(':')[0] == 'Authorization': passin = self.datosxml[1]['passwdpath'] password = ReadPassword(passin, user) h = hashlib.sha1(bytes(password, 'utf-8')) h.update(bytes(nonce, 'utf-8')) # comprobamos que las claves son las correctas pswd = data[3].split('=')[1].split('\r')[0][1:-1] if pswd == h.hexdigest(): msend = 'SIP/2.0 200 OK\r\n\r\n' uaclient.CLog( 'Sent to ' + userip + ':' + userport + ': ' + msend, datosxml[2]['path']) self.wfile.write(bytes(msend, 'utf-8')) self.dicdb[user] = uslist UserDatabase(self.dicdb, self.dbpath) except IndexError: msend = ('SIP/2.0 401 Unauthorized' + '\r\n' + 'WWW Authenticate: Digest nonce="' + nonce + '"' + '\r\n\r\n') uaclient.CLog( 'Sent to ' + userip + ':' + userport + ': ' + msend, datosxml[2]['path']) self.wfile.write(bytes(msend, 'utf-8'))
def Ack(self, data): ''' Funcion para reenviar el Ack al server ''' datosxml = proxy_parser_xml(sys.argv[1]) user = data[0].split(':')[1].split(' ')[0] with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as my_socket: my_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) my_socket.connect((self.dicdb[user][1], int(self.dicdb[user][2]))) msend = ''.join(data) uaclient.CLog( 'Sent to ' + self.dicdb[user][1] + ':' + str(self.dicdb[user][2]) + ': ' + msend + '\r\n', datosxml[2]['path']) my_socket.send(bytes(msend, 'utf-8') + b'\r\n')
else: msend = 'SIP/2.0 405 Method Not Allowed\r\n\r\n' uaclient.CLog( 'Sent to ' + self.dicdb[username][1] + ':' + int(self.dicdb[username][2]) + ': ' + msend + '\r\n', datosxml[2]['path']) self.wfile.write(bytes(msend, 'utf-8')) if __name__ == '__main__': if len(sys.argv) != 2: sys.exit('Usage: python3 proxy_registrar.py config') datosxml = proxy_parser_xml(sys.argv[1]) username = datosxml[0]['name'] if username == '': username = '******' proxyip = datosxml[0]['ip'] if proxyip == '': proxyip = '127.0.0.1' proxyport = datosxml[0]['puerto'] uaclient.CLog('Starting...', datosxml[2]['path']) try: serv = socketserver.UDPServer((proxyip, int(proxyport)), PHandler) print('Server ' + username + ' listening at port ' + proxyport + '...') serv.serve_forever() except KeyboardInterrupt: print('Finalizado servidor proxy') uaclient.CLog('Finishing.', datosxml[2]['path'])
uaclient.CLog( 'Sent to ' + DATAXML[3]['ip'] + ':' + DATAXML[3]['puerto'] + ': ' + msend, DATAXML[4]['path']) self.wfile.write(bytes(msend, 'utf-8')) elif DATOS[0].split(' ')[0] == 'ACK': os.system('./mp32rtp -i ' + DATAXML[1]['ip'] + ' -p ' + DATAXML[2]['puerto'] + ' < ' + DATAXML[5]['path']) elif DATOS[0].split(' ')[0] == 'BYE': msend = 'SIP/2.0 200 OK\r\n\r\n' uaclient.CLog( 'Sent to ' + DATAXML[3]['ip'] + ':' + DATAXML[3]['puerto'] + ': ' + msend, DATAXML[4]['path']) self.wfile.write(bytes(msend, 'utf-8')) if __name__ == '__main__': if len(sys.argv) != 2: sys.exit('Usage: python3 uaserver.py config') DATAXML = uaclient.parser_xml(sys.argv[1]) DATAXML[1]['ip'] = '127.0.0.1' PORT = DATAXML[1]['puerto'] uaclient.CLog('Starting...', DATAXML[4]['path']) try: serv = socketserver.UDPServer((DATAXML[1]['ip'], int(PORT)), SHandler) print('Listening...') serv.serve_forever() except KeyboardInterrupt: uaclient.CLog('Finishing.', DATAXML[4]['path']) print('Finalizado uaserver')