添加心跳包检测
This commit is contained in:
1 parent
127f58bd36
commit
ec5fcf1ee7
5 files changed
+149
-67
No files matched your search
+12
-4
@@ -61,12 +61,16 @@ const SM4 = (key,data)=>{
|
||||
return new Uint8Array(encryptedBytes);
|
||||
}
|
||||
const encrypt = (key, data, fileName, setting, message) => {
|
||||
// if(!encryptFunction){
|
||||
// encryptFunction = setting.activeFuction
|
||||
// }
|
||||
let encryptFunction = {0:'不加密',16:'AES128',19:'SM4',32:'AES256'}[key.length]
|
||||
return blob2Uint8Array(data).then(data => {
|
||||
let encrypted_data = null
|
||||
if(setting.activeFuction === "不加密"){
|
||||
if(encryptFunction === "不加密"|| key === ''){
|
||||
encrypted_data = data
|
||||
// AES
|
||||
}else if(setting.activeFuction != "SM4"){
|
||||
}else if(encryptFunction != "SM4"){
|
||||
message.info(`开始加密${fileName}`);
|
||||
encrypted_data = AES(key,data)
|
||||
message.info(`${fileName}加密完成`);
|
||||
@@ -83,11 +87,15 @@ const encrypt = (key, data, fileName, setting, message) => {
|
||||
|
||||
// setting为pinia存储的setting。。。
|
||||
const encryptString = (key, str, setting) => {
|
||||
// if(!encryptFunction){
|
||||
// encryptFunction = setting.activeFuction
|
||||
// }
|
||||
// console.log(key,key.length)
|
||||
if(setting.activeFuction === "不加密" || key === ''){
|
||||
let encryptFunction = {0:'不加密',16:'AES128',19:'SM4',32:'AES256'}[key.length]
|
||||
if(encryptFunction === "不加密" || key === ''){
|
||||
// return sm4.encrypt(str, 'miaospringmiaospringmiaospringao')
|
||||
return Base64.encode(str)
|
||||
}else if(setting.activeFuction !== "SM4"){
|
||||
}else if(encryptFunction !== "SM4"){
|
||||
key = aesjs.utils.utf8.toBytes(key);
|
||||
var data = aesjs.utils.utf8.toBytes(str);
|
||||
var aesCtr = new aesjs.ModeOfOperation.ctr(key, new aesjs.Counter(5));
|
||||
|
||||
+46
-14
@@ -38,6 +38,7 @@ import {NText,NDataTable,NButton,NInput} from 'naive-ui'
|
||||
import { useSettingStore } from "@/pinia/store";
|
||||
import { generateKey,encryptString } from '@/func/upload';
|
||||
import { decryptString } from '@/func/download';
|
||||
import WebsocketHeartbeatJs from 'websocket-heartbeat-js';
|
||||
|
||||
|
||||
|
||||
@@ -61,18 +62,26 @@ const codeInput = ref('')
|
||||
const key = ref('')
|
||||
const msg = ref('')
|
||||
const joinedGroup = []
|
||||
const uuid = ref('')
|
||||
|
||||
|
||||
const gID2key = {}
|
||||
const options = {
|
||||
url: baseUrl,
|
||||
pingTimeout: 20000,
|
||||
pongTimeout: 10000,
|
||||
reconnectTimeout: 3000,
|
||||
pingMsg: JSON.stringify({type:'heartbeat'})
|
||||
}
|
||||
let socket = new WebsocketHeartbeatJs(options);
|
||||
|
||||
// const socket = new WebSocket(baseUrl);
|
||||
// socket.addEventListener("open", (event) => {
|
||||
// // socket.send("");
|
||||
// });
|
||||
|
||||
|
||||
const socket = new WebSocket(baseUrl);
|
||||
socket.addEventListener("open", (event) => {
|
||||
// socket.send("");
|
||||
});
|
||||
|
||||
|
||||
socket.addEventListener("message", (event) => {
|
||||
socket.onmessage= (event) => {
|
||||
const msg = JSON.parse(event.data)
|
||||
data.value.push({from:'server',msg:event.data})
|
||||
if(msg.type){
|
||||
@@ -83,8 +92,8 @@ socket.addEventListener("message", (event) => {
|
||||
gID.value=msg.gID
|
||||
}else if(type1 === 'joinVerify'){
|
||||
let gID = msg.gID
|
||||
let key = gID2key[gID]
|
||||
console.log(gID2key,gID,key)
|
||||
let key = gID2key[gID].key
|
||||
// console.log(gID2key,gID,key)
|
||||
let res = decryptString(key,msg.verify)
|
||||
console.log(res,key,msg.verify)
|
||||
if(res === 'miaoSpring'){
|
||||
@@ -93,18 +102,40 @@ socket.addEventListener("message", (event) => {
|
||||
}
|
||||
}else if(type1 === 'history'){
|
||||
let gID = msg.gID
|
||||
let key = gID2key[gID]
|
||||
let key = gID2key[gID].key
|
||||
for(let m of JSON.parse(msg.history)){
|
||||
data.value.push({from:'decrypt',msg:decryptString(key,m.msg)})
|
||||
}
|
||||
}else if(type1 === 'uuid'){
|
||||
uuid.value = msg.uuid
|
||||
}
|
||||
}else if(type === 'gMsg'){
|
||||
let gID = msg.gID
|
||||
let key = gID2key[gID]
|
||||
let key = gID2key[gID].key
|
||||
data.value.push({from:'decrypt',msg:decryptString(key,msg.msg)})
|
||||
}
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
socket.onopen = ()=>{
|
||||
if(uuid.value){
|
||||
socket.send(JSON.stringify({type:'uuid',type1:'set',uuid:uuid.value}))
|
||||
}else{
|
||||
socket.send(JSON.stringify({type:'uuid',type1:'get'}))
|
||||
}
|
||||
console.log(joinedGroup)
|
||||
let toJoin = [...joinedGroup]
|
||||
joinedGroup.length = 0
|
||||
for(let gID of toJoin){
|
||||
let code = gID+gID2key[gID].key
|
||||
console.log(code)
|
||||
joinGroup(code)
|
||||
}
|
||||
}
|
||||
|
||||
socket.onreconnect = (e) => {
|
||||
console.log('reconnecting...');
|
||||
}
|
||||
|
||||
function createGroup(){
|
||||
let keyt = generateKey(setting)
|
||||
@@ -118,7 +149,8 @@ function joinGroup(code){
|
||||
if(joinedGroup.includes(gID)){
|
||||
return
|
||||
}
|
||||
gID2key[gID] = key
|
||||
gID2key[gID] = {}
|
||||
gID2key[gID].key = key
|
||||
joinGroup_Verify(gID)
|
||||
}
|
||||
|
||||
@@ -127,7 +159,7 @@ function joinGroup_Verify(gID){
|
||||
}
|
||||
|
||||
function sendMsg(gID,msg){
|
||||
let key = gID2key[gID]
|
||||
let key = gID2key[gID].key
|
||||
msg = encryptString(key,msg,setting)
|
||||
socket.send(JSON.stringify({type:'message',msg,gID}))
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user