feat: 檢驗用戶的 設備 ID

This commit is contained in:
CrescentLeaf
2025-09-21 12:28:44 +08:00
parent 83719f5f44
commit e5dd3ade51
9 changed files with 67 additions and 38 deletions

View File

@@ -34,10 +34,22 @@ export default class ApiManager {
}
static initEvents() {
const io = this.socketIoServer
io.on('connection', (socket) => {
// TODO: fix ip == undefined
// https://github.com/denoland/deno/blob/7938d5d2a448b876479287de61e9e3b8c6109bc8/ext/node/polyfills/net.ts#L1713
const ip = socket.conn.remoteAddress
const deviceId = socket.handshake.auth.device_id as string
socket.on('disconnect', (_reason) => {
console.log(chalk.yellow('[斷]') + ` ${ip} disconnected`)
})
console.log(chalk.green('[連]') + ` ${ip} connected`)
socket.on("The_White_Silk", (name: string, args: { [key: string]: unknown }, callback_: (ret: ApiCallbackMessage) => void) => {
function callback(ret: ApiCallbackMessage) {
console.log(chalk.blue('[發]') + ` ${socket.request.socket.remoteAddress} <- ${ret.code == 200 ? chalk.green(ret.msg) : chalk.red(ret.msg)} [${ret.code}]${ ret.data ? (' <extras: ' + JSON.stringify(ret.data) + '>') : ''}`)
console.log(chalk.blue('[發]') + ` ${ip} <- ${ret.code == 200 ? chalk.green(ret.msg) : chalk.red(ret.msg)} [${ret.code}]${ret.data ? (' <extras: ' + JSON.stringify(ret.data) + '>') : ''}`)
return callback_(ret)
}
try {
@@ -45,9 +57,11 @@ export default class ApiManager {
msg: "Invalid request.",
code: 400
})
console.log(chalk.red('[收]') + ` ${socket.request.socket.remoteAddress} -> ${chalk.yellow(name)} <args: ${JSON.stringify(args)}>`)
console.log(chalk.red('[收]') + ` ${ip} -> ${chalk.yellow(name)} <args: ${JSON.stringify(args)}>`)
return callback(this.event_listeners[name]?.(args) || {
return callback(this.event_listeners[name]?.(args, {
deviceId
}) || {
code: 501,
msg: "Not implmented",
})
@@ -59,7 +73,7 @@ export default class ApiManager {
code: err instanceof DataWrongError ? 400 : 500,
msg: "錯誤: " + err.message
})
} catch(_e) {}
} catch (_e) { }
}
})
})