Beispiel #1
0
    def propose_session(self, recv_dtg):
        dtg = Datagram(TransportAim.SESSION_PROPOSAL, self.gainer.ip,
                       self.gainer.port, recv_dtg.source_ip,
                       recv_dtg.source_port)
        if recv_dtg.source_ip in self.gainer.sessions.keys():
            session = self.gainer.sessions[recv_dtg.source_ip]
            dtg.set_payload(session)
            self.send_datagram(dtg)
        else:
            session = {
                'session_id': len(self.gainer.sessions),
                'server_ip': self.gainer.ip,
                'server_port': self.gainer.port,
                'client_ip': recv_dtg.source_ip,
                'client_port': recv_dtg.source_port,
                'AES_key': utils.random_string()
            }
            self.gainer.sessions[session['client_ip']] = session

            dtg.set_payload(session)
            self.send_datagram(dtg)

            self.gainer.AES_ciphers[recv_dtg.source_ip] = None
            cipher = AES.new(session['AES_key'].encode(), AES.MODE_ECB)
            self.gainer.AES_ciphers[recv_dtg.source_ip] = cipher
Beispiel #2
0
 def server_close_session(self, session):
     print("Before closing session:", self.gainer.sessions)
     dtg = Datagram(TransportAim.CLOSE, self.gainer.ip, self.gainer.port,
                    session['client_ip'], session['client_port'])
     app_layer_resp = {'verb': AppVerb.CLOSE, 'message': "Session closed."}
     dtg.set_payload(app_layer_resp)
     self.send_datagram(dtg)
     del self.gainer.sessions[session['client_ip']]
     print("After closing session:", self.gainer.sessions)
Beispiel #3
0
 def client_close_session(self, app_layer_req):
     server_ip = app_layer_req['data']['server_ip']
     dtg = Datagram(TransportAim.CLOSE, self.gainer.ip, self.gainer.port,
                    self.gainer.sessions[server_ip]['server_ip'],
                    self.gainer.sessions[server_ip]['server_port'])
     dtg.set_payload(app_layer_req)
     self.send_datagram(dtg)
     dtg, address = self.gainer.transport.receive_datagram()
     print(dtg.aim)
     print(self.gainer.sessions)
     del self.gainer.sessions[server_ip]
     print(self.gainer.sessions)
Beispiel #4
0
 def send_packet(self, payload, num_payloads, payload_index):
     packet = Datagram()
     packet.set_head(message_type=1,
                     message_id=1,
                     num_payloads=num_payloads,
                     payload_index=payload_index,
                     payload_size=len(payload),
                     error_type=0,
                     restart_index=0)
     packet.set_payload(payload=payload)
     packet.set_EOP(0)
     self.transmitter.send(packet.get_datagram())
Beispiel #5
0
 def delete_user(self, data, session):
     dtg = Datagram(TransportAim.APP_RESPONSE, self.gainer.ip,
                    self.gainer.port, session['client_ip'],
                    session['client_port'])
     if data['username'] not in self.gainer.users.keys():
         app_layer_resp = {
             'verb': AppVerb.ERR,
             'message': "This user doe not exists in the database."
         }
     else:
         del self.gainer.users[data['username']]
         app_layer_resp = {'verb': AppVerb.OK, 'message': "User deleted."}
     dtg.set_payload(app_layer_resp)
     self.gainer.transport.send_datagram(dtg)
Beispiel #6
0
 def client_send_data(self, app_layer_req, dest_ip):
     if dest_ip not in self.gainer.sessions.keys():
         session = self.gainer.transport.get_session(dest_ip)
         if not session:
             raise Exception(
                 "Could not get a session after several attempts.")
     session = self.gainer.sessions[dest_ip]
     print("===========================================================")
     dtg = Datagram(TransportAim.APP_REQUEST, self.gainer.ip,
                    self.gainer.port, session['server_ip'],
                    config.SERVER_PORT)
     dtg.set_payload(app_layer_req)
     self.gainer.transport.send_datagram(dtg)
     recv_dtg, address = self.gainer.transport.receive_datagram()
     print("App response:", recv_dtg.get_payload())