From 3685cb8868746aa65482c52cdaf753424c698a92 Mon Sep 17 00:00:00 2001 From: lyswhut Date: Wed, 6 Sep 2023 21:50:58 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=BD=9C=E5=9C=A8=E5=90=8C?= =?UTF-8?q?=E6=AD=A5=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/modules/sync/client/client.ts | 12 ++++++------ src/main/modules/sync/client/data.ts | 2 +- .../sync/client/modules/dislike/localEvent.ts | 8 +++++++- .../sync/client/modules/list/localEvent.ts | 8 +++++++- src/main/modules/sync/migrate.ts | 3 +-- .../modules/sync/server/modules/dislike/manage.ts | 15 ++++++++------- .../sync/server/modules/dislike/sync/sync.ts | 7 ++++--- .../modules/sync/server/modules/list/manage.ts | 15 ++++++++------- .../modules/sync/server/modules/list/sync/sync.ts | 3 ++- src/main/modules/sync/server/server/server.ts | 4 ++-- src/main/modules/sync/server/user/data.ts | 2 +- src/main/modules/sync/utils.ts | 4 ++++ 12 files changed, 51 insertions(+), 32 deletions(-) diff --git a/src/main/modules/sync/client/client.ts b/src/main/modules/sync/client/client.ts index ddc88382..a26e3ea3 100644 --- a/src/main/modules/sync/client/client.ts +++ b/src/main/modules/sync/client/client.ts @@ -189,12 +189,12 @@ export const connect = (urlInfo: LX.Sync.Client.UrlInfo, keyInfo: LX.Sync.Client onError(error, path, groupName) { const name = groupName ?? '' log.error(`sync call ${name} ${path.join('.')} error:`, error) - if (groupName == null) return - client?.close(SYNC_CLOSE_CODE.failed) - sendSyncStatus({ - status: false, - message: error.message, - }) + // if (groupName == null) return + // client?.close(SYNC_CLOSE_CODE.failed) + // sendSyncStatus({ + // status: false, + // message: error.message, + // }) }, }) diff --git a/src/main/modules/sync/client/data.ts b/src/main/modules/sync/client/data.ts index 487eff88..1fb8b75a 100644 --- a/src/main/modules/sync/client/data.ts +++ b/src/main/modules/sync/client/data.ts @@ -1,6 +1,7 @@ import fs from 'node:fs' import path from 'node:path' import { File } from '../../../../common/constants_sync' +import { exists } from '../utils' let syncAuthKeys: Record @@ -11,7 +12,6 @@ const saveSyncAuthKeys = async() => { return fs.promises.writeFile(syncAuthKeysFilePath, JSON.stringify(syncAuthKeys), 'utf8') } -const exists = async(path: string) => fs.promises.stat(path).then(() => true).catch(() => false) export const initClientInfo = async() => { if (syncAuthKeys != null) return const syncAuthKeysFilePath = path.join(global.lxDataPath, File.clientDataPath, File.syncAuthKeysJSON) diff --git a/src/main/modules/sync/client/modules/dislike/localEvent.ts b/src/main/modules/sync/client/modules/dislike/localEvent.ts index 1e6f73f1..4a8211e3 100644 --- a/src/main/modules/sync/client/modules/dislike/localEvent.ts +++ b/src/main/modules/sync/client/modules/dislike/localEvent.ts @@ -1,3 +1,4 @@ +import { SYNC_CLOSE_CODE } from '@common/constants_sync' import { registerDislikeActionEvent } from '@main/modules/sync/dislikeEvent' let unregisterLocalListAction: (() => void) | null @@ -11,7 +12,12 @@ export const registerEvent = (socket: LX.Sync.Client.Socket) => { unregisterEvent() unregisterLocalListAction = registerDislikeActionEvent((action) => { if (!socket.moduleReadys?.dislike) return - void socket.remoteQueueDislike.onDislikeSyncAction(action) + void socket.remoteQueueDislike.onDislikeSyncAction(action).catch(err => { + // TODO send status + socket.moduleReadys.dislike = false + socket.close(SYNC_CLOSE_CODE.failed) + console.log(err.message) + }) }) } diff --git a/src/main/modules/sync/client/modules/list/localEvent.ts b/src/main/modules/sync/client/modules/list/localEvent.ts index 9ba543f6..0b94e637 100644 --- a/src/main/modules/sync/client/modules/list/localEvent.ts +++ b/src/main/modules/sync/client/modules/list/localEvent.ts @@ -1,3 +1,4 @@ +import { SYNC_CLOSE_CODE } from '@common/constants_sync' import { registerListActionEvent } from '@main/modules/sync/listEvent' let unregisterLocalListAction: (() => void) | null @@ -11,7 +12,12 @@ export const registerEvent = (socket: LX.Sync.Client.Socket) => { unregisterEvent() unregisterLocalListAction = registerListActionEvent((action) => { if (!socket.moduleReadys?.list) return - void socket.remoteQueueList.onListSyncAction(action) + void socket.remoteQueueList.onListSyncAction(action).catch(err => { + // TODO send status + socket.moduleReadys.list = false + socket.close(SYNC_CLOSE_CODE.failed) + console.log(err.message) + }) }) } diff --git a/src/main/modules/sync/migrate.ts b/src/main/modules/sync/migrate.ts index df7a69ff..e17aca93 100644 --- a/src/main/modules/sync/migrate.ts +++ b/src/main/modules/sync/migrate.ts @@ -1,6 +1,7 @@ import { File } from '../../../common/constants_sync' import fs from 'node:fs' import path from 'node:path' +import { exists } from './utils' interface ServerKeyInfo { clientId: string @@ -13,8 +14,6 @@ interface ServerKeyInfo { } -const exists = async(path: string) => fs.promises.stat(path).then(() => true).catch(() => false) - // 迁移 v2 sync 数据 export default async(dataPath: string) => { const syncDataPath = path.join(dataPath, 'sync') diff --git a/src/main/modules/sync/server/modules/dislike/manage.ts b/src/main/modules/sync/server/modules/dislike/manage.ts index 618061a0..e60fcd4c 100644 --- a/src/main/modules/sync/server/modules/dislike/manage.ts +++ b/src/main/modules/sync/server/modules/dislike/manage.ts @@ -27,13 +27,14 @@ export class DislikeManage { } getCurrentListInfoKey = async() => { - const snapshotInfo = await this.snapshotDataManage.getSnapshotInfo() - if (snapshotInfo.latest) { - return snapshotInfo.latest - } - snapshotInfo.latest = toMD5((await this.getDislikeRules()).trim()) - this.snapshotDataManage.saveSnapshotInfo(snapshotInfo) - return snapshotInfo.latest + // const snapshotInfo = await this.snapshotDataManage.getSnapshotInfo() + // if (snapshotInfo.latest) { + // return snapshotInfo.latest + // } + // snapshotInfo.latest = toMD5((await this.getDislikeRules()).trim()) + // this.snapshotDataManage.saveSnapshotInfo(snapshotInfo) + // return snapshotInfo.latest + return this.createSnapshot() } getDeviceCurrentSnapshotKey = async(clientId: string) => { diff --git a/src/main/modules/sync/server/modules/dislike/sync/sync.ts b/src/main/modules/sync/server/modules/dislike/sync/sync.ts index b76847f0..a44d2975 100644 --- a/src/main/modules/sync/server/modules/dislike/sync/sync.ts +++ b/src/main/modules/sync/server/modules/dislike/sync/sync.ts @@ -23,7 +23,7 @@ const getRemoteDataMD5 = async(socket: LX.Sync.Server.Socket): Promise = } // const getLocalDislikeData async(socket: LX.Sync.Server.Socket): Promise => { -// return getUserSpace(socket.userInfo.name).listManage.getListData() +// return getUserSpace(socket.userInfo.name).dislikeManage.getListData() // } const getSyncMode = async(socket: LX.Sync.Server.Socket): Promise => new Promise((resolve, reject) => { const handleDisconnect = (err: Error) => { @@ -54,7 +54,7 @@ const finishedSync = async(socket: LX.Sync.Server.Socket) => { const setLocalList = async(socket: LX.Sync.Server.Socket, listData: LX.Dislike.DislikeRules) => { await setLocalDislikeData(listData) const userSpace = getUserSpace(socket.userInfo.name) - return userSpace.listManage.createSnapshot() + return userSpace.dislikeManage.createSnapshot() } const overwriteRemoteListData = async(socket: LX.Sync.Server.Socket, listData: LX.Dislike.DislikeRules, key: string, excludeIds: string[] = []) => { @@ -196,7 +196,8 @@ const handleMergeListDataFromSnapshot = async(socket: LX.Sync.Server.Socket, sna const syncDislike = async(socket: LX.Sync.Server.Socket) => { // socket.data.snapshotFilePath = getSnapshotFilePath(socket.keyInfo) // console.log(socket.keyInfo) - if (!(socket.feature.dislike as LX.Sync.DislikeConfig).skipSnapshot) { + if (!socket.feature.dislike) throw new Error('dislike feature options not available') + if (!socket.feature.dislike.skipSnapshot) { const user = getUserSpace(socket.userInfo.name) const userCurrentDislikeInfoKey = await user.dislikeManage.getDeviceCurrentSnapshotKey(socket.keyInfo.clientId) if (userCurrentDislikeInfoKey) { diff --git a/src/main/modules/sync/server/modules/list/manage.ts b/src/main/modules/sync/server/modules/list/manage.ts index 6b338163..bc9f936a 100644 --- a/src/main/modules/sync/server/modules/list/manage.ts +++ b/src/main/modules/sync/server/modules/list/manage.ts @@ -27,13 +27,14 @@ export class ListManage { } getCurrentListInfoKey = async() => { - const snapshotInfo = await this.snapshotDataManage.getSnapshotInfo() - if (snapshotInfo.latest) { - return snapshotInfo.latest - } - snapshotInfo.latest = toMD5(JSON.stringify(await this.getListData())) - this.snapshotDataManage.saveSnapshotInfo(snapshotInfo) - return snapshotInfo.latest + // const snapshotInfo = await this.snapshotDataManage.getSnapshotInfo() + // if (snapshotInfo.latest) { + // return snapshotInfo.latest + // } + // snapshotInfo.latest = toMD5(JSON.stringify(await this.getListData())) + // this.snapshotDataManage.saveSnapshotInfo(snapshotInfo) + // return snapshotInfo.latest + return this.createSnapshot() } getDeviceCurrentSnapshotKey = async(clientId: string) => { diff --git a/src/main/modules/sync/server/modules/list/sync/sync.ts b/src/main/modules/sync/server/modules/list/sync/sync.ts index 95053fa8..9994dd01 100644 --- a/src/main/modules/sync/server/modules/list/sync/sync.ts +++ b/src/main/modules/sync/server/modules/list/sync/sync.ts @@ -398,7 +398,8 @@ const handleMergeListDataFromSnapshot = async(socket: LX.Sync.Server.Socket, sna const syncList = async(socket: LX.Sync.Server.Socket) => { // socket.data.snapshotFilePath = getSnapshotFilePath(socket.keyInfo) // console.log(socket.keyInfo) - if (!(socket.feature.list as LX.Sync.ListConfig).skipSnapshot) { + if (!socket.feature.list) throw new Error('list feature options not available') + if (!socket.feature.list.skipSnapshot) { const user = getUserSpace(socket.userInfo.name) const userCurrentListInfoKey = await user.listManage.getDeviceCurrentSnapshotKey(socket.keyInfo.clientId) if (userCurrentListInfoKey) { diff --git a/src/main/modules/sync/server/server/server.ts b/src/main/modules/sync/server/server/server.ts index 6a01cd4b..1c113032 100644 --- a/src/main/modules/sync/server/server/server.ts +++ b/src/main/modules/sync/server/server/server.ts @@ -192,8 +192,8 @@ const handleStartServer = async(port = 9527, ip = '0.0.0.0') => await new Promis const name = groupName ?? '' const deviceName = socket.keyInfo?.deviceName ?? '' log.error(`sync call ${deviceName} ${name} ${path.join('.')} error:`, error) - if (groupName == null) return - socket.close(SYNC_CLOSE_CODE.failed) + // if (groupName == null) return + // socket.close(SYNC_CLOSE_CODE.failed) }, }) socket.remote = msg2call.remote diff --git a/src/main/modules/sync/server/user/data.ts b/src/main/modules/sync/server/user/data.ts index 276d4897..0938ff48 100644 --- a/src/main/modules/sync/server/user/data.ts +++ b/src/main/modules/sync/server/user/data.ts @@ -4,6 +4,7 @@ import { randomBytes } from 'node:crypto' import { throttle } from '@common/utils/common' import { filterFileName, toMD5 } from '../utils' import { File } from '@common/constants_sync' +import { exists } from '../../utils' interface ServerInfo { @@ -20,7 +21,6 @@ const saveServerInfoThrottle = throttle(() => { }) }) let serverInfo: ServerInfo -const exists = async(path: string) => fs.promises.stat(path).then(() => true).catch(() => false) export const initServerInfo = async() => { if (serverInfo != null) return const serverInfoFilePath = path.join(global.lxDataPath, File.serverDataPath, File.serverInfoJSON) diff --git a/src/main/modules/sync/utils.ts b/src/main/modules/sync/utils.ts index 139c2344..8bf7bb42 100644 --- a/src/main/modules/sync/utils.ts +++ b/src/main/modules/sync/utils.ts @@ -1,5 +1,6 @@ import { createCipheriv, createDecipheriv, publicEncrypt, privateDecrypt, constants } from 'node:crypto' import os, { networkInterfaces } from 'node:os' +import fs from 'node:fs' import zlib from 'node:zlib' import cp from 'node:child_process' @@ -88,3 +89,6 @@ export const rsaEncrypt = (buffer: Buffer, key: string): string => { export const rsaDecrypt = (buffer: Buffer, key: string): Buffer => { return privateDecrypt({ key, padding: constants.RSA_PKCS1_OAEP_PADDING }, buffer) } + + +export const exists = async(path: string) => fs.promises.stat(path).then(() => true).catch(() => false)