mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-05 19:20:12 +08:00
refactor: move client to sperate packages, rename cli to container
This commit is contained in:
1 parent
41a12c9aa8
commit
6ace18475b
15 files changed
+51
-28
No files matched your search
+1
-1
@@ -13,7 +13,7 @@
|
||||
"scripts": {
|
||||
"app": "yarn workspace app dev",
|
||||
"server": "yarn workspace server dev",
|
||||
"cli": "yarn workspace server cli",
|
||||
"cli": "yarn workspace client start",
|
||||
"lint": "eslint --ext .js,.vue .",
|
||||
"format": "prettier --ignore-path .prettierignore --write app server"
|
||||
},
|
||||
|
||||
@@ -2,8 +2,14 @@
|
||||
import { stdin, stdout, exit } from 'process'
|
||||
import { io } from 'socket.io-client'
|
||||
import { program } from 'commander'
|
||||
import { get } from './utils/env.js'
|
||||
|
||||
function get(key, defaultValue) {
|
||||
const value = process.env[key]
|
||||
if (value != null) {
|
||||
return value
|
||||
}
|
||||
return defaultValue
|
||||
}
|
||||
stdin.setRawMode(true)
|
||||
stdin.resume()
|
||||
stdin.setEncoding('utf8')
|
||||
@@ -60,7 +66,9 @@ async function connect(sessionId) {
|
||||
console.log('Connected to the server.')
|
||||
})
|
||||
|
||||
socket.on('message', function incoming(data) {
|
||||
socket.on('message', console.log)
|
||||
|
||||
socket.on('terminal', function incoming(data) {
|
||||
stdout.write(data)
|
||||
})
|
||||
|
||||
@@ -74,7 +82,7 @@ async function connect(sessionId) {
|
||||
socket.close()
|
||||
exit()
|
||||
} else {
|
||||
socket.send(key)
|
||||
socket.emit('terminal', key)
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"name": "client",
|
||||
"version": "1.0.0",
|
||||
"main": "index.js",
|
||||
"license": "MIT",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"start": "node index.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"commander": "^11.0.0",
|
||||
"socket.io-client": "^4.7.2"
|
||||
}
|
||||
}
|
||||
File renamed without changes.
File renamed without changes.
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"name": "cli",
|
||||
"name": "container",
|
||||
"version": "1.0.0",
|
||||
"main": "index.js",
|
||||
"license": "MIT",
|
||||
File renamed without changes.
File renamed without changes.
@@ -14,10 +14,8 @@
|
||||
"dockerode": "^3.3.5",
|
||||
"express": "^4.18.2",
|
||||
"jsonwebtoken": "^9.0.1",
|
||||
"marked": "^5.1.1",
|
||||
"mongoose": "^7.4.0",
|
||||
"socket.io": "^4.7.1",
|
||||
"socket.io-client": "^4.7.1",
|
||||
"swagger-ui-express": "^5.0.0",
|
||||
"yaml": "^2.3.1"
|
||||
},
|
||||
|
||||
@@ -14,6 +14,6 @@ WORKDIR /home/rudeus
|
||||
|
||||
COPY ./server/quests/helloworld/home /home/rudeus/
|
||||
|
||||
COPY ./packages/cli /usr/local/lib/cli
|
||||
COPY ./packages/container /usr/local/lib/container
|
||||
|
||||
CMD [ "/bin/zsh" ]
|
||||
@@ -29,7 +29,7 @@ precmd() {
|
||||
export CMD_ERROR_FILE=/tmp/cmd.error
|
||||
|
||||
# Now run your Node.js script with the result as an argument
|
||||
node /usr/local/lib/cli/cli.js
|
||||
node /usr/local/lib/container/cli.js
|
||||
|
||||
rm /tmp/cmd.out /tmp/cmd.error 2>/dev/null # cleanup
|
||||
}
|
||||
|
||||
@@ -6,7 +6,8 @@ import commandHandler from '../../game/commandHandler.js'
|
||||
|
||||
export async function newCommand(req, res) {
|
||||
console.log('new command:', req.body)
|
||||
const { token, command, pwd, output, error } = req.body
|
||||
const { token, command, pwd, output, error, ...additionalData } = req.body
|
||||
console.log('additionalData:', additionalData)
|
||||
|
||||
if (!command) {
|
||||
res.status(400).json({ message: 'command is required' })
|
||||
|
||||
@@ -50,10 +50,12 @@ export default (server) => {
|
||||
const stream = await attachContainer(container, { token })
|
||||
|
||||
stream.on('data', (chunk) => {
|
||||
socket.send(chunk.toString())
|
||||
socket.emit('terminal', chunk.toString())
|
||||
})
|
||||
|
||||
socket.on('message', function incoming(message) {
|
||||
socket.on('message', console.log)
|
||||
|
||||
socket.on('terminal', function incoming(message) {
|
||||
stream.write(message)
|
||||
})
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@ const containerOptions = {
|
||||
? []
|
||||
: [
|
||||
`${projectPath}/server/quests/helloworld/home:/home/rudeus`,
|
||||
`${projectPath}/packages/cli:/usr/local/lib/cli`,
|
||||
`${projectPath}/packages/container:/usr/local/lib/container`,
|
||||
],
|
||||
},
|
||||
}
|
||||
|
||||
@@ -1811,14 +1811,14 @@ end-of-stream@^1.1.0, end-of-stream@^1.4.1:
|
||||
dependencies:
|
||||
once "^1.4.0"
|
||||
|
||||
engine.io-client@~6.5.1:
|
||||
version "6.5.1"
|
||||
resolved "https://registry.yarnpkg.com/engine.io-client/-/engine.io-client-6.5.1.tgz#1735fb8ae3bae5ae13115e18d2f484daf005dd9c"
|
||||
integrity sha512-hE5wKXH8Ru4L19MbM1GgYV/2Qo54JSMh1rlJbfpa40bEWkCKNo3ol2eOtGmowcr+ysgbI7+SGL+by42Q3pt/Ng==
|
||||
engine.io-client@~6.5.2:
|
||||
version "6.5.2"
|
||||
resolved "https://registry.yarnpkg.com/engine.io-client/-/engine.io-client-6.5.2.tgz#8709e22c291d4297ae80318d3c8baeae71f0e002"
|
||||
integrity sha512-CQZqbrpEYnrpGqC07a9dJDz4gePZUgTPMU3NKJPSeQOyw27Tst4Pl3FemKoFGAlHzgZmKjoRmiJvbWfhCXUlIg==
|
||||
dependencies:
|
||||
"@socket.io/component-emitter" "~3.1.0"
|
||||
debug "~4.3.1"
|
||||
engine.io-parser "~5.1.0"
|
||||
engine.io-parser "~5.2.1"
|
||||
ws "~8.11.0"
|
||||
xmlhttprequest-ssl "~2.0.0"
|
||||
|
||||
@@ -1827,6 +1827,11 @@ engine.io-parser@~5.1.0:
|
||||
resolved "https://registry.yarnpkg.com/engine.io-parser/-/engine.io-parser-5.1.0.tgz#d593d6372d7f79212df48f807b8cace1ea1cb1b8"
|
||||
integrity sha512-enySgNiK5tyZFynt3z7iqBR+Bto9EVVVvDFuTT0ioHCGbzirZVGDGiQjZzEp8hWl6hd5FSVytJGuScX1C1C35w==
|
||||
|
||||
engine.io-parser@~5.2.1:
|
||||
version "5.2.1"
|
||||
resolved "https://registry.yarnpkg.com/engine.io-parser/-/engine.io-parser-5.2.1.tgz#9f213c77512ff1a6cc0c7a86108a7ffceb16fcfb"
|
||||
integrity sha512-9JktcM3u18nU9N2Lz3bWeBgxVgOKpw7yhRaoxQA3FUDZzzw+9WlA6p4G4u0RixNkg14fH7EfEc/RhpurtiROTQ==
|
||||
|
||||
engine.io@~6.5.0:
|
||||
version "6.5.1"
|
||||
resolved "https://registry.yarnpkg.com/engine.io/-/engine.io-6.5.1.tgz#59725f8593ccc891abb47f1efcdc52a089525a56"
|
||||
@@ -3432,11 +3437,6 @@ makeerror@1.0.12:
|
||||
dependencies:
|
||||
tmpl "1.0.5"
|
||||
|
||||
marked@^5.1.1:
|
||||
version "5.1.1"
|
||||
resolved "https://registry.yarnpkg.com/marked/-/marked-5.1.1.tgz#40b3963bb9da225314f746d5012ba7e34942f636"
|
||||
integrity sha512-bTmmGdEINWmOMDjnPWDxGPQ4qkDLeYorpYbEtFOXzOruTwUE671q4Guiuchn4N8h/v6NGd7916kXsm3Iz4iUSg==
|
||||
|
||||
media-typer@0.3.0:
|
||||
version "0.3.0"
|
||||
resolved "https://registry.yarnpkg.com/media-typer/-/media-typer-0.3.0.tgz#8710d7af0aa626f8fffa1ce00168545263255748"
|
||||
@@ -4269,14 +4269,14 @@ socket.io-adapter@~2.5.2:
|
||||
dependencies:
|
||||
ws "~8.11.0"
|
||||
|
||||
socket.io-client@^4.7.1:
|
||||
version "4.7.1"
|
||||
resolved "https://registry.yarnpkg.com/socket.io-client/-/socket.io-client-4.7.1.tgz#48e5f703abe4fb0402182bcf9c06b7820fb3453b"
|
||||
integrity sha512-Qk3Xj8ekbnzKu3faejo4wk2MzXA029XppiXtTF/PkbTg+fcwaTw1PlDrTrrrU4mKoYC4dvlApOnSeyLCKwek2w==
|
||||
socket.io-client@^4.7.2:
|
||||
version "4.7.2"
|
||||
resolved "https://registry.yarnpkg.com/socket.io-client/-/socket.io-client-4.7.2.tgz#f2f13f68058bd4e40f94f2a1541f275157ff2c08"
|
||||
integrity sha512-vtA0uD4ibrYD793SOIAwlo8cj6haOeMHrGvwPxJsxH7CeIksqJ+3Zc06RvWTIFgiSqx4A3sOnTXpfAEE2Zyz6w==
|
||||
dependencies:
|
||||
"@socket.io/component-emitter" "~3.1.0"
|
||||
debug "~4.3.2"
|
||||
engine.io-client "~6.5.1"
|
||||
engine.io-client "~6.5.2"
|
||||
socket.io-parser "~4.2.4"
|
||||
|
||||
socket.io-parser@~4.2.4:
|
||||
|
||||
Reference in new issue
Block a user