抓包精灵-上传功能服务端代码

服务端代码

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
const express = require('express');
const multiparty = require('multiparty');
const fs = require('fs');
const path = require('path');
const os = require('os');

const app = express();

// 确保上传目录存在
app.post('/', (req, res) => {
const form = new multiparty.Form();
form.uploadDir = generateSavePath(req); // 设置上传目录
console.log(form.uploadDir)
if (!fs.existsSync(form.uploadDir)) {
fs.mkdirSync(form.uploadDir, {recursive: true});
}
form.keepExtensions = true; // 保持文件扩展名
form.parse(req, (err, fields, files) => {
if (err) {
console.error('Error parsing form:', err);
return res.status(500).json({error: 'Error parsing form'});
}

console.log('Fields:', fields);
console.log('Files:', files);

res.json({message: 'File uploaded successfully.'});
});
});
const generateSavePath = (req) => {
const vpnStart = req.headers['vpn_start_time'];
const appName = req.headers['app_name'];
const ipAndPort = req.headers['ipandport'];
return path.join(__dirname, 'uploads', vpnStart, appName, ipAndPort);
};

function getLocalIP() {
const interfaces = os.networkInterfaces();
for (const name of Object.keys(interfaces)) {
for (const net of interfaces[name]) {
if (net.family === 'IPv4' && !net.internal) {
return net.address;
}
}
}
return '0.0.0.0';
}

app.listen(3000, () => {
const localIP = getLocalIP();
console.log(`Server is running on http://${localIP}:3000`);
});

下载运行必要npm包

npm install express

npm install multiparty

npm install fs

npm install path

npm install os

运行

node server.js


抓包精灵-上传功能服务端代码
https://cason.work/2024/07/29/抓包精灵-上传功能服务端代码/
作者
Cason Mo
发布于
2024年7月29日
许可协议