aigems commited on
Commit
3b4170f
·
1 Parent(s): 5737f77
Files changed (4) hide show
  1. Dockerfile +48 -0
  2. app.js +53 -0
  3. package.json +13 -0
  4. start.sh +37 -0
Dockerfile ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM ubuntu:22.04
2
+
3
+ ENV DEBIAN_FRONTEND=noninteractive
4
+ ENV NODE_VERSION=20
5
+
6
+ RUN apt-get update && apt-get install -y \
7
+ python3 \
8
+ python3-pip \
9
+ wget \
10
+ curl \
11
+ gnupg \
12
+ build-essential \
13
+ zlib1g-dev \
14
+ iproute2 \
15
+ && apt-get clean \
16
+ && rm -rf /var/lib/apt/lists/*
17
+
18
+ RUN curl -fsSL https://deb.nodesource.com/setup_${NODE_VERSION}.x | bash - \
19
+ && apt-get install -y nodejs
20
+
21
+ RUN wget https://matt.ucc.asn.au/dropbear/dropbear-2024.85.tar.bz2 \
22
+ && tar xjf dropbear-2024.85.tar.bz2 \
23
+ && cd dropbear-2024.85 \
24
+ && ./configure \
25
+ && make \
26
+ && make install \
27
+ && cd .. \
28
+ && rm -rf dropbear-2024.85 dropbear-2024.85.tar.bz2
29
+
30
+ RUN useradd -u 1000 -m -s /bin/bash user \
31
+ && echo 'user:password' | chpasswd
32
+
33
+ WORKDIR /home/user/app
34
+
35
+ COPY package*.json ./
36
+ RUN npm ci
37
+
38
+ COPY . .
39
+
40
+ RUN chown -R user:user /home/user \
41
+ && mkdir -p /etc/dropbear \
42
+ && chown -R user:user /etc/dropbear
43
+
44
+ EXPOSE 3000 2222
45
+
46
+ USER user
47
+
48
+ CMD ["/bin/bash", "start.sh"]
app.js ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const express = require('express');
2
+ const { Client } = require('ssh2');
3
+ const fs = require('fs').promises;
4
+
5
+ const app = express();
6
+ const port = process.env.PORT || 3000;
7
+
8
+ app.use(express.json());
9
+
10
+ app.post('/execute', async (req, res) => {
11
+ const { command } = req.body;
12
+
13
+ if (!command) {
14
+ return res.status(400).json({ error: 'Command is required' });
15
+ }
16
+
17
+ try {
18
+ const conn = new Client();
19
+ const privateKey = await fs.readFile('/home/user/.ssh/id_rsa');
20
+
21
+ conn.on('ready', () => {
22
+ conn.exec(command, (err, stream) => {
23
+ if (err) {
24
+ conn.end();
25
+ return res.status(500).json({ error: 'Failed to execute command' });
26
+ }
27
+
28
+ let output = '';
29
+ stream.on('close', (code, signal) => {
30
+ conn.end();
31
+ res.json({ output: output.trim(), code });
32
+ }).on('data', (data) => {
33
+ output += data;
34
+ }).stderr.on('data', (data) => {
35
+ output += data;
36
+ });
37
+ });
38
+ }).on('error', (err) => {
39
+ res.status(500).json({ error: 'SSH connection failed', details: err.message });
40
+ }).connect({
41
+ host: 'localhost',
42
+ port: 2222,
43
+ username: 'user',
44
+ privateKey
45
+ });
46
+ } catch (error) {
47
+ res.status(500).json({ error: 'Internal server error', details: error.message });
48
+ }
49
+ });
50
+
51
+ app.listen(port, '0.0.0.0', () => {
52
+ console.log(`Server running on port ${port}`);
53
+ });
package.json ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "ssh-web-app",
3
+ "version": "1.0.0",
4
+ "description": "Web app to execute commands via SSH",
5
+ "main": "app.js",
6
+ "scripts": {
7
+ "start": "node app.js"
8
+ },
9
+ "dependencies": {
10
+ "express": "^4.17.1",
11
+ "ssh2": "^1.11.0"
12
+ }
13
+ }
start.sh ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/bash
2
+
3
+ set -e
4
+ set -x
5
+
6
+ if [ ! -f /etc/dropbear/dropbear_rsa_host_key ]; then
7
+ dropbearkey -t rsa -f /etc/dropbear/dropbear_rsa_host_key
8
+ fi
9
+
10
+ if [ ! -f ~/.ssh/id_rsa ]; then
11
+ mkdir -p ~/.ssh
12
+ ssh-keygen -t rsa -N "" -f ~/.ssh/id_rsa
13
+ cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
14
+ chmod 600 ~/.ssh/authorized_keys
15
+ fi
16
+
17
+ /usr/local/sbin/dropbear -R -F -E -p 2222 &
18
+
19
+ sleep 2
20
+
21
+ echo "测试SSH连接..."
22
+ if ssh -o StrictHostKeyChecking=no -p 2222 localhost 'echo SSH连接成功'; then
23
+ echo "SSH连接测试成功"
24
+ else
25
+ echo "SSH连接测试失败"
26
+ exit 1
27
+ fi
28
+
29
+ npm start &
30
+
31
+ echo "当前用户: $(whoami)"
32
+ echo "Dropbear 进程: $(pgrep dropbear)"
33
+ echo "Node.js 进程: $(pgrep node)"
34
+ echo "2222 端口监听状态: $(ss -tuln | grep 2222)"
35
+ echo "3000 端口监听状态: $(ss -tuln | grep 3000)"
36
+
37
+ wait