pull/1155/head
lyswhut 2023-01-12 14:08:28 +08:00
parent 1c802ad5f5
commit 1dce2c1f7a
2 changed files with 46 additions and 40 deletions

View File

@ -18,49 +18,54 @@ export const authCode = async(req: http.IncomingMessage, res: http.ServerRespons
let ip = req.socket.remoteAddress let ip = req.socket.remoteAddress
// console.log(req.headers) // console.log(req.headers)
if (typeof req.headers.m == 'string' && ip && (requestIps.get(ip) ?? 0) < 10) { if (typeof req.headers.m == 'string') {
if (req.headers.m) { if (ip && (requestIps.get(ip) ?? 0) < 10) {
label: if (req.headers.m) {
if (req.headers.i) { // key验证 label:
if (typeof req.headers.i != 'string') break label if (req.headers.i) { // key验证
const keyInfo = getClientKeyInfo(req.headers.i) if (typeof req.headers.i != 'string') break label
if (!keyInfo) break label const keyInfo = getClientKeyInfo(req.headers.i)
let text if (!keyInfo) break label
try { let text
text = aesDecrypt(req.headers.m, keyInfo.key) try {
} catch (err) { text = aesDecrypt(req.headers.m, keyInfo.key)
break label } catch (err) {
} break label
// console.log(text) }
if (text.startsWith(SYNC_CODE.authMsg)) { // console.log(text)
code = 200 if (text.startsWith(SYNC_CODE.authMsg)) {
const deviceName = text.replace(SYNC_CODE.authMsg, '') || 'Unknown' code = 200
if (deviceName != keyInfo.deviceName) { const deviceName = text.replace(SYNC_CODE.authMsg, '') || 'Unknown'
keyInfo.deviceName = deviceName if (deviceName != keyInfo.deviceName) {
setClientKeyInfo(keyInfo) keyInfo.deviceName = deviceName
setClientKeyInfo(keyInfo)
}
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')
key = Buffer.from(key).toString('base64')
// console.log(req.headers.m, authCode, key)
let text
try {
text = aesDecrypt(req.headers.m, key)
} catch (err) {
break label
}
// console.log(text)
if (text.startsWith(SYNC_CODE.authMsg)) {
code = 200
const data = text.split('\n')
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)
} }
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')
key = Buffer.from(key).toString('base64')
// console.log(req.headers.m, authCode, key)
let text
try {
text = aesDecrypt(req.headers.m, key)
} catch (err) {
break label
}
// console.log(text)
if (text.startsWith(SYNC_CODE.authMsg)) {
code = 200
const data = text.split('\n')
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)
} }
} }
} else {
code = 403
msg = SYNC_CODE.msgBlockedIp
} }
} }
res.writeHead(code) res.writeHead(code)

View File

@ -3,5 +3,6 @@ export const SYNC_CODE = {
idPrefix: 'OjppZDo6', idPrefix: 'OjppZDo6',
authMsg: 'lx-music auth::', authMsg: 'lx-music auth::',
msgAuthFailed: 'Auth failed', msgAuthFailed: 'Auth failed',
msgBlockedIp: 'Blocked IP',
msgConnect: 'lx-music connect', msgConnect: 'lx-music connect',
} as const } as const