def getPayload(self):
        payload = bytearray()

        payload.extend(Proto.build_fixed_int(1, self.protocolVersion))
        payload.extend(Proto.build_null_str(self.serverVersion))
        payload.extend(Proto.build_fixed_int(4, self.connectionId))
        payload.extend(Proto.build_fixed_str(8, self.challenge1))
        payload.extend(Proto.build_filler(1))
        payload.extend(Proto.build_fixed_int(2, self.capabilityFlags >> 16))
        payload.extend(Proto.build_fixed_int(1, self.characterSet))
        payload.extend(Proto.build_fixed_int(2, self.statusFlags))
        payload.extend(Proto.build_fixed_int(2, self.capabilityFlags & 0xffff))

        if self.hasCapabilityFlag(Flags.CLIENT_PLUGIN_AUTH):
            payload.extend(Proto.build_fixed_int(1, self.authPluginDataLength))
        else:
            payload.extend(Proto.build_filler(1))

        payload.extend(Proto.build_filler(10))

        if self.hasCapabilityFlag(Flags.CLIENT_SECURE_CONNECTION):
            payload.extend(Proto.build_fixed_str(
                max(13, self.authPluginDataLength - 8),
                           self.challenge2))
        if self.hasCapabilityFlag(Flags.CLIENT_PLUGIN_AUTH):
            payload.extend(Proto.build_null_str(self.authPluginName))

        return payload
Esempio n. 2
0
    def getPayload(self):
        payload = bytearray()

        if self.hasCapabilityFlag(Flags.CLIENT_PROTOCOL_41):
            payload.extend(Proto.build_fixed_int(4, self.capabilityFlags))
            payload.extend(Proto.build_fixed_int(4, self.maxPacketSize))
            payload.extend(Proto.build_fixed_int(1, self.characterSet))
            payload.extend(Proto.build_filler(23))
            payload.extend(Proto.build_null_str(self.username))

            if self.hasCapabilityFlag(
                    Flags.CLIENT_PLUGIN_AUTH_LENENC_CLIENT_DATA):
                payload.extend(Proto.build_lenenc_int(len(self.authResponse)))
                payload.extend(
                    Proto.build_fixed_str(len(self.authResponse),
                                          self.authResponse))
            elif self.hasCapabilityFlag(Flags.CLIENT_SECURE_CONNECTION):
                payload.extend(Proto.build_lenenc_int(len(self.authResponse)))
                payload.extend(
                    Proto.build_fixed_str(len(self.authResponse),
                                          self.authResponse))
            else:
                payload.extend(Proto.build_null_str(self.authResponse))

            if self.hasCapabilityFlag(Flags.CLIENT_CONNECT_WITH_DB):
                payload.extend(Proto.build_null_str(self.schema))

            if self.hasCapabilityFlag(Flags.CLIENT_PLUGIN_AUTH):
                payload.extend(Proto.build_null_str(self.pluginName))

            if self.hasCapabilityFlag(Flags.CLIENT_CONNECT_ATTRS):
                attributes = bytearray()
                for k, v in self.clientAttributes.items():
                    attributes.extend(Proto.build_lenenc_str(k))
                    attributes.extend(Proto.build_lenenc_str(v))
                payload.extend(Proto.build_lenenc_int(len(attributes)))
                payload.extend(attributes)

        else:
            payload.extend(Proto.build_fixed_int(2, self.capabilityFlags))
            payload.extend(Proto.build_fixed_int(3, self.maxPacketSize))
            payload.extend(Proto.build_null_str(self.username))
            if self.hasCapabilityFlag(Flags.CLIENT_CONNECT_WITH_DB):
                payload.extend(Proto.build_null_str(self.authResponse))
                payload.extend(Proto.build_null_str(self.schema))
            else:
                payload.extend(Proto.build_eop_str(self.authResponse))

        return payload