Skip to content

Commit

Permalink
修复潜在同步问题
Browse files Browse the repository at this point in the history
  • Loading branch information
lyswhut committed Sep 6, 2023
1 parent fcffcb5 commit 3685cb8
Show file tree
Hide file tree
Showing 12 changed files with 51 additions and 32 deletions.
12 changes: 6 additions & 6 deletions src/main/modules/sync/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
// })
},
})

Expand Down
2 changes: 1 addition & 1 deletion src/main/modules/sync/client/data.ts
Original file line number Diff line number Diff line change
@@ -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<string, LX.Sync.ClientKeyInfo>
Expand All @@ -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)
Expand Down
8 changes: 7 additions & 1 deletion src/main/modules/sync/client/modules/dislike/localEvent.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { SYNC_CLOSE_CODE } from '@common/constants_sync'
import { registerDislikeActionEvent } from '@main/modules/sync/dislikeEvent'

let unregisterLocalListAction: (() => void) | null
Expand All @@ -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)
})
})
}

Expand Down
8 changes: 7 additions & 1 deletion src/main/modules/sync/client/modules/list/localEvent.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { SYNC_CLOSE_CODE } from '@common/constants_sync'
import { registerListActionEvent } from '@main/modules/sync/listEvent'

let unregisterLocalListAction: (() => void) | null
Expand All @@ -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)
})
})
}

Expand Down
3 changes: 1 addition & 2 deletions src/main/modules/sync/migrate.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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')
Expand Down
15 changes: 8 additions & 7 deletions src/main/modules/sync/server/modules/dislike/manage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
7 changes: 4 additions & 3 deletions src/main/modules/sync/server/modules/dislike/sync/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const getRemoteDataMD5 = async(socket: LX.Sync.Server.Socket): Promise<string> =
}

// const getLocalDislikeData async(socket: LX.Sync.Server.Socket): Promise<LX.Sync.Dislike.ListData> => {
// return getUserSpace(socket.userInfo.name).listManage.getListData()
// return getUserSpace(socket.userInfo.name).dislikeManage.getListData()
// }
const getSyncMode = async(socket: LX.Sync.Server.Socket): Promise<LX.Sync.Dislike.SyncMode> => new Promise((resolve, reject) => {
const handleDisconnect = (err: Error) => {
Expand Down Expand Up @@ -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[] = []) => {
Expand Down Expand Up @@ -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) {
Expand Down
15 changes: 8 additions & 7 deletions src/main/modules/sync/server/modules/list/manage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
3 changes: 2 additions & 1 deletion src/main/modules/sync/server/modules/list/sync/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/modules/sync/server/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/main/modules/sync/server/user/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions src/main/modules/sync/utils.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand Down Expand Up @@ -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)

0 comments on commit 3685cb8

Please sign in to comment.