feat: connect to localhost

This commit is contained in:
wancat committed 2023-08-17 19:43:55 +08:00
1 parent 577352e51d
commit a74233d7f9
6 files changed
+82 -121

No files matched your search

+61 -119
View File
@@ -1,5 +1,3 @@
<script setup></script>
<template> <template>
<font-awesome-icon :icon="['fas', 'terminal']" class="text-text p-1" /> <font-awesome-icon :icon="['fas', 'terminal']" class="text-text p-1" />
<button class="text-text h-[5%]">Terminal</button> <button class="text-text h-[5%]">Terminal</button>
@@ -13,128 +11,72 @@
</p> --> </p> -->
</div> </div>
</template> </template>
<script> <script setup>
import { Terminal } from 'xterm' import { Terminal } from 'xterm'
import { FitAddon } from 'xterm-addon-fit' import { FitAddon } from 'xterm-addon-fit'
// import { AttachAddon } from 'xterm-addon-attach' import { AttachAddon } from 'xterm-addon-attach'
import { stdin, stdout, exit } from 'process'
import 'xterm/css/xterm.css' import 'xterm/css/xterm.css'
import { io } from 'socket.io-client' import { io } from 'socket.io-client'
// import connect from 'server/src/client' // import connect from 'server/src/client'
export default { import { onMounted, ref } from 'vue'
name: 'TerminalPart',
data() { const rows = ref(40)
return { const cols = ref(100)
term: '', // 保存terminal实例 const terminal = ref(null)
rows: 40,
cols: 100, onMounted(() => {
} const term = new Terminal({
}, rendererType: 'canvas', // 渲染类型
mounted() { rows: rows.value, // 行数
this.initXterm() cols: cols.value, // 不指定行数,自动回车后光标从下一行开始
}, convertEol: true, // 启用时,光标将设置为下一行的开头
methods: { // scrollback: 50, //终端中的回滚量
initXterm() { disableStdin: false, // 是否应禁用输入
const xthis = this // cursorStyle: "underline", //光标样式
const term = new Terminal({ cursorBlink: true, // 光标闪烁
rendererType: 'canvas', // 渲染类型 theme: {
rows: xthis.rows, // 行数 foreground: '#ECECEC', // 字体
cols: xthis.cols, // 不指定行数,自动回车后光标从下一行开始 background: '#000000', // 背景色
convertEol: true, // 启用时,光标将设置为下一行的开头 cursor: 'help', // 设置光标
// scrollback: 50, //终端中的回滚量 lineHeight: 20,
disableStdin: false, // 是否应禁用输入 },
// cursorStyle: "underline", //光标样式 })
cursorBlink: true, // 光标闪烁 // const host = 'wss://odyssey.wancat.cc'
theme: { const host = 'ws://localhost:3000'
foreground: '#ECECEC', // 字体 const socket = io(host, {
background: '#000000', // 背景色 query: {
cursor: 'help', // 设置光标 // session_id: sessionId,
lineHeight: 20, session_id: '64d0de0367459c13004bc83f',
}, },
}) })
const ws = 'wss://odyssey.wancat.cc' socket.on('connect', function open() {
// const ws = 'ws://localhost:3000' console.log('Connected to the server.')
const socket = io(ws, { })
query: { socket.on('disconnect', function close() {
// session_id: sessionId, console.log('Disconnected from the server.')
session_id: '64cb80132077b982b86bdc4f', })
}, socket.on('message', (message) => {
}) console.log(message)
socket.on('message', function incoming(data) { term.write(message)
console.log(data) })
}) term.onData((key) => {
socket.on('open', function open() { console.log(key)
console.log('Connected to the server.') socket.send(key)
}) })
// socket.on('message', function incoming(data) { term.open(terminal.value)
// stdout.write(data)
// }) const fitAddon = new FitAddon()
socket.on('close', function close() { term.loadAddon(fitAddon)
console.log('Disconnected from the server.') fitAddon.fit()
exit() function resizeScreen() {
}) try {
socket.on('message', (message) => { // 窗口大小改变时,触发xterm的resize方法使自适应
term.onData(socket.emit('input', message))
})
// const attachAddon = new AttachAddon(socket)
// 创建terminal实例
// term.loadAddon(attachAddon)
term.open(this.$refs.terminal)
// 换行并输入起始符 $
term.prompt = () => {
term.write('\r\n\x1b[33mLinux Odyssey$\x1b[0m ')
}
term.prompt()
// canvas背景全屏
const fitAddon = new FitAddon()
term.loadAddon(fitAddon)
fitAddon.fit() fitAddon.fit()
function resizeScreen() { } catch (e) {
try { console.log('e', e.message)
// 窗口大小改变时,触发xterm的resize方法使自适应 }
fitAddon.fit() }
} catch (e) { window.addEventListener('resize', resizeScreen)
console.log('e', e.message) })
}
}
window.addEventListener('resize', resizeScreen)
xthis.term = term
xthis.socket = socket
xthis.runFakeTerminal()
},
runFakeTerminal() {
const xthis = this
const { term } = xthis
if (term.xinitialized) return
// 初始化
term.xinitialized = true
term.writeln('Welcome to \x1b[1;32mLinux Odyssey\x1b[0m!')
term.prompt()
// 添加事件监听器,支持输入方法
term.onKey((e) => {
const printable =
!e.domEvent.altKey &&
!e.domEvent.altGraphKey &&
!e.domEvent.ctrlKey &&
!e.domEvent.metaKey
if (e.domEvent.keyCode === 13) {
term.prompt()
} else if (e.domEvent.keyCode === 8) {
// back 删除的情况
if (term.xcore.buffer.x > 2) {
term.write('\b \b')
}
} else if (printable) {
term.write(e.key)
}
console.log(1, 'print', e.key)
})
term.onData((key) => {
// 粘贴的情况
if (key.length > 1) term.write(key)
})
},
},
}
</script> </script>
+8
View File
@@ -4,4 +4,12 @@ import vue from '@vitejs/plugin-vue'
// https://vitejs.dev/config/ // https://vitejs.dev/config/
export default defineConfig({ export default defineConfig({
plugins: [vue()], plugins: [vue()],
server: {
proxy: {
'/api': {
target: 'http://localhost:3000', // the server url
ws: true,
},
},
},
}) })
+1
View File
@@ -11,6 +11,7 @@
}, },
"dependencies": { "dependencies": {
"commander": "^11.0.0", "commander": "^11.0.0",
"cors": "^2.8.5",
"dockerode": "^3.3.5", "dockerode": "^3.3.5",
"express": "^4.18.2", "express": "^4.18.2",
"jsonwebtoken": "^9.0.1", "jsonwebtoken": "^9.0.1",
+5 -1
View File
@@ -4,7 +4,11 @@ import Session from '../models/session.js'
import { defaultUser, genSessionJWT } from '../utils/auth.js' import { defaultUser, genSessionJWT } from '../utils/auth.js'
export default (server) => { export default (server) => {
const io = new Server(server) const io = new Server(server, {
cors: {
origin: '*',
},
})
io.use(async (socket, next) => { io.use(async (socket, next) => {
console.log('Authenticating...') console.log('Authenticating...')
+6
View File
@@ -3,6 +3,7 @@ import express from 'express'
import http from 'http' import http from 'http'
import YAML from 'yaml' import YAML from 'yaml'
import swaggerUI from 'swagger-ui-express' import swaggerUI from 'swagger-ui-express'
import cors from 'cors'
import socketServer from './api/socket.js' import socketServer from './api/socket.js'
import connectDB from './db.js' import connectDB from './db.js'
@@ -22,6 +23,11 @@ async function main() {
const swaggerDocument = YAML.parse(file) const swaggerDocument = YAML.parse(file)
app.use('/api-docs', swaggerUI.serve, swaggerUI.setup(swaggerDocument)) app.use('/api-docs', swaggerUI.serve, swaggerUI.setup(swaggerDocument))
app.use(express.json()) app.use(express.json())
app.use(
cors({
origin: '*',
})
)
app.get('/', (req, res) => { app.get('/', (req, res) => {
res.send('Hello World!') res.send('Hello World!')
+1 -1
View File
@@ -1602,7 +1602,7 @@ cookie@~0.4.1:
resolved "https://registry.yarnpkg.com/cookie/-/cookie-0.4.2.tgz#0e41f24de5ecf317947c82fc789e06a884824432" resolved "https://registry.yarnpkg.com/cookie/-/cookie-0.4.2.tgz#0e41f24de5ecf317947c82fc789e06a884824432"
integrity sha512-aSWTXFzaKWkvHO1Ny/s+ePFpvKsPnjc551iI41v3ny/ow6tBG5Vd+FuqGNhh1LxOmVzOlGUriIlOaokOvhaStA== integrity sha512-aSWTXFzaKWkvHO1Ny/s+ePFpvKsPnjc551iI41v3ny/ow6tBG5Vd+FuqGNhh1LxOmVzOlGUriIlOaokOvhaStA==
cors@~2.8.5: cors@^2.8.5, cors@~2.8.5:
version "2.8.5" version "2.8.5"
resolved "https://registry.yarnpkg.com/cors/-/cors-2.8.5.tgz#eac11da51592dd86b9f06f6e7ac293b3df875d29" resolved "https://registry.yarnpkg.com/cors/-/cors-2.8.5.tgz#eac11da51592dd86b9f06f6e7ac293b3df875d29"
integrity sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g== integrity sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g==