添加主题字体颜色设置

This commit is contained in:
lyswhut
2022-12-30 12:12:09 +08:00
parent a7c2857808
commit 6ff1d683a5
33 changed files with 628 additions and 189 deletions
-23
View File
@@ -1,23 +0,0 @@
const { EventEmitter } = require('events')
const SYNC_EVENT_NAME = require('./name')
class Sync extends EventEmitter {
status(status) {
this.emit(SYNC_EVENT_NAME.status, status)
}
sync_list(data) {
this.emit(SYNC_EVENT_NAME.sync_list, data)
}
sync_handle_list(data) {
this.emit(SYNC_EVENT_NAME.sync_handle_list, data)
}
action_list(data) {
this.emit(SYNC_EVENT_NAME.sync_action_list, data)
}
}
module.exports = Sync
-6
View File
@@ -1,6 +0,0 @@
module.exports = {
sync_action_list: 'sync_action_list',
sync_list: 'sync_list',
sync_handle_list: 'sync_handle_list',
status: 'status',
}
+4 -1
View File
@@ -9,7 +9,7 @@ let removeListener: (() => void) | null
type listAction = 'list:action'
const handleListAction = ({ action, data }: LX.Sync.ActionList) => {
// console.log(action, data)
// console.log('handleListAction', action)
switch (action) {
case 'list_data_overwrite':
@@ -144,11 +144,14 @@ const broadcast = async(action: listAction, data: any, excludeIds: string[] = []
}
export const sendListAction = async(action: LX.Sync.ActionList) => {
console.log('sendListAction', action.action)
// io.sockets
return await broadcast('list:action', JSON.stringify(action))
}
export const registerListHandler = (_io: Server, socket: LX.Sync.Socket) => {
unregisterListHandler()
io = _io
socket.on('list:action', msg => {
// console.log(msg)
+23 -23
View File
@@ -1,4 +1,5 @@
import http from 'http'
import { SYNC_CODE } from './config'
import {
aesEncrypt,
aesDecrypt,
@@ -6,19 +7,17 @@ import {
getClientKeyInfo,
setClientKeyInfo,
} from './utils'
const authMsg = 'lx-music auth::'
const helloMsg = 'Hello~::^-^::~v2~'
import querystring from 'node:querystring'
const requestIps = new Map<string, number>()
export const authCode = async(req: http.IncomingMessage, res: http.ServerResponse, authCode: string) => {
let code = 401
let msg = 'Forbidden'
let msg: string = SYNC_CODE.msgAuthFailed
let ip = req.socket.remoteAddress
// console.log(req.headers)
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) {
label:
if (req.headers.i) {
@@ -27,36 +26,36 @@ export const authCode = async(req: http.IncomingMessage, res: http.ServerRespons
if (!keyInfo) break label
let text
try {
text = aesDecrypt(req.headers.m, keyInfo.key, keyInfo.iv)
text = aesDecrypt(req.headers.m, keyInfo.key)
} catch (err) {
break label
}
console.log(text)
if (text.startsWith(authMsg)) {
// console.log(text)
if (text.startsWith(SYNC_CODE.authMsg)) {
code = 200
const deviceName = text.replace(authMsg, '') || 'Unknown'
const deviceName = text.replace(SYNC_CODE.authMsg, '') || 'Unknown'
if (deviceName != keyInfo.deviceName) {
keyInfo.deviceName = deviceName
setClientKeyInfo(keyInfo)
}
msg = aesEncrypt(helloMsg, keyInfo.key, keyInfo.iv)
msg = aesEncrypt(SYNC_CODE.helloMsg, keyInfo.key)
}
} else {
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')
// console.log(authCode, key, iv)
// console.log(req.headers.m, authCode, key)
let text
try {
text = aesDecrypt(req.headers.m, key, iv)
text = aesDecrypt(req.headers.m, key)
} catch (err) {
break label
}
console.log(text)
if (text.startsWith(authMsg)) {
// console.log(text)
if (text.startsWith(SYNC_CODE.authMsg)) {
code = 200
const deviceName = text.replace(authMsg, '') || 'Unknown'
msg = aesEncrypt(JSON.stringify(createClientKeyInfo(deviceName)), key, iv)
const deviceName = text.replace(SYNC_CODE.authMsg, '') || 'Unknown'
msg = aesEncrypt(JSON.stringify(createClientKeyInfo(deviceName)), key)
}
}
}
@@ -72,20 +71,21 @@ export const authCode = async(req: http.IncomingMessage, res: http.ServerRespons
}
export const authConnect = async(req: http.IncomingMessage) => {
const url = new URL(req.url as string)
const i = url.searchParams.get('i')
const t = url.searchParams.get('t')
const query = querystring.parse((req.url as string).split('?')[1])
const i = query.i
const t = query.t
label:
if (i && t) {
if (typeof i == 'string' && typeof t == 'string') {
const keyInfo = getClientKeyInfo(i)
if (!keyInfo) break label
let text
try {
text = aesDecrypt(t, keyInfo.key, keyInfo.iv)
text = aesDecrypt(t, keyInfo.key)
} catch (err) {
break label
}
if (text == 'lx-music connect') return
// console.log(text)
if (text == SYNC_CODE.msgConnect) return
}
throw new Error('failed')
}
+7
View File
@@ -0,0 +1,7 @@
export const SYNC_CODE = {
helloMsg: 'Hello~::^-^::~v2~',
idPrefix: 'OjppZDo6',
authMsg: 'lx-music auth::',
msgAuthFailed: 'Auth failed',
msgConnect: 'lx-music connect',
} as const
+12 -2
View File
@@ -7,6 +7,7 @@ import { getAddress, getServerId, generateCode as handleGenerateCode, getClientK
import { syncList, removeSnapshot } from './syncList'
import { log } from '@common/utils'
import { sendStatus } from '@main/modules/winMain'
import { SYNC_CODE } from './config'
let status: LX.Sync.Status = {
@@ -44,6 +45,14 @@ const handleConnection = (io: Server, socket: LX.Sync.Socket) => {
}
}
const handleUnconnection = () => {
console.log('unconnection')
// console.log(socket.handshake.query)
for (const module of Object.values(modules)) {
module.unregisterListHandler()
}
}
const authConnection = (req: http.IncomingMessage, callback: (err: string | null | undefined, success: boolean) => void) => {
// console.log(req.headers)
// // console.log(req.auth)
@@ -66,11 +75,11 @@ const handleStartServer = async(port = 9527) => await new Promise((resolve, reje
switch (req.url) {
case '/hello':
code = 200
msg = 'Hello~::^-^::~v2'
msg = SYNC_CODE.helloMsg
break
case '/id':
code = 200
msg = 'OjppZDo6' + getServerId()
msg = SYNC_CODE.idPrefix + getServerId()
break
case '/ah':
void authCode(req, res, status.code)
@@ -103,6 +112,7 @@ const handleStartServer = async(port = 9527) => await new Promise((resolve, reje
console.log('disconnect', reason)
status.devices.splice(status.devices.findIndex(k => k.clientId == keyInfo?.clientId), 1)
sendStatus(status)
if (!status.devices.length) handleUnconnection()
})
if (typeof socket.handshake.query.i != 'string') return socket.disconnect(true)
const keyInfo = getClientKeyInfo(socket.handshake.query.i)
+1 -1
View File
@@ -267,7 +267,7 @@ const handleSyncList = async(socket: LX.Sync.Socket): Promise<LX.Sync.ListData |
if (remoteListData.defaultList.length || remoteListData.loveList.length || remoteListData.userList.length) {
const mergedList = await handleMergeListData(socket)
console.log('handleMergeListData', 'mergedList')
console.log(mergedList)
// console.log(mergedList)
if (!mergedList) return null
listData.defaultList = mergedList.defaultList
listData.loveList = mergedList.loveList
+5 -7
View File
@@ -5,9 +5,7 @@ import getStore from '@main/utils/store'
const STORE_NAME = 'sync'
interface KeyInfos {
[key: string]: LX.Sync.KeyInfo
}
type KeyInfos = Record<string, LX.Sync.KeyInfo>
export const getAddress = (): string[] => {
const nets = networkInterfaces()
@@ -71,13 +69,13 @@ export const generateCode = (): string => {
return Math.random().toString().substring(2, 8)
}
export const aesEncrypt = (buffer: string | Buffer, key: string, iv: string): string => {
const cipher = createCipheriv('aes-128-cbc', Buffer.from(key, 'base64'), Buffer.from(iv, 'base64'))
export const aesEncrypt = (buffer: string | Buffer, key: string): string => {
const cipher = createCipheriv('aes-128-ecb', Buffer.from(key, 'base64'), '')
return Buffer.concat([cipher.update(buffer), cipher.final()]).toString('base64')
}
export const aesDecrypt = (text: string, key: string, iv: string): string => {
const decipher = createDecipheriv('aes-128-cbc', Buffer.from(key, 'base64'), Buffer.from(iv, 'base64'))
export const aesDecrypt = (text: string, key: string): string => {
const decipher = createDecipheriv('aes-128-ecb', Buffer.from(key, 'base64'), '')
return Buffer.concat([decipher.update(Buffer.from(text, 'base64')), decipher.final()]).toString()
}