add vite obfuscator

This commit is contained in:
landaiqing
2024-10-21 17:42:05 +08:00
parent 4aeb94063a
commit b114996e1c
12 changed files with 393 additions and 137 deletions

View File

@@ -11,7 +11,7 @@ export const generateClientId = () => {
},
cacheFor: {
mode: "restore",
expire: 1000 * 60 * 60 * 24 * 7
expire: 1000 * 60 * 60 * 24
}
}
);

View File

@@ -1,49 +1,49 @@
import {defineStore} from 'pinia';
import {ref} from "vue";
const expiresTime: number = 60 * 30; // 自定义过期时间 30 分钟
const expiredStorage: Storage = getExpiredStorage(localStorage, expiresTime);
function getExpiredStorage(storage: Storage, expiresTime: string | number | Date) {
return {
getItem(key: string) {
const itemStr = storage.getItem(key);
if (!itemStr) {
return null;
}
// 读取的时候是字符串,需要转换为对象
const item = JSON.parse(itemStr);
const now: Date = new Date();
if (now.getTime() > item.expiry) {
storage.removeItem(key);
return null;
}
// pinia 的持久化插件要求返回JSON字符串
return JSON.stringify(item.value);
},
setItem(key: string, value: string) {
const now = new Date();
const item = {
value: JSON.parse(value), // value是JSON字符串需要转换为对象
expiry: now.getTime() + Number(expiresTime) * 1000,
};
storage.setItem(key, JSON.stringify(item)); // 存储又要转为JSON字符串
},
removeItem(key: string) {
storage.removeItem(key);
},
clear() {
Object.keys(storage).forEach((key: string) => {
storage.removeItem(key);
});
},
key(index: number): string | null {
const keys = Object.keys(storage);
return keys[index] || null;
},
length: Object.keys(storage).length,
} as Storage;
}
// const expiresTime: number = 60 * 30; // 自定义过期时间 30 分钟
// const expiredStorage: Storage = getExpiredStorage(localStorage, expiresTime);
//
// function getExpiredStorage(storage: Storage, expiresTime: string | number | Date) {
// return {
// getItem(key: string) {
// const itemStr = storage.getItem(key);
// if (!itemStr) {
// return null;
// }
// // 读取的时候是字符串,需要转换为对象
// const item = JSON.parse(itemStr);
// const now: Date = new Date();
// if (now.getTime() > item.expiry) {
// storage.removeItem(key);
// return null;
// }
// // pinia 的持久化插件要求返回JSON字符串
// return JSON.stringify(item.value);
// },
// setItem(key: string, value: string) {
// const now = new Date();
// const item = {
// value: JSON.parse(value), // value是JSON字符串需要转换为对象
// expiry: now.getTime() + Number(expiresTime) * 1000,
// };
// storage.setItem(key, JSON.stringify(item)); // 存储又要转为JSON字符串
// },
// removeItem(key: string) {
// storage.removeItem(key);
// },
// clear() {
// Object.keys(storage).forEach((key: string) => {
// storage.removeItem(key);
// });
// },
// key(index: number): string | null {
// const keys = Object.keys(storage);
// return keys[index] || null;
// },
// length: Object.keys(storage).length,
// } as Storage;
// }
export const useClientStore = defineStore(
'clientId',
@@ -68,7 +68,7 @@ export const useClientStore = defineStore(
// 开启数据持久化
persist: {
key: 'clientId',
storage: expiredStorage,
storage: localStorage,
pick: ["clientId"],
}
}

View File

@@ -35,12 +35,18 @@ export const useWebSocketStore = defineStore('websocket', () => {
state.wsService?.close(isActiveClose);
}
// 新增的获取 WebSocket 状态的方法
function getReadyState() {
return state.wsService ? state.wsService.getReadyState() : WebSocket.CLOSED;
}
return {
initialize,
sendMessage,
on,
onEvent,
close,
getReadyState
};
}, {
persist: false,

View File

@@ -21,6 +21,7 @@ export class WebSocketService {
this.ws.addEventListener('message', this.handleMessage);
this.ws.addEventListener('error', this.handleError);
this.ws.addEventListener('close', this.handleClose);
}
public close(isActiveClose = false): void {
@@ -82,4 +83,8 @@ export class WebSocketService {
console.warn('尝试发送消息时WebSocket未连接');
}
}
public getReadyState(): number {
return this.ws ? this.ws.readyState : WebSocket.CLOSED;
}
}

View File

@@ -43,8 +43,7 @@ const client = useStore().client;
* Get the redirect url of Github OAuth
*/
async function getGithubRedirectUrl() {
const clientId: string = await getLocalClientId() as string;
const res: any = await getGithubUrl(clientId);
const res: any = await getGithubUrl(client.getClientId() as string);
if (res.code === 200 && res.data) {
githubRedirectUrl.value = res.data;
}
@@ -64,8 +63,7 @@ async function getGiteeRedirectUrl() {
* Get the redirect url of QQ OAuth
*/
async function getQQRedirectUrl() {
const clientId: string = await getLocalClientId() as string;
const res: any = await getQQUrl(clientId);
const res: any = await getQQUrl(client.getClientId() as string);
if (res.code === 200 && res.data) {
qqRedirectUrl.value = res.data;
}
@@ -81,18 +79,6 @@ async function getClientId() {
}
}
/**
* 获取本地client_id
*/
async function getLocalClientId() {
if (client.getClientId()) {
return client.getClientId();
} else {
await getClientId();
return client.getClientId();
}
}
/**
* Open the Github OAuth page in a new window with debounce
*/
@@ -223,9 +209,11 @@ function openQQUrl() {
}
onBeforeMount(() => {
getGithubRedirectUrl();
getGiteeRedirectUrl();
getQQRedirectUrl();
getClientId().then(() => {
getGithubRedirectUrl();
getGiteeRedirectUrl();
getQQRedirectUrl();
});
});
</script>
<style src="./index.scss" scoped>

View File

@@ -1,7 +1,6 @@
<template>
<div style="display: flex; flex-direction: column; align-items: center; justify-content: center;">
<Header/>
<CommentReply/>
</div>
@@ -16,7 +15,6 @@ import {SmileOutlined} from "@ant-design/icons-vue";
const websocket = useStore().websocket;
const userInfo = useStore().user;
const wsOptions = {
url: import.meta.env.VITE_MESSAGE_SOCKET_URL + "?user_id=" + userInfo.user.uid + "&token=" + userInfo.user.accessToken,
};
@@ -36,7 +34,6 @@ onMounted(() => {
onUnmounted(() => {
websocket.close(false);
});
</script>
<style scoped>

View File

@@ -87,11 +87,6 @@ async function getClientId() {
* 获取二维码
*/
async function getQrCode() {
if (!client.getClientId()) {
status.value = 'expired';
await getClientId();
await getQrCode();
} else {
const res: any = await generateQrCode(client.getClientId() || "");
if (res.code === 200 && res.data) {
status.value = 'active';
@@ -100,23 +95,11 @@ async function getQrCode() {
} else {
status.value = 'expired';
}
}
}
/**
* 获取本地client_id
*/
function getLocalClientId() {
if (client.getClientId()) {
return client.getClientId();
} else {
getClientId();
return client.getClientId();
}
}
const wsOptions = {
url: import.meta.env.VITE_QR_SOCKET_URL + "?client_id=" + getLocalClientId(),
url: import.meta.env.VITE_QR_SOCKET_URL + "?client_id=" + client.getClientId(),
};
@@ -146,7 +129,9 @@ async function handleListenMessage() {
}
onMounted(async () => {
await getQrCode();
getClientId().then(async () => {
await getQrCode();
});
});
</script>
<style src="./index.scss" scoped>

View File

@@ -44,8 +44,7 @@ const qqRedirectUrl = ref<string>('');
* Get the redirect url of Github OAuth
*/
async function getGithubRedirectUrl() {
const clientId: string = await getLocalClientId() as string;
const res: any = await getGithubUrl(clientId);
const res: any = await getGithubUrl(client.getClientId() as string);
if (res.code === 200 && res.data) {
githubRedirectUrl.value = res.data;
}
@@ -65,8 +64,7 @@ async function getGiteeRedirectUrl() {
* Get the redirect url of QQ OAuth
*/
async function getQQRedirectUrl() {
const clientId: string = await getLocalClientId() as string;
const res: any = await getQQUrl(clientId);
const res: any = await getQQUrl(client.getClientId() as string);
if (res.code === 200 && res.data) {
qqRedirectUrl.value = res.data;
}
@@ -82,18 +80,6 @@ async function getClientId() {
}
}
/**
* 获取本地client_id
*/
async function getLocalClientId() {
if (client.getClientId()) {
return client.getClientId();
} else {
await getClientId();
return client.getClientId();
}
}
/**
* Open the Github OAuth page in a new window with debounce
*/
@@ -222,9 +208,11 @@ function openQQUrl() {
}
onBeforeMount(() => {
getGithubRedirectUrl();
getGiteeRedirectUrl();
getQQRedirectUrl();
getClientId().then(() => {
getGithubRedirectUrl();
getGiteeRedirectUrl();
getQQRedirectUrl();
});
});
</script>
<style src="./index.scss" scoped>