完善同步
parent
6ff1d683a5
commit
c16de232ad
|
@ -53,7 +53,6 @@ declare namespace LX {
|
||||||
interface KeyInfo {
|
interface KeyInfo {
|
||||||
clientId: string
|
clientId: string
|
||||||
key: string
|
key: string
|
||||||
iv: string
|
|
||||||
deviceName: string
|
deviceName: string
|
||||||
connectionTime?: number
|
connectionTime?: number
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,7 @@ import {
|
||||||
createClientKeyInfo,
|
createClientKeyInfo,
|
||||||
getClientKeyInfo,
|
getClientKeyInfo,
|
||||||
setClientKeyInfo,
|
setClientKeyInfo,
|
||||||
|
rsaEncrypt,
|
||||||
} from './utils'
|
} from './utils'
|
||||||
import querystring from 'node:querystring'
|
import querystring from 'node:querystring'
|
||||||
|
|
||||||
|
@ -20,7 +21,7 @@ export const authCode = async(req: http.IncomingMessage, res: http.ServerRespons
|
||||||
if (typeof req.headers.m == 'string' && ip && (requestIps.get(ip) ?? 0) < 10) {
|
if (typeof req.headers.m == 'string' && ip && (requestIps.get(ip) ?? 0) < 10) {
|
||||||
if (req.headers.m) {
|
if (req.headers.m) {
|
||||||
label:
|
label:
|
||||||
if (req.headers.i) {
|
if (req.headers.i) { // key验证
|
||||||
if (typeof req.headers.i != 'string') break label
|
if (typeof req.headers.i != 'string') break label
|
||||||
const keyInfo = getClientKeyInfo(req.headers.i)
|
const keyInfo = getClientKeyInfo(req.headers.i)
|
||||||
if (!keyInfo) break label
|
if (!keyInfo) break label
|
||||||
|
@ -40,7 +41,7 @@ export const authCode = async(req: http.IncomingMessage, res: http.ServerRespons
|
||||||
}
|
}
|
||||||
msg = aesEncrypt(SYNC_CODE.helloMsg, keyInfo.key)
|
msg = aesEncrypt(SYNC_CODE.helloMsg, keyInfo.key)
|
||||||
}
|
}
|
||||||
} else {
|
} else { // 连接码验证
|
||||||
let key = ''.padStart(16, Buffer.from(authCode).toString('hex'))
|
let key = ''.padStart(16, Buffer.from(authCode).toString('hex'))
|
||||||
// const iv = Buffer.from(key.split('').reverse().join('')).toString('base64')
|
// const iv = Buffer.from(key.split('').reverse().join('')).toString('base64')
|
||||||
key = Buffer.from(key).toString('base64')
|
key = Buffer.from(key).toString('base64')
|
||||||
|
@ -54,8 +55,10 @@ export const authCode = async(req: http.IncomingMessage, res: http.ServerRespons
|
||||||
// console.log(text)
|
// console.log(text)
|
||||||
if (text.startsWith(SYNC_CODE.authMsg)) {
|
if (text.startsWith(SYNC_CODE.authMsg)) {
|
||||||
code = 200
|
code = 200
|
||||||
const deviceName = text.replace(SYNC_CODE.authMsg, '') || 'Unknown'
|
const data = text.split('\n')
|
||||||
msg = aesEncrypt(JSON.stringify(createClientKeyInfo(deviceName)), key)
|
const publicKey = `-----BEGIN PUBLIC KEY-----\n${data[1]}\n-----END PUBLIC KEY-----`
|
||||||
|
const deviceName = data[2] || 'Unknown'
|
||||||
|
msg = rsaEncrypt(Buffer.from(JSON.stringify(createClientKeyInfo(deviceName))), publicKey)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import { networkInterfaces } from 'os'
|
import { networkInterfaces } from 'os'
|
||||||
import { randomBytes, createCipheriv, createDecipheriv } from 'crypto'
|
import { randomBytes, createCipheriv, createDecipheriv, publicEncrypt, privateDecrypt, constants } from 'crypto'
|
||||||
import { join } from 'path'
|
import { join } from 'path'
|
||||||
import getStore from '@main/utils/store'
|
import getStore from '@main/utils/store'
|
||||||
|
|
||||||
|
@ -41,7 +41,6 @@ export const createClientKeyInfo = (deviceName: string): LX.Sync.KeyInfo => {
|
||||||
const keyInfo: LX.Sync.KeyInfo = {
|
const keyInfo: LX.Sync.KeyInfo = {
|
||||||
clientId: randomBytes(4 * 4).toString('base64'),
|
clientId: randomBytes(4 * 4).toString('base64'),
|
||||||
key: randomBytes(16).toString('base64'),
|
key: randomBytes(16).toString('base64'),
|
||||||
iv: randomBytes(16).toString('base64'),
|
|
||||||
deviceName,
|
deviceName,
|
||||||
}
|
}
|
||||||
const store = getStore(STORE_NAME)
|
const store = getStore(STORE_NAME)
|
||||||
|
@ -79,6 +78,13 @@ export const aesDecrypt = (text: string, key: string): string => {
|
||||||
return Buffer.concat([decipher.update(Buffer.from(text, 'base64')), decipher.final()]).toString()
|
return Buffer.concat([decipher.update(Buffer.from(text, 'base64')), decipher.final()]).toString()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const rsaEncrypt = (buffer: Buffer, key: string): string => {
|
||||||
|
return publicEncrypt({ key, padding: constants.RSA_PKCS1_OAEP_PADDING }, buffer).toString('base64')
|
||||||
|
}
|
||||||
|
export const rsaDecrypt = (buffer: Buffer, key: string): Buffer => {
|
||||||
|
return privateDecrypt({ key, padding: constants.RSA_PKCS1_OAEP_PADDING }, buffer)
|
||||||
|
}
|
||||||
|
|
||||||
export const encryptMsg = (keyInfo: LX.Sync.KeyInfo, msg: string): string => {
|
export const encryptMsg = (keyInfo: LX.Sync.KeyInfo, msg: string): string => {
|
||||||
return msg
|
return msg
|
||||||
// if (!keyInfo) return ''
|
// if (!keyInfo) return ''
|
||||||
|
|
Loading…
Reference in New Issue