Add basically support for encrypted app list syncing

This commit is contained in:
Jonas Lochmann
2022-07-25 02:00:00 +02:00
parent acdec990ea
commit 8a5e46811e
104 changed files with 5287 additions and 55 deletions
@@ -0,0 +1,119 @@
/*
* server component for the TimeLimit App
* Copyright (C) 2019 - 2022 Jonas Lochmann
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import * as Sequelize from 'sequelize'
import { Database } from '../../../database'
import { ClientDataStatusDevicesExtended } from '../../../object/clientdatastatus'
import { ServerExtendedDeviceData, ServerCryptContainer } from '../../../object/serverdatastatus'
import { FamilyEntry } from './family-entry'
import { types } from '../../../database/encryptedapplist'
export async function getDeviceDetailList ({ database, transaction, familyEntry, devicesDetail }: {
database: Database
transaction: Sequelize.Transaction
familyEntry: FamilyEntry
devicesDetail: ClientDataStatusDevicesExtended
}): Promise<Array<ServerExtendedDeviceData> | null> {
const serverEncryptedAppsVersions = (await database.encryptedAppList.findAll({
where: {
familyId: familyEntry.familyId,
},
attributes: ['deviceId', 'type', 'version'],
transaction
})).map((item) => ({
deviceId: item.deviceId,
type: item.type,
version: item.version
}))
const devicesWithChangedBaseApps: Array<string> = []
const devicesWithChangedDiffApps: Array<string> = []
serverEncryptedAppsVersions.forEach((item) => {
if (item.type === types.base) {
if (!devicesDetail[item.deviceId] || devicesDetail[item.deviceId].appsB !== item.version) {
devicesWithChangedBaseApps.push(item.deviceId)
}
} else if (item.type === types.diff) {
if (!devicesDetail[item.deviceId] || devicesDetail[item.deviceId].appsD !== item.version) {
devicesWithChangedDiffApps.push(item.deviceId)
}
}
})
const updatedDeviceIds = Array.from(new Set([...devicesWithChangedBaseApps, ...devicesWithChangedDiffApps]))
if (updatedDeviceIds.length === 0) return null
const updatedBaseApps = devicesWithChangedBaseApps.length === 0 ? [] : (await database.encryptedAppList.findAll({
where: {
familyId: familyEntry.familyId,
deviceId: {
[Sequelize.Op.in]: devicesWithChangedBaseApps
},
type: types.base
},
attributes: [
'deviceId',
'version',
'data'
],
transaction
})).map((item) => ({
deviceId: item.deviceId,
version: item.version,
data: item.data
}))
const updatedDiffApps = devicesWithChangedDiffApps.length === 0 ? [] : (await database.encryptedAppList.findAll({
where: {
familyId: familyEntry.familyId,
deviceId: {
[Sequelize.Op.in]: devicesWithChangedDiffApps
},
type: types.diff
},
attributes: [
'deviceId',
'version',
'data'
],
transaction
})).map((item) => ({
deviceId: item.deviceId,
version: item.version,
data: item.data
}))
return updatedDeviceIds.map((deviceId) => {
const appsBase = updatedBaseApps.find((item) => item.deviceId === deviceId)
const appsDiff = updatedDiffApps.find((item) => item.deviceId === deviceId)
return {
deviceId,
appsBase: appsBase ? wrapServerCryptContainer(appsBase) : undefined,
appsDiff: appsDiff ? wrapServerCryptContainer(appsDiff) : undefined
}
})
}
function wrapServerCryptContainer({ version, data }: { version: string, data: Buffer }): ServerCryptContainer {
return {
version,
data: data.toString('base64')
}
}
@@ -65,7 +65,8 @@ export async function getDeviceList ({ database, transaction, familyEntry }: {
wasAsEnabled: item.wasAsEnabled,
activityLevelBlocking: item.activityLevelBlocking,
qOrLater: item.isQorLater,
mFlags: item.manipulationFlags
mFlags: item.manipulationFlags,
pk: item.publicKey ? item.publicKey.toString('base64') : undefined
}))
}
}
@@ -26,25 +26,32 @@ import {
getCategoryAssignedApps, getCategoryBaseDatas, getCategoryDataToSync,
getRules, getTasks, getUsedTimes
} from './category'
import { getDeviceDetailList } from './device-detail'
import { getDeviceList } from './device-list'
import { getFamilyEntry } from './family-entry'
import { getUserList } from './user-list'
import { getKeyRequests } from './key-requests'
import { getKeyResponses } from './key-responses'
export const generateServerDataStatus = async ({ database, clientStatus, familyId, transaction }: {
export const generateServerDataStatus = async ({
database, clientStatus, familyId, deviceId, transaction
}: {
database: Database
clientStatus: ClientDataStatus
familyId: string
deviceId: string
transaction: Sequelize.Transaction
}): Promise<ServerDataStatus> => {
const familyEntry = await getFamilyEntry({ database, familyId, transaction })
const doesClientSupportTasks = clientStatus.clientLevel !== undefined && clientStatus.clientLevel >= 3
const doesClientSupportCryptoApps = clientStatus.clientLevel !== undefined && clientStatus.clientLevel >= 4
const result: ServerDataStatus = {
fullVersion: config.alwaysPro ? 1 : (
familyEntry.hasFullVersion ? parseInt(familyEntry.fullVersionUntil, 10) : 0
),
message: await getStatusMessage({ database, transaction }) || undefined,
apiLevel: 3
apiLevel: 4
}
if (familyEntry.deviceListVersion !== clientStatus.devices) {
@@ -103,5 +110,30 @@ export const generateServerDataStatus = async ({ database, clientStatus, familyI
})
}
if (doesClientSupportCryptoApps) {
result.devices2 = await getDeviceDetailList({
database,
transaction,
familyEntry,
devicesDetail: clientStatus.devicesDetail || {}
}) || undefined
result.krq = await getKeyRequests({
database,
transaction,
familyEntry,
deviceId,
lastSeenRequestIndex: clientStatus.kri || null
}) || undefined
result.kr = await getKeyResponses({
database,
transaction,
familyEntry,
deviceId,
lastSeenRequestIndex: clientStatus.kr || null
}) || undefined
}
return result
}
@@ -0,0 +1,58 @@
/*
* server component for the TimeLimit App
* Copyright (C) 2019 - 2022 Jonas Lochmann
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import * as Sequelize from 'sequelize'
import { Database } from '../../../database'
import { ServerKeyRequest } from '../../../object/serverdatastatus'
import { FamilyEntry } from './family-entry'
export async function getKeyRequests ({ database, transaction, familyEntry, lastSeenRequestIndex, deviceId }: {
database: Database
transaction: Sequelize.Transaction
familyEntry: FamilyEntry
lastSeenRequestIndex: number | null
deviceId: string
}): Promise<Array<ServerKeyRequest> | null> {
const data = await database.keyRequest.findAll({
where: {
familyId: familyEntry.familyId,
senderDeviceId: {
[Sequelize.Op.ne]: deviceId
},
...(lastSeenRequestIndex === null ? {} : {
serverSequenceNumber: {
[Sequelize.Op.gt]: lastSeenRequestIndex
}
})
},
transaction,
limit: 32
})
if (data.length === 0) return null
return data.map((item) => ({
srvSeq: parseInt(item.serverSequenceNumber),
senId: item.senderDeviceId,
senSeq: parseInt(item.senderSequenceNumber),
deviceId: item.deviceId || undefined,
categoryId: item.categoryId || undefined,
type: item.type,
tempKey: item.tempKey.toString('base64'),
signature: item.signature.toString('base64')
}))
}
@@ -0,0 +1,63 @@
/*
* server component for the TimeLimit App
* Copyright (C) 2019 - 2022 Jonas Lochmann
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import * as Sequelize from 'sequelize'
import { Database } from '../../../database'
import { ServerKeyResponse } from '../../../object/serverdatastatus'
import { FamilyEntry } from './family-entry'
export async function getKeyResponses ({ database, transaction, familyEntry, lastSeenRequestIndex, deviceId }: {
database: Database
transaction: Sequelize.Transaction
familyEntry: FamilyEntry
lastSeenRequestIndex: number | null
deviceId: string
}): Promise<Array<ServerKeyResponse> | null> {
if (lastSeenRequestIndex !== null) {
await database.keyResponse.destroy({
where: {
familyId: familyEntry.familyId,
receiverDeviceId: deviceId,
replyServerSequenceNumber: {
[Sequelize.Op.lte]: lastSeenRequestIndex.toString(10)
}
},
transaction
})
}
const data = await database.keyResponse.findAll({
where: {
familyId: familyEntry.familyId,
receiverDeviceId: deviceId,
},
order: [['replyServerSequenceNumber', 'ASC']],
transaction,
limit: 32
})
if (data.length === 0) return null
return data.map((item) => ({
srvSeq: parseInt(item.replyServerSequenceNumber),
sender: item.senderDeviceId,
rqSeq: parseInt(item.requestClientSequenceNumber),
tempKey: item.tempKey.toString('base64'),
cryptKey: item.encryptedKey.toString('base64'),
signature: item.signature.toString('base64')
}))
}