feat: connect to different container by sessions

Based on the file summaries provided, the best label for this commit would be "feat". This is because the commit introduces several new features to the codebase, such as adding a new ESLint rule, adding a new property to a function, adding authentication middleware, adding a query parameter, and adding a new function to handle user authentication.: update authentication and session handling

- Add `"camelcase"` rule to ESLint configuration
- Add `containerId` property to the `newSession` object in the `createSession` function
- Add authentication middleware to the socket.io connection
- Add a query parameter `session_id` to the socket.io connection in `client.js`
- Add `defaultUser` function to handle user authentication
- Update the `authMiddleware` function to use the `defaultUser` function
- Add `defaultUser` function to handle user authentication in `auth.js`
- Update `getOrCreateContainer` function to use the `id` parameter instead of `name` parameter
This commit is contained in:
wancat committed 2023-07-24 12:21:05 +08:00
1 parent c83ca750e1
commit 95fc25d0ad
7 files changed
+57 -15

No files matched your search

@@ -25,6 +25,7 @@ export async function createSession(req, res) {
const newSession = new Session({
user: req.user,
quest,
containerId: `quest-${quest.id}-${req.user.username}-${Date.now()}`,
})
try {
+27 -1
View File
@@ -1,12 +1,38 @@
import { Server } from 'socket.io'
import { getOrCreateContainer, attachContainer } from '../utils/docker.js'
import Session from '../models/session.js'
import { defaultUser } from '../utils/auth.js'
export default (server) => {
const io = new Server(server)
io.use(async (socket, next) => {
console.log('Authenticating...')
try {
const user = await defaultUser()
// eslint-disable-next-line no-param-reassign
socket.user = user
next()
} catch (err) {
console.error(err)
next(err)
}
})
io.on('connection', async (socket) => {
console.log('Connected to the client.')
const container = await getOrCreateContainer('odyssey-test')
const session = await Session.findOne({
_id: socket.handshake.query.session_id,
user: socket.user,
})
if (!session) {
socket.send('Session not found.')
socket.disconnect()
return
}
const container = await getOrCreateContainer(session.containerId)
const stream = await attachContainer(container)
stream.on('data', (chunk) => {