File size: 1,747 Bytes
3b4170f
 
 
c371630
3b4170f
 
565dfa2
 
 
 
 
 
3b4170f
2317df1
c371630
2317df1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3b4170f
 
2317df1
 
 
 
 
 
 
3b4170f
 
c371630
 
3b4170f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
const express = require('express');
const { Client } = require('ssh2');
const app = express();
const port = 7860;

app.use(express.json());
app.use(express.static('public'));

// 添加根路径处理
app.get('/', (req, res) => {
    res.sendFile(__dirname + '/public/index.html');
});

app.post('/ssh', async (req, res) => {
    const conn = new Client();
    
    try {
        await new Promise((resolve, reject) => {
            conn.on('ready', resolve)
                .on('error', reject)
                .connect({
                    host: 'localhost',
                    port: 2202,
                    username: 'user',
                    password: 'password',
                    readyTimeout: 5000,
                    debug: console.log
                });
        });

        console.log('SSH Connection established');

        const { stdout, stderr } = await new Promise((resolve, reject) => {
            conn.exec(req.body.command, (err, stream) => {
                if (err) reject(err);
                let stdout = '', stderr = '';
                stream.on('close', (code, signal) => {
                    resolve({ stdout, stderr, code, signal });
                }).on('data', (data) => {
                    stdout += data;
                }).stderr.on('data', (data) => {
                    stderr += data;
                });
            });
        });

        conn.end();
        res.json({ output: stdout, error: stderr });
    } catch (error) {
        console.error('SSH error:', error);
        res.status(500).json({ error: error.message });
    }
});

app.listen(port, () => {
    console.log(`Web SSH app listening at http://localhost:${port}`);
});