From 9ac26be65dceea63d167af0bcaf664673259432a Mon Sep 17 00:00:00 2001 From: lyswhut Date: Wed, 30 Mar 2022 16:22:13 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=B0=E5=BD=95=E8=BF=9E=E6=8E=A5=E6=97=B6?= =?UTF-8?q?=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/modules/sync/server/server.js | 8 ++++++-- src/main/modules/sync/server/syncList.js | 12 +++++++----- src/main/modules/sync/server/utils.js | 6 ++++++ 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/main/modules/sync/server/server.js b/src/main/modules/sync/server/server.js index 5f8a484d..167f671d 100644 --- a/src/main/modules/sync/server/server.js +++ b/src/main/modules/sync/server/server.js @@ -3,8 +3,8 @@ const sio = require('socket.io') const { createHttpTerminator } = require('http-terminator') const modules = require('../modules') const { authCode, authConnect } = require('./auth') -const { getAddress, getServerId, generateCode, getClientKeyInfo } = require('./utils') -const syncList = require('./syncList') +const { getAddress, getServerId, generateCode, getClientKeyInfo, setClientKeyInfo } = require('./utils') +const { syncList, removeSnapshot } = require('./syncList') const { log } = require('@common/utils') @@ -84,6 +84,8 @@ const handleStartServer = (port = 9527) => new Promise((resolve, reject) => { global.lx_event.sync.status(status) }) const keyInfo = getClientKeyInfo(socket.handshake.query.i) + keyInfo.connectionTime = Date.now() + setClientKeyInfo(keyInfo) // socket.lx_keyInfo = keyInfo socket.data.keyInfo = keyInfo try { @@ -172,3 +174,5 @@ exports.generateCode = async() => { global.lx_event.sync.status(status) return status.code } + +exports.removeSnapshot = removeSnapshot diff --git a/src/main/modules/sync/server/syncList.js b/src/main/modules/sync/server/syncList.js index 3f5814c9..4a300e4a 100644 --- a/src/main/modules/sync/server/syncList.js +++ b/src/main/modules/sync/server/syncList.js @@ -1,8 +1,6 @@ -const path = require('path') const fs = require('fs') const fsPromises = fs.promises -const { app } = require('electron') -const { encryptMsg, decryptMsg } = require('./utils') +const { encryptMsg, decryptMsg, getSnapshotFilePath } = require('./utils') const SYNC_EVENT_NAMES = require('../event/name') const { common: COMMON_EVENT_NAME } = require('@main/events/_name') const { throttle } = require('@common/utils') @@ -403,7 +401,7 @@ const registerUpdateSnapshotTask = (socket, snapshot) => { } const syncList = async socket => { - socket.data.snapshotFilePath = path.join(app.getPath('userData'), `snapshot-${Buffer.from(socket.data.keyInfo.clientId).toString('hex').substring(0, 10)}.json`) + socket.data.snapshotFilePath = getSnapshotFilePath(socket.data.keyInfo) let fileData let isSyncRequired = false try { @@ -423,7 +421,7 @@ const checkSyncQueue = async() => { await wait() return checkSyncQueue() } -module.exports = async(_io, socket) => { +exports.syncList = async(_io, socket) => { io = _io await checkSyncQueue() syncingId = socket.data.keyInfo.clientId @@ -434,3 +432,7 @@ module.exports = async(_io, socket) => { syncingId = null }) } +exports.removeSnapshot = keyInfo => { + const filePath = getSnapshotFilePath(keyInfo) + return fsPromises.unlink(filePath) +} diff --git a/src/main/modules/sync/server/utils.js b/src/main/modules/sync/server/utils.js index e880fd83..12c8d239 100644 --- a/src/main/modules/sync/server/utils.js +++ b/src/main/modules/sync/server/utils.js @@ -1,5 +1,7 @@ +const { app } = require('electron') const { networkInterfaces } = require('os') const { randomBytes, createCipheriv, createDecipheriv } = require('crypto') +const path = require('path') const getStore = require('@common/store') const STORE_NAME = 'sync' @@ -91,3 +93,7 @@ exports.decryptMsg = (keyInfo, enMsg) => { // } // return msg } + +exports.getSnapshotFilePath = keyInfo => { + return path.join(app.getPath('userData'), `snapshot-${Buffer.from(keyInfo.clientId).toString('hex').substring(0, 10)}.json`) +}