🆕 内置常用脚本

This commit is contained in:
chaos-zhu 2024-08-02 16:00:19 +08:00
parent 44d9557f17
commit 71958699ea
10 changed files with 82 additions and 38 deletions

View File

@ -82,13 +82,13 @@ pm2 start index.js --name easynode-server
> 安装
```shell
wget https://mirror.ghproxy.com/https://raw.githubusercontent.com/chaos-zhu/easynode/main/client/easynode-client-install.sh | bash
curl -o- https://mirror.ghproxy.com/https://raw.githubusercontent.com/chaos-zhu/easynode/main/client/easynode-client-install.sh | bash
```
> 卸载
```shell
wget https://mirror.ghproxy.com/https://raw.githubusercontent.com/chaos-zhu/easynode/main/client/easynode-client-uninstall.sh | bash
curl -o- https://mirror.ghproxy.com/https://raw.githubusercontent.com/chaos-zhu/easynode/main/client/easynode-client-uninstall.sh | bash
```
> 查看客户端状态:`systemctl status easynode-client`

View File

@ -0,0 +1,17 @@
[
{
"name": "easynode客户端安装",
"command": "curl -o- https://mirror.ghproxy.com/https://raw.githubusercontent.com/chaos-zhu/easynode/main/client/easynode-client-install.sh | bash",
"description": "easynode-客户端-安装脚本"
},
{
"name": "easynode客户端卸载",
"command": "curl -o- https://mirror.ghproxy.com/https://raw.githubusercontent.com/chaos-zhu/easynode/main/client/easynode-client-uninstall.sh | bash",
"description": "easynode-客户端-卸载脚本"
},
{
"name": "查询本机公网IP",
"command": "curl ifconfig.me",
"description": "查询本机公网IP"
}
]

View File

@ -1,4 +1,9 @@
const { readScriptList, writeScriptList } = require('../utils')
const localShellJson = require('../config/shell.json')
const { readScriptList, writeScriptList, randomStr } = require('../utils')
let localShell = JSON.parse(JSON.stringify(localShellJson)).map((item) => {
return { ...item, id: randomStr(10), index: '--', description: item.description + '|内置脚本' }
})
async function getScriptList({ res }) {
let data = await readScriptList()
@ -6,15 +11,20 @@ async function getScriptList({ res }) {
return { ...item, id: item._id }
})
data?.sort((a, b) => Number(b.index || 0) - Number(a.index || 0))
data.push(...localShell)
res.success({ data })
}
async function getLocalScriptList({ res }) {
res.success({ data: localShell })
}
const addScript = async ({ res, request }) => {
let { body: { name, remark, content, index } } = request
if (!name || !content) return res.fail({ data: false, msg: '参数错误' })
let { body: { name, description, command, index } } = request
if (!name || !command) return res.fail({ data: false, msg: '参数错误' })
index = Number(index) || 0
let scriptsList = await readScriptList()
let record = { name, remark, content, index }
let record = { name, description, command, index }
scriptsList.push(record)
await writeScriptList(scriptsList)
res.success({ data: '添加成功' })
@ -22,13 +32,13 @@ const addScript = async ({ res, request }) => {
const updateScriptList = async ({ res, request }) => {
let { params: { id } } = request
let { body: { name, remark, content, index } } = request
if (!name || !content) return res.fail({ data: false, msg: '参数错误' })
let { body: { name, description, command, index } } = request
if (!name || !command) return res.fail({ data: false, msg: '参数错误' })
let scriptsList = await readScriptList()
let idx = scriptsList.findIndex(item => item._id === id)
if (idx === -1) return res.fail({ data: false, msg: `脚本ID${ id }不存在` })
const { _id } = scriptsList[idx]
let record = Object.assign({ _id }, { name, remark, content, index })
let record = Object.assign({ _id }, { name, description, command, index })
scriptsList.splice(idx, 1, record)
await writeScriptList(scriptsList)
res.success({ data: '修改成功' })
@ -47,6 +57,7 @@ const removeScript = async ({ res, request }) => {
module.exports = {
addScript,
getScriptList,
getLocalScriptList,
updateScriptList,
removeScript
}

View File

@ -151,26 +151,24 @@ function initScriptsDB() {
// eslint-disable-next-line no-async-promise-executor
return new Promise(async (resolve) => {
let scriptList = await readScriptList()
let clientInstallScript = 'wget https://mirror.ghproxy.com/https://raw.githubusercontent.com/chaos-zhu/easynode/main/client/easynode-client-install.sh && sh easynode-client-install.sh'
let clientUninstallScript = 'wget https://mirror.ghproxy.com/https://raw.githubusercontent.com/chaos-zhu/easynode/main/client/easynode-client-uninstall.sh && sh easynode-client-uninstall.sh'
let clientVersion = process.env.CLIENT_VERSION
consola.info('客户端版本:', clientVersion)
let installId = `clientInstall${ clientVersion }`
let uninstallId = `clientUninstall${ clientVersion }`
let clientInstallScript = 'curl -o- https://mirror.ghproxy.com/https://raw.githubusercontent.com/chaos-zhu/easynode/main/client/easynode-client-install.sh | bash'
let clientUninstallScript = 'curl -o- https://mirror.ghproxy.com/https://raw.githubusercontent.com/chaos-zhu/easynode/main/client/easynode-client-uninstall.sh | bash'
let installId = 'clientInstall'
let uninstallId = 'clientUninstall'
let isClientInstall = scriptList?.find(script => script._id = installId)
let isClientUninstall = scriptList?.find(script => script._id = uninstallId)
let writeFlag = false
if (!isClientInstall) {
console.info('初始化客户端安装脚本')
scriptList.push({ _id: installId, name: `easynode-客户端-${ clientVersion }安装脚本`, remark: '系统内置|重启生成', content: clientInstallScript, index: 1 })
scriptList.push({ _id: installId, name: 'easynode-客户端-安装脚本', description: '系统内置|重启生成', command: clientInstallScript, index: 1 })
writeFlag = true
} else {
console.info('客户端安装脚本已存在')
}
if (!isClientUninstall) {
console.info('初始化客户端卸载脚本')
scriptList.push({ _id: uninstallId, name: `easynode-客户端-${ clientVersion }卸载脚本`, remark: '系统内置|重启生成', content: clientUninstallScript, index: 0 })
scriptList.push({ _id: uninstallId, name: 'easynode-客户端-卸载脚本', description: '系统内置|重启生成', command: clientUninstallScript, index: 0 })
writeFlag = true
} else {
console.info('客户端卸载脚本已存在')
@ -185,5 +183,5 @@ module.exports = async () => {
await initNotifyDB()
await initGroupDB()
await initEmailNotifyDB()
await initScriptsDB()
}
// await initScriptsDB()
}

View File

@ -3,7 +3,7 @@ const { getHostList, addHost, updateHost, removeHost, importHost } = require('..
const { login, getpublicKey, updatePwd, getLoginRecord } = require('../controller/user')
const { getSupportEmailList, getUserEmailList, updateUserEmailList, removeUserEmail, pushEmail, getNotifyList, updateNotifyList } = require('../controller/notify')
const { getGroupList, addGroupList, updateGroupList, removeGroup } = require('../controller/group')
const { getScriptList, addScript, updateScriptList, removeScript } = require('../controller/scripts')
const { getScriptList, getLocalScriptList, addScript, updateScriptList, removeScript } = require('../controller/scripts')
const { getOnekeyRecord, removeOnekeyRecord } = require('../controller/onekey')
const ssh = [
@ -149,6 +149,11 @@ const scripts = [
path: '/script',
controller: getScriptList
},
{
method: 'get',
path: '/local-script',
controller: getLocalScriptList
},
{
method: 'post',
path: '/script',

View File

@ -91,6 +91,9 @@ export default {
getScriptList() {
return axios({ url: '/script', method: 'get' })
},
getLocalScriptList() {
return axios({ url: '/local-script', method: 'get' })
},
addScript(data) {
return axios({ url: '/script', method: 'post', data })
},

View File

@ -1,7 +1,7 @@
import { io } from 'socket.io-client'
import { defineStore, acceptHMRUpdate } from 'pinia'
import $api from '@/api'
import ping from '@/utils/ping'
// import ping from '@/utils/ping'
const useStore = defineStore({
id: 'global',
@ -11,6 +11,7 @@ const useStore = defineStore({
groupList: [],
sshList: [],
scriptList: [],
localScriptList: [],
HostStatusSocket: null,
user: localStorage.getItem('user') || null,
token: sessionStorage.getItem('token') || localStorage.getItem('token') || null,
@ -39,6 +40,7 @@ const useStore = defineStore({
await this.getHostList()
await this.getSSHList()
await this.getScriptList()
// await this.getLocalScriptList()
},
async getHostList() {
const { data: hostList } = await $api.getHostList()
@ -61,6 +63,11 @@ const useStore = defineStore({
// console.log('scriptList:', scriptList)
this.$patch({ scriptList })
},
async getLocalScriptList() {
const { data: localScriptList } = await $api.getLocalScriptList()
// console.log('localScriptList:', localScriptList)
this.$patch({ localScriptList })
},
// getHostPing() {
// setInterval(() => {
// this.hostList.forEach((item) => {

View File

@ -326,7 +326,7 @@ let selectAllHost = (val) => {
let handleImportScript = (scriptObj) => {
isClient.value = scriptObj.id.startsWith('client')
formData.command = scriptObj.content
formData.command = scriptObj.command
}
let getStatusType = (status) => {

View File

@ -6,12 +6,15 @@
<el-table v-loading="loading" :data="scriptList">
<el-table-column prop="index" label="序号" width="100px" />
<el-table-column prop="name" label="名称" />
<el-table-column prop="remark" label="备注" />
<el-table-column prop="content" label="脚本内容" show-overflow-tooltip />
<el-table-column prop="description" label="描述" />
<el-table-column prop="command" label="指令内容" show-overflow-tooltip />
<el-table-column label="操作">
<template #default="{ row }">
<el-button type="primary" @click="handleChange(row)">修改</el-button>
<el-button v-show="row.id !== 'own'" type="danger" @click="handleRemove(row)">删除</el-button>
<template v-if="row.index !== '--'">
<el-button type="primary" @click="handleChange(row)">修改</el-button>
<el-button v-show="row.id !== 'own'" type="danger" @click="handleRemove(row)">删除</el-button>
</template>
<span v-else>--</span>
</template>
</el-table-column>
</el-table>
@ -40,9 +43,9 @@
autocomplete="off"
/>
</el-form-item>
<el-form-item label="备注" prop="remark">
<el-form-item label="描述" prop="description">
<el-input
v-model="formData.remark"
v-model="formData.description"
clearable
placeholder=""
autocomplete="off"
@ -56,9 +59,9 @@
autocomplete="off"
/>
</el-form-item>
<el-form-item prop="content" label="内容">
<el-form-item prop="command" label="内容">
<el-input
v-model="formData.content"
v-model="formData.command"
type="textarea"
:rows="5"
clearable
@ -89,17 +92,17 @@ let isModify = ref(false)
let formData = reactive({
name: '',
remark: '',
description: '',
index: 0,
content: ''
command: ''
})
const rules = computed(() => {
return {
name: { required: true, trigger: 'change' },
remark: { required: false, trigger: 'change' },
description: { required: false, trigger: 'change' },
index: { required: false, type: 'number', trigger: 'change' },
content: { required: true, trigger: 'change' }
command: { required: true, trigger: 'change' }
}
})

View File

@ -219,10 +219,10 @@ const handleCommandHost = (host) => {
}
const handleExecScript = (scriptObj) => {
// console.log(scriptObj.content)
if (!isSyncAllSession.value) return handleInputCommand(scriptObj.content)
const { command } = scriptObj
if (!isSyncAllSession.value) return handleInputCommand(command)
terminalRefs.value.forEach(terminalRef => {
terminalRef.inputCommand(scriptObj.content)
terminalRef.inputCommand(command)
})
}
@ -307,7 +307,7 @@ const handleInputCommand = async (command) => {
const curTerminalRef = terminalRefs.value[activeTabIndex.value]
await $nextTick()
curTerminalRef?.focusTab()
curTerminalRef.inputCommand(`${ command }\n`)
curTerminalRef.inputCommand(`${ command }`) // \n
showInputCommand.value = false
}
</script>