diff --git a/conf/cfg.json b/conf/cfg.json index b1fcbf2..e503517 100644 --- a/conf/cfg.json +++ b/conf/cfg.json @@ -37,11 +37,7 @@ "router": { "filter": { "whiteDir": [ - "socket", - "tables", - "worker", - "oss", - "openai" + ], "whiteFile": [ "router.js" diff --git a/local.js b/local.js index 68ebe1d..01a2463 100644 --- a/local.js +++ b/local.js @@ -1,20 +1,47 @@ -const express = require("express"); //express -const conf = require("./conf/cfg"); //conf -const fileApiRouters = require("./src/router")(conf); //file routers +const express = require("express"); +const conf = require("./conf/cfg.json"); +const fileApiRouters = require("./src/controller/router")(); let resRouter = conf.router.res; +const db = require("./src/tables/db"); //db let app = express(); -console.log("resource including...") +console.log("api init start ...") //res -for(let key in resRouter) app.use(key,express.static(resRouter[key])); +for (let key in resRouter) { + app.use(key, express.static(resRouter[key])); +} //file api -for(let key in fileApiRouters) app.use(key,fileApiRouters[key]) +for (let key in fileApiRouters) { + app.use(key, fileApiRouters[key]) +} +if (!conf.db.open) {// 没开db -app.listen(conf.node.port); -console.log("express init...") + app.use(async function (req, res, next) { + req.ctx = {}; + req.ctx.tables = {}; + req.ctx.dbClient = {}; + await next(); + }) + app.listen(conf.node.port); + console.log("express init done ...") + console.log("web server runing on ", conf.node.port, " successful"); +} else { // 开了db + (async () => { + let { tables, dbClient } = await db.excute(conf) + console.log("db init done ...") + app.use(async function (req, res, next) { + req.ctx = {}; + req.ctx.tables = tables; + req.ctx.dbClient = dbClient; + await next(); + }) -console.log("server runing on ",conf.node.port," successful"); \ No newline at end of file + app.listen(conf.node.port); + console.log("express init done ...") + console.log("web server runing on ", conf.node.port, " successful"); + })(); +} \ No newline at end of file diff --git a/localsocket.js b/localsocket.js index b60c9cd..b2949a3 100644 --- a/localsocket.js +++ b/localsocket.js @@ -1,45 +1,25 @@ const http = require('http'); // http const socketIO = require('socket.io'); //socket -const app = require("express")(); //express const db = require("./src/tables/db"); //db const conf = require("./conf/cfg.json"); //conf -const utils = require("./utils/utils"); //utils const socket = require("./src/socket/index") //socket handler -let tables = {}; -let sql = {}; +// Socket连接监听 +let io = socketIO.listen(http.createServer().listen(conf.ws.port)); -if(conf.db.open){ - // db init - let dbData = db.excute(conf); - tables = dbData.tables; - sql = dbData.sql; - - app.use(async function (req,res,next) { - req.ctx = {}; - req.ctx.tables = tables; - req.ctx.sql = sql; - req.ctx.Sql = Sql; - await next(); - }) - console.log("db init...") -} +if (!conf.db.open) {// 没开db + + console.log("db not open ...") + socket.excute({}, {}, io); + console.log("socket init done ...") + console.log("socket server listen on ", conf.ws.port, " successful"); +} else { // 开了db - -//log flow init --日志流水初始 -app.use(async function (req,res,next) { - res.tl = {}; - res.tl.flowId = utils.genFlow(); - await next(); -}) -console.log("flow init...") - -//Socket连接监听 -let io = socketIO.listen( - http.createServer().listen(conf.ws.port) -); -conf.ws.io = io; -socket.excute(tables, sql, conf); -console.log("socket init...") - -console.log("socket listen on ",conf.ws.port," successful"); \ No newline at end of file + (async () => { + let { tables, dbClient } = await db.excute(conf) + console.log("db init done ...") + socket.excute(tables, dbClient, io); + console.log("socket init done ...") + console.log("socket server listen on ", conf.ws.port, " successful"); + })(); +} \ No newline at end of file diff --git a/res/js/index.js b/res/js/index.js index c4c2b12..3a423f9 100644 --- a/res/js/index.js +++ b/res/js/index.js @@ -1662,7 +1662,12 @@ axios.get(window.prefix + "/api/comm/initData", {}).then((initData) => { return; } this.setNickName(); - this.socket.emit('createAndJoin', { room: this.roomId, nickName : this.nickName }); + this.socket.emit('createAndJoin', { + room: this.roomId, + type : 'password', + password : '', + nickName : this.nickName + }); this.isJoined = true; this.addPopup({ title : "文件房间", @@ -1694,7 +1699,11 @@ axios.get(window.prefix + "/api/comm/initData", {}).then((initData) => { return; } this.setNickName(); - this.socket.emit('createAndJoin', { room: this.roomId, type: type, nickName : this.nickName }); + this.socket.emit('createAndJoin', { + room: this.roomId, + type: type, + nickName : this.nickName + }); this.isJoined = true; this.addPopup({ title : "流媒体房间", @@ -1735,7 +1744,12 @@ axios.get(window.prefix + "/api/comm/initData", {}).then((initData) => { return; } this.setNickName(); - this.socket.emit('createAndJoin', { room: this.roomId, password: password, nickName : this.nickName }); + this.socket.emit('createAndJoin', { + room: this.roomId, + type : 'password', + password: password, + nickName : this.nickName + }); this.isJoined = true; this.addPopup({ title : "密码房间", diff --git a/server.js b/server.js index 0541dbd..a8948ea 100644 --- a/server.js +++ b/server.js @@ -1,12 +1,13 @@ -const express = require("express"); //express -const conf = require("./conf/cfg"); //conf -const fileApiRouters = require("./src/router")(conf); //file routers +const express = require("express"); +const fs = require('fs'); +const https = require('https'); +const conf = require("./conf/cfg.json"); +const fileApiRouters = require("./src/controller/router")(); let resRouter = conf.router.res; -const fs = require('fs'); // fs -const https = require('https'); // http +const db = require("./src/tables/db"); let app = express(); -console.log("resource including...") +console.log("api init start ...") //res for(let key in resRouter) app.use(key,express.static(resRouter[key])); @@ -14,12 +15,43 @@ for(let key in resRouter) app.use(key,express.static(resRouter[key])); //file api for(let key in fileApiRouters) app.use(key,fileApiRouters[key]) -let options = { - key: fs.readFileSync('./conf/keys/server.key'), - cert: fs.readFileSync('./conf/keys/server.crt') +if (!conf.db.open) {// 没开db + + app.use(async function (req, res, next) { + req.ctx = {}; + req.ctx.tables = {}; + req.ctx.dbClient = {}; + await next(); + }) + let options = { + key: fs.readFileSync('./conf/keys/server.key'), + cert: fs.readFileSync('./conf/keys/server.crt') + } + https.createServer(options,app).listen(conf.node.port); + console.log("express init done ...") + console.log("web server runing on ", conf.node.port, " successful"); + +} else {// 开了db + + (async () => { + let { tables, dbClient } = await db.excute(conf) + console.log("db init done ...") + app.use(async function (req, res, next) { + req.ctx = {}; + req.ctx.tables = tables; + req.ctx.dbClient = dbClient; + await next(); + }) + + let options = { + key: fs.readFileSync('./conf/keys/server.key'), + cert: fs.readFileSync('./conf/keys/server.crt') + } + https.createServer(options,app).listen(conf.node.port); + console.log("express init done ...") + console.log("web server runing on ", conf.node.port, " successful"); + })(); + } -https.createServer(options,app).listen(conf.node.port); -console.log("express init...") - -console.log("server runing on ",conf.node.port," successful"); \ No newline at end of file +console.log("web server runing on ",conf.node.port," successful"); \ No newline at end of file diff --git a/serversocket.js b/serversocket.js index 89d4e15..e839794 100644 --- a/serversocket.js +++ b/serversocket.js @@ -1,50 +1,33 @@ -const https = require('https'); // http -const socketIO = require('socket.io'); //socket -const app = require("express")(); //express -const fs = require('fs'); // fs -const db = require("./src/tables/db"); //db -const conf = require("./conf/cfg.json"); //conf -const utils = require("./utils/utils"); //utils -const socket = require("./src/socket/index") //socket handler - -let tables = {}; -let sql = {}; -if(conf.db.open){ - // db init - let dbData = db.excute(conf); - tables = dbData.tables; - sql = dbData.sql; - app.use(async function (req,res,next) { - req.ctx = {}; - req.ctx.tables = tables; - req.ctx.sql = sql; - req.ctx.Sql = Sql; - await next(); - }) - console.log("db init...") -} - - -//log flow init --日志流水初始 -app.use(async function (req,res,next) { - res.tl = {}; - res.tl.flowId = utils.genFlow(); - await next(); -}) -console.log("flow init...") - +const https = require('https'); +const socketIO = require('socket.io'); +const fs = require('fs'); +const db = require("./src/tables/db"); +const conf = require("./conf/cfg.json"); +const socket = require("./src/socket/index") //Socket连接监听 let options = { - key: fs.readFileSync('./conf/keys/server.key'), - cert: fs.readFileSync('./conf/keys/server.crt') + key: fs.readFileSync('./conf/keys/server.key'), + cert: fs.readFileSync('./conf/keys/server.crt') } let io = socketIO.listen( - https.createServer(options).listen(conf.ws.ssl_port) + https.createServer(options).listen(conf.ws.ssl_port) ); -conf.ws.io = io; -socket.excute(tables, sql, conf); -console.log("socket init...") +if (!conf.db.open) {// 没开db -console.log("socket listen on ",conf.ws.ssl_port," successful"); \ No newline at end of file + console.log("db not open ...") + socket.excute({}, {}, io); + console.log("socket init done ...") + console.log("socket server listen on ", conf.ws.port, " successful"); + +} else {// 开了db + + (async () => { + let { tables, dbClient } = await db.excute(conf) + console.log("db init done ...") + socket.excute(tables, dbClient, io); + console.log("socket init done ...") + console.log("socket server listen on ", conf.ws.port, " successful"); + })(); +} \ No newline at end of file diff --git a/src/bussiness/dog/dog.js b/src/bussiness/dog/dog.js new file mode 100644 index 0000000..26fa761 --- /dev/null +++ b/src/bussiness/dog/dog.js @@ -0,0 +1,67 @@ +const dog = require("./../../dao/dog/dog"); + +/** + * 操作记录 + * @param {*} data + */ +async function dogData(data) { + let req = { + ctx: { + tables: data.tables + }, + params: data + }; + let res = 0; + + try { + res = await dog.addDogData(req, null); + } catch (e) { + console.log(e) + } + + return res && res.dataValues ? res.dataValues.id : 0 +} + + + +/** + * 获取最近10条公共聊天室数据 + * @param {*} data + */ +async function getDogChating10Info(data) { + let req = { + ctx: { + tables: data.tables, + dbClient: data.dbClient + } + }; + + return await dog.getDogChating10Info(req, null); +} + +/** + * 获取操作数据信息 + * @param {*} data + * @returns + */ +async function getDogManageInfo(data){ + let req = { + ctx: { + tables: data.tables, + dbClient: data.dbClient, + sockets: data.sockets + }, + params: { + limit: 10, + day: data.day, + } + } + return await dog.getDogManageInfo(req, null); +} + + +module.exports = { + dogData, + getDogChating10Info, + getDogManageInfo +} \ No newline at end of file diff --git a/src/bussiness/manage/dataPage.js b/src/bussiness/manage/dataPage.js new file mode 100644 index 0000000..cd37a7d --- /dev/null +++ b/src/bussiness/manage/dataPage.js @@ -0,0 +1,225 @@ +const bussinessDog = require("./../../bussiness/dog/dog") +const cfg = require("../../../conf/cfg.json"); +const dbOpen = cfg.db.open + +/** + * 获取数据传输页面 + * @param {*} data + * @returns + */ +async function getDataPageHtml(data) { + if (!dbOpen) { + return 'db配置未开启'; + } + let resData = await bussinessDog.getDogManageInfo({ + tables: data.tables, + dbClient: data.dbClient, + sockets: data.sockets, + day: data.day, + }) + return ` + + +
+ 当前查询时间: + +
+
+
+
+ {{chooseDay}}文件传输 +
+
+

{{transferFileToday}}次

+

总计文件传输 + {{transferFileAll}}次 + + +

+
+
+
+
+
+
+ {{chooseDay}}文件传输大小 + +
+
+

{{transferFileSizeTodady}}M

+

+ 总计文件传输大小 + 暂不统计 + +

+
+
+
+
+
+
+ {{chooseDay}}文本传输 + +
+
+ +

{{transferTxtTodady}}次

+

+ 总计文本传输 + {{transferTxtAll}}次 + +

+
+
+
+
+
+
+ {{chooseDay}}公共频道发言 + +
+
+ +

{{transferCommTxtToday}}次

+

+ 总计公共频道发言 + {{transferCommTxtAll}}次 + +

+
+
+
+ +
+
+
+
{{chooseDay}}文件传输列表
+ + + + + + + + + + +
房间频道 文件名称 文件大小 发送时间
{{file.room}}
{{file.name}}
{{file.size}}M{{file.createTime}}
+
+
+
+ +
+
+
+
{{chooseDay}}文本传输列表
+ + + + + + + + + + +
房间频道 文本内容 文本长度 发送时间
{{txt.room}}
{{txt.content}}
{{txt.size}}字符{{txt.createTime}}
+
+
+
+ +
+
+
+
{{chooseDay}}公共频道发言
+ + + + + + + + + + +
房间频道 文本内容 文本长度 发送时间
{{txt.room}}
{{txt.content}}
{{txt.size}}字符{{txt.createTime}}
+
+
+
+ +
+
+ + + ` +} + + +module.exports = { + getDataPageHtml, +} diff --git a/src/bussiness/manage/roomPage.js b/src/bussiness/manage/roomPage.js new file mode 100644 index 0000000..fbca527 --- /dev/null +++ b/src/bussiness/manage/roomPage.js @@ -0,0 +1,194 @@ +const bussinessRoom = require("./../../bussiness/room/room") +const cfg = require("../../../conf/cfg.json"); +const dbOpen = cfg.db.open + +/** + * 获取房间页面 + * @param {*} data + * @returns + */ +async function getRoomPageHtml(data) { + if (!dbOpen) { + return 'db配置未开启'; + } + let resData = await bussinessRoom.getRoomHistoryInfo({ + tables: data.tables, + dbClient: data.dbClient, + sockets: data.sockets, + day: data.day, + }) + + return ` + + +
+ 当前查询时间: + +
+
+
+
+ {{chooseDay}}房间频道创建/加入 +
+
+

{{createRoomToday}}个

+

总计房间频道创建/加入 + {{createRoomAll}}个 + + +

+
+
+
+
+
+
+ {{chooseDay}}加入房间人数 + +
+
+

{{joinRoomTodady}}人

+

+ 总计加入房间人数 + {{joinRoomAll}}人 + +

+
+
+
+
+
+
+
在线房间列表
+ + + + + + + + + + +
房间频道 在线状态 房间人数 创建时间
{{r.room}} + + + + {{r.status}} + {{r.userNumber}}{{r.createTime}}
+
+
+
+
+
+
+
{{chooseDay}}房间统计
+ + + + + + + + + +
房间频道 聚合创建次数 最近创建时间
{{r.room}}{{r.count}}{{r.createTime}}
+
+
+
+ +
+
+
+
{{chooseDay}}访问设备/IP
+ + + + + + + + + + +
房间频道 IP地址 设备信息 时间
{{userAgentIp.room}}{{userAgentIp.Ip}}{{userAgentIp.userAgent}}{{userAgentIp.createTime}}
+
+
+
+ +
+
+ + + ` +} + + +module.exports = { + getRoomPageHtml +} + diff --git a/src/bussiness/manage/settingPage.js b/src/bussiness/manage/settingPage.js new file mode 100644 index 0000000..2693236 --- /dev/null +++ b/src/bussiness/manage/settingPage.js @@ -0,0 +1,262 @@ +const bussinessRoom = require("./../../bussiness/room/room") +const cfg = require("../../../conf/cfg.json"); +const dbOpen = cfg.db.open + +/** + * 获取设置房间页面 + * @param {*} data + * @returns + */ +async function getSettingPageHtml(data) { + if (!dbOpen) { + return 'db配置未开启'; + } + let resData = await bussinessRoom.getOrCreateManageRoom({ + tables: data.tables, + rname: data.room, + sid: data.socketId, + ip: data.ip, + device: data.userAgent, + content: JSON.stringify({ + openSendBug: true, + openScreen: true, + openOnlineUser: true, + openShareRoom: true, + openAiChat: true, + openGetCodeFile: true, + openVideoShare: true, + openPasswordRoom: true, + openScreenShare: true, + openFileTransfer: true, + openTxtTransfer: true, + openTurnServer: true, + openNetworkIcon: true, + openUseTurnIcon: true, + openCommRoom: true, + openRefleshRoom: true, + openNotice: true, + allowNumber: true, + allowChinese: true, + allowSymbol: true, + noticeMsgList:[] + }) + }) + + return ` + +
+
+
+
+
+
数据传输功能开关设置
+
+
+
+ +
+
+
+
+ +
+
+
+
+ +
+
+
+
+ +
+
+
+
+ +
+
+
+
+ +
+
+
+
+ +
+
+
+
+ +
+
+
+
+ +
+
+
+
+ +
+
+
+
+ +
+
+
+
+ +
+
+
+
+ +
+
+
+
+ +
+
+
+
+ +
+
+
+
+ +
+
+
+
+ +
+
+
+
+
+
+ +
+
+
+
数据传输房间号格式设置
+
+
+
+
+ +
+
+ +
+
+ +
+
+
+
+
+
+
+ +
+
+
+
数据传输公告设置
+
+
+
+
+ + +
+
+
+
+
+
+
+ +
+
+ + + ` +} + + +module.exports = { + getSettingPageHtml +} diff --git a/src/bussiness/notify/notify.js b/src/bussiness/notify/notify.js new file mode 100644 index 0000000..1f5e00b --- /dev/null +++ b/src/bussiness/notify/notify.js @@ -0,0 +1,70 @@ +const conf = require("../../../conf/cfg.json"); +const request = require('request'); +const qiwei = conf.notify.qiwei; +const open = conf.notify.open; + +// 统计企微机器人发送map +const qiweiMap = {} +for (let key in qiwei) { + qiweiMap[qiwei[key]] = { + time: new Date().valueOf(), + count: 0 + }; +} + +/** + * 企业微信通知 + * @param {*} msg + */ +function requestMsg(msg) { + if(!open){ + return + } + let finalKey = ""; + for (let key in qiweiMap) { + // 单个还没达到20次,直接用 + if (qiweiMap[key].count < 20) { + qiweiMap[key].count += 1; + finalKey = key; + break; + } else { + //达到20次,看看时间如果在1分钟内,说明达到限制,直接跳过 + if ((new Date().valueOf() / 1000) - (qiweiMap[key].time / 1000) <= 60) { + continue; + } else { + //达到20次,但是时间超过1分钟,我们尝试清零 + qiweiMap[key].count = 1; + qiweiMap[key].time = new Date().valueOf() + finalKey = key; + break; + } + } + } + + if (finalKey === '' && qiwei.length > 0) { + finalKey = qiwei[0]; + } + + msg = msg + `机器人KEY: ${finalKey}\n`; + msg = msg + `机器人KEY列表: ${JSON.stringify(qiweiMap)}\n`; + + request({ + url: "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=" + finalKey, + method: "POST", + headers: { + "content-type": "application/json", + }, + body: JSON.stringify({ + msgtype: "markdown", + markdown: { + content: msg, + } + }) + }, function (error, response, body) { + console.log('提示成功!', qiweiMap); + }); +} + +module.exports = { + requestMsg +} \ No newline at end of file diff --git a/src/bussiness/notify/notifySocketEvent.js b/src/bussiness/notify/notifySocketEvent.js new file mode 100644 index 0000000..673e4d0 --- /dev/null +++ b/src/bussiness/notify/notifySocketEvent.js @@ -0,0 +1,327 @@ + +const notify = require("./notify"); +const utils = require("./../../utils/utils"); + +/** + * 发送公共频道聊天通知 + * @param {*} data + */ +function sendChatingNotify(data) { + let notifyMsg = `## 文件传输通知 - ${data.title}` + + ` - ${data.room}\n` + + `库记录ID: ${data.recoderId}\n` + + `消息体ID: ${data.msgRecoderId}\n` + + `发送方ID: ${data.socketId}\n` + + `文本内容: ${decodeURIComponent(data.msg)}\n` + + `当前时间: ${utils.formateDateTime(new Date(), "yyyy-MM-dd hh:mm:ss")}\n` + + `访问IP: ${data.ip}\n` + + `访问设备: ${data.userAgent}\n`; + notify.requestMsg(notifyMsg) +} + + +/** + * 发送开始发送文件通知 + * @param {*} data + */ +function sendFileInfoNotify(data) { + let notifyMsg = `## 文件传输通知 - ${data.title}` + + ` - ${data.room}\n` + + `库记录ID: ${data.recoderId}\n` + + `发送方ID: ${data.from}\n` + + `文件名称: ${data.name}\n` + + `文件类型: ${data.type}\n` + + `文件大小: ${data.size} == (${data.size / 1024 / 1024}M)\n` + + `当前时间: ${utils.formateDateTime(new Date(), "yyyy-MM-dd hh:mm:ss")}\n` + + `访问IP: ${data.ip}\n` + + `访问设备: ${data.userAgent}\n`; + notify.requestMsg(notifyMsg) +} + + +/** + * 发送文件发送完毕通知 + * @param {*} data + */ +function sendFileDoneNotify(data) { + let notifyMsg = `## 文件传输通知 - ${data.title}` + + ` - ${data.room}\n` + + `发送方ID: ${data.from}\n` + + `接收方ID: ${data.to}\n` + + `文件名称: ${data.name}\n` + + `文件类型: ${data.type}\n` + + `文件大小: ${data.size} == (${data.size / 1024 / 1024}M)\n` + + `当前时间: ${utils.formateDateTime(new Date(), "yyyy-MM-dd hh:mm:ss")}\n` + + `访问IP: ${data.ip}\n` + + `访问设备: ${data.userAgent}\n`; + notify.requestMsg(notifyMsg) +} + + +/** + * 发送文本内容通知 + * @param {*} data + */ +function sendTxtNotify(data) { + let notifyMsg = `## 文件传输通知 - ${data.title}` + + ` - ${data.room}\n` + + `库记录ID: ${data.recoderId}\n` + + `发送方ID: ${data.from}\n` + + `文本内容: ${decodeURIComponent(data.content)}\n` + + `当前时间: ${utils.formateDateTime(new Date(), "yyyy-MM-dd hh:mm:ss")}\n` + + `访问IP: ${data.ip}\n` + + `访问设备: ${data.userAgent}\n`; + notify.requestMsg(notifyMsg) +} + + +/** + * 发送开始录屏通知 + * @param {*} data + */ +function sendStartScreenNotify(data) { + let notifyMsg = `## 文件传输通知 - ${data.title}\n` + + `当前时间: ${utils.formateDateTime(new Date(), "yyyy-MM-dd hh:mm:ss")}\n` + + `访问IP: ${data.ip}\n` + + `访问设备: ${data.userAgent}\n`; + notify.requestMsg(notifyMsg) +} + + +/** + * 发送停止录屏通知 + * @param {*} data + */ +function sendStopScreenNotify(data) { + let notifyMsg = `## 文件传输通知 - ${data.title}\n` + + `录屏时长: ${data.cost}秒\n` + + `录屏大小: ${data.size} == (${data.size / 1024 / 1024}M)\n` + + `当前时间: ${utils.formateDateTime(new Date(), "yyyy-MM-dd hh:mm:ss")}\n` + + `访问IP: ${data.ip}\n` + + `访问设备: ${data.userAgent}\n`; + notify.requestMsg(notifyMsg) +} + + +/** + * 发送开始屏幕共享通知 + * @param {*} data + */ +function sendStartScreenShareNotify(data) { + let notifyMsg = `## 文件传输通知 - ${data.title}` + + ` - ${data.room}\n` + + `当前时间: ${utils.formateDateTime(new Date(), "yyyy-MM-dd hh:mm:ss")}\n` + + `访问IP: ${data.ip}\n` + + `访问设备: ${data.userAgent}\n`; + notify.requestMsg(notifyMsg) +} + + +/** + * 发送停止屏幕共享通知 + * @param {*} data + */ +function sendStopScreenShareNotify(data) { + let notifyMsg = `## 文件传输通知 - ${data.title}` + + ` - ${data.room}\n` + + `共享时长: ${data.cost}秒\n` + + `当前时间: ${utils.formateDateTime(new Date(), "yyyy-MM-dd hh:mm:ss")}\n` + + `访问IP: ${data.ip}\n` + + `访问设备: ${data.userAgent}\n`; + notify.requestMsg(notifyMsg) +} + + +/** + * 发送开始音视频通话通知 + * @param {*} data + */ +function sendStartVideoShareNotify(data) { + let notifyMsg = `## 文件传输通知 - ${data.title}` + + ` - ${data.room}\n` + + `当前时间: ${utils.formateDateTime(new Date(), "yyyy-MM-dd hh:mm:ss")}\n` + + `访问IP: ${data.ip}\n` + + `访问设备: ${data.userAgent}\n`; + notify.requestMsg(notifyMsg) +} + + +/** + * 发送停止音视频通话通知 + * @param {*} data + */ +function sendStopVideoShareNotify(data) { + let notifyMsg = `## 文件传输通知 - ${data.title}` + + ` - ${data.room}\n` + + `通话时长: ${data.cost}秒\n` + + `当前时间: ${utils.formateDateTime(new Date(), "yyyy-MM-dd hh:mm:ss")}\n` + + `访问IP: ${data.ip}\n` + + `访问设备: ${data.userAgent}\n`; + notify.requestMsg(notifyMsg) +} + + +/** + * 发送取件码通知 + * @param {*} data + */ +function sendCodeFileNotify(data) { + let notifyMsg = `## 文件传输通知 - ${data.title}\n` + + `取件码ID: ${data.codeId}秒\n` + + `当前时间: ${utils.formateDateTime(new Date(), "yyyy-MM-dd hh:mm:ss")}\n` + + `访问IP: ${data.ip}\n` + + `访问设备: ${data.userAgent}\n`; + notify.requestMsg(notifyMsg) +} + + +/** + * 发送openai聊天通知 + * @param {*} data + */ +function sendOpenaiChatNotify(data) { + let notifyMsg = `## 文件传输通知 - ${data.title}` + + ` - ${data.room}\n` + + `KEYS状态: ${data.keys}\n` + + `聊天内容: ${data.content}\n` + + `回复内容: ${data.answer}\n` + + `当前时间: ${utils.formateDateTime(new Date(), "yyyy-MM-dd hh:mm:ss")}\n` + + `访问IP: ${data.ip}\n` + + `访问设备: ${data.userAgent}\n`; + notify.requestMsg(notifyMsg) +} + + +/** + * 发送意见反馈通知 + * @param {*} data + */ +function sendBugNotify(data) { + let notifyMsg = `## 文件传输通知 - ${data.title}\n` + + `反馈内容: ${data.msg}\n` + + `当前时间: ${utils.formateDateTime(new Date(), "yyyy-MM-dd hh:mm:ss")}\n` + + `访问IP: ${data.ip}\n` + + `访问设备: ${data.userAgent}\n`; + notify.requestMsg(notifyMsg) +} + + +/** + * 发送管理后台登录失败通知 + * @param {*} data + */ +function sendManageLoginFailedNotify(data) { + let notifyMsg = `## 文件传输通知 - ${data.title}` + + ` - ${data.room}\n` + + `访问密码: ${data.value}\n` + + `当前时间: ${utils.formateDateTime(new Date(), "yyyy-MM-dd hh:mm:ss")}\n` + + `访问IP: ${data.ip}\n` + + `访问设备: ${data.userAgent}\n`; + notify.requestMsg(notifyMsg) +} + + +/** + * 发送管理后台登录成功通知 + * @param {*} data + */ +function sendManageLoginSuccessNotify(data) { + let notifyMsg = `## 文件传输通知 - ${data.title}` + + ` - ${data.room}\n` + + `访问密码: ${data.value}\n` + + `TOKEN: ${data.token}\n` + + `当前时间: ${utils.formateDateTime(new Date(), "yyyy-MM-dd hh:mm:ss")}\n` + + `访问IP: ${data.ip}\n` + + `访问设备: ${data.userAgent}\n`; + notify.requestMsg(notifyMsg) +} + + +/** + * 发送管理后台修改配置通知 + * @param {*} data + */ +function sendManageUpdateInfoNotify(data) { + let notifyMsg = `## 文件传输通知 - ${data.title}` + + ` - ${data.room}\n` + + `修改内容: ${data.content}\n` + + `TOKEN: ${data.token}\n` + + `当前时间: ${utils.formateDateTime(new Date(), "yyyy-MM-dd hh:mm:ss")}\n` + + `访问IP: ${data.ip}\n` + + `访问设备: ${data.userAgent}\n`; + notify.requestMsg(notifyMsg) +} + + +/** + * 发送管理后台非法修改配置通知 + * @param {*} data + */ +function sendManageUpdateFailedNotify(data) { + let notifyMsg = `## 文件传输通知 - ${data.title}` + + ` - ${data.room}\n` + + `修改内容: ${data.content}\n` + + `TOKEN: ${data.token}\n` + + `当前时间: ${utils.formateDateTime(new Date(), "yyyy-MM-dd hh:mm:ss")}\n` + + `访问IP: ${data.ip}\n` + + `访问设备: ${data.userAgent}\n`; + notify.requestMsg(notifyMsg) +} + + +/** + * 发送创建/加入房间通知 + * @param {*} data + */ +function sendCreateJoinRoomNotify(data) { + let notifyMsg = `## 文件传输通知 - ${data.title}` + + ` - ${data.room}\n` + + `库记录ID: ${data.recoderId}\n` + + `连接方ID: ${data.socketId}\n` + + `房间密码: ${data.password}\n` + + `当前时间: ${utils.formateDateTime(new Date(), "yyyy-MM-dd hh:mm:ss")}\n` + + `访问IP: ${data.ip}\n` + + `访问设备: ${data.userAgent}\n`; + notify.requestMsg(notifyMsg) +} + + +/** + * 退出房间通知 + * @param {*} data + */ +function sendExitRoomNotify(data) { + let notifyMsg = `## 文件传输通知 - ${data.title}` + + ` - ${data.room}\n` + + `库记录ID: ${data.recoderId}\n` + + `连接方ID: ${data.socketId}\n` + + `当前时间: ${utils.formateDateTime(new Date(), "yyyy-MM-dd hh:mm:ss")}\n` + + `访问IP: ${data.ip}\n` + + `访问设备: ${data.userAgent}\n`; + notify.requestMsg(notifyMsg) +} + + + + +module.exports = { + sendExitRoomNotify, + sendCreateJoinRoomNotify, + sendManageUpdateFailedNotify, + sendManageUpdateInfoNotify, + sendManageLoginSuccessNotify, + sendManageLoginFailedNotify, + sendBugNotify, + sendStopScreenNotify, + sendStartScreenNotify, + sendStopScreenShareNotify, + sendStartScreenShareNotify, + sendStopVideoShareNotify, + sendStartVideoShareNotify, + sendTxtNotify, + sendCodeFileNotify, + sendFileDoneNotify, + sendFileInfoNotify, + sendChatingNotify, + sendOpenaiChatNotify +} \ No newline at end of file diff --git a/src/openai/openai.js b/src/bussiness/openai/openai.js similarity index 98% rename from src/openai/openai.js rename to src/bussiness/openai/openai.js index 4062a8e..424ef03 100644 --- a/src/openai/openai.js +++ b/src/bussiness/openai/openai.js @@ -4,7 +4,7 @@ */ const request = require('request') -const openai = require('../../conf/cfg.json').openai +const openai = require('../../../conf/cfg.json').openai let keysMap = {} /** diff --git a/src/oss/aly.js b/src/bussiness/oss/aly.js similarity index 100% rename from src/oss/aly.js rename to src/bussiness/oss/aly.js diff --git a/src/oss/qiniu.js b/src/bussiness/oss/qiniu.js similarity index 100% rename from src/oss/qiniu.js rename to src/bussiness/oss/qiniu.js diff --git a/src/oss/seafile.js b/src/bussiness/oss/seafile.js similarity index 98% rename from src/oss/seafile.js rename to src/bussiness/oss/seafile.js index bccbccd..6ec419a 100644 --- a/src/oss/seafile.js +++ b/src/bussiness/oss/seafile.js @@ -4,7 +4,7 @@ */ const request = require('request'); -const seafile = require('../../conf/cfg.json').oss.seafile +const seafile = require('../../../conf/cfg.json').oss.seafile /** * 获取token diff --git a/src/bussiness/room/room.js b/src/bussiness/room/room.js new file mode 100644 index 0000000..09c13f0 --- /dev/null +++ b/src/bussiness/room/room.js @@ -0,0 +1,117 @@ +const room = require("./../../dao/room/room"); + +/** + * 创建/加入房间数据入库 + * @param {*} data + */ +async function createJoinRoom(data) { + let req = { + ctx: { + tables: data.tables + }, + params: { + uid: "1", + uname: "user", + rname: data.roomName, + sid: data.socketId, + pwd: data.password, + ip: data.ip, + device: data.device, + url: data.url || "", + content: JSON.stringify(data.content) + } + }; + + let res = await room.createJoinRoom(req, null); + + return res && res.dataValues ? res.dataValues.id : 0 +} + + +/** + * 退出房间 + * @param {*} data + */ +async function exitRoom(data) { + let req = { + ctx: { + tables: data.tables + }, + params: { + sid: data.sid + } + }; + + let res = await room.exitRoomBySid(req, null); + console.log("退出成功 : ", data, res) + return 0; +} + + +/** + * 更新管理后台频道设置信息 + * @param {*} data + */ +async function updateManageRoom(data) { + let req = { + ctx: { + tables: data.tables + }, + params: { + id: data.id, + content: data.content + } + }; + + return await room.updateRoomContent(req, null); +} + +/** + * 获取管理后台频道设置信息 + * 管理频道房间号不存在就创建 + * @param {*} data + */ +async function getOrCreateManageRoom(data) { + let req = { + ctx: { + tables: data.tables + }, + params: { + sid: data.socketId, + ip: data.ip, + device: data.device, + content: data.content + } + }; + + return await room.getOrCreateManageRoom(req, null); +} + +/** + * 获取加入/创建房间统计信息 + * @param {*} data + * @returns + */ +async function getRoomHistoryInfo(data){ + let req = { + ctx: { + tables: data.tables, + sockets : data.sockets, + dbClient : data.dbClient + }, + params: { + limit : 10, + day : data.day + } + }; + + return await room.getRoomHistoryInfo(req, null); +} + +module.exports = { + getOrCreateManageRoom, + updateManageRoom, + exitRoom, + createJoinRoom, + getRoomHistoryInfo +} \ No newline at end of file diff --git a/src/comm/comm.js b/src/comm/comm.js deleted file mode 100644 index 9146cdd..0000000 --- a/src/comm/comm.js +++ /dev/null @@ -1,97 +0,0 @@ -const utils = require("../../utils/utils"); -const conf = require("../../conf/cfg.json"); -const request = require('request'); -const wsConf = conf.ws; -const webrtcConf = conf.webrtc; -const qiwei = conf.notify.qiwei; -const open = conf.notify.open; - - -// 统计企微机器人发送map -const qiweiMap = {} -for (let key in qiwei) { - qiweiMap[qiwei[key]] = { - time: new Date().valueOf(), - count: 0 - }; -} - - -//获取ip地址,初始化等相关配置 -function initData(req, res) { - var regexIP = /^((?:(?:25[0-5]|2[0-4]\d|[01]?\d?\d)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d?\d))$/; - let ip = utils.getLocalIP(); - if (!regexIP.test(ip)) { - ip = utils.getClientIP(req) - } - if (!regexIP.test(ip)) { - ip = "127.0.0.1" - } - - let data = { - wsHost: wsConf.ws_online ? wsConf.ws_online : "ws://" + ip + ":" + wsConf.port, - rtcConfig: { iceServers: webrtcConf.iceServers }, - options: webrtcConf.options - }; - - res.json(data) -} - - -/** - * 企业微信通知 - * @param {*} msg - */ -function requestMsg(msg) { - if(!open){ - return - } - let finalKey = ""; - for (let key in qiweiMap) { - // 单个还没达到20次,直接用 - if (qiweiMap[key].count < 20) { - qiweiMap[key].count += 1; - finalKey = key; - break; - } else { - //达到20次,看看时间如果在1分钟内,说明达到限制,直接跳过 - if ((new Date().valueOf() / 1000) - (qiweiMap[key].time / 1000) <= 60) { - continue; - } else { - //达到20次,但是时间超过1分钟,我们尝试清零 - qiweiMap[key].count = 1; - qiweiMap[key].time = new Date().valueOf() - finalKey = key; - break; - } - } - } - - if (finalKey === '' && qiwei.length > 0) { - finalKey = qiwei[0]; - } - - msg = msg + `机器人KEY: ${finalKey}\n`; - msg = msg + `机器人KEY列表: ${JSON.stringify(qiweiMap)}\n`; - - request({ - url: "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=" + finalKey, - method: "POST", - headers: { - "content-type": "application/json", - }, - body: JSON.stringify({ - msgtype: "markdown", - markdown: { - content: msg, - } - }) - }, function (error, response, body) { - console.log('提示成功!', qiweiMap); - }); -} - -module.exports = { - initData, - requestMsg -} \ No newline at end of file diff --git a/src/controller/comm/comm.js b/src/controller/comm/comm.js new file mode 100644 index 0000000..1b73000 --- /dev/null +++ b/src/controller/comm/comm.js @@ -0,0 +1,33 @@ +const utils = require("../../utils/utils"); +const conf = require("../../../conf/cfg.json"); +const wsConf = conf.ws; +const webrtcConf = conf.webrtc; + + +/** + * 获取ip地址,初始化等相关配置 + * @param {*} req + * @param {*} res + */ +function initData(req, res) { + let regexIP = /^((?:(?:25[0-5]|2[0-4]\d|[01]?\d?\d)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d?\d))$/; + let ip = utils.getLocalIP(); + if (!regexIP.test(ip)) { + ip = utils.getClientIP(req) + } + if (!regexIP.test(ip)) { + ip = "127.0.0.1" + } + + let data = { + wsHost: wsConf.ws_online ? wsConf.ws_online : "ws://" + ip + ":" + wsConf.port, + rtcConfig: { iceServers: webrtcConf.iceServers }, + options: webrtcConf.options + }; + + res.json(data) +} + +module.exports = { + initData, +} \ No newline at end of file diff --git a/src/comm/index.js b/src/controller/comm/index.js similarity index 100% rename from src/comm/index.js rename to src/controller/comm/index.js diff --git a/src/file/index.js b/src/controller/dog/index.js similarity index 50% rename from src/file/index.js rename to src/controller/dog/index.js index b2af316..0e99bbd 100644 --- a/src/file/index.js +++ b/src/controller/dog/index.js @@ -1,9 +1,14 @@ const express = require('express'); -const file = require("./file"); module.exports = function () { const router = express.Router(); + router.get("/dog", (req, res) => { + res.json({ + dog : "request dog api ok!" + }) + }); + return router; } \ No newline at end of file diff --git a/src/room/index.js b/src/controller/room/index.js similarity index 50% rename from src/room/index.js rename to src/controller/room/index.js index 4546f91..bf2d4c6 100644 --- a/src/room/index.js +++ b/src/controller/room/index.js @@ -1,9 +1,14 @@ const express = require('express'); -const room = require("./room"); module.exports = function () { const router = express.Router(); + router.get("/room", (req, res) => { + res.json({ + room : "request room api ok!" + }) + }); + return router; } \ No newline at end of file diff --git a/src/controller/router.js b/src/controller/router.js new file mode 100644 index 0000000..99a795b --- /dev/null +++ b/src/controller/router.js @@ -0,0 +1,23 @@ +const fs = require('fs'); +const path = require('path'); +const cfg = require("../../conf/cfg.json"); + +module.exports = () => { + let routers = {}; + let handlerConf = cfg.router.filter; + let dirs = fs.readdirSync(__dirname); + for (let file of dirs) { + //过滤文件夹和文件 + if (handlerConf.whiteDir.includes(file) || handlerConf.whiteFile.includes(file)) continue; + try { + //添加router + let stat = fs.statSync(path.join(__dirname, file, 'index.js')); + if (stat && stat.isFile()) { + routers["/api/" + file] = require("./" + file + '/index')(); + } + } catch (e) { + console.log(e); + } + } + return routers; +} \ No newline at end of file diff --git a/src/dog/dog.js b/src/dao/dog/dog.js similarity index 88% rename from src/dog/dog.js rename to src/dao/dog/dog.js index 3a2e053..47ee2fb 100644 --- a/src/dog/dog.js +++ b/src/dao/dog/dog.js @@ -1,5 +1,5 @@ const utils = require("../../utils/utils"); -const dbOpen = require("../../conf/cfg.json").db.open; +const dbOpen = require("../../../conf/cfg.json").db.open; /** * 添加操作dog数据 @@ -61,7 +61,7 @@ async function getDogManageInfo(req, res, next) { let nextDay = utils.getNextDay(chooseDay); //某日传输聚合列表 - const [transferListToday, metadata] = await ctx.sql.query(`select name, room_id, content, handshake, created_at from dog where created_at >= "${chooseDay}" and created_at <= "${nextDay}" order by created_at desc`); + const [transferListToday, metadata] = await ctx.dbClient.query(`select name, room_id, content, handshake, created_at from dog where created_at >= "${chooseDay}" and created_at <= "${nextDay}" order by created_at desc`); //发送文件 let fileTransferList = transferListToday.filter(element => { @@ -113,7 +113,7 @@ async function getDogManageInfo(req, res, next) { //全部数量统计 - const [transferAll, metadata1] = await ctx.sql.query(`select name, count(*) as user from dog group by name`); + const [transferAll, metadata1] = await ctx.dbClient.query(`select name, count(*) as user from dog group by name`); transferAll.forEach(element => { if (element.name === '准备发送文件') { data.transferFileAll += element.user @@ -145,7 +145,7 @@ async function getDogManageInfo(req, res, next) { async function getDogChating10Info(req, res, next) { let ctx = req.ctx || {}; - const [list, metadata] = await ctx.sql.query(`select name, room_id, content, socket_id, created_at from dog where name = '公共聊天室' order by created_at desc limit 10`); + const [list, metadata] = await ctx.dbClient.query(`select name, room_id, content, socket_id, created_at from dog where name = '公共聊天室' order by created_at desc limit 10`); let resList = [] list.forEach(element => { diff --git a/src/dao/room/room.js b/src/dao/room/room.js new file mode 100644 index 0000000..09bda33 --- /dev/null +++ b/src/dao/room/room.js @@ -0,0 +1,277 @@ + +const utils = require("../../utils/utils"); +const cfg = require("../../../conf/cfg.json") +const manageConfig = cfg.router.manage; +const dbOpen = cfg.db.open; + +// 默认开关数据 +const defaultSwitchData = { + openSendBug: true, + openScreen: true, + openOnlineUser: true, + openShareRoom: true, + openAiChat: true, + openGetCodeFile: true, + openVideoShare: true, + openPasswordRoom: true, + openScreenShare: true, + openFileTransfer: true, + openTxtTransfer: true, + openTurnServer: true, + openNetworkIcon: true, + openUseTurnIcon: true, + openCommRoom: true, + openRefleshRoom: true, + openNotice: true, + allowNumber: true, + allowChinese: true, + allowSymbol: true, + noticeMsgList: [], +} + +/** + * 管理后台特殊房间入口、配置信息 + * @param {*} req + * @param {*} res + * @param {*} next + */ +async function getOrCreateManageRoom(req, res, next) { + let ctx = req.ctx || {}; + let params = req.params || {}; + let data = {}; + + let manageRoomList = await ctx.tables.Room.findAll({ + where: { + rname: manageConfig.room, + flag: 1 + } + }); + + if (manageRoomList.length === 0) { + data = await ctx.tables.Room.create({ + rcode: utils.genRoom(), + rname: manageConfig.room, + flag: 1, + sid: params.sid, + ip: params.ip, + device: params.device, + content: JSON.stringify(defaultSwitchData) + }); + + console.log("创建管理房间配置成功") + + manageRoomList = await ctx.tables.Room.findAll({ + where: { + rname: manageConfig.room, + flag: 1 + } + }); + } + + manageRoomList = manageRoomList.length >= 1 ? manageRoomList[0] : manageRoomList + + if (res) { + res.json(manageRoomList) + } else { + return manageRoomList; + } +} + + +/** + * 获取房间统计信息 + * @param {*} req + * @param {*} res + * @param {*} next + */ +async function getRoomHistoryInfo(req, res, next) { + let ctx = req.ctx || {}; + let params = req.params || {}; + let sockets = ctx.sockets || {}; + let data = { + createRoomToday: 0, + createRoomAll: 0, + joinRoomTodady: 0, + joinRoomAll: 0, + onlineRoomList: [], + todayRoomList: [], + userAgentIpList: [] + }; + + //当前在线房间列表 + for (let room in sockets.adapter.rooms) { + if (room.length > 15) { + continue + } + data.onlineRoomList.push({ + status: "在线", + room: room, + userNumber: sockets.adapter.rooms[room] ? Object.keys(sockets.adapter.rooms[room].sockets).length : 0, + createTime: sockets.adapter.rooms[room].createTime + }) + } + + let day; + try { + day = new Date(params.day) + } catch (e) { + day = new Date() + } + let chooseDay = utils.formateDateTime(day, "yyyy-MM-dd"); + let nextDay = utils.getNextDay(chooseDay); + + // 某日房间聚合列表,数量统计 + const [roomCoutingListToday, metadata] = await ctx.dbClient.query( + `select rname, any_value(created_at) as created_at, count(*) as user from room where created_at >= "${chooseDay}" and created_at <= "${nextDay}" group by rname order by created_at desc`); + + data.createRoomToday += roomCoutingListToday.length; + roomCoutingListToday.forEach(element => { + data.joinRoomTodady += element.user; + data.todayRoomList.push({ + room: element.rname, + count: element.user, + createTime: utils.formateDateTime(new Date(element.created_at), "yyyy-MM-dd hh:mm:ss"), + }) + }); + + // 全部数量统计 + const [roomCoutingListAll, metadata1] = await ctx.dbClient.query(`select count(*) as user from room group by rname`); + data.createRoomAll += roomCoutingListAll.length; + roomCoutingListAll.forEach(element => { + data.joinRoomAll += element.user + }); + + // 某日房间设备统计列表 + const [roomListAgent, metadata2] = await ctx.dbClient.query(`select rname, content, created_at from room where created_at >= "${utils.formateDateTime(new Date(), "yyyy-MM-dd")}" order by created_at desc`); + roomListAgent.forEach(element => { + let content = JSON.parse(element.content); + if (content && content.handshake) { + data.userAgentIpList.push({ + room: element.rname, + userAgent: content.handshake.headers['user-agent'], + Ip: content.handshake.headers['x-real-ip'] || content.handshake.headers['x-forwarded-for'] || content.handshake.headers['host'], + createTime: utils.formateDateTime(new Date(element.created_at), "yyyy-MM-dd hh:mm:ss"), + }) + } + }); + + data.chooseDay = chooseDay; + + if (res) { + res.json(data) + } else { + return data; + } +} + +/** + * 创建/加入房间 + * @param {*} req + * @param {*} res + * @param {*} next + */ +async function createJoinRoom(req, res, next) { + let ctx = req.ctx || {}; + let params = req.params || {}; + let data = {}; + + data = await ctx.tables.Room.create({ + uid: params.uid, + uname: params.uname, + rcode: utils.genRoom(), + rname: params.rname, + sid: params.sid, + ip: params.ip, + device: params.device, + url: params.url, + content: params.content + }); + + if (res) { + res.json(data) + } else { + return data; + } +} + + +/** + * 更新房间 + * @param {*} req + * @param {*} res + * @param {*} next + */ +async function updateRoomContent(req, res, next) { + let ctx = req.ctx || {}; + let params = req.params || {}; + + let data = await ctx.tables.Room.update({ + content: params.content, + }, { + where: { + id: params.id, + flag: 1 + } + }); + + if (res) { + res.json(data) + } else { + return data; + } +} + + +/** + * 退出房间 + * @param {*} req + * @param {*} res + * @param {*} next + */ +async function exitRoomBySid(req, res, next) { + let ctx = req.ctx || {}; + let params = req.params || {}; + + console.log(ctx.tables) + + let data = await ctx.tables.Room.update({ + status: 1 + }, { + where: { + sid: params.sid + } + }); + + if (res) { + res.json(data) + } else { + return data; + } +} + + +module.exports = dbOpen ? { + getRoomHistoryInfo, + createJoinRoom, + updateRoomContent, + exitRoomBySid, + getOrCreateManageRoom +} : { + getRoomHistoryInfo: () => { + return {} + }, + createJoinRoom: () => { + return {} + }, + updateRoomContent: () => { + return {} + }, + exitRoomBySid: () => { + return {} + }, + getOrCreateManageRoom: () => { + return { + content: JSON.stringify(defaultSwitchData) + } + }, +} \ No newline at end of file diff --git a/src/dog/index.js b/src/dog/index.js deleted file mode 100644 index 7ccbd15..0000000 --- a/src/dog/index.js +++ /dev/null @@ -1,9 +0,0 @@ -const express = require('express'); -const dog = require("./dog"); - - -module.exports = function () { - const router = express.Router(); - - return router; -} \ No newline at end of file diff --git a/src/file/file.js b/src/file/file.js deleted file mode 100644 index e69de29..0000000 diff --git a/src/room/room.js b/src/room/room.js deleted file mode 100644 index 0dc678c..0000000 --- a/src/room/room.js +++ /dev/null @@ -1,276 +0,0 @@ - -const utils = require("../../utils/utils"); -const manageConfig = require("../../conf/cfg.json").router.manage; -const dbOpen = require("../../conf/cfg.json").db.open; - -// 默认开关数据 -const defaultSwitchData = { - openSendBug: true, - openScreen: true, - openOnlineUser: true, - openShareRoom: true, - openAiChat: true, - openGetCodeFile: true, - openVideoShare: true, - openPasswordRoom: true, - openScreenShare: true, - openFileTransfer: true, - openTxtTransfer: true, - openTurnServer: true, - openNetworkIcon: true, - openUseTurnIcon: true, - openCommRoom: true, - openRefleshRoom: true, - openNotice: true, - allowNumber: true, - allowChinese: true, - allowSymbol: true, - noticeMsgList : [], - } - -/** - * 管理后台特殊房间入口、配置信息 - * @param {*} req - * @param {*} res - * @param {*} next - */ -async function getOrCreateManageRoom(req, res, next) { - let ctx = req.ctx || {}; - let params = req.params || {}; - let data = {}; - - let manageRoomList = await ctx.tables.Room.findAll({ - where: { - rname: manageConfig.room, - flag :1 - } - }); - - if (manageRoomList.length === 0) { - data = await ctx.tables.Room.create({ - rcode: utils.genRoom(), - rname: manageConfig.room, - flag: 1, - sid: params.sid, - ip: params.ip, - device: params.device, - content: JSON.stringify(defaultSwitchData) - }); - - console.log("创建管理房间配置成功") - - manageRoomList = await ctx.tables.Room.findAll({ - where: { - rname: manageConfig.room, - flag :1 - } - }); - } - - manageRoomList = manageRoomList.length >= 1 ? manageRoomList[0] : manageRoomList - - if (res) { - res.json(manageRoomList) - } else { - return manageRoomList; - } -} - - -/** - * 获取房间统计信息 - * @param {*} req - * @param {*} res - * @param {*} next - */ -async function getManageRoomInfo(req, res, next) { - let ctx = req.ctx || {}; - let params = req.params || {}; - let sockets = ctx.sockets || {}; - let data = { - createRoomToday: 0, - createRoomAll: 0, - joinRoomTodady: 0, - joinRoomAll: 0, - onlineRoomList: [], - todayRoomList: [], - userAgentIpList: [] - }; - - //当前在线房间列表 - for (let room in sockets.adapter.rooms) { - if (room.length > 15) { - continue - } - data.onlineRoomList.push({ - status: "在线", - room: room, - userNumber: sockets.adapter.rooms[room] ? Object.keys(sockets.adapter.rooms[room].sockets).length : 0, - createTime: sockets.adapter.rooms[room].createTime - }) - } - - let day; - try{ - day = new Date(params.day) - }catch(e){ - day = new Date() - } - let chooseDay = utils.formateDateTime(day, "yyyy-MM-dd"); - let nextDay = utils.getNextDay(chooseDay); - - // 某日房间聚合列表,数量统计 - const [roomCoutingListToday, metadata] = await ctx.sql.query( - `select rname, any_value(created_at) as created_at, count(*) as user from room where created_at >= "${chooseDay}" and created_at <= "${nextDay}" group by rname order by created_at desc`); - - data.createRoomToday += roomCoutingListToday.length; - roomCoutingListToday.forEach(element => { - data.joinRoomTodady += element.user; - data.todayRoomList.push({ - room: element.rname, - count: element.user, - createTime: utils.formateDateTime(new Date(element.created_at), "yyyy-MM-dd hh:mm:ss"), - }) - }); - - // 全部数量统计 - const [roomCoutingListAll, metadata1] = await ctx.sql.query(`select count(*) as user from room group by rname`); - data.createRoomAll += roomCoutingListAll.length; - roomCoutingListAll.forEach(element => { - data.joinRoomAll += element.user - }); - - // 某日房间设备统计列表 - const [roomListAgent, metadata2] = await ctx.sql.query(`select rname, content, created_at from room where created_at >= "${utils.formateDateTime(new Date(), "yyyy-MM-dd")}" order by created_at desc`); - roomListAgent.forEach(element => { - let content = JSON.parse(element.content); - if(content && content.handshake){ - data.userAgentIpList.push({ - room: element.rname, - userAgent: content.handshake.headers['user-agent'], - Ip: content.handshake.headers['x-real-ip'] || content.handshake.headers['x-forwarded-for'] || content.handshake.headers['host'], - createTime: utils.formateDateTime(new Date(element.created_at), "yyyy-MM-dd hh:mm:ss"), - }) - } - }); - - data.chooseDay = chooseDay; - - if (res) { - res.json(data) - } else { - return data; - } -} - -/** - * 创建/加入房间 - * @param {*} req - * @param {*} res - * @param {*} next - */ -async function createJoinRoom(req, res, next) { - let ctx = req.ctx || {}; - let params = req.params || {}; - let data = {}; - - data = await ctx.tables.Room.create({ - uid: params.uid, - uname: params.uname, - rcode: utils.genRoom(), - rname: params.rname, - sid: params.sid, - ip: params.ip, - device: params.device, - url: params.url, - content: params.content - }); - - if (res) { - res.json(data) - } else { - return data; - } -} - - -/** - * 更新房间 - * @param {*} req - * @param {*} res - * @param {*} next - */ -async function updateRoomContent(req, res, next) { - let ctx = req.ctx || {}; - let params = req.params || {}; - - let data = await ctx.tables.Room.update({ - content: params.content, - }, { - where: { - id: params.id, - flag : 1 - } - }); - - if (res) { - res.json(data) - } else { - return data; - } -} - - -/** - * 退出房间 - * @param {*} req - * @param {*} res - * @param {*} next - */ -async function exitRoomBySid(req, res, next) { - let ctx = req.ctx || {}; - let params = req.params || {}; - - console.log(ctx.tables) - - let data = await ctx.tables.Room.update({ - status: 1 - }, { - where: { - sid: params.sid - } - }); - - if (res) { - res.json(data) - } else { - return data; - } -} - - -module.exports = dbOpen ? { - getManageRoomInfo, - createJoinRoom, - updateRoomContent, - exitRoomBySid, - getOrCreateManageRoom -} : { - getManageRoomInfo : () => { - return {} - }, - createJoinRoom : () => { - return {} - }, - updateRoomContent : () => { - return {} - }, - exitRoomBySid : () => { - return {} - }, - getOrCreateManageRoom : () => { - return { - content : JSON.stringify(defaultSwitchData) - } - }, -} \ No newline at end of file diff --git a/src/router.js b/src/router.js deleted file mode 100644 index 95bdfeb..0000000 --- a/src/router.js +++ /dev/null @@ -1,24 +0,0 @@ -const fs = require('fs'); -const path = require('path'); - -module.exports = (conf) => { - let routers = {}; - let handlerConf = conf.router.filter; - - //file模块 - let dirs = fs.readdirSync(__dirname); - for (let file of dirs) { - //过滤文件夹和文件 - if(handlerConf.whiteDir.includes(file) || handlerConf.whiteFile.includes(file)) continue; - try { - //添加router - let stat = fs.statSync(path.join(__dirname, file, 'index.js')); - if (stat && stat.isFile()) { - routers["/api/"+file] = require("./" + file + '/index')(); - } - } catch (e) { - console.log(e); - } - } - return routers; -} \ No newline at end of file diff --git a/src/socket/bussiness.js b/src/socket/bussiness.js deleted file mode 100644 index 8b1f587..0000000 --- a/src/socket/bussiness.js +++ /dev/null @@ -1,1127 +0,0 @@ - -const room = require("../room/room"); -const dog = require("../dog/dog"); -const comm = require("../comm/comm"); -const utils = require("../../utils/utils"); -const dbOpen = require("../../conf/cfg.json").db.open; - -/** - * 操作记录 - * @param {*} data - */ -async function dogData(data) { - let req = { - ctx: { - tables: data.tables - }, - params: data - }; - let res = 0; - try { - res = await dog.addDogData(req, null); - } catch (e) { - console.log(e) - } - return res && res.dataValues ? res.dataValues.id : 0 -} - - -/** - * 创建/加入房间数据入库 - * @param {*} data - */ -async function createJoinRoom(data) { - let req = { - ctx: { - tables: data.tables - }, - params: { - uid: "1", - uname: "user", - rname: data.roomName, - sid: data.socketId, - pwd: data.password, - ip: data.ip, - device: data.device, - url: data.url || "", - content: JSON.stringify(data.content) - } - }; - - let res = await room.createJoinRoom(req, null); - - return res && res.dataValues ? res.dataValues.id : 0 -} - - -/** - * 退出房间 - * @param {*} data - */ -async function exitRoom(data) { - let req = { - ctx: { - tables: data.tables - }, - params: { - sid: data.sid - } - }; - let res = await room.exitRoomBySid(req, null); - console.log("退出成功 : ", data, res) - return 0; -} - - -/** - * 更新管理后台频道设置信息 - * @param {*} data - */ -async function updateManageRoom(data) { - let req = { - ctx: { - tables: data.tables - }, - params: { - id: data.id, - content: data.content - } - }; - let res = await room.updateRoomContent(req, null); - return res; -} - -/** - * 获取管理后台频道设置信息 - * 管理频道房间号不存在就创建 - * @param {*} data - */ -async function getOrCreateManageRoom(data) { - let req = { - ctx: { - tables: data.tables - }, - params: { - sid: data.socketId, - ip: data.ip, - device: data.device, - content: data.content - } - }; - let res = await room.getOrCreateManageRoom(req, null); - return res; -} - - -/** - * 获取最近10条公共聊天室数据 - * @param {*} data - */ -async function getDogChating10Info(data) { - let req = { - ctx: { - tables: data.tables, - sql: data.sql - } - }; - let res = await dog.getDogChating10Info(req, null); - return res; -} - - -/** - * 发送公共频道聊天通知 - * @param {*} data - */ -function sendChatingNotify(data) { - let notifyMsg = `## 文件传输通知 - ${data.title}` + - ` - ${data.room}\n` + - `库记录ID: ${data.recoderId}\n` + - `消息体ID: ${data.msgRecoderId}\n` + - `发送方ID: ${data.socketId}\n` + - `文本内容: ${decodeURIComponent(data.msg)}\n` + - `当前时间: ${utils.formateDateTime(new Date(), "yyyy-MM-dd hh:mm:ss")}\n` + - `访问IP: ${data.ip}\n` + - `访问设备: ${data.userAgent}\n`; - comm.requestMsg(notifyMsg) -} - - -/** - * 发送开始发送文件通知 - * @param {*} data - */ -function sendFileInfoNotify(data) { - let notifyMsg = `## 文件传输通知 - ${data.title}` + - ` - ${data.room}\n` + - `库记录ID: ${data.recoderId}\n` + - `发送方ID: ${data.from}\n` + - `文件名称: ${data.name}\n` + - `文件类型: ${data.type}\n` + - `文件大小: ${data.size} == (${data.size / 1024 / 1024}M)\n` + - `当前时间: ${utils.formateDateTime(new Date(), "yyyy-MM-dd hh:mm:ss")}\n` + - `访问IP: ${data.ip}\n` + - `访问设备: ${data.userAgent}\n`; - comm.requestMsg(notifyMsg) -} - - -/** - * 发送文件发送完毕通知 - * @param {*} data - */ -function sendFileDoneNotify(data) { - let notifyMsg = `## 文件传输通知 - ${data.title}` + - ` - ${data.room}\n` + - `发送方ID: ${data.from}\n` + - `接收方ID: ${data.to}\n` + - `文件名称: ${data.name}\n` + - `文件类型: ${data.type}\n` + - `文件大小: ${data.size} == (${data.size / 1024 / 1024}M)\n` + - `当前时间: ${utils.formateDateTime(new Date(), "yyyy-MM-dd hh:mm:ss")}\n` + - `访问IP: ${data.ip}\n` + - `访问设备: ${data.userAgent}\n`; - comm.requestMsg(notifyMsg) -} - - -/** - * 发送文本内容通知 - * @param {*} data - */ -function sendTxtNotify(data) { - let notifyMsg = `## 文件传输通知 - ${data.title}` + - ` - ${data.room}\n` + - `库记录ID: ${data.recoderId}\n` + - `发送方ID: ${data.from}\n` + - `文本内容: ${decodeURIComponent(data.content)}\n` + - `当前时间: ${utils.formateDateTime(new Date(), "yyyy-MM-dd hh:mm:ss")}\n` + - `访问IP: ${data.ip}\n` + - `访问设备: ${data.userAgent}\n`; - comm.requestMsg(notifyMsg) -} - - -/** - * 发送开始录屏通知 - * @param {*} data - */ -function sendStartScreenNotify(data) { - let notifyMsg = `## 文件传输通知 - ${data.title}\n` + - `当前时间: ${utils.formateDateTime(new Date(), "yyyy-MM-dd hh:mm:ss")}\n` + - `访问IP: ${data.ip}\n` + - `访问设备: ${data.userAgent}\n`; - comm.requestMsg(notifyMsg) -} - - -/** - * 发送停止录屏通知 - * @param {*} data - */ -function sendStopScreenNotify(data) { - let notifyMsg = `## 文件传输通知 - ${data.title}\n` + - `录屏时长: ${data.cost}秒\n` + - `录屏大小: ${data.size} == (${data.size / 1024 / 1024}M)\n` + - `当前时间: ${utils.formateDateTime(new Date(), "yyyy-MM-dd hh:mm:ss")}\n` + - `访问IP: ${data.ip}\n` + - `访问设备: ${data.userAgent}\n`; - comm.requestMsg(notifyMsg) -} - - -/** - * 发送开始屏幕共享通知 - * @param {*} data - */ -function sendStartScreenShareNotify(data) { - let notifyMsg = `## 文件传输通知 - ${data.title}` + - ` - ${data.room}\n` + - `当前时间: ${utils.formateDateTime(new Date(), "yyyy-MM-dd hh:mm:ss")}\n` + - `访问IP: ${data.ip}\n` + - `访问设备: ${data.userAgent}\n`; - comm.requestMsg(notifyMsg) -} - - -/** - * 发送停止屏幕共享通知 - * @param {*} data - */ -function sendStopScreenShareNotify(data) { - let notifyMsg = `## 文件传输通知 - ${data.title}` + - ` - ${data.room}\n` + - `共享时长: ${data.cost}秒\n` + - `当前时间: ${utils.formateDateTime(new Date(), "yyyy-MM-dd hh:mm:ss")}\n` + - `访问IP: ${data.ip}\n` + - `访问设备: ${data.userAgent}\n`; - comm.requestMsg(notifyMsg) -} - - -/** - * 发送开始音视频通话通知 - * @param {*} data - */ -function sendStartVideoShareNotify(data) { - let notifyMsg = `## 文件传输通知 - ${data.title}` + - ` - ${data.room}\n` + - `当前时间: ${utils.formateDateTime(new Date(), "yyyy-MM-dd hh:mm:ss")}\n` + - `访问IP: ${data.ip}\n` + - `访问设备: ${data.userAgent}\n`; - comm.requestMsg(notifyMsg) -} - - -/** - * 发送停止音视频通话通知 - * @param {*} data - */ -function sendStopVideoShareNotify(data) { - let notifyMsg = `## 文件传输通知 - ${data.title}` + - ` - ${data.room}\n` + - `通话时长: ${data.cost}秒\n` + - `当前时间: ${utils.formateDateTime(new Date(), "yyyy-MM-dd hh:mm:ss")}\n` + - `访问IP: ${data.ip}\n` + - `访问设备: ${data.userAgent}\n`; - comm.requestMsg(notifyMsg) -} - - -/** - * 发送取件码通知 - * @param {*} data - */ -function sendCodeFileNotify(data) { - let notifyMsg = `## 文件传输通知 - ${data.title}\n` + - `取件码ID: ${data.codeId}秒\n` + - `当前时间: ${utils.formateDateTime(new Date(), "yyyy-MM-dd hh:mm:ss")}\n` + - `访问IP: ${data.ip}\n` + - `访问设备: ${data.userAgent}\n`; - comm.requestMsg(notifyMsg) -} - - -/** - * 发送openai聊天通知 - * @param {*} data - */ -function sendOpenaiChatNotify(data) { - let notifyMsg = `## 文件传输通知 - ${data.title}` + - ` - ${data.room}\n` + - `KEYS状态: ${data.keys}\n` + - `聊天内容: ${data.content}\n` + - `回复内容: ${data.answer}\n` + - `当前时间: ${utils.formateDateTime(new Date(), "yyyy-MM-dd hh:mm:ss")}\n` + - `访问IP: ${data.ip}\n` + - `访问设备: ${data.userAgent}\n`; - comm.requestMsg(notifyMsg) -} - - -/** - * 发送意见反馈通知 - * @param {*} data - */ -function sendBugNotify(data) { - let notifyMsg = `## 文件传输通知 - ${data.title}\n` + - `反馈内容: ${data.msg}\n` + - `当前时间: ${utils.formateDateTime(new Date(), "yyyy-MM-dd hh:mm:ss")}\n` + - `访问IP: ${data.ip}\n` + - `访问设备: ${data.userAgent}\n`; - comm.requestMsg(notifyMsg) -} - - -/** - * 发送管理后台登录失败通知 - * @param {*} data - */ -function sendManageLoginFailedNotify(data) { - let notifyMsg = `## 文件传输通知 - ${data.title}` + - ` - ${data.room}\n` + - `访问密码: ${data.value}\n` + - `当前时间: ${utils.formateDateTime(new Date(), "yyyy-MM-dd hh:mm:ss")}\n` + - `访问IP: ${data.ip}\n` + - `访问设备: ${data.userAgent}\n`; - comm.requestMsg(notifyMsg) -} - - -/** - * 发送管理后台登录成功通知 - * @param {*} data - */ -function sendManageLoginSuccessNotify(data) { - let notifyMsg = `## 文件传输通知 - ${data.title}` + - ` - ${data.room}\n` + - `访问密码: ${data.value}\n` + - `TOKEN: ${data.token}\n` + - `当前时间: ${utils.formateDateTime(new Date(), "yyyy-MM-dd hh:mm:ss")}\n` + - `访问IP: ${data.ip}\n` + - `访问设备: ${data.userAgent}\n`; - comm.requestMsg(notifyMsg) -} - - -/** - * 发送管理后台修改配置通知 - * @param {*} data - */ -function sendManageUpdateInfoNotify(data) { - let notifyMsg = `## 文件传输通知 - ${data.title}` + - ` - ${data.room}\n` + - `修改内容: ${data.content}\n` + - `TOKEN: ${data.token}\n` + - `当前时间: ${utils.formateDateTime(new Date(), "yyyy-MM-dd hh:mm:ss")}\n` + - `访问IP: ${data.ip}\n` + - `访问设备: ${data.userAgent}\n`; - comm.requestMsg(notifyMsg) -} - - -/** - * 发送管理后台非法修改配置通知 - * @param {*} data - */ -function sendManageUpdateFailedNotify(data) { - let notifyMsg = `## 文件传输通知 - ${data.title}` + - ` - ${data.room}\n` + - `修改内容: ${data.content}\n` + - `TOKEN: ${data.token}\n` + - `当前时间: ${utils.formateDateTime(new Date(), "yyyy-MM-dd hh:mm:ss")}\n` + - `访问IP: ${data.ip}\n` + - `访问设备: ${data.userAgent}\n`; - comm.requestMsg(notifyMsg) -} - - -/** - * 发送创建/加入房间通知 - * @param {*} data - */ -function sendCreateJoinRoomNotify(data) { - let notifyMsg = `## 文件传输通知 - ${data.title}` + - ` - ${data.room}\n` + - `库记录ID: ${data.recoderId}\n` + - `连接方ID: ${data.socketId}\n` + - `房间密码: ${data.password}\n` + - `当前时间: ${utils.formateDateTime(new Date(), "yyyy-MM-dd hh:mm:ss")}\n` + - `访问IP: ${data.ip}\n` + - `访问设备: ${data.userAgent}\n`; - comm.requestMsg(notifyMsg) -} - - -/** - * 退出房间通知 - * @param {*} data - */ -function sendExitRoomNotify(data) { - let notifyMsg = `## 文件传输通知 - ${data.title}` + - ` - ${data.room}\n` + - `库记录ID: ${data.recoderId}\n` + - `连接方ID: ${data.socketId}\n` + - `当前时间: ${utils.formateDateTime(new Date(), "yyyy-MM-dd hh:mm:ss")}\n` + - `访问IP: ${data.ip}\n` + - `访问设备: ${data.userAgent}\n`; - comm.requestMsg(notifyMsg) -} - - -/** - * 获取房间页面 - * @param {*} data - * @returns - */ -async function getRoomPageHtml(data) { - if (!dbOpen) { - return 'db配置未开启'; - } - let resData = await room.getManageRoomInfo({ - ctx: { - tables: data.tables, - sql: data.sql, - sockets: data.sockets, - }, - params: { - limit: 10, - day: data.day, - } - }, null) - return ` - - -
- 当前查询时间: - -
-
-
-
- {{chooseDay}}房间频道创建/加入 -
-
-

{{createRoomToday}}个

-

总计房间频道创建/加入 - {{createRoomAll}}个 - - -

-
-
-
-
-
-
- {{chooseDay}}加入房间人数 - -
-
-

{{joinRoomTodady}}人

-

- 总计加入房间人数 - {{joinRoomAll}}人 - -

-
-
-
-
-
-
-
在线房间列表
- - - - - - - - - - -
房间频道 在线状态 房间人数 创建时间
{{r.room}} - - - - {{r.status}} - {{r.userNumber}}{{r.createTime}}
-
-
-
-
-
-
-
{{chooseDay}}房间统计
- - - - - - - - - -
房间频道 聚合创建次数 最近创建时间
{{r.room}}{{r.count}}{{r.createTime}}
-
-
-
- -
-
-
-
{{chooseDay}}访问设备/IP
- - - - - - - - - - -
房间频道 IP地址 设备信息 时间
{{userAgentIp.room}}{{userAgentIp.Ip}}{{userAgentIp.userAgent}}{{userAgentIp.createTime}}
-
-
-
- -
-
- - - ` -} - -/** - * 获取数据传输页面 - * @param {*} data - * @returns - */ -async function getDataPageHtml(data) { - if (!dbOpen) { - return 'db配置未开启'; - } - let resData = await dog.getDogManageInfo({ - ctx: { - tables: data.tables, - sql: data.sql, - sockets: data.sockets - }, - params: { - limit: 10, - day: data.day, - } - }, null) - return ` - - -
- 当前查询时间: - -
-
-
-
- {{chooseDay}}文件传输 -
-
-

{{transferFileToday}}次

-

总计文件传输 - {{transferFileAll}}次 - - -

-
-
-
-
-
-
- {{chooseDay}}文件传输大小 - -
-
-

{{transferFileSizeTodady}}M

-

- 总计文件传输大小 - 暂不统计 - -

-
-
-
-
-
-
- {{chooseDay}}文本传输 - -
-
- -

{{transferTxtTodady}}次

-

- 总计文本传输 - {{transferTxtAll}}次 - -

-
-
-
-
-
-
- {{chooseDay}}公共频道发言 - -
-
- -

{{transferCommTxtToday}}次

-

- 总计公共频道发言 - {{transferCommTxtAll}}次 - -

-
-
-
- -
-
-
-
{{chooseDay}}文件传输列表
- - - - - - - - - - -
房间频道 文件名称 文件大小 发送时间
{{file.room}}
{{file.name}}
{{file.size}}M{{file.createTime}}
-
-
-
- -
-
-
-
{{chooseDay}}文本传输列表
- - - - - - - - - - -
房间频道 文本内容 文本长度 发送时间
{{txt.room}}
{{txt.content}}
{{txt.size}}字符{{txt.createTime}}
-
-
-
- -
-
-
-
{{chooseDay}}公共频道发言
- - - - - - - - - - -
房间频道 文本内容 文本长度 发送时间
{{txt.room}}
{{txt.content}}
{{txt.size}}字符{{txt.createTime}}
-
-
-
- -
-
- - - ` -} - -/** - * 获取设置房间页面 - * @param {*} data - * @returns - */ -async function getSettingPageHtml(data) { - if (!dbOpen) { - return 'db配置未开启'; - } - let resData = await getOrCreateManageRoom({ - tables: data.tables, - rname: data.room, - sid: data.socketId, - ip: data.ip, - device: data.userAgent, - content: JSON.stringify({ - openSendBug: true, - openScreen: true, - openOnlineUser: true, - openShareRoom: true, - openAiChat: true, - openGetCodeFile: true, - openVideoShare: true, - openPasswordRoom: true, - openScreenShare: true, - openFileTransfer: true, - openTxtTransfer: true, - openTurnServer: true, - openNetworkIcon: true, - openUseTurnIcon: true, - openCommRoom: true, - openRefleshRoom: true, - openNotice: true, - allowNumber: true, - allowChinese: true, - allowSymbol: true, - noticeMsgList:[] - }) - }) - - return ` - -
-
-
-
-
-
数据传输功能开关设置
-
-
-
- -
-
-
-
- -
-
-
-
- -
-
-
-
- -
-
-
-
- -
-
-
-
- -
-
-
-
- -
-
-
-
- -
-
-
-
- -
-
-
-
- -
-
-
-
- -
-
-
-
- -
-
-
-
- -
-
-
-
- -
-
-
-
- -
-
-
-
- -
-
-
-
- -
-
-
-
-
-
- -
-
-
-
数据传输房间号格式设置
-
-
-
-
- -
-
- -
-
- -
-
-
-
-
-
-
- -
-
-
-
数据传输公告设置
-
-
-
-
- - -
-
-
-
-
-
-
- -
-
- - - ` -} - - -module.exports = { - sendExitRoomNotify, - sendCreateJoinRoomNotify, - sendManageUpdateFailedNotify, - sendManageUpdateInfoNotify, - sendManageLoginSuccessNotify, - sendManageLoginFailedNotify, - sendBugNotify, - sendStopScreenNotify, - sendStartScreenNotify, - sendStopScreenShareNotify, - sendStartScreenShareNotify, - sendStopVideoShareNotify, - sendStartVideoShareNotify, - sendTxtNotify, - sendCodeFileNotify, - sendFileDoneNotify, - sendFileInfoNotify, - sendChatingNotify, - sendOpenaiChatNotify, - getOrCreateManageRoom, - updateManageRoom, - exitRoom, - createJoinRoom, - dogData, - getRoomPageHtml, - getDataPageHtml, - getSettingPageHtml, - getDogChating10Info -} \ No newline at end of file diff --git a/src/socket/connect.js b/src/socket/connect.js new file mode 100644 index 0000000..f170d2f --- /dev/null +++ b/src/socket/connect.js @@ -0,0 +1,89 @@ +const rtcDisConnect = require("./rtcDisConnect/disconnect"); +const rtcOffer = require("./rtcOffer/offer"); +const rtcAnswer = require("./rtcAnswer/answer"); +const rtcCandidate = require("./rtcCandidate/candidate"); +const rtcCount = require("./rtcCount/count"); +const rtcExit = require("./rtcExit/exit"); +const rtcCommData = require("./rtcCommData/commData"); +const rtcCreateJoin = require("./rtcCreateJoin/createJoin"); +const rtcManageConfirm = require("./rtcManage/confirm"); +const rtcManageChange = require("./rtcManage/change"); +const rtcManageReload = require("./rtcManage/reload"); +const rtcMessage = require("./rtcMessage/message"); +const rtcChating = require("./rtcChating/chating"); +const rtcOpenai = require("./rtcOpenai/openai"); + +module.exports = (io, socket, tables, dbClient) => { + + rtcCount.count(io, socket, tables, dbClient, {}) + + // 断开连接 + socket.on('disconnect', (data)=>{ + rtcDisConnect.disconnect(io, socket, tables, dbClient, data) + }); + + // webrtc offer 消息 + socket.on('offer', (data) => { + rtcOffer.offer(io, socket, tables, dbClient, data) + }); + + // webrtc answer 消息 + socket.on('answer', (data) => { + rtcAnswer.answer(io, socket, tables, dbClient, data) + }); + + // webrtc candidate 消息 + socket.on('candidate', (data) => { + rtcCandidate.candidate(io, socket, tables, dbClient, data) + }); + + // 在线人数统计 + socket.on('count', (data) => { + rtcCount.count(io, socket, tables, dbClient, data) + }); + + // 退出房间 + socket.on('exit', (data) => { + rtcExit.exit(io, socket, tables, dbClient, data) + }); + + // 获取初始数据 + socket.on("getCommData", (data) => { + rtcCommData.commData(io, socket, tables, dbClient, data) + }) + + // 创建或加入房间 + socket.on('createAndJoin', (data) => { + rtcCreateJoin.createJoin(io, socket, tables, dbClient, data) + }); + + // 管理后台登陆验证 + socket.on('manageConfirm', (data) => { + rtcManageConfirm.confirm(io, socket, tables, dbClient, data) + }); + + // 管理后台修改数据 + socket.on('manageChange', (data) => { + rtcManageChange.change(io, socket, tables, dbClient, data) + }); + + // 管理后台刷新 + socket.on('manageReload', (data) => { + rtcManageReload.reload(io, socket, tables, dbClient, data) + }); + + // 公共消息 + socket.on('message', (data) => { + rtcMessage.message(io, socket, tables, dbClient, data) + }); + + // 公共聊天频道 + socket.on('chatingComm', (data) => { + rtcChating.chating(io, socket, tables, dbClient, data) + }); + + // openai聊天 + socket.on('openai', (data) => { + rtcOpenai.openai(io, socket, tables, dbClient, data) + }); +} \ No newline at end of file diff --git a/src/socket/index.js b/src/socket/index.js index fd06cc5..434d946 100644 --- a/src/socket/index.js +++ b/src/socket/index.js @@ -1,745 +1,20 @@ -const template = require("./template"); -const seafile = require('../oss/seafile') -const openai = require("../openai/openai") -const SocketHandler = template.SocketHandler; -const { - router -} = require("../../conf/cfg.json"); -const manageConfig = router.manage; -const { - genRoom, - formateDateTime -} = require("../../utils/utils"); -const { - sendExitRoomNotify, - sendCreateJoinRoomNotify, - sendManageUpdateFailedNotify, - sendManageUpdateInfoNotify, - sendManageLoginSuccessNotify, - sendManageLoginFailedNotify, - sendBugNotify, - sendStopScreenNotify, - sendStartScreenNotify, - sendStopScreenShareNotify, - sendStartScreenShareNotify, - sendStopVideoShareNotify, - sendStartVideoShareNotify, - sendTxtNotify, - sendFileDoneNotify, - sendFileInfoNotify, - sendChatingNotify, - sendCodeFileNotify, - sendOpenaiChatNotify, - updateManageRoom, - exitRoom, - createJoinRoom, - dogData, - getOrCreateManageRoom, - getRoomPageHtml, - getDataPageHtml, - getSettingPageHtml, - getDogChating10Info -} = require("./bussiness"); - -// 数据表 -let tables = {}; -// 登陆token数组 -let tokens = []; -// sql操作 -let sql = {}; -// 公共聊天数据 -let chatingComm = [] -// 开关数据 -let cacheSwitchData = {}; -// 通知事件定义 -let opName = { - "sendFileInfo": "准备发送文件", - "sendDone": "文件发送完毕", - "sendBugs": "收到问题反馈", - "sendTxt": "发送文本内容", - "startScreen": "开始网页录屏", - "stopScreen": "停止网页录屏", - "startScreenShare": "开始屏幕共享", - "stopScreenShare": "停止屏幕共享", - "startVideoShare": "开始音视频通话", - "stopVideoShare": "停止音视频通话", - "startPasswordRoom": "创建密码房间", - "addCodeFile": "添加取货码文件", - "getCodeFile" : "取件码取件", - "openaiChat" : "ChatGPT聊天" -} +const connect = require("./connect"); /** * 执行器 - * @param {*} tabs - * @param {*} config + * @param {*} tables + * @param {*} dbClient + * @param {*} io + * @returns */ -async function excute(tabs, sequelize, config) { - tables = tabs; - sql = sequelize; - - let io = config.ws.io; +async function excute(tables, dbClient, io) { if (io === undefined || io === null) { console.error("init socket error "); return; } - //前置执行函数 - if (config.ws.beforeInit) { - config.ws.beforeInit(); - } - - chatingComm = await getDogChating10Info({ - tables: tables, - sql: sql, - }); - - listen(io); - - //后置执行函数 - if (config.ws.afterInit) { - config.ws.afterInit(); - } -} - - -/** - * 监听函数 - * @param {*} io - */ -function listen(io) { io.sockets.on('connection', function (socket) { - - var handler = new SocketHandler(io.sockets, socket); - - // 通知一下在线人数 - handler._count() - - // 断开连接 - socket.on('disconnect', async function (reason) { - handler._disconnect({}); - await exitRoom({ sid: socket.id, tables: tables }) - }); - - // ice - socket.on('offer', function (message) { - handler._offer(message, {}) - }); - - // ice - socket.on('answer', function (message) { - handler._answer(message, {}) - }); - - // ice - socket.on('candidate', function (message) { - handler._candidate(message, {}) - }); - - // 在线人数统计 - socket.on('count', function (message) { - handler._count(message, {}) - }); - - // 退出 - socket.on('exit', async function (message) { - try { - let recoderId = message.recoderId; - handler._exit(message, {}) - - if (recoderId != undefined) { - await exitRoom({ sid: socket.id, tables: tables }) - - let handshake = socket.handshake - let userAgent = handshake.headers['user-agent'].toString().substr(0, 255); - let ip = handshake.headers['x-real-ip'] || handshake.headers['x-forwarded-for'] || handshake.headers['host']; - - sendExitRoomNotify({ - title: "退出房间", - room: message.room, - socketId: socket.id, - recoderId: message.recoderId, - userAgent: userAgent, - ip: ip - }) - } - } catch (e) { - handler._message({ - room: message.room, - emitType: "tips", - to: socket.id, - msg: "系统错误" - }, {}) - } - }); - - - socket.on("getCommData", async function () { - let handshake = socket.handshake - let userAgent = handshake.headers['user-agent'].toString().substr(0, 255); - let ip = handshake.headers['x-real-ip'] || handshake.headers['x-forwarded-for'] || handshake.headers['host']; - - let manageInfo = await getOrCreateManageRoom({ - tables: tables, - sid: socket.id, - ip: ip, - device: userAgent, - content: "" - }) - let switchData = JSON.parse(manageInfo.content) - if (switchData && switchData.keys) { - delete switchData.keys; - } - if(switchData){ - cacheSwitchData = switchData - } - handler._message({ - emitType: "commData", - switchData: switchData, - chatingCommData: chatingComm, - }) - }) - - // 创建或加入房间 - socket.on('createAndJoin', async function (message) { - try { - let room = message.room; - let type = message.type; - if (room && room.length > 15) { - room = room.toString().substr(0, 14); - } - let password = message.password || ''; - if (password && password.length > 6) { - password = password.toString().substr(0, 6); - } - - let handshake = socket.handshake - let userAgent = handshake.headers['user-agent'].toString().substr(0, 255); - let ip = handshake.headers['x-real-ip'] || handshake.headers['x-forwarded-for'] || handshake.headers['host']; - - let recoderId = 0; - if (manageConfig.room !== room) { - recoderId = await createJoinRoom({ - socketId: socket.id, - roomName: room, - ip: ip, - device: userAgent, - content: { handshake: handshake }, - password: password, - tables: tables - }); - } - - handler._createAndJoin(message, { - created: { recoderId: recoderId }, - joined: { recoderId: recoderId } - }) - - // 管理员房间 - if (room === manageConfig.room) { - message.socketId = socket.id; - socket.emit("manageCheck", message) - } - - sendCreateJoinRoomNotify({ - title: recoderId === 0 ? "创建/加入后台管理房间" : "创建/加入房间", - password: password, - socketId: socket.id, - room: room, - recoderId: recoderId, - userAgent: userAgent, - ip: ip, - }) - - io.sockets.adapter.rooms[room].createTime = formateDateTime(new Date(), "yyyy-MM-dd hh:mm:ss") - } catch (e) { - console.log(e) - handler._message({ - room: message.room, - emitType: "tips", - to: socket.id, - msg: "系统错误" - }, {}) - } - }); - - // 管理后台登陆验证 - socket.on('manageConfirm', async function (message) { - try { - let handshake = socket.handshake - let userAgent = handshake.headers['user-agent'].toString().substr(0, 255); - let ip = handshake.headers['x-real-ip'] || handshake.headers['x-forwarded-for'] || handshake.headers['host']; - - if (message.value !== manageConfig.password || message.room !== manageConfig.room) { - handler._message({ - room: message.room, - emitType: "tips", - to: socket.id, - msg: "秘钥不正确" - }, {}) - sendManageLoginFailedNotify({ - title: "管理后台登录失败", - room: message.room, - value: message.value, - userAgent: userAgent, - ip: ip - }) - return - } - - let token = genRoom(); - tokens.push(token) - - sendManageLoginSuccessNotify({ - title: "管理后台登录成功", - token: token, - room: message.room, - value: message.value, - userAgent: userAgent, - ip: ip - }) - - socket.emit("manage", { - token: token, - socketId: socket.id, - title: manageConfig.title, - content: [{ - title: "房间频道", - html: await getRoomPageHtml({ - tables: tables, - sql: sql, - sockets: io.sockets, - day: formateDateTime(new Date(), "yyyy-MM-dd"), - }) - }, { - title: "数据传输", - html: await getDataPageHtml({ - tables: tables, - sql: sql, - sockets: io.sockets, - day: formateDateTime(new Date(), "yyyy-MM-dd"), - }) - }, { - title: "其他设置", - html: await getSettingPageHtml({ - tables: tables, - rname: message.room, - sid: socket.socketId, - ip: ip, - device: userAgent - }) - }] - }) - } catch (e) { - console.log(e) - handler._message({ - room: message.room, - emitType: "tips", - to: socket.id, - msg: "系统错误" - }, {}) - } - }); - - // 管理后台修改数据 - socket.on('manageChange', async function (message) { - try { - let handshake = socket.handshake - let userAgent = handshake.headers['user-agent'].toString().substr(0, 255); - let ip = handshake.headers['x-real-ip'] || handshake.headers['x-forwarded-for'] || handshake.headers['host']; - - if (!message.token || !tokens.includes(message.token)) { - sendManageUpdateFailedNotify({ - title: "管理后台非法修改配置", - token: message.token, - room: message.room, - content: message.content, - userAgent: userAgent, - ip: ip - }) - return - } - - await updateManageRoom({ - tables: tables, - id: message.id, - content: JSON.stringify(message.content) - }) - - sendManageUpdateInfoNotify({ - title: "管理后台修改配置", - token: message.token, - room: message.room, - content: JSON.stringify(message.content), - userAgent: userAgent, - ip: ip - }) - - handler._message({ - room: message.room, - emitType: "tips", - to: socket.id, - msg: "更新成功" - }, {}) - - //通知下全频道 - let switchData = message.content - if (switchData && switchData.keys) { - delete switchData.keys; - } - if(switchData){ - cacheSwitchData = switchData - } - handler._commDataChange({ - switchData: switchData, - }) - - } catch (e) { - console.log(e) - handler._message({ - room: message.room, - emitType: "tips", - to: socket.id, - msg: "系统错误" - }, {}) - } - }); - - // 管理后台刷新 - socket.on('manageReload', async function (message) { - try { - let handshake = socket.handshake - let userAgent = handshake.headers['user-agent'].toString().substr(0, 255); - let ip = handshake.headers['x-real-ip'] || handshake.headers['x-forwarded-for'] || handshake.headers['host']; - - if (!message.token || !tokens.includes(message.token)) { - sendManageUpdateFailedNotify({ - title: "管理后台非法刷新数据", - token: message.token, - room: message.room, - content: message.content, - userAgent: userAgent, - ip: ip - }) - return - } - - socket.emit("manage", { - token: message.token, - socketId: socket.id, - title: manageConfig.title, - content: [{ - title: "房间频道", - html: await getRoomPageHtml({ - tables: tables, - sql: sql, - sockets: io.sockets, - day: message.content, - }) - }, { - title: "数据传输", - html: await getDataPageHtml({ - tables: tables, - sql: sql, - sockets: io.sockets, - day: message.content, - }) - }, { - title: "其他设置", - html: await getSettingPageHtml({ - tables: tables, - rname: message.room, - sid: socket.socketId, - ip: ip, - device: userAgent - }) - }] - }) - - sendManageUpdateInfoNotify({ - title: "管理后台重新获取配置", - token: message.token, - room: message.room, - content: JSON.stringify(message.content), - userAgent: userAgent, - ip: ip - }) - - } catch (e) { - console.log(e) - handler._message({ - room: message.room, - emitType: "tips", - to: socket.id, - msg: "系统错误" - }, {}) - } - }); - - // 公共消息 - socket.on('message', async function (message) { - try { - let emitType = message.emitType; - let handshake = socket.handshake - let userAgent = handshake.headers['user-agent'].toString().substr(0, 255); - let ip = handshake.headers['x-real-ip'] || handshake.headers['x-forwarded-for'] || handshake.headers['host']; - - handler._message(message, {}) - - if (emitType === 'sendFileInfo') { - sendFileInfoNotify({ - title: opName.sendFileInfo, - room: message.room, - recoderId: message.recoderId, - from: message.from, - name: message.name, - type: message.type, - size: message.size, - userAgent: userAgent, - ip: ip - }) - } - - if (emitType === 'sendDone') { - sendFileDoneNotify({ - title: opName.sendDone, - room: message.room, - to: message.to, - from: message.from, - name: message.name, - type: message.type, - size: message.size, - userAgent: userAgent, - ip: ip - }) - } - - if (emitType === 'sendBugs') { - sendBugNotify({ - title: opName.sendBugs, - room: message.room, - msg: message.msg, - userAgent: userAgent, - ip: ip - }) - } - - if (emitType === 'sendTxt') { - sendTxtNotify({ - title: opName.sendTxt, - room: message.room, - from: message.from, - recoderId: message.recoderId, - content: message.content, - userAgent: userAgent, - ip: ip - }) - } - - if (emitType === 'startScreen') { - sendStartScreenNotify({ - title: opName.startScreen, - userAgent: userAgent, - ip: ip - }) - } - - if (emitType === 'stopScreen') { - sendStopScreenNotify({ - title: opName.stopScreen, - userAgent: message.userAgent, - cost: message.cost, - size: message.size, - userAgent: userAgent, - ip: ip - }) - } - - if (emitType === 'startScreenShare') { - sendStartScreenShareNotify({ - title: opName.startScreenShare, - userAgent: userAgent, - ip: ip, - room: message.room - }) - } - - if (emitType === 'stopScreenShare') { - sendStopScreenShareNotify({ - title: opName.stopScreenShare, - userAgent: message.userAgent, - cost: message.cost, - userAgent: userAgent, - ip: ip, - room: message.room - }) - } - - if (emitType === 'startVideoShare') { - sendStartVideoShareNotify({ - title: opName.startVideoShare, - userAgent: userAgent, - ip: ip, - room: message.room - }) - } - - if (emitType === 'stopVideoShare') { - sendStopVideoShareNotify({ - title: opName.stopVideoShare, - userAgent: message.userAgent, - cost: message.cost, - userAgent: userAgent, - ip: ip, - room: message.room - }) - } - - if (opName[message.emitType]) { - await dogData({ - tables: tables, - name: opName[message.emitType], - roomId: "", - socketId: "", - device: userAgent, - flag: 0, - content: JSON.stringify(message), - handshake: JSON.stringify(handshake), - ip: ip - }); - } - } catch (e) { - console.log(e) - handler._message({ - room: message.room, - emitType: "tips", - to: socket.id, - msg: "系统错误" - }, {}) - } - }); - - // 公共聊天频道 - socket.on('chatingComm', async function (message) { - try { - if(!cacheSwitchData.openCommRoom){ - handler._message({ - room: message.room, - emitType: "tips", - to: socket.id, - msg: "当前功能已暂时关闭,有问题可以加群交流" - }, {}) - return - } - message.time = new Date().toLocaleString() - - if (chatingComm.length < 10) { - chatingComm.push(message) - } else { - chatingComm.shift() - chatingComm.push(message) - } - - handler._chatingComm(message, {}) - - let handshake = socket.handshake - let userAgent = handshake.headers['user-agent'].toString().substr(0, 255); - let ip = handshake.headers['x-real-ip'] || handshake.headers['x-forwarded-for'] || handshake.headers['host']; - - let recoderId = await dogData({ - name: "公共聊天室", - tables: tables, - roomId: message.room, - socketId: message.socketId, - device: userAgent, - flag: 0, - content: message.msg, - handshake: JSON.stringify(handshake), - ip: ip - }); - - sendChatingNotify({ - title: '公共聊天频道', - room: message.room, - recoderId: recoderId, - msgRecoderId: recoderId, - socketId: message.socketId, - msg: message.msg, - userAgent: userAgent, - ip: ip - }) - } catch (e) { - console.log(e) - handler._message({ - room: message.room, - emitType: "tips", - to: socket.id, - msg: "系统错误" - }, {}) - } - }); - - // openai聊天 - socket.on('openai', async function (message) { - if(!cacheSwitchData.openAiChat){ - handler._message({ - room: message.room, - emitType: "tips", - to: socket.id, - msg: "当前功能已暂时关闭,有问题可以加群交流" - }, {}) - return - } - - let roomId = message.room || 10086; - let content = message.content; - let value = message.value; - if(value){ - value = value.substr(0, 1000) - } - - // 有上下文,结合上下文 - if(content){ - content = content.substr(0, 5000); - content += "。\n,假定以上的内容是提问者和你的对话,结合上面的语境回答下面的问题,并且注意如果提问内容或者你的回答内容涉及政治,请回复 “我已经被设置关闭涉政问答功能”。提问内容为 : "+value; - }else{ - // 没有就默认 - content = value; - } - - let handshake = socket.handshake - let userAgent = handshake.headers['user-agent'].toString().substr(0, 255); - let ip = handshake.headers['x-real-ip'] || handshake.headers['x-forwarded-for'] || handshake.headers['host']; - - message.time = new Date().toLocaleString() - message.type = "openai"; - message.content = await openai.openaiChat(content, roomId); - message.value = ""; - handler._openaiChat(message) - - await dogData({ - name: opName.openaiChat, - tables: tables, - roomId: roomId, - socketId: message.socketId, - device: userAgent, - flag: 0, - content: decodeURIComponent(message.content), - handshake: JSON.stringify(handshake), - ip: ip - }); - - sendOpenaiChatNotify({ - title: opName.openaiChat, - room: roomId, - keys : JSON.stringify(openai.apiKeysStatus()), - content: content, - answer : message.content, - userAgent: userAgent, - ip: ip - }) - }); - + connect(io, socket, tables, dbClient) }); } diff --git a/src/socket/rtcAnswer/answer.js b/src/socket/rtcAnswer/answer.js new file mode 100644 index 0000000..ac5341d --- /dev/null +++ b/src/socket/rtcAnswer/answer.js @@ -0,0 +1,21 @@ +/** + * webrtc answer + * 转发answer消息至room其他客户端 [from,to,room,sdp] + * @param {*} io socketio对象 + * @param {*} socket 单个socket连接 + * @param {*} tables 数据表对象 + * @param {*} dbClient sequelize-orm对象 + * @param {*} data event参数 + * @returns + */ +async function answer(io, socket, tables, dbClient, data){ + let otherClient = io.sockets.connected[data.to]; + if (!otherClient) { + return; + } + otherClient.emit('answer', data); +} + +module.exports = { + answer +} \ No newline at end of file diff --git a/src/socket/rtcCandidate/candidate.js b/src/socket/rtcCandidate/candidate.js new file mode 100644 index 0000000..bc045df --- /dev/null +++ b/src/socket/rtcCandidate/candidate.js @@ -0,0 +1,21 @@ +/** + * webrtc candidate + * 转发candidate消息至room其他客户端 [from,to,room,candidate[sdpMid,sdpMLineIndex,sdp]] + * @param {*} io socketio对象 + * @param {*} socket 单个socket连接 + * @param {*} tables 数据表对象 + * @param {*} dbClient sequelize-orm对象 + * @param {*} data event参数 + * @returns + */ +async function candidate(io, socket, tables, dbClient, data){ + let otherClient = io.sockets.connected[data.to]; + if (!otherClient){ + return; + } + otherClient.emit('candidate', data); +} + +module.exports = { + candidate +} \ No newline at end of file diff --git a/src/socket/rtcChating/chating.js b/src/socket/rtcChating/chating.js new file mode 100644 index 0000000..e4a1989 --- /dev/null +++ b/src/socket/rtcChating/chating.js @@ -0,0 +1,80 @@ +const bussinessDog = require("./../../bussiness/dog/dog") +const rtcMessage = require("./../rtcMessage/message"); +const bussinessNotify = require("./../../bussiness/notify/notifySocketEvent") +const rtcCommData = require("./../rtcCommData/commData"); +const utils = require("./../../utils/utils"); + +/** + * 公共聊天频道 + * @param {*} io socketio对象 + * @param {*} socket 单个socket连接 + * @param {*} tables 数据表对象 + * @param {*} dbClient sequelize-orm对象 + * @param {*} data event参数 + * @returns + */ +async function chating(io, socket, tables, dbClient, data){ + try { + let cacheSwitchData = rtcCommData.getCacheSwitchData() + let chatingComm = rtcCommData.getChatingComm() + + if(!cacheSwitchData.openCommRoom){ + rtcMessage.message(io, socket, tables, dbClient, { + room: data.room, + emitType: "tips", + to: socket.id, + msg: "当前功能已暂时关闭,有问题可以加群交流" + }) + return + } + + data.time = new Date().toLocaleString() + + if (chatingComm.length < 10) { + chatingComm.push(data) + } else { + chatingComm.shift() + chatingComm.push(data) + } + rtcCommData.setChatingComm(chatingComm); + + io.sockets.emit("chatingComm", data); + + let {handshake, userAgent, ip} = utils.getSocketClientInfo(socket); + + let recoderId = await bussinessDog.dogData({ + name: "公共聊天室", + tables: tables, + roomId: data.room, + socketId: data.socketId, + device: userAgent, + flag: 0, + content: decodeURIComponent(data.msg), + handshake: JSON.stringify(handshake), + ip: ip + }); + + bussinessNotify.sendChatingNotify({ + title: '公共聊天频道', + room: data.room, + recoderId: recoderId, + msgRecoderId: recoderId, + socketId: data.socketId, + msg: decodeURIComponent(data.msg), + userAgent: userAgent, + ip: ip + }) + } catch (e) { + console.log(e) + rtcMessage.message(io, socket, tables, dbClient, { + room: data.room, + emitType: "tips", + to: socket.id, + msg: "系统错误" + }) + } +} + +module.exports = { + chating +} \ No newline at end of file diff --git a/src/socket/rtcCommData/commData.js b/src/socket/rtcCommData/commData.js new file mode 100644 index 0000000..9565dec --- /dev/null +++ b/src/socket/rtcCommData/commData.js @@ -0,0 +1,89 @@ +const bussinessRoom = require("./../../bussiness/room/room") +const bussinessDog = require("./../../bussiness/dog/dog") +const rtcMessage = require("./../rtcMessage/message"); +const utils = require("./../../utils/utils"); + +// 公共聊天数据缓存 +let chatingComm = null +// 开关数据缓存 +let cacheSwitchData = null + + +/** + * 获取初始数据 + * @param {*} io socketio对象 + * @param {*} socket 单个socket连接 + * @param {*} tables 数据表对象 + * @param {*} dbClient sequelize-orm对象 + * @param {*} data event参数 + * @returns + */ +async function commData(io, socket, tables, dbClient, data){ + let {handshake, userAgent, ip} = utils.getSocketClientInfo(socket); + + let manageInfo = await bussinessRoom.getOrCreateManageRoom({ + tables: tables, + sid: socket.id, + ip: ip, + device: userAgent, + content: "" + }) + let switchData = JSON.parse(manageInfo.content) + if(switchData){ + cacheSwitchData = switchData + } + + if(!chatingComm){ + chatingComm = await bussinessDog.getDogChating10Info({ + tables, dbClient + }); + } + + rtcMessage.message(io, socket, tables, dbClient, { + emitType: "commData", + switchData: switchData, + chatingCommData: chatingComm || [], + }) +} + +/** + * 获取 chatingComm + * @returns + */ +function getChatingComm(){ + return chatingComm; +} + +/** + * 更新 chatingComm + * @returns + */ +function setChatingComm(data){ + chatingComm = data + return chatingComm; +} + +/** + * 获取 cacheSwitchData + * @returns + */ +function getCacheSwitchData(){ + return cacheSwitchData; +} + +/** + * 更新 cacheSwitchData + * @param {*} data + */ +function setCacheSwitchData(data){ + cacheSwitchData = data + return cacheSwitchData; +} + +module.exports = { + commData, + getCacheSwitchData, + setCacheSwitchData, + getChatingComm, + setChatingComm +} \ No newline at end of file diff --git a/src/socket/rtcCount/count.js b/src/socket/rtcCount/count.js new file mode 100644 index 0000000..dc39d2e --- /dev/null +++ b/src/socket/rtcCount/count.js @@ -0,0 +1,23 @@ +/** + * 在线人数统计广播 + * @param {*} io socketio对象 + * @param {*} socket 单个socket连接 + * @param {*} tables 数据表对象 + * @param {*} dbClient sequelize-orm对象 + * @param {*} data event参数 + * @returns + */ +async function count(io, socket, tables, dbClient, data){ + try{ + let allManCount = Object.keys(io.sockets.connected).length || 0; + io.sockets.emit("count", { + mc : allManCount + }) + }catch(e){ + console.log(e) + } +} + +module.exports = { + count +} \ No newline at end of file diff --git a/src/socket/rtcCreateJoin/createJoin.js b/src/socket/rtcCreateJoin/createJoin.js new file mode 100644 index 0000000..2cf7dc9 --- /dev/null +++ b/src/socket/rtcCreateJoin/createJoin.js @@ -0,0 +1,241 @@ +const bussinessRoom = require("./../../bussiness/room/room") +const bussinessNotify = require("./../../bussiness/notify/notifySocketEvent") +const rtcCount = require("./../rtcCount/count"); +const rtcMessage = require("./../rtcMessage/message"); +const utils = require("./../../utils/utils"); +const cfg = require("./../../../conf/cfg.json") + + +/** + * 用户创建或加入房间 + * @param {*} io + * @param {*} socket + * @param {*} tables + * @param {*} dbClient + * @param {*} data + * @returns + */ +async function userCreateAndJoin(io, socket, tables, dbClient, data){ + let {handshake, userAgent, ip} = utils.getSocketClientInfo(socket); + + let {room, type, nickName, password = ''} = data; + + if (room && room.length > 15) { + room = room.toString().substr(0, 14); + } + + if(nickName && nickName.length > 20){ + nickName = nickName.substr(0, 20); + } + //设置昵称 + io.sockets.connected[socket.id].nickName = nickName; + + if(password && password.length > 6){ + password = password.toString().substr(0,6); + } + + let recoderId = await bussinessRoom.createJoinRoom({ + socketId: socket.id, + roomName: room, + ip: ip, + device: userAgent, + content: { handshake: handshake }, + password: password, + tables: tables + }); + + let clientsInRoom = io.sockets.adapter.rooms[room]; + let numClients = clientsInRoom ? Object.keys(clientsInRoom.sockets).length : 0; + + if (numClients === 0) { + socket.join(room); + + socket.emit('created', { + id: socket.id, + room: room, + nickName : nickName, + peers: [], + type: type, + recoderId : recoderId + }); + + //密码房间 首次创建设置密码 + if(type === 'password'){ + io.sockets.adapter.rooms[room].password = password + } + }else { + //流媒体房间只允许两个人同时在线 + if((type === 'screen' || type === 'video') && numClients >= 2){ + socket.emit("tips", { + room : data.room, + to : socket.id, + msg : "当前房间已满,请开启其他房间号发起操作" + }); + return + } + + // 密码房间,检查密码 + if(type === 'password'){ + let roomPassword = io.sockets.adapter.rooms[room].password; + if(roomPassword !== password){ + socket.emit("tips", { + room : data.room, + nickName : nickName, + to : socket.id, + msg : "密码错误", + reload : true + }); + return + } + } + + io.sockets.in(room).emit('joined',{ + id: socket.id, + room: room, + nickName : nickName, + type: type, + recoderId : recoderId + }); + + let peers = new Array(); + let otherSocketIds = Object.keys(clientsInRoom.sockets); + for (let i = 0; i < otherSocketIds.length; i++) { + let otherSocketId = otherSocketIds[i] + let peerNickName = io.sockets.connected[otherSocketId].nickName + peers.push({ + id: otherSocketId, + nickName: peerNickName + }); + } + + socket.join(room); + + socket.emit('created', { + id: socket.id, + room: room, + nickName : nickName, + peers: peers, + type: type, + recoderId : recoderId + }); + } + + rtcCount.count(io, socket, tables, dbClient, data); + + io.sockets.adapter.rooms[room].createTime = utils.formateDateTime(new Date(), "yyyy-MM-dd hh:mm:ss") + + bussinessNotify.sendCreateJoinRoomNotify({ + title: "用户创建/加入房间", + password: password, + socketId: socket.id, + room: room, + recoderId: recoderId, + userAgent: userAgent, + ip: ip, + }) +} + +/** + * 管理员创建或加入房间 + * @param {*} io + * @param {*} socket + * @param {*} tables + * @param {*} dbClient + * @param {*} data + * @returns + */ +async function manageCreateAndJoin(io, socket, tables, dbClient, data){ + let {handshake, userAgent, ip} = utils.getSocketClientInfo(socket); + + let {room, nickName} = data; + + if (room && room.length > 15) { + room = room.toString().substr(0, 14); + } + + if(nickName && nickName.length > 20){ + nickName = nickName.substr(0, 20); + } + //设置昵称 + io.sockets.connected[socket.id].nickName = nickName; + + let clientsInRoom = io.sockets.adapter.rooms[room]; + + let numClients = clientsInRoom ? Object.keys(clientsInRoom.sockets).length : 0; + + if(numClients > 0){ + socket.emit("tips", { + room : data.room, + to : socket.id, + msg : "管理员房间已被登录" + }); + return + } + + socket.join(room); + + socket.emit('created', { + id: socket.id, + room: room, + nickName : nickName, + peers: [], + recoderId : 0 + }); + io.sockets.adapter.rooms[room].createTime = utils.formateDateTime(new Date(), "yyyy-MM-dd hh:mm:ss") + + rtcCount.count(io, socket, tables, dbClient, data); + + // 下一步,让管理员输入密码 + data.socketId = socket.id; + + socket.emit("manageCheck", data) + + bussinessNotify.sendCreateJoinRoomNotify({ + title: "管理员创建/加入后台管理房间", + password: "", + socketId: socket.id, + room: room, + recoderId: 0, + userAgent: userAgent, + ip: ip, + }) +} + + +/** + * 创建或加入房间 + * @param {*} io socketio对象 + * @param {*} socket 单个socket连接 + * @param {*} tables 数据表对象 + * @param {*} dbClient sequelize-orm对象 + * @param {*} data event参数 + * @returns + */ +async function createJoin(io, socket, tables, dbClient, data){ + try { + let room = data.room; + if (room && room.length > 15) { + room = room.toString().substr(0, 14); + } + + if (cfg.router.manage.room !== room) { + // 用户加入/创建房间 + userCreateAndJoin(io, socket, tables, dbClient, data) + }else{ + //管理员是一个专属房间,不能创建记录 + manageCreateAndJoin(io, socket, tables, dbClient, data) + } + } catch (e) { + console.log(e) + rtcMessage.message(io, socket, tables, dbClient, { + room: data.room, + emitType: "tips", + to: socket.id, + msg: "系统错误" + }) + } +} + +module.exports = { + createJoin +} \ No newline at end of file diff --git a/src/socket/rtcDisConnect/disconnect.js b/src/socket/rtcDisConnect/disconnect.js new file mode 100644 index 0000000..c1c298c --- /dev/null +++ b/src/socket/rtcDisConnect/disconnect.js @@ -0,0 +1,24 @@ +const bussinessRoom = require("./../../bussiness/room/room") +const rtcCount = require("./../rtcCount/count"); + +/** + * 断开连接的操作 + * @param {*} io socketio对象 + * @param {*} socket 单个socket连接 + * @param {*} tables 数据表对象 + * @param {*} dbClient sequelize-orm对象 + * @param {*} data event参数 + */ +async function disconnect(io, socket, tables, dbClient, data){ + socket.broadcast.emit('exit', { + from : socket.id, + }); + + await bussinessRoom.exitRoom({ sid: socket.id, tables: tables }); + + rtcCount.count(io, socket, tables, dbClient, data) +} + +module.exports = { + disconnect +} \ No newline at end of file diff --git a/src/socket/rtcExit/exit.js b/src/socket/rtcExit/exit.js new file mode 100644 index 0000000..acfe8c0 --- /dev/null +++ b/src/socket/rtcExit/exit.js @@ -0,0 +1,60 @@ +const bussinessRoom = require("./../../bussiness/room/room") +const bussinessNotify = require("./../../bussiness/notify/notifySocketEvent") +const rtcCount = require("./../rtcCount/count"); +const rtcMessage = require("./../rtcMessage/message"); +const utils = require("./../../utils/utils"); + +/** + * 退出房间 + * @param {*} io socketio对象 + * @param {*} socket 单个socket连接 + * @param {*} tables 数据表对象 + * @param {*} dbClient sequelize-orm对象 + * @param {*} data event参数 + * @returns + */ +async function exit(io, socket, tables, dbClient, data){ + try { + let room = data.room; + + socket.leave(room); + + let clientsInRoom = io.sockets.adapter.rooms[room]; + if (clientsInRoom) { + let otherSocketIds = Object.keys(clientsInRoom.sockets); + for (let i = 0; i < otherSocketIds.length; i++) { + let otherSocket = io.sockets.connected[otherSocketIds[i]]; + otherSocket.emit('exit', data); + } + } + + let recoderId = data.recoderId; + if (recoderId != undefined) { + await bussinessRoom.exitRoom({ sid: socket.id, tables }) + + let {handshake, userAgent, ip} = utils.getSocketClientInfo(socket); + + bussinessNotify.sendExitRoomNotify({ + title: "退出房间", + room: data.room, + socketId: socket.id, + recoderId: data.recoderId, + userAgent: userAgent, + ip: ip + }) + } + + rtcCount.count(io, socket, tables, dbClient, data) + } catch (e) { + rtcMessage.message(io, socket, tables, dbClient, { + room: data.room, + emitType: "tips", + to: socket.id, + msg: "系统错误" + }) + } +} + +module.exports = { + exit +} \ No newline at end of file diff --git a/src/socket/rtcManage/change.js b/src/socket/rtcManage/change.js new file mode 100644 index 0000000..d0b51e8 --- /dev/null +++ b/src/socket/rtcManage/change.js @@ -0,0 +1,79 @@ +const bussinessNotify = require("./../../bussiness/notify/notifySocketEvent") +const bussinessRoom = require("./../../bussiness/room/room") +const rtcMessage = require("./../rtcMessage/message"); +const rtcConfirm = require("./confirm"); +const rtcCommData = require("./../rtcCommData/commData") +const utils = require("./../../utils/utils"); + +/** + * 管理后台修改数据 + * @param {*} io socketio对象 + * @param {*} socket 单个socket连接 + * @param {*} tables 数据表对象 + * @param {*} dbClient sequelize-orm对象 + * @param {*} data event参数 + * @returns + */ +async function change(io, socket, tables, dbClient, data){ + try { + let {handshake, userAgent, ip} = utils.getSocketClientInfo(socket); + + let tokens = rtcConfirm.getTokenList(); + + if (!data.token || !tokens.includes(data.token)) { + bussinessNotify.sendManageUpdateFailedNotify({ + title: "管理后台非法修改配置", + token: data.token, + room: data.room, + content: data.content, + userAgent: userAgent, + ip: ip + }) + return + } + + await bussinessRoom.updateManageRoom({ + tables: tables, + id: data.id, + content: JSON.stringify(data.content) + }) + + bussinessNotify.sendManageUpdateInfoNotify({ + title: "管理后台修改配置", + token: data.token, + room: data.room, + content: JSON.stringify(data.content), + userAgent: userAgent, + ip: ip + }) + + rtcMessage.message(io, socket, tables, dbClient, { + room: data.room, + emitType: "tips", + to: socket.id, + msg: "更新成功" + }) + + //更新下缓存,通知下全频道 + let switchData = data.content + if(switchData){ + rtcCommData.setCacheSwitchData(switchData) + } + io.sockets.emit("commData", { + switchData : switchData + }) + + } catch (e) { + console.log(e) + rtcMessage.message(io, socket, tables, dbClient, { + room: data.room, + emitType: "tips", + to: socket.id, + msg: "系统错误" + }) + } +} + +module.exports = { + change +} \ No newline at end of file diff --git a/src/socket/rtcManage/confirm.js b/src/socket/rtcManage/confirm.js new file mode 100644 index 0000000..8edd5b8 --- /dev/null +++ b/src/socket/rtcManage/confirm.js @@ -0,0 +1,105 @@ +const bussinessManageDataPage = require("./../../bussiness/manage/dataPage") +const bussinessManageRoomPage = require("./../../bussiness/manage/roomPage") +const bussinessManageSettingPage = require("./../../bussiness/manage/settingPage") +const bussinessNotify = require("./../../bussiness/notify/notifySocketEvent") +const rtcMessage = require("./../rtcMessage/message"); +const utils = require("./../../utils/utils"); +const cfg = require("./../../../conf/cfg.json") +const manageConfig = cfg.router.manage + +// 登陆token列表 +let tokens = []; + +/** + * 管理后台登陆验证密码 + * @param {*} io socketio对象 + * @param {*} socket 单个socket连接 + * @param {*} tables 数据表对象 + * @param {*} dbClient sequelize-orm对象 + * @param {*} data event参数 + * @returns + */ +async function confirm(io, socket, tables, dbClient, data){ + try { + let {handshake, userAgent, ip} = utils.getSocketClientInfo(socket); + + if (data.value !== manageConfig.password || data.room !== manageConfig.room) { + rtcMessage.message(io, socket, tables, dbClient, { + room: data.room, + emitType: "tips", + to: socket.id, + msg: "秘钥不正确" + }) + + bussinessNotify.sendManageLoginFailedNotify({ + title: "管理后台登录失败", + room: data.room, + value: data.value, + userAgent: userAgent, + ip: ip + }) + return + } + + let token = utils.genRoom(); + tokens.push(token) + + bussinessNotify.sendManageLoginSuccessNotify({ + title: "管理后台登录成功", + token: token, + room: data.room, + value: data.value, + userAgent: userAgent, + ip: ip + }) + + socket.emit("manage", { + token: token, + socketId: socket.id, + title: manageConfig.title, + content: [{ + title: "房间频道", + html: await bussinessManageRoomPage.getRoomPageHtml({ + tables: tables, + dbClient: dbClient, + sockets: io.sockets, + day: utils.formateDateTime(new Date(), "yyyy-MM-dd"), + }) + }, { + title: "数据传输", + html: await bussinessManageDataPage.getDataPageHtml({ + tables: tables, + dbClient: dbClient, + sockets: io.sockets, + day: utils.formateDateTime(new Date(), "yyyy-MM-dd"), + }) + }, { + title: "其他设置", + html: await bussinessManageSettingPage.getSettingPageHtml({ + tables: tables, + rname: data.room, + sid: socket.socketId, + ip: ip, + device: userAgent + }) + }] + }) + } catch (e) { + console.log(e) + rtcMessage.message(io, socket, tables, dbClient, { + room: data.room, + emitType: "tips", + to: socket.id, + msg: "系统错误" + }) + } +} + +function getTokenList(){ + return tokens; +} + +module.exports = { + confirm, + getTokenList +} \ No newline at end of file diff --git a/src/socket/rtcManage/reload.js b/src/socket/rtcManage/reload.js new file mode 100644 index 0000000..4524740 --- /dev/null +++ b/src/socket/rtcManage/reload.js @@ -0,0 +1,91 @@ +const bussinessManageDataPage = require("./../../bussiness/manage/dataPage") +const bussinessManageRoomPage = require("./../../bussiness/manage/roomPage") +const bussinessManageSettingPage = require("./../../bussiness/manage/settingPage") +const bussinessNotify = require("./../../bussiness/notify/notifySocketEvent") +const rtcMessage = require("./../rtcMessage/message"); +const rtcConfirm = require("./confirm"); +const utils = require("./../../utils/utils"); +const cfg = require("./../../../conf/cfg.json") +const manageConfig = cfg.router.manage + +/** + * 管理后台登陆验证 + * @param {*} io socketio对象 + * @param {*} socket 单个socket连接 + * @param {*} tables 数据表对象 + * @param {*} dbClient sequelize-orm对象 + * @param {*} data event参数 + * @returns + */ +async function reload(io, socket, tables, dbClient, data){ + try { + let {handshake, userAgent, ip} = utils.getSocketClientInfo(socket); + let tokens = rtcConfirm.getTokenList(); + + if (!data.token || !tokens.includes(data.token)) { + bussinessNotify.sendManageUpdateFailedNotify({ + title: "管理后台非法刷新数据", + token: data.token, + room: data.room, + content: data.content, + userAgent: userAgent, + ip: ip + }) + return + } + + socket.emit("manage", { + token: data.token, + socketId: socket.id, + title: manageConfig.title, + content: [{ + title: "房间频道", + html: await bussinessManageRoomPage.getRoomPageHtml({ + tables: tables, + dbClient: dbClient, + sockets: io.sockets, + day: data.content, + }) + }, { + title: "数据传输", + html: await bussinessManageDataPage.getDataPageHtml({ + tables: tables, + dbClient: dbClient, + sockets: io.sockets, + day: data.content, + }) + }, { + title: "其他设置", + html: await bussinessManageSettingPage.getSettingPageHtml({ + tables: tables, + rname: data.room, + sid: socket.id, + ip: ip, + device: userAgent + }) + }] + }) + + bussinessNotify.sendManageUpdateInfoNotify({ + title: "管理后台重新获取配置", + token: data.token, + room: data.room, + content: JSON.stringify(data.content), + userAgent: userAgent, + ip: ip + }) + + } catch (e) { + console.log(e) + rtcMessage.message(io, socket, tables, dbClient, { + room: data.room, + emitType: "tips", + to: socket.id, + msg: "系统错误" + }) + } +} + +module.exports = { + reload +} \ No newline at end of file diff --git a/src/socket/rtcMessage/message.js b/src/socket/rtcMessage/message.js new file mode 100644 index 0000000..bc43ea2 --- /dev/null +++ b/src/socket/rtcMessage/message.js @@ -0,0 +1,201 @@ +const bussinessDog = require("./../../bussiness/dog/dog") +const bussinessNotify = require("./../../bussiness/notify/notifySocketEvent") +const utils = require("./../../utils/utils"); + +// 通知事件定义 +let opName = { + "sendFileInfo": "准备发送文件", + "sendDone": "文件发送完毕", + "sendBugs": "收到问题反馈", + "sendTxt": "发送文本内容", + "startScreen": "开始网页录屏", + "stopScreen": "停止网页录屏", + "startScreenShare": "开始屏幕共享", + "stopScreenShare": "停止屏幕共享", + "startVideoShare": "开始音视频通话", + "stopVideoShare": "停止音视频通话", + "startPasswordRoom": "创建密码房间", + "addCodeFile": "添加取货码文件", + "getCodeFile" : "取件码取件", + "openaiChat" : "ChatGPT聊天" +} + + +/** + * 公共模板广播消息 + * @param {*} io socketio对象 + * @param {*} socket 单个socket连接 + * @param {*} tables 数据表对象 + * @param {*} dbClient sequelize-orm对象 + * @param {*} data event参数 + * @returns + */ +async function message(io, socket, tables, dbClient, data){ + try { + let {emitType, room, from, to} = data; + + let {handshake, userAgent, ip} = utils.getSocketClientInfo(socket); + + let clientsInRoom = io.sockets.adapter.rooms[room]; + + //特殊事件 + if(emitType === 'commData'){ + socket.emit('commData',{ + switchData : data.switchData, + chatingCommData : data.chatingCommData + }); + return + } + + if (clientsInRoom) { + let otherSocketIds = Object.keys(clientsInRoom.sockets); + for (let i = 0; i < otherSocketIds.length; i++) { + let otherSocket = io.sockets.connected[otherSocketIds[i]]; + + if(to && to === otherSocket.id){ //有指定发送id,不用走广播 + otherSocket.emit(emitType, data); + return; + } + if(from != otherSocket.id){ + otherSocket.emit(emitType, data); + } + } + } + + if (emitType === 'sendFileInfo') { + bussinessNotify.sendFileInfoNotify({ + title: opName.sendFileInfo, + room: data.room, + recoderId: data.recoderId, + from: data.from, + name: data.name, + type: data.type, + size: data.size, + userAgent: userAgent, + ip: ip + }) + } + + if (emitType === 'sendDone') { + bussinessNotify.sendFileDoneNotify({ + title: opName.sendDone, + room: data.room, + to: data.to, + from: data.from, + name: data.name, + type: data.type, + size: data.size, + userAgent: userAgent, + ip: ip + }) + } + + if (emitType === 'sendBugs') { + bussinessNotify.sendBugNotify({ + title: opName.sendBugs, + room: data.room, + msg: data.msg, + userAgent: userAgent, + ip: ip + }) + } + + if (emitType === 'sendTxt') { + bussinessNotify.sendTxtNotify({ + title: opName.sendTxt, + room: data.room, + from: data.from, + recoderId: data.recoderId, + content: data.content, + userAgent: userAgent, + ip: ip + }) + } + + if (emitType === 'startScreen') { + bussinessNotify.sendStartScreenNotify({ + title: opName.startScreen, + userAgent: userAgent, + ip: ip + }) + } + + if (emitType === 'stopScreen') { + bussinessNotify.sendStopScreenNotify({ + title: opName.stopScreen, + userAgent: data.userAgent, + cost: data.cost, + size: data.size, + userAgent: userAgent, + ip: ip + }) + } + + if (emitType === 'startScreenShare') { + bussinessNotify.sendStartScreenShareNotify({ + title: opName.startScreenShare, + userAgent: userAgent, + ip: ip, + room: data.room + }) + } + + if (emitType === 'stopScreenShare') { + bussinessNotify.sendStopScreenShareNotify({ + title: opName.stopScreenShare, + userAgent: data.userAgent, + cost: data.cost, + userAgent: userAgent, + ip: ip, + room: data.room + }) + } + + if (emitType === 'startVideoShare') { + bussinessNotify.sendStartVideoShareNotify({ + title: opName.startVideoShare, + userAgent: userAgent, + ip: ip, + room: data.room + }) + } + + if (emitType === 'stopVideoShare') { + bussinessNotify.sendStopVideoShareNotify({ + title: opName.stopVideoShare, + userAgent: data.userAgent, + cost: data.cost, + userAgent: userAgent, + ip: ip, + room: data.room + }) + } + + if (opName[data.emitType]) { + await bussinessDog.dogData({ + tables: tables, + name: opName[data.emitType], + roomId: "", + socketId: "", + device: userAgent, + flag: 0, + content: JSON.stringify(data), + handshake: JSON.stringify(handshake), + ip: ip + }); + } + + } catch (e) { + console.log(e) + socket.emit("tips", { + room: data.room, + emitType: "tips", + to: socket.id, + msg: "系统错误" + }); + } +} + +module.exports = { + message +} \ No newline at end of file diff --git a/src/socket/rtcOffer/offer.js b/src/socket/rtcOffer/offer.js new file mode 100644 index 0000000..43851d2 --- /dev/null +++ b/src/socket/rtcOffer/offer.js @@ -0,0 +1,21 @@ +/** + * webrtc offer + * 转发offer消息至room其他客户端 [from,to,room,sdp] + * @param {*} io socketio对象 + * @param {*} socket 单个socket连接 + * @param {*} tables 数据表对象 + * @param {*} dbClient sequelize-orm对象 + * @param {*} data event参数 + * @returns + */ +async function offer(io, socket, tables, dbClient, data){ + let otherClient = io.sockets.connected[data.to]; + if (!otherClient) { + return; + } + otherClient.emit('offer', data); +} + +module.exports = { + offer +} \ No newline at end of file diff --git a/src/socket/rtcOpenai/openai.js b/src/socket/rtcOpenai/openai.js new file mode 100644 index 0000000..6fd64dd --- /dev/null +++ b/src/socket/rtcOpenai/openai.js @@ -0,0 +1,82 @@ +const bussinessDog = require("./../../bussiness/dog/dog") +const rtcMessage = require("./../rtcMessage/message"); +const bussinessNotify = require("./../../bussiness/notify/notifySocketEvent") +const rtcCommData = require("./../rtcCommData/commData"); +const utils = require("./../../utils/utils"); +const bussinessOpenai = require("./../../bussiness/openai/openai") + +/** + * 公共聊天频道 + * @param {*} io socketio对象 + * @param {*} socket 单个socket连接 + * @param {*} tables 数据表对象 + * @param {*} dbClient sequelize-orm对象 + * @param {*} data event参数 + * @returns + */ +async function openai(io, socket, tables, dbClient, data){ + let cacheSwitchData = rtcCommData.getCacheSwitchData() + + if(!cacheSwitchData.openAiChat){ + rtcMessage.message(io, socket, tables, dbClient, { + room: data.room, + emitType: "tips", + to: socket.id, + msg: "当前功能已暂时关闭,有问题可以加群交流" + }) + return + } + + let {roomId = "10086", content, value} = data; + if(value){ + value = value.substr(0, 1000) + } + + // 有上下文,结合上下文 + if(content){ + content = content.substr(0, 5000); + content += "。\n,假定以上的内容是提问者和你的对话,结合上面的语境回答下面的问题,并且注意如果提问内容或者你的回答内容涉及政治,请回复 “我已经被设置关闭涉政问答功能”。提问内容为 : "+value; + }else{ + // 没有就默认 + content = value; + } + + let {handshake, userAgent, ip} = utils.getSocketClientInfo(socket); + + data.time = new Date().toLocaleString() + data.type = "openai"; + data.content = await bussinessOpenai.openaiChat(content, roomId); + data.value = ""; + + let to = socket.id; + let otherSocket = io.sockets.connected[to]; + if(otherSocket){ + otherSocket.emit("openaiAnswer", data); + } + + await bussinessDog.dogData({ + name: opName.openaiChat, + tables: tables, + roomId: roomId, + socketId: data.socketId, + device: userAgent, + flag: 0, + content: decodeURIComponent(data.content), + handshake: JSON.stringify(handshake), + ip: ip + }); + + bussinessNotify.sendOpenaiChatNotify({ + title: opName.openaiChat, + room: roomId, + keys : JSON.stringify(bussinessOpenai.apiKeysStatus()), + content: content, + answer : message.content, + userAgent: userAgent, + ip: ip + }) +} + +module.exports = { + openai +} \ No newline at end of file diff --git a/src/socket/template.js b/src/socket/template.js deleted file mode 100644 index 8158635..0000000 --- a/src/socket/template.js +++ /dev/null @@ -1,288 +0,0 @@ -/** - * 封装socket连接公共方法 - */ - class SocketHandler { - constructor(sockets,socket){ - this.sockets = sockets; - this.socket = socket; - } - - /** - * 后台修改配置广播 - * @param {*} message - * @param {*} params - */ - _commDataChange( message, params){ - try{ - this.sockets.emit("commData", message) - }catch(e){ - console.log(e) - } - } - - /** - * 公共聊天频道 - * @param {*} message - * @param {*} params - */ - _chatingComm( message, params){ - try{ - this.sockets.emit("chatingComm", message) - }catch(e){ - console.log(e) - } - } - - /** - * 数量 - * @param {*} message - * @param {*} params - */ - _count( message, params){ - try{ - let allManCount = Object.keys(this.sockets.connected).length || 0; - this.sockets.emit("count", { - mc : allManCount - }) - }catch(e){ - console.log(e) - } - } - - /** - * 链接断开 - * @param {*} message - * @param {*} params - */ - _disconnect(message, params){ - let socketId = this.socket.id; - let data = { - from : socketId, - }; - this.socket.broadcast.emit('exit',data); - this._count(message, params) - } - - /** - * 创建并加入 - * @param {*} message - * @param {*} params - */ - _createAndJoin(message, params){ - let room = message.room; - let type = message.type; - let nickName = message.nickName; - let password = message.password || ''; - - if(nickName && nickName.length > 20){ - nickName = nickName.substr(0, 20); - } - - if(password && password.length > 6){ - password = password.toString().substr(0,6); - } - - let clientsInRoom = this.sockets.adapter.rooms[room]; - let numClients = clientsInRoom ? Object.keys(clientsInRoom.sockets).length : 0; - - if (numClients === 0) { - this.socket.join(room); - let createdData = { - id: this.socket.id, - room: room, - nickName : nickName, - peers: [], - type: type - }; - Object.assign(createdData,params['created']) - this.socket.emit('created', createdData); - - // 首次创建-直接设置密码即可 - this.sockets.adapter.rooms[room].password = password - }else { - //流媒体房间只允许两个人同时在线 - if((type === 'screen' || type === 'video') && numClients >= 2){ - this.socket.emit("tips", { - room : message.room, - to : this.socket.id, - msg : "当前房间已满,请开启其他房间号发起操作" - }); - return - } - - // 密码不对 - let roomPassword = this.sockets.adapter.rooms[room].password; - if(roomPassword !== password){ - this.socket.emit("tips", { - room : message.room, - nickName : nickName, - to : this.socket.id, - msg : "密码错误", - reload : true - }); - return - } - - let joinedData = { - id: this.socket.id, - room: room, - nickName : nickName, - type: type - }; - Object.assign(joinedData,params['joined']) - this.sockets.in(room).emit('joined',joinedData); - - let peers = new Array(); - let otherSocketIds = Object.keys(clientsInRoom.sockets); - for (let i = 0; i < otherSocketIds.length; i++) { - let otherSocketId = otherSocketIds[i] - let peerNickName = this.sockets.connected[otherSocketId].nickName - peers.push({ - id: otherSocketId, - nickName: peerNickName - }); - } - - this.socket.join(room); - - let createdData = { - id: this.socket.id, - room: room, - nickName : nickName, - peers: peers, - type: type - }; - Object.assign(createdData,params['created']) - this.socket.emit('created', createdData); - } - this._count(message, params) - - this.sockets.connected[this.socket.id].nickName = nickName; - } - - - /** - * 【offer】转发offer消息至room其他客户端 [from,to,room,sdp] - * @param {*} message - * @param {*} params - */ - _offer(message, params){ - let otherClient = this.sockets.connected[message.to]; - if (!otherClient) { - return; - } - otherClient.emit('offer', message); - } - - - - /** - * 【answer】转发answer消息至room其他客户端 [from,to,room,sdp] - * @param {*} message - * @param {*} params - */ - _answer(message, params){ - let otherClient = this.sockets.connected[message.to]; - if (!otherClient) { - return; - } - otherClient.emit('answer', message); - } - - - /** - * 【candidate】转发candidate消息至room其他客户端 [from,to,room,candidate[sdpMid,sdpMLineIndex,sdp]] - * @param {*} message - * @param {*} params - */ - _candidate(message, params){ - let otherClient = this.sockets.connected[message.to]; - if (!otherClient){ - return; - } - otherClient.emit('candidate', message); - } - - - - /** - * 【exit】关闭连接转发exit消息至room其他客户端 [from,room] - * @param {*} message - * @param {*} params - */ - _exit(message, params){ - let room = message.room; - - this.socket.leave(room); - - let clientsInRoom = this.sockets.adapter.rooms[room]; - if (clientsInRoom) { - let otherSocketIds = Object.keys(clientsInRoom.sockets); - for (let i = 0; i < otherSocketIds.length; i++) { - let otherSocket = this.sockets.connected[otherSocketIds[i]]; - otherSocket.emit('exit', message); - } - } - this._count(message, params) - } - - - _message(message){ - let room = message.room; - let emitType = message.emitType; - let from = message.from; - let to = message.to; - let clientsInRoom = this.sockets.adapter.rooms[room]; - - //特殊事件 - if(emitType === 'commData'){ - this.socket.emit('commData',{ - switchData : message.switchData, - chatingCommData : message.chatingCommData - }); - return - } - - if (clientsInRoom) { - let otherSocketIds = Object.keys(clientsInRoom.sockets); - for (let i = 0; i < otherSocketIds.length; i++) { - let otherSocket = this.sockets.connected[otherSocketIds[i]]; - if(to && to === otherSocket.id){ //有指定发送id,不用走广播 - otherSocket.emit(emitType, message); - return; - } - if(from != otherSocket.id){ - otherSocket.emit(emitType, message); - } - } - } - } - - /** - * 取件码 - * @param {*} message - */ - _getCodeFile(message){ - let to = this.socket.id; - let otherSocket = this.sockets.connected[to]; - if(otherSocket){ - otherSocket.emit("getCodeFile", message); - } - } - - /** - * ai聊天 - * @param {*} message - */ - _openaiChat(message){ - let to = this.socket.id; - let otherSocket = this.sockets.connected[to]; - if(otherSocket){ - otherSocket.emit("openaiAnswer", message); - } - } -} - -module.exports = { - SocketHandler : SocketHandler -} \ No newline at end of file diff --git a/src/tables/db.js b/src/tables/db.js index 8e52d71..c3d0e45 100644 --- a/src/tables/db.js +++ b/src/tables/db.js @@ -1,83 +1,52 @@ -const Sql = require('sequelize'); +const sequelizeObj = require('sequelize'); const fs = require('fs'); -let sql = null; -let models = {}; +async function excute(config) { + let dbConf = config.db.mysql; -//export outside -function excute(config) { - init(config); + let dbClient = new sequelizeObj(dbConf.dbName, dbConf.user, dbConf.password, dbConf.other.sequelize); + + try { + let connect = await dbClient.authenticate(); + console.log('db connect ok ... '); + } catch (e) { + console.log('db connect err ...', e); + } - return { - tables : models, - sql : sql, - Sql : Sql - }; + let tables = {} + let files = fs.readdirSync(__dirname); + for (let f of files) { + if (f[0] == '.' || f == 'db.js') continue; + try { + let fn = require('./' + f); + if (typeof fn == 'function') { + let ms = fn(dbClient, sequelizeObj); + for (let k in ms) { + tables[k] = ms[k]; + } + } + } catch (e) { + console.log(e); + } + } + + try { + let res = await dbClient.sync({ + force: false + }); + console.log("db sync ok ..."); + } catch (e) { + console.log("db sync err : ",e); + } + + return { + tables, + dbClient, + }; } -//main -function init(config) { - if(config.db.beforeInit){ - config.db.beforeInit(); - } - - let dbConf = config.db.mysql; - sql = new Sql(dbConf.dbName, dbConf.user, dbConf.password, dbConf.other.sequelize); - connectDb(sql); - initTable(sql); - syncDb(sql); - - if(config.db.afterInit){ - config.db.afterInit(); - } -} - - -//connect to db -async function connectDb(sql){ - try { - let connect = await sql.authenticate(); - console.log('连接成功'); - } catch (e) { - console.log('连接失败'); - } -} - -//init table -async function initTable() { - let files = fs.readdirSync(__dirname); - for (let f of files) { - if (f[0] == '.' || f == 'db.js') continue; - try { - let fn = require('./' + f); - if (typeof fn == 'function') { - let ms = fn(sql, Sql); - for (let k in ms) { - models[k] = ms[k]; - } - } - } catch (e) { - console.log(e); - } - } -} - -// sync db tables -async function syncDb(sql) { - try{ - let res = sql.sync({ - force: false - }); - console.log("同步成功"); - }catch(e){ - console.log("同步失败"); - } -} - -//other init methods ... - module.exports = { - excute + excute } diff --git a/src/tables/dog.js b/src/tables/dog.js index 1b7aff9..96b30a6 100644 --- a/src/tables/dog.js +++ b/src/tables/dog.js @@ -1,51 +1,50 @@ // dog module.exports = (sequelize, DataTypes) => { - return { - Dog : sequelize.define('dog', { - id : { - type: DataTypes.INTEGER, - comment: '功能id', - primaryKey: true, - autoIncrement : true - }, - name : { - type: DataTypes.STRING(20), - comment: '操作功能名称' - }, - room_id : { - type: DataTypes.STRING(25), - comment: '房间号' - }, - socket_id : { - type: DataTypes.STRING(25), - comment: '连接id' - }, - device : { - type: DataTypes.STRING(256), - comment: '设备' - }, - flag : { - type: DataTypes.INTEGER, - comment: '标志位', - defaultValue: 0, - }, - content : { - type: DataTypes.TEXT, - comment: '详细信息' - }, - handshake : { - type: DataTypes.TEXT, - comment: '客户端信息' - } - },{ - timestamps: true, - comment: '功能记录表', - indexes: [{ - name: 'created_at_index', - method: 'BTREE', - fields: ['created_at'] - }] - }) - }; + return { + Dog: sequelize.define('dog', { + id: { + type: DataTypes.INTEGER, + comment: '功能id', + primaryKey: true, + autoIncrement: true + }, + name: { + type: DataTypes.STRING(20), + comment: '操作功能名称' + }, + room_id: { + type: DataTypes.STRING(25), + comment: '房间号' + }, + socket_id: { + type: DataTypes.STRING(25), + comment: '连接id' + }, + device: { + type: DataTypes.STRING(256), + comment: '设备' + }, + flag: { + type: DataTypes.INTEGER, + comment: '标志位', + defaultValue: 0, + }, + content: { + type: DataTypes.TEXT, + comment: '详细信息' + }, + handshake: { + type: DataTypes.TEXT, + comment: '客户端信息' + } + }, { + timestamps: true, + comment: '功能记录表', + indexes: [{ + name: 'created_at_index', + method: 'BTREE', + fields: ['created_at'] + }] + }) + }; } - \ No newline at end of file diff --git a/src/tables/room.js b/src/tables/room.js index 9d47083..988f504 100644 --- a/src/tables/room.js +++ b/src/tables/room.js @@ -1,77 +1,76 @@ // room module.exports = (sequelize, DataTypes) => { - return { - Room : sequelize.define('room', { - id : { - type: DataTypes.INTEGER, - primaryKey: true, - autoIncrement : true, - unique: true, - comment: '数据id', - }, - rcode : { - type: DataTypes.STRING(30), - comment: '房间随机编号' - }, - rname : { - type: DataTypes.STRING(32), - comment: '房间频道号码' - }, - uid : { - type: DataTypes.INTEGER, - comment: '属于的用户id, 待定~', - }, - uname : { - type: DataTypes.STRING(20), - comment: '姓名, 待定~' - }, - sid : { - type: DataTypes.STRING(30), - comment: '进入房间时的sessionId' - }, - pwd: { - type: DataTypes.STRING(6), - comment: '房间密码' - }, - status : { - type: DataTypes.INTEGER, - comment: '房间状态', - defaultValue : 0 - }, - ip : { - type: DataTypes.STRING(32), - comment: 'ip' - }, - device : { - type: DataTypes.STRING(256), - comment: '设备' - }, - localtion : { - type: DataTypes.STRING(256), - comment: '地理位置' - }, - url : { - type: DataTypes.STRING(256), - comment: '请求url' - }, - flag : { - type: DataTypes.INTEGER, - comment: '标志位', - defaultValue: 0, - }, - content : { - type: DataTypes.TEXT, - comment: '详细信息' - } - },{ - timestamps: true, - comment: '房间表', - indexes: [{ - name: 'created_at_index', - method: 'BTREE', - fields: ['created_at'] - }] - }) - }; + return { + Room: sequelize.define('room', { + id: { + type: DataTypes.INTEGER, + primaryKey: true, + autoIncrement: true, + unique: true, + comment: '数据id', + }, + rcode: { + type: DataTypes.STRING(30), + comment: '房间随机编号' + }, + rname: { + type: DataTypes.STRING(32), + comment: '房间频道号码' + }, + uid: { + type: DataTypes.INTEGER, + comment: '属于的用户id, 待定~', + }, + uname: { + type: DataTypes.STRING(20), + comment: '姓名, 待定~' + }, + sid: { + type: DataTypes.STRING(30), + comment: '进入房间时的sessionId' + }, + pwd: { + type: DataTypes.STRING(6), + comment: '房间密码' + }, + status: { + type: DataTypes.INTEGER, + comment: '房间状态', + defaultValue: 0 + }, + ip: { + type: DataTypes.STRING(32), + comment: 'ip' + }, + device: { + type: DataTypes.STRING(256), + comment: '设备' + }, + localtion: { + type: DataTypes.STRING(256), + comment: '地理位置' + }, + url: { + type: DataTypes.STRING(256), + comment: '请求url' + }, + flag: { + type: DataTypes.INTEGER, + comment: '标志位', + defaultValue: 0, + }, + content: { + type: DataTypes.TEXT, + comment: '详细信息' + } + }, { + timestamps: true, + comment: '房间表', + indexes: [{ + name: 'created_at_index', + method: 'BTREE', + fields: ['created_at'] + }] + }) + }; } - \ No newline at end of file diff --git a/src/utils/check/content.js b/src/utils/check/content.js new file mode 100644 index 0000000..ad000c7 --- /dev/null +++ b/src/utils/check/content.js @@ -0,0 +1,25 @@ +const checkLib = require("./core").trie + +function contentFilter(text, replaceChar = '*') { + let filteredText = ''; + let currentIndex = 0; + + while (currentIndex < text.length) { + const match = checkLib.search(text.slice(currentIndex)); + if (match) { + filteredText += replaceChar.repeat(match.length); + currentIndex += match.length; + } else { + filteredText += text[currentIndex]; + currentIndex++; + } + } + return filteredText; +} + +module.exports = { + contentFilter +} + + +console.log(contentFilter("草泥马,休息休息,傻逼")) diff --git a/src/utils/check/core.js b/src/utils/check/core.js new file mode 100644 index 0000000..681df43 --- /dev/null +++ b/src/utils/check/core.js @@ -0,0 +1,69 @@ +class TrieNode { + constructor() { + this.children = new Map(); + this.isEndOfWord = false; + } +} + +class Trie { + constructor() { + this.root = new TrieNode(); + } + + insert(word) { + let node = this.root; + for (const char of word) { + if (!node.children.has(char)) { + node.children.set(char, new TrieNode()); + } + node = node.children.get(char); + } + node.isEndOfWord = true; + } + + search(word) { + let node = this.root; + let match = ''; + for (const char of word) { + if (node.children.has(char)) { + match += char; + node = node.children.get(char); + if (node.isEndOfWord) { + return match; + } + } else { + break; + } + } + return null; + } +} + +// 敏感词库 +let sensitiveWords = require("./words") + +// 将敏感词库转换为前缀树 +let trie = null; +(function buildTrie(){ + if(!trie){ + trie = new Trie(); + for (const word of sensitiveWords) { + trie.insert(word); + } + let mem = process.memoryUsage(); + let format = function (bytes) { + return (bytes / 1024 / 1024).toFixed(4) + 'MB'; + }; + + console.log({ + heapTotal : format(mem.heapTotal), + heapUsed : format(mem.heapUsed), + rss : format(mem.rss) + }); + } +})() + + +module.exports = { + trie +} \ No newline at end of file diff --git a/src/utils/queue/queue.js b/src/utils/queue/queue.js new file mode 100644 index 0000000..828c8e2 --- /dev/null +++ b/src/utils/queue/queue.js @@ -0,0 +1,76 @@ +class tlQueue{ + constructor(options){ + let { + max = 500, // 最多处理任务数量 + queueThreshold = 2, // 1秒最多并发任务数量,超过进入队列 + queueRate = 1000, // 队列处理单个任务时间间隔, 单位ms + consumerFunc = ()=>{}, // 消费逻辑 Function + queue = [], // 任务队列 + count = 0, // 计数器 + queueId = -1, // 消费轮询器id + } = options; + + this.options = options; + this.max = max; + this.queueThreshold = queueThreshold; + this.queueRate = queueRate; + this.queue = queue; + this.count = count; + this.consumerFunc = consumerFunc; + this.queueId = queueId; + + this.init() + } + + /** + * 启动 + */ + init(){ + this.queueId = setInterval(() => { + if(this.queue.length > 0){ + this.consume(); + console.log("tl-queue pop : ",this.queue.length) + } + }, this.queueRate); + } + + /** + * 推数据 + * @param {*} data + */ + produce(data) { + //size <= queueThreshold, 允许一些并发执行 + if(this.queue.length <= this.queueThreshold){ + this.consume(); + return + } + // 队列满 + if(this.queue.length > this.max){ + console.warn("tl-queue out of size : ",this.queue.length) + return + } + + console.warn("tl-queue push : ",this.queue.length) + this.queue.push(data) + } + + /** + * 拉数据 + */ + consume(){ + this.consumerFunc(this.queue.shift(), this.options); + } + + /** + * 销毁任务 + */ + destroy() { + this.queue = []; + clearInterval(this.queueId); + } +} + + +module.exports = { + tlQueue +} \ No newline at end of file diff --git a/src/utils/utils.js b/src/utils/utils.js new file mode 100644 index 0000000..0cc86d5 --- /dev/null +++ b/src/utils/utils.js @@ -0,0 +1,141 @@ +const os = require('os'); + +/** + * 获取本机ip + * @returns + */ +function getLocalIP() { + const osType = os.type(); //系统类型 + const netInfo = os.networkInterfaces(); //网络信息 + let ip = ''; + if (osType === 'Windows_NT') { + for (let dev in netInfo) { + //win7的网络信息中显示为本地连接,win10显示为以太网 + if (dev === '本地连接' || dev === '以太网') { + for (let j = 0; j < netInfo[dev].length; j++) { + if (netInfo[dev][j].family === 'IPv4') { + ip = netInfo[dev][j].address; + break; + } + } + } + } + + } else if (osType === 'Linux') { + for (let dev in netInfo) { + let iface = netInfo[dev]; + for (let i = 0; i < iface.length; i++) { + let alias = iface[i]; + if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal) { + return alias.address; + } + } + } + } + + return ip; +} + +/** + * 获取请求的ip + * @param {*} request + * @returns + */ +function getClientIP(request) { + let ip = request.headers['x-forwarded-for'] || + request.ip || + request.connection.remoteAddress || + request.socket.remoteAddress || + request.connection.socket.remoteAddress; + + if (ip.split(',').length > 0) { + ip = ip.split(',')[0] + } + + ip = ip.substr(ip.lastIndexOf(':') + 1, ip.length); + return ip; +} + +/** + * 生成随机数 + * @param {*} req + * @returns + */ +function genFlow(req) { + return num = Math.floor(Math.random(100000000) * 100000000 + 1); +} + +/** + * 生成随机数 + * @param {*} req + * @returns + */ +function genRoom(req) { + return num = Math.floor(Math.random(100000000) * 100000000 + 1); +} + +/** + * 格式化时间 + * @param {*} time + * @param {*} format + * @returns + */ +function formateDateTime(time, format) { + let o = { + 'M+': time.getMonth() + 1, // 月份 + 'd+': time.getDate(), // 日 + 'h+': time.getHours(), // 小时 + 'm+': time.getMinutes(), // 分 + 's+': time.getSeconds(), // 秒 + 'q+': Math.floor((time.getMonth() + 3) / 3), // 季度 + S: time.getMilliseconds(), // 毫秒 + }; + if (/(y+)/.test(format)) { + format = format.replace(RegExp.$1, (time.getFullYear() + '').substring(4 - RegExp.$1.length)); + } + for (let k in o) { + if (new RegExp('(' + k + ')').test(format)) { + format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ('00' + o[k]).substring(('' + o[k]).length)); + } + } + return format; +} + +/** + * 获取当前时间下一天 + * @param {*} time + * @returns + */ +function getNextDay(time) { + let date = new Date(time); + date.setDate(date.getDate() + 1); + let y = date.getFullYear(); + let m = date.getMonth() < 9 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1; + let d = date.getDate() < 10 ? "0" + date.getDate() : date.getDate(); + return y + "-" + m + "-" + d; +} + +/** + * 获取socket中的请求客户端信息 + * @param {*} socket + * @returns + */ +function getSocketClientInfo(socket){ + let handshake = socket.handshake + let userAgent = handshake.headers['user-agent'].toString().substr(0, 255); + let ip = handshake.headers['x-real-ip'] || handshake.headers['x-forwarded-for'] || handshake.headers['host']; + + return { + handshake, userAgent, ip + } +} + +module.exports = { + getLocalIP, + getClientIP, + genFlow, + genRoom, + formateDateTime, + getNextDay, + getSocketClientInfo +} \ No newline at end of file diff --git a/src/worker/index.js b/src/utils/worker/index.js similarity index 100% rename from src/worker/index.js rename to src/utils/worker/index.js diff --git a/src/worker/worker.js b/src/utils/worker/worker.js similarity index 100% rename from src/worker/worker.js rename to src/utils/worker/worker.js