新增数据同步服务端模式已认证设备列表管理

This commit is contained in:
lyswhut
2023-08-29 15:40:42 +08:00
parent 5d877d81e3
commit 7d17423e4b
24 changed files with 595 additions and 158 deletions
+2
View File
@@ -9,6 +9,8 @@ export {
stopServer,
getStatus as getServerStatus,
generateCode,
getDevices as getServerDevices,
removeDevice as removeServerDevice,
} from './server'
export {
+3 -9
View File
@@ -1,14 +1,8 @@
import {
startServer,
stopServer,
getStatus,
generateCode,
} from './server'
export {
startServer,
stopServer,
getStatus,
generateCode,
}
getDevices,
removeDevice,
} from './server'
@@ -44,6 +44,10 @@ export class ListManage {
await this.snapshotDataManage.updateDeviceSnapshotKey(clientId, key)
}
removeDevice = async(clientId: string) => {
this.snapshotDataManage.removeSnapshotInfo(clientId)
}
getListData = async(): Promise<LX.Sync.List.ListData> => {
return getLocalListData()
}
@@ -68,6 +68,15 @@ export class SnapshotDataManage {
this.saveSnapshotInfoThrottle()
}
removeSnapshotInfo = (clientId: string) => {
let client = this.snapshotInfo.clients[clientId]
if (!client) return
if (client.snapshotKey) this.clientSnapshotKeys.splice(this.clientSnapshotKeys.indexOf(client.snapshotKey), 1)
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
delete this.snapshotInfo.clients[clientId]
this.saveSnapshotInfoThrottle()
}
getSnapshot = async(name: string) => {
const filePath = path.join(this.snapshotDir, `snapshot_${name}`)
let listData: LX.Sync.List.ListData
@@ -1,4 +1,5 @@
// import { SYNC_CLOSE_CODE } from '../../../../constants'
import { removeSelectModeListener, sendCloseSelectMode, sendSelectMode } from '@main/modules/winMain'
import { getUserSpace, getUserConfig } from '../../../user'
import { getLocalListData, setLocalListData } from '@main/modules/sync/utils'
// import { LIST_IDS } from '@common/constants'
@@ -29,9 +30,26 @@ const getRemoteListMD5 = async(socket: LX.Sync.Server.Socket): Promise<string> =
// const getLocalListData = async(socket: LX.Sync.Server.Socket): Promise<LX.Sync.List.ListData> => {
// return getUserSpace(socket.userInfo.name).listManage.getListData()
// }
const getSyncMode = async(socket: LX.Sync.Server.Socket): Promise<LX.Sync.List.SyncMode> => {
return socket.remoteQueueList.list_sync_get_sync_mode()
}
const getSyncMode = async(socket: LX.Sync.Server.Socket): Promise<LX.Sync.List.SyncMode> => new Promise((resolve, reject) => {
const handleDisconnect = (err: Error) => {
sendCloseSelectMode()
removeSelectModeListener()
reject(err)
}
let removeEventClose = socket.onClose(handleDisconnect)
sendSelectMode(socket.keyInfo.deviceName, (mode) => {
if (mode == null) {
reject(new Error('cancel'))
return
}
resolve(mode)
removeSelectModeListener()
removeEventClose()
})
})
// const getSyncMode = async(socket: LX.Sync.Server.Socket): Promise<LX.Sync.List.SyncMode> => {
// return socket.remoteQueueList.list_sync_get_sync_mode()
// }
const finishedSync = async(socket: LX.Sync.Server.Socket) => {
await socket.remoteQueueList.list_sync_finished()
+3 -9
View File
@@ -1,14 +1,8 @@
import {
startServer,
stopServer,
getStatus,
generateCode,
} from './server'
export {
startServer,
stopServer,
getStatus,
generateCode,
}
getDevices,
removeDevice,
} from './server'
@@ -385,3 +385,18 @@ export const generateCode = async() => {
sendServerStatus(status)
return status.code
}
export const getDevices = async() => {
const userSpace = getUserSpace()
return userSpace.getDecices()
}
export const removeDevice = async(clientId: string) => {
if (wss) {
for (const client of wss.clients) {
if (client.keyInfo.clientId == clientId) client.close(SYNC_CLOSE_CODE.normal)
}
}
const userSpace = getUserSpace()
await userSpace.removeDevice(clientId)
}
+11 -1
View File
@@ -3,7 +3,7 @@ import path from 'node:path'
import { randomBytes } from 'node:crypto'
import { throttle } from '@common/utils/common'
import { filterFileName, toMD5 } from '../utils'
import { File } from '../../../../../common/constants_sync'
import { File } from '@common/constants_sync'
interface ServerInfo {
@@ -105,6 +105,10 @@ export class UserDataManage {
devicesInfo: DevicesInfo
private readonly saveDevicesInfoThrottle: () => void
getAllClientKeyInfo = () => {
return Object.values(this.devicesInfo.clients).sort((a, b) => (b.lastConnectDate ?? 0) - (a.lastConnectDate ?? 0))
}
saveClientKeyInfo = (keyInfo: LX.Sync.ServerKeyInfo) => {
if (this.devicesInfo.clients[keyInfo.clientId] == null && Object.keys(this.devicesInfo.clients).length > 101) throw new Error('max keys')
this.devicesInfo.clients[keyInfo.clientId] = keyInfo
@@ -116,6 +120,12 @@ export class UserDataManage {
return this.devicesInfo.clients[clientId] ?? null
}
removeClientKeyInfo = async(clientId: string) => {
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
delete this.devicesInfo.clients[clientId]
this.saveDevicesInfoThrottle()
}
isIncluedsClient = (clientId: string) => {
return Object.values(this.devicesInfo.clients).some(client => client.clientId == clientId)
}
+11 -1
View File
@@ -6,6 +6,8 @@ import {
export interface UserSpace {
dataManage: UserDataManage
listManage: ListManage
getDecices: () => Promise<LX.Sync.ServerKeyInfo[]>
removeDevice: (clientId: string) => Promise<void>
}
const users = new Map<string, UserSpace>()
@@ -31,9 +33,17 @@ export const getUserSpace = (userName = 'default') => {
if (!user) {
console.log('new user data manage:', userName)
const dataManage = new UserDataManage(userName)
const listManage = new ListManage(dataManage)
users.set(userName, user = {
dataManage,
listManage: new ListManage(dataManage),
listManage,
async getDecices() {
return this.dataManage.getAllClientKeyInfo()
},
async removeDevice(clientId) {
await listManage.removeDevice(clientId)
await dataManage.removeClientKeyInfo(clientId)
},
})
}
return user
+17 -1
View File
@@ -1,6 +1,16 @@
import { mainHandle } from '@common/mainIpc'
import { WIN_MAIN_RENDERER_EVENT_NAME } from '@common/ipcNames'
import { startServer, stopServer, getServerStatus, generateCode, connectServer, disconnectServer, getClientStatus } from '@main/modules/sync'
import {
startServer,
stopServer,
getServerStatus,
generateCode,
connectServer,
disconnectServer,
getClientStatus,
getServerDevices,
removeServerDevice,
} from '@main/modules/sync'
import { sendEvent } from '../main'
let selectModeListenr: ((mode: LX.Sync.List.SyncMode | null) => void) | null = null
@@ -27,6 +37,12 @@ export default () => {
break
}
})
mainHandle<never, LX.Sync.ServerDevices>(WIN_MAIN_RENDERER_EVENT_NAME.sync_get_server_devices, async() => {
return getServerDevices()
})
mainHandle<string>(WIN_MAIN_RENDERER_EVENT_NAME.sync_remove_server_device, async({ params: clientId }) => {
await removeServerDevice(clientId)
})
}