def handleRegister(self, data): (dUsername, dPassword) = netstruct.unpack("b$b$", data) username = str(dUsername) password = str(dPassword) print "Login:"******"Passwort:", password userExists = False for userrow in DatabaseConnector.Instance().getConnector().execute( "SELECT * FROM chat_users WHERE username=?", (username.lower(), )): userExists = True if userExists: responseData = netstruct.pack("ib$", 3, "Username already Exists") self.sendPacket(1, responseData) return pwhash = bcrypt.hashpw(password, bcrypt.gensalt()) DatabaseConnector.Instance().getConnector().execute( "INSERT INTO chat_users (id, username, password, ismod, isadmin, lastlogin, lastip, banned, banreason, banliftdate) VALUES(NULL, ?, ?, 0, 0, NULL, '0.0.0.0', 0, '', 0) ", ( username.lower(), pwhash, )) DatabaseConnector.Instance().writeDB() self.m_clientNick = username responseData = netstruct.pack("ib$", 0, "") self.sendPacket(1, responseData)
def proto_to_bytes(proto: dict) -> bytes: res = b'' for component in proto.get('components', []): res += netstruct.pack(b'I$', component['name'].encode()) if component.get('interface'): res += b'\xff' + netstruct.pack(b'I$', bytes.fromhex(component['interface'])) else: res += b'\x00' # we should also handle patch case res += netstruct.pack(b'I$', bytes.fromhex(component.get('implementation', ''))) res = netstruct.pack(b'hI$', proto['expected_env_version'], res) return res
def sendRoomMessage(self, room, message): #RoomManager.Instace().getRoomByID(roomID) data = netstruct.pack("b$", room.getRoomID()) data += self.m_lengthdString.build( dict( length=len(message), data=message, )) self.sendPacket(3, data)
def sendPacket(self, packetID, data): packetData = netstruct.pack("i", packetID) packetData += data if self.m_encryptionEnabled: print "Sending Encrypted Packet" encData = self.m_encryptionModule.encryptData(packetData) self.m_socket.send(encData) else: print "Sending Plain Packet" self.m_socket.send(packetData) sleep(0.05)
def broadcastMessage(self, message): msgData = netstruct.pack("b$b$b$", message.getRoomID(), message.getMessageID(), message.getOwnerID()) content = message.getMessage() msgData += self.m_lengthdString.build( dict( length=len(content), data=content, )) self.sendPacket(3, msgData) return
def sendPacket(self, packetID, data): try: packetData = netstruct.pack("i", packetID) packetData += data if self.m_encryptionEnabled: encData = self.m_encryptionModule.encryptData(packetData) self.m_clientSocket.send(encData) else: self.m_clientSocket.send(packetData) except Exception: RoomManager.Instance().removeUserFromAllRooms(self) print "Send Packet Failed" return time.sleep(0.05)
def handleLogin(self, data): (dUsername, dPassword) = netstruct.unpack("b$b$", data) username = str(dUsername) password = str(dPassword) print "Login:"******"Passwort:", password loginValid = False errorCode = 0 errorReason = "" userIsBanned = 0 banReason = "" banLift = -1 for userrow in DatabaseConnector.Instance().getConnector().execute( "SELECT * FROM chat_users WHERE username=?", (username.lower(), )): if bcrypt.checkpw(password.encode('utf-8'), userrow[2].encode('utf-8')): loginValid = True userIsBanned = userrow[7] banReason = userrow[8] banLift = userrow[9] if loginValid: print "Login is Valid" if userIsBanned != 0: errorCode = 2 errorReason = "Banned: " + str(banReason) print "User is Banned!" else: print "Login is Invalid" errorCode = 1 errorReason = "Username/Password Invalid" self.m_clientNick = username responseData = netstruct.pack("ib$", errorCode, errorReason) self.sendPacket(1, responseData)
def handleRoomListRequest(self, data): print "Got Room List Request" for room in RoomManager.Instance().getAllRooms(): roomPacket = netstruct.pack("b$iib$", room.getRoomID(), 0, 1337, room.getRoomName()) self.sendPacket(2, roomPacket)
def test_too_many(self): with self.assertRaises(netstruct.error): netstruct.pack(b"4i", 1, 2, 3, 4, 5)
def test_enough(self): self.assertEqual( netstruct.pack(b"4b", 1, 2, 3, 4), b"\x01\x02\x03\x04" )
def test_not_enough(self): with self.assertRaises(netstruct.error): netstruct.pack(b"4i", 1, 2, 3)
def test_enough(self): self.assertEqual(netstruct.pack(b"4b", 1, 2, 3, 4), b"\x01\x02\x03\x04")
def set_password(self, service, username, password): password = netstruct.pack(self.pattern, username.encode('UTF-8'), password.encode('UTF-8')).decode('UTF-8') username = service return super().set_password(service, username, password)
def serialize(self) -> bytes: return super().serialize() + \ self.endpoint.serialize() + \ netstruct.pack(b'H$', self._initiator_name.encode('utf-8'))
def pack(self, symm_key): ct, nonce = self._encrypt(symm_key) return netstruct.pack(b'!b$b$i', ct, nonce, self.user_id)
def _encrypt(self, symm_key): buffer = netstruct.pack(b'!b$b$b$i', self.ticket, self.new_ticket, self.ip.encode(), self.new_n) aesgcm = AESGCM(symm_key) nonce = os.urandom(32) return aesgcm.encrypt(nonce, buffer, str(self.user_id).encode()), nonce
def test_string(self): self.assertEqual(netstruct.pack(b"b$i", b"Hello.", 42), b"\x06Hello.\x00\x00\x00\x2A")
def test_empty(self): self.assertEqual(netstruct.pack(b""), b"")
def register(self, username, password, callback): self.m_loginCallback = callback data = netstruct.pack("b$b$", username, password) self.sendPacket(7, data)
def joinRoom(self, roomid, password, callback): data = netstruct.pack("b$b$", roomid, password) self.sendPacket(6, data)
def test_string(self): self.assertEqual( netstruct.pack(b"b$i", b"Hello.", 42), b"\x06Hello.\x00\x00\x00\x2A" )