feat: add new API for recording executed commands

- Add the `jsonwebtoken` dependency to the server's `package.json`
- Add a new file `commandController.js` to the server's API controllers
- Add a new route `/commands` to the server's API routes
- Add a new file `commands.js` to the server's API routes
- Modify the `index.js` file in the server's API routes to import and use the `commands.js` route
- Add a `newCommand` function to the `commandController.js` file
- Add a `Command` model to the `command.js` file in the server's models
- Add the `genSessionJWT` and `verifySessionJWT` functions to the `auth.js` file in the server's utils
- Modify the `attachContainer` function in the `docker.js` file in the server's utils to include a `token` parameter
- Modify the `swagger.yaml` file to include a new route `/commands` for recording executed commands in a session

Signed-off-by: wancat <wancat@wancat.cc>
This commit is contained in:
wancat committed 2023-07-24 16:39:04 +08:00
1 parent fe3f4ddc73
commit 3cea54ce71
11 files changed
+194 -6

No files matched your search

+31 -1
View File
@@ -1,4 +1,5 @@
/* eslint-disable import/prefer-default-export */
import jwt from 'jsonwebtoken'
import config from '../config.js'
import User from '../models/user.js'
export async function defaultUser() {
@@ -12,3 +13,32 @@ export async function defaultUser() {
}
return user
}
export function genSessionJWT(session) {
return new Promise((resolve, reject) => {
jwt.sign(
{ session_id: session.id },
config.jwtSecret,
{ expiresIn: '1h' },
(err, token) => {
if (err) {
reject(err)
} else {
resolve(token)
}
}
)
})
}
export function verifySessionJWT(token) {
return new Promise((resolve, reject) => {
jwt.verify(token, config.jwtSecret, (err, decoded) => {
if (err) {
reject(err)
} else {
resolve(decoded)
}
})
})
}
+2 -1
View File
@@ -46,7 +46,7 @@ export async function getOrCreateContainer(id) {
}
}
export async function attachContainer(container) {
export async function attachContainer(container, { token }) {
// Create an exec instance with bash shell
const exec = await container.exec({
AttachStdin: true,
@@ -54,6 +54,7 @@ export async function attachContainer(container) {
AttachStderr: true,
Cmd: ['/bin/bash'],
Tty: true,
Env: [`TOKEN=${token}`],
})
const execOutput = await exec.start({