forked from cyclic-software/starter-micro-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
82 lines (72 loc) · 2.35 KB
/
index.js
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import http from 'http';
import fetch from 'node-fetch';
const server = http.createServer(async (req, res) => {
// Discord webhook URL
const discordWebhookURL = process.env.WEBHOOK; // Assuming you've set the environment variable
try {
// Function to send request details to Discord
const sendToDiscord = async (method, url, headers, ip, body) => {
// Format request details for Discord message
let discordMessage = `
🚀 **Received Request Details** 🛰️
\`\`\`plaintext
📌 Method: ${method}
🌐 URL: ${url}
💻 Remote IP: ${ip}
\`\`\`
📎 **Headers** 📄
\`\`\`
${headers}
\`\`\``;
// Include body section if request body exists
if (body.trim() !== '') {
discordMessage += `
📦 **Body** 📦
\`\`\`
${body}
\`\`\`
`;
}
// Send formatted message to Discord webhook
await fetch(discordWebhookURL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
content: discordMessage,
}),
});
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Request details sent to Discord webhook!');
};
// Store request details
const requestMethod = req.method;
const requestUrl = req.url;
const requestHeaders = Object.entries(req.headers).map(([key, value]) => `${key}: ${value}`).join('\n');
// Get the client's IP address from x-forwarded-for
const xForwardedFor = req.headers['x-forwarded-for'];
const remoteIP = xForwardedFor ? xForwardedFor.split(',')[0] : 'Not available';
let requestBody = '';
// Capture request body for all request types except GET
if (requestMethod !== 'GET') {
let bodyChunks = [];
req.on('data', (chunk) => {
bodyChunks.push(chunk);
});
req.on('end', () => {
requestBody = Buffer.concat(bodyChunks).toString();
sendToDiscord(requestMethod, requestUrl, requestHeaders, remoteIP, requestBody);
});
} else {
sendToDiscord(requestMethod, requestUrl, requestHeaders, remoteIP, requestBody);
}
} catch (error) {
res.writeHead(500, { 'Content-Type': 'text/plain' });
res.end(`Error occurred while sending request details to Discord! ${error}`);
}
});
const PORT = process.env.PORT || 3000;
server.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});