Exemple #1
0
def RecieveRegister():

    data = my_socket.recv(1024).decode('utf-8')
    if data != '':
        Server = Server = datos[3]['ip'] + ':' + datos[3]['puerto'] + ' '
        if data.split(' ')[1] == '401':
            uaserver.AddtoLog(datos[4]['path'], Server + data, 'Receive')
            print('Recibido 401')
            NONCE = data.split('=')[1]
            NONCE = NONCE.split('"')[1]
            Password = datos[0]['passwd']
            h = hashlib.sha1(bytes(Password, 'utf-8'))
            h.update(bytes(NONCE, 'utf-8'))
            Message = METHOD + ' sip:' + USER + ':' + str(SERVERPORT)
            Message += ' SIP/2.0\r\n' + 'Expires: ' + sys.argv[3] + '\r\n'
            Message += 'Authorization: Digest response=' + h.hexdigest()
            Message += '\r\n\r\n'
            my_socket.send(bytes(Message, 'utf-8'))
            uaserver.AddtoLog(datos[4]['path'], Server + Message, 'Send')
            RecieveRegister()

        elif data.split(' ')[1] == '200':
            print('Recibido 200 ok')
            uaserver.AddtoLog(datos[4]['path'], Server + data, 'Receive')

        elif data.split(' ')[1] == '400':
            print('Recubido 400')
            uaserver.AddtoLog(datos[4]['path'], Server + data, 'Receive')
Exemple #2
0
def ManageInvite(datos):

    USER = datos[0]['username']
    SERVERPORT = int(datos[1]['puerto'])

    Message = 'INVITE sip:' + sys.argv[3] + ' SIP/2.0\r\n'
    Message += 'Content-Type: application/sdp\r\n\r\n' + 'v=0\r\n'
    Message += 'o=' + USER + ' 127.0.0.1\r\n' + 's=hungry\r\n'
    Message += 't=0\r\n' + 'm=audio ' + datos[2]['puerto'] + ' RTP\r\n'

    my_socket.send(bytes(Message, 'utf-8'))
    Server = datos[3]['ip'] + ':' + datos[3]['puerto'] + ' '
    uaserver.AddtoLog(datos[4]['path'], Server + Message, 'Send')

    data = my_socket.recv(1024).decode('utf-8')
    uaserver.AddtoLog(datos[4]['path'], Server + data, 'Receive')
    if len(data.split(' ')) > 6:

        if data.split(' ')[5] == '200':
            Message = 'ACK sip:' + sys.argv[3] + ' SIP/2.0\r\n\r\n'
            my_socket.send(bytes(Message, 'utf-8'))
            uaserver.AddtoLog(datos[4]['path'], Server + Message, 'Send')
            SendRTP(datos, data)
    else:
        print('Recibido ' + data)
        uaserver.AddtoLog(datos[4]['path'], Server + data, 'Error')
Exemple #3
0
    def SendtoServer(self, DATA):
        userserv = 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.DicUsers[userserv][1], int(self.DicUsers[userserv][2])))

            Message = ''.join(DATA)
            Server = '127.0.0.1:' + self.DicUsers[userserv][1] + ' '
            uaserver.AddtoLog(self.datos[2]['path'], Server + Message, 'Send')

            my_socket.send(bytes(Message, 'utf-8'))
            if DATA[0].split(' ')[0] == 'INVITE':
                try:
                    self.ReceiveAnsInvite(my_socket, userserv)
                except ConnectionRefusedError:
                    print('No server listening at port ' +
                          self.DicUsers[userserv][2])
                    self.wfile.write(b"SIP/2.0 404 User Not Found\r\n\r\n")
                    Client = self.client_address[0] + ':'
                    Client += str(self.client_address[1]) + ' '
                    Message = "SIP/2.0 404 User Not Found"
                    uaserver.AddtoLog(self.datos[2]['path'], Message, 'Error')
                    uaserver.AddtoLog(self.datos[2]['path'], Client + Message,
                                      'Send')

            elif DATA[0].split(' ')[0] == 'BYE':
                self.ReceiveAnsBye(my_socket, userserv)
Exemple #4
0
def ManageBye(datos):

    Message = 'BYE sip:' + sys.argv[3] + ' SIP/2.0\r\n\r\n'
    my_socket.send(bytes(Message, 'utf-8'))
    Server = datos[3]['ip'] + ':' + datos[3]['puerto'] + ' '
    uaserver.AddtoLog(datos[4]['path'], Server + Message, 'Send')
    data = my_socket.recv(1024).decode('utf-8')
    uaserver.AddtoLog(datos[4]['path'], Server + data, 'Receive')
Exemple #5
0
    def ReceiveAnsBye(self, my_socket, user):

        data = my_socket.recv(1024)
        datadec = data.decode('utf-8')
        Server = self.DicUsers[user][1] + self.DicUsers[user][2] + ' '
        uaserver.AddtoLog(self.datos[2]['path'], Server + datadec, 'Receive')
        if datadec.split(' ')[1] == '200':
            self.wfile.write(data)
            uaserver.AddtoLog(self.datos[2]['path'], Server + datadec, 'Send')
        else:
            print('Recibido 400')
Exemple #6
0
    def RegisterManager(self, Data):

        NONCE = '123456789'
        if len(Data) < 3:
            self.wfile.write(b'SIP/2.0 400 Bad Request')
        else:

            username = Data[0].split(':')[1]
            serverport = Data[0].split(':')[2].split(' ')[0]
            expires = time.time() + float(Data[1].split(' ')[1].split('\r')[0])
            Client = self.client_address[0] + ':'
            Client += str(self.client_address[1]) + ' '
            self.ExpiresCheck()
            DataBaseFich(self.datos[1]['path'], self.DicUsers)
            print('recibe register')

            if username in self.DicUsers:
                self.wfile.write(b"SIP/2.0 200 OK\r\n\r\n")
                uaserver.AddtoLog(self.datos[2]['path'],
                                  Client + "SIP/2.0 200 OK\r\n\r\n", 'Send')
                self.DicUsers[username][3] = expires
                DataBaseFich(self.datos[1]['path'], self.DicUsers)
            else:
                if Data[2].split(':')[0] == 'Authorization':
                    Password = GetPassword(self.datos[1]['passwdpath'],
                                           username)
                    h = hashlib.sha1(bytes(Password, 'utf-8'))
                    h.update(bytes(NONCE, 'utf-8'))
                    if Data[2].split('=')[1].split('\r')[0] == h.hexdigest():
                        self.wfile.write(b"SIP/2.0 200 OK\r\n\r\n")
                        self.DicUsers[username] = [
                            username, self.client_address[0], serverport,
                            expires,
                            time.time()
                        ]
                        DataBaseFich(self.datos[1]['path'], self.DicUsers)
                        uaserver.AddtoLog(self.datos[2]['path'],
                                          Client + "SIP/2.0 200 OK\r\n\r\n",
                                          'Send')
                    else:
                        print('fallo en el nonce')

                else:
                    Message = "SIP/2.0 401 Unauthorized" + '\r\n'
                    Message += 'WWW-Authenticate: Digest nonce="'
                    Message += NONCE + '"\r\n\r\n'
                    self.wfile.write(bytes(Message, 'utf-8'))
                    uaserver.AddtoLog(self.datos[2]['path'], Message, 'Error')
                    uaserver.AddtoLog(self.datos[2]['path'], Client + Message,
                                      'Send')
Exemple #7
0
    def InviteManager(self, DATA):
        print('recibe invite')
        self.ExpiresCheck()
        Clientin = DATA[4].split('=')[1].split(' ')[0] in self.DicUsers
        Serverin = DATA[0].split(':')[1].split(' ')[0] in self.DicUsers
        if Clientin and Serverin:

            self.SendtoServer(DATA)
        else:
            self.wfile.write(b"SIP/2.0 404 User Not Found\r\n\r\n")
            Client = self.client_address[0] + ':'
            Client += str(self.client_address[1]) + ' '
            Message = "SIP/2.0 404 User Not Found"
            uaserver.AddtoLog(self.datos[2]['path'], Message, 'Error')
            uaserver.AddtoLog(self.datos[2]['path'], Client + Message, 'Send')
Exemple #8
0
def SendRTP(datos, DATA):

    ServerIP = DATA.split('\r\n')[8].split(' ')[1]
    ServerRTPPort = DATA.split('\r\n')[11].split(' ')[1]
    audio = datos[5]['path']
    order = "./mp32rtp -i " + ServerIP + " -p " + ServerRTPPort + " < " + audio
    os.system(order)
    order = "cvlc rtp://@127.0.0.1:" + datos[2]['puerto']
    os.system(order)
    Server = ServerIP + ':'
    Server += ServerRTPPort + ' '
    uaserver.AddtoLog(datos[4]['path'], Server + 'Enviando RTP', 'Send')
Exemple #9
0
def ManageRegister(datos):

    USER = datos[0]['username']
    SERVERPORT = int(datos[1]['puerto'])

    Message = METHOD + ' sip:' + USER + ':' + str(SERVERPORT)
    Message += ' SIP/2.0\r\n' + 'Expires: ' + sys.argv[3] + '\r\n\r\n'

    my_socket.send(bytes(Message, 'utf-8'))
    Server = datos[3]['ip'] + ':' + datos[3]['puerto'] + ' '
    uaserver.AddtoLog(datos[4]['path'], Server + Message, 'Send')
    RecieveRegister()
Exemple #10
0
    def handle(self):

        DATA = []

        for line in self.rfile:
            DATA.append(line.decode('utf-8'))
        Message = ' '.join(DATA)
        Client = self.client_address[0] + ':'
        Client += str(self.client_address[1]) + ' '
        uaserver.AddtoLog(self.datos[2]['path'], Client + Message, 'Receive')

        if DATA[0].split(' ')[0] == 'REGISTER':
            self.RegisterManager(DATA)
        elif DATA[0].split(' ')[0] == 'INVITE':
            self.InviteManager(DATA)
        elif DATA[0].split(' ')[0] == 'ACK':
            if DATA[0].split(':')[1].split(' ')[0] in self.DicUsers:
                self.SendtoServer(DATA)
            else:
                self.wfile.write(b"SIP/2.0 404 User Not Found\r\n\r\n")
                Message = "SIP/2.0 404 User Not Found"
                uaserver.AddtoLog(self.datos[2]['path'], Message, 'Error')
                uaserver.AddtoLog(self.datos[2]['path'], Client + Message,
                                  'Send')

        elif DATA[0].split(' ')[0] == 'BYE':
            if DATA[0].split(':')[1].split(' ')[0] in self.DicUsers:
                self.SendtoServer(DATA)
            else:
                self.wfile.write(b"SIP/2.0 404 User Not Found\r\n\r\n")
                Message = "SIP/2.0 404 User Not Found"
                uaserver.AddtoLog(self.datos[2]['path'], Message, 'Error')
                uaserver.AddtoLog(self.datos[2]['path'], Client + Message,
                                  'Send')
        else:
            self.wfile.write(b"SIP/2.0 405 Method Not Allowed\r\n\r\n")
            Message = "SIP/2.0 405 Method Not Allowed"
            uaserver.AddtoLog(self.datos[2]['path'], Message, 'Error')
            uaserver.AddtoLog(self.datos[2]['path'], Client + Message, 'Send')
Exemple #11
0
    USER = datos[0]['username']
    PASSWORD = datos[0]['passwd']
    METHOD = sys.argv[2]
    SERVERIP = datos[1]['ip']
    if SERVERIP == '':
        SERVERIP = '127.0.0.1'
    SERVERPORT = int(datos[1]['puerto'])
    PROXYIP = datos[3]['ip']
    if PROXYIP == '':
        PROXYIP = '127.0.0.1'
    PROXYPORT = int(datos[3]['puerto'])

    # Creamos el socket, lo configuramos y lo atamos a un servidor/puerto
    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((PROXYIP, PROXYPORT))

        try:

            if METHOD == 'REGISTER':
                ManageRegister(datos)
            elif METHOD == 'INVITE':
                ManageInvite(datos)
            elif METHOD == 'BYE':
                ManageBye(datos)
        except ConnectionRefusedError:
            Message = 'No server listening at ' + PROXYIP
            Message += ' port ' + str(PROXYPORT)
            print(Message)
            uaserver.AddtoLog(datos[4]['path'], Message, 'Error')
Exemple #12
0
                self.SendtoServer(DATA)
            else:
                self.wfile.write(b"SIP/2.0 404 User Not Found\r\n\r\n")
                Message = "SIP/2.0 404 User Not Found"
                uaserver.AddtoLog(self.datos[2]['path'], Message, 'Error')
                uaserver.AddtoLog(self.datos[2]['path'], Client + Message,
                                  'Send')
        else:
            self.wfile.write(b"SIP/2.0 405 Method Not Allowed\r\n\r\n")
            Message = "SIP/2.0 405 Method Not Allowed"
            uaserver.AddtoLog(self.datos[2]['path'], Message, 'Error')
            uaserver.AddtoLog(self.datos[2]['path'], Client + Message, 'Send')


if __name__ == "__main__":
    # Creamos servidor de eco y escuchamos
    if len(sys.argv) != 2:
        sys.exit('Usage: python3 uaserver.py config')

    datos = parsercreator(sys.argv[1])
    uaserver.AddtoLog(datos[2]['path'], 'Starting...', 'Status')
    IP = datos[0]['ip']
    PORT = int(datos[0]['puerto'])
    serv = socketserver.UDPServer((IP, PORT), EchoHandler)
    print("Server " + datos[0]['name'] + ' listening at port ' + str(PORT))
    try:
        serv.serve_forever()
    except KeyboardInterrupt:
        print('Cerrando servidor')
        uaserver.AddtoLog(datos[2]['path'], 'Finshing.', 'Status')