-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
63 lines (52 loc) · 1.76 KB
/
server.js
File metadata and controls
63 lines (52 loc) · 1.76 KB
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
const express = require("express");
const axios = require("axios");
const https = require("https");
const bodyParser = require("body-parser");
const cors = require("cors");
const app = express();
const PORT = process.env.PORT || 3000;
// Configuration (make these environment variables in real use)
const ENDPOINT_LINK = process.env.ENDPOINT_LINK || "https://example.com";
const ENDPOINT_PORT = process.env.ENDPOINT_PORT || 7108;
const USERNAME = process.env.PROXY_USER || "username";
const PASSWORD = process.env.PROXY_PASS || "password";
// Middleware
app.use(cors());
app.use(bodyParser.json());
app.use("/proxy", async (req, res) => {
try {
const targetPath = req.originalUrl.replace("/proxy", "");
const targetUrl = `${ENDPOINT_LINK}:${ENDPOINT_PORT}${targetPath}`;
console.log("Forwarding request to:", targetUrl);
const response = await axios({
method: req.method,
url: targetUrl,
headers: {
"Content-Type": "application/json",
Authorization:
"Basic " + Buffer.from(`${USERNAME}:${PASSWORD}`).toString("base64"),
...req.headers,
},
data: req.body,
timeout: 15000,
httpsAgent: new https.Agent({ rejectUnauthorized: false }),
});
res.status(response.status).json(response.data);
} catch (err) {
console.error("Proxy error:", err.message);
if (err.response) {
// If the target server responded with an error
return res
.status(err.response.status)
.json({ error: err.response.data || err.message });
}
res.status(500).json({ error: err.message });
}
});
// Export app for testing
if (require.main === module) {
app.listen(PORT, () => {
console.log(`Proxy server running on http://localhost:${PORT}`);
});
}
module.exports = app;