diff --git a/server/app/controller/group.js b/server/app/controller/group.js index db81b6f..30c2f20 100644 --- a/server/app/controller/group.js +++ b/server/app/controller/group.js @@ -1,7 +1,10 @@ const { readGroupList, writeGroupList, readHostList, writeHostList, randomStr } = require('../utils') async function getGroupList({ res }) { - const data = await readGroupList() + let data = await readGroupList() + data = data.map(item => { + return { ...item, id: item._id } + }) data?.sort((a, b) => Number(b.index || 0) - Number(a.index || 0)) res.success({ data }) } diff --git a/server/app/controller/host.js b/server/app/controller/host.js index ba5a866..f318c44 100644 --- a/server/app/controller/host.js +++ b/server/app/controller/host.js @@ -5,9 +5,11 @@ async function getHostList({ res }) { let data = await readHostList() data?.sort((a, b) => Number(b.index || 0) - Number(a.index || 0)) data = data.map((item) => { - const isConfig = Boolean(item.username && item.port && (item[item.authType])) + const { username, port, authType, _id: id } = item + const isConfig = Boolean(username && port && (item[authType])) return { ...item, + id, isConfig, password: '', privateKey: '' diff --git a/server/app/socket/sftp-cache/.gitkeep b/server/app/socket/sftp-cache/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/web/src/main.js b/web/src/main.js index 0afde18..756645a 100644 --- a/web/src/main.js +++ b/web/src/main.js @@ -25,6 +25,7 @@ app.config.globalProperties.$store = useStore() const serviceURI = import.meta.env.DEV ? process.env.serviceURI : location.origin app.config.globalProperties.$serviceURI = serviceURI app.config.globalProperties.$clientPort = process.env.clientPort || 22022 +app.config.globalProperties.$store.$patch({ serviceURI }) console.warn('ISDEV: ', import.meta.env.DEV) console.warn('serviceURI: ', serviceURI) diff --git a/web/src/router/index.js b/web/src/router/index.js index b1c3e0c..54f8c2c 100644 --- a/web/src/router/index.js +++ b/web/src/router/index.js @@ -5,8 +5,6 @@ import { createRouter, createWebHistory } from 'vue-router' // import terminal from '@views/terminal/index.vue' // import test from '@views/test/index.vue' -const HostList = () => import('@views/list/index.vue') - const Login = () => import('@views/login/index.vue') const Container = () => import('@views/index.vue') const Server = () => import('@views/server/index.vue') diff --git a/web/src/store/index.js b/web/src/store/index.js index 5af481b..c9746e1 100644 --- a/web/src/store/index.js +++ b/web/src/store/index.js @@ -1,3 +1,4 @@ +import { io } from 'socket.io-client' import { defineStore, acceptHMRUpdate } from 'pinia' import $api from '@/api' import ping from '@/utils/ping' @@ -5,9 +6,11 @@ import ping from '@/utils/ping' const useStore = defineStore({ id: 'global', state: () => ({ + serviceURI: null, hostList: [], groupList: [], sshList: [], + HostStatusSocket: null, user: localStorage.getItem('user') || null, token: sessionStorage.getItem('token') || localStorage.getItem('token') || null, title: '' @@ -31,18 +34,15 @@ const useStore = defineStore({ this.$patch({ token: null }) }, async getMainData() { - const { data: groupList } = await $api.getGroupList() - const { data: hostList } = await $api.getHostList() - const { data: sshList } = await $api.getSSHList() - // console.log('hostList:', hostList) - // console.log('groupList:', groupList) - // console.log('sshList:', sshList) - this.$patch({ groupList, hostList, sshList }) + await this.getGroupList() + await this.getHostList() + await this.getSSHList() }, async getHostList() { const { data: hostList } = await $api.getHostList() // console.log('hostList:', hostList) this.$patch({ hostList }) + this.wsHostStatus() }, async getGroupList() { const { data: groupList } = await $api.getGroupList() @@ -67,11 +67,39 @@ const useStore = defineStore({ // console.warn('Please tick \'Preserve Log\'') }, 1500) }, - async sortHostList(list) { - let hostList = list.map(({ host }) => { - return this.hostList.find(item => item.host === host) + async wsHostStatus() { + if (this.HostStatusSocket) this.HostStatusSocket.close() + let socketInstance = io(this.serviceURI, { + path: '/clients', + forceNew: true, + reconnectionDelay: 5000, + reconnectionAttempts: 2 + }) + this.HostStatusSocket = socketInstance + socketInstance.on('connect', () => { + let flag = 5 + console.log('clients websocket 已连接: ', socketInstance.id) + let token = this.token + socketInstance.emit('init_clients_data', { token }) + socketInstance.on('clients_data', (data) => { + if ((flag++ % 5) === 0) this.getHostPing() + this.hostList.forEach(item => { + const { host } = item + if (data[host] === null) return { ...item } + return Object.assign(item, data[host]) + }) + }) + socketInstance.on('token_verify_fail', (message) => { + console.log('token 验证失败:', message) + // $router.push('/login') + }) + }) + socketInstance.on('disconnect', () => { + console.error('clients websocket 连接断开') + }) + socketInstance.on('connect_error', (message) => { + console.error('clients websocket 连接出错: ', message) }) - this.$patch({ hostList }) } } }) diff --git a/web/src/views/server/index.vue b/web/src/views/server/index.vue index 15230fa..ab73092 100644 --- a/web/src/views/server/index.vue +++ b/web/src/views/server/index.vue @@ -37,14 +37,12 @@