diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..da935ad --- /dev/null +++ b/Dockerfile @@ -0,0 +1,16 @@ +FROM node:latest + +WORKDIR /app + +COPY package*.json ./ + +RUN npm install + +COPY . . + +RUN mkdir -p /app/secrets && chmod -R 700 /app/secrets + +EXPOSE 5164 +ENV NODE_ENV=production + +CMD ["node", "."] diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..a80232f --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,11 @@ +services: + app: + build: + context: . + ports: + - "5164:3000" + volumes: + - ./secrets:/app/secrets + environment: + PORT: 3000 + NODE_ENV: production diff --git a/main.js b/main.js index 54f0fae..2bc424f 100644 --- a/main.js +++ b/main.js @@ -6,13 +6,12 @@ import expressWs from 'express-ws'; import { spawn } from 'node-pty'; import json from './secrets/config.json' with { type: 'json' }; -const __filename = fileURLToPath(import.meta.url); -const __dirname = path.dirname(__filename); +const __dirname = path.dirname(fileURLToPath(import.meta.url)), + PORT = process.env.PORT || 3000, + app = express(); -const app = express(); expressWs(app); // enable websockets for the app -const PORT = 3000; // configure session app.use(session({ @@ -21,6 +20,7 @@ app.use(session({ saveUninitialized: false })); + // parse form data app.use(express.urlencoded({ extended: true })); app.use(express.json({ extended: true })); @@ -36,11 +36,13 @@ function requireAuth(req, res, next) { } } + // serve login page app.get('/login', (req, res) => { res.sendFile('login.html', { root: path.join(__dirname, 'HTML') }); }); + // process login app.post('/login', (req, res) => { try { @@ -74,11 +76,13 @@ app.post('/login', (req, res) => { } }); + // shell interface app.get('/shell', requireAuth, (req, res) => { res.sendFile('shell.html', { root: path.join(__dirname, 'HTML') }); }); + // logout route app.get('/logout', (req, res) => { req.session.destroy(() => { @@ -86,6 +90,7 @@ app.get('/logout', (req, res) => { }); }); + // when a websocket is opened at /shell-ws, spawn a fish shell in a pty app.ws('/shell-ws', (ws, req) => { // spawn a fish shell using node-pty with a pseudo-terminal @@ -110,4 +115,8 @@ app.ws('/shell-ws', (ws, req) => { ws.on('close', () => shell.kill()); }); -app.listen(PORT, () => console.log('server listening on http://localhost:' + PORT)); \ No newline at end of file + +app.get('/', (req, res) => res.end()); + + +app.listen(PORT, '0.0.0.0', () => console.log('server listening on http://localhost:' + PORT)); \ No newline at end of file